]> git.karo-electronics.de Git - mv-sheeva.git/blob - fs/btrfs/inode.c
Btrfs: trivial include fixups
[mv-sheeva.git] / fs / btrfs / inode.c
1 /*
2  * Copyright (C) 2007 Oracle.  All rights reserved.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public
6  * License v2 as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public
14  * License along with this program; if not, write to the
15  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16  * Boston, MA 021110-1307, USA.
17  */
18
19 #include <linux/buffer_head.h>
20 #include <linux/fs.h>
21 #include <linux/pagemap.h>
22 #include <linux/highmem.h>
23 #include <linux/time.h>
24 #include <linux/init.h>
25 #include <linux/string.h>
26 #include <linux/smp_lock.h>
27 #include <linux/backing-dev.h>
28 #include <linux/mpage.h>
29 #include <linux/swap.h>
30 #include <linux/writeback.h>
31 #include <linux/statfs.h>
32 #include <linux/compat.h>
33 #include <linux/bit_spinlock.h>
34 #include "ctree.h"
35 #include "disk-io.h"
36 #include "transaction.h"
37 #include "btrfs_inode.h"
38 #include "ioctl.h"
39 #include "print-tree.h"
40
41 struct btrfs_iget_args {
42         u64 ino;
43         struct btrfs_root *root;
44 };
45
46 static struct inode_operations btrfs_dir_inode_operations;
47 static struct inode_operations btrfs_symlink_inode_operations;
48 static struct inode_operations btrfs_dir_ro_inode_operations;
49 static struct inode_operations btrfs_file_inode_operations;
50 static struct address_space_operations btrfs_aops;
51 static struct address_space_operations btrfs_symlink_aops;
52 static struct file_operations btrfs_dir_file_operations;
53
54 static struct kmem_cache *btrfs_inode_cachep;
55 struct kmem_cache *btrfs_trans_handle_cachep;
56 struct kmem_cache *btrfs_transaction_cachep;
57 struct kmem_cache *btrfs_bit_radix_cachep;
58 struct kmem_cache *btrfs_path_cachep;
59
60 #define S_SHIFT 12
61 static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
62         [S_IFREG >> S_SHIFT]    = BTRFS_FT_REG_FILE,
63         [S_IFDIR >> S_SHIFT]    = BTRFS_FT_DIR,
64         [S_IFCHR >> S_SHIFT]    = BTRFS_FT_CHRDEV,
65         [S_IFBLK >> S_SHIFT]    = BTRFS_FT_BLKDEV,
66         [S_IFIFO >> S_SHIFT]    = BTRFS_FT_FIFO,
67         [S_IFSOCK >> S_SHIFT]   = BTRFS_FT_SOCK,
68         [S_IFLNK >> S_SHIFT]    = BTRFS_FT_SYMLINK,
69 };
70
71 void btrfs_read_locked_inode(struct inode *inode)
72 {
73         struct btrfs_path *path;
74         struct btrfs_inode_item *inode_item;
75         struct btrfs_root *root = BTRFS_I(inode)->root;
76         struct btrfs_key location;
77         u64 alloc_group_block;
78         int ret;
79
80         path = btrfs_alloc_path();
81         BUG_ON(!path);
82         mutex_lock(&root->fs_info->fs_mutex);
83
84         memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
85         ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
86         if (ret) {
87                 btrfs_free_path(path);
88                 goto make_bad;
89         }
90         inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
91                                   path->slots[0],
92                                   struct btrfs_inode_item);
93
94         inode->i_mode = btrfs_inode_mode(inode_item);
95         inode->i_nlink = btrfs_inode_nlink(inode_item);
96         inode->i_uid = btrfs_inode_uid(inode_item);
97         inode->i_gid = btrfs_inode_gid(inode_item);
98         inode->i_size = btrfs_inode_size(inode_item);
99         inode->i_atime.tv_sec = btrfs_timespec_sec(&inode_item->atime);
100         inode->i_atime.tv_nsec = btrfs_timespec_nsec(&inode_item->atime);
101         inode->i_mtime.tv_sec = btrfs_timespec_sec(&inode_item->mtime);
102         inode->i_mtime.tv_nsec = btrfs_timespec_nsec(&inode_item->mtime);
103         inode->i_ctime.tv_sec = btrfs_timespec_sec(&inode_item->ctime);
104         inode->i_ctime.tv_nsec = btrfs_timespec_nsec(&inode_item->ctime);
105         inode->i_blocks = btrfs_inode_nblocks(inode_item);
106         inode->i_generation = btrfs_inode_generation(inode_item);
107         alloc_group_block = btrfs_inode_block_group(inode_item);
108         BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
109                                                        alloc_group_block);
110
111         btrfs_free_path(path);
112         inode_item = NULL;
113
114         mutex_unlock(&root->fs_info->fs_mutex);
115
116         switch (inode->i_mode & S_IFMT) {
117 #if 0
118         default:
119                 init_special_inode(inode, inode->i_mode,
120                                    btrfs_inode_rdev(inode_item));
121                 break;
122 #endif
123         case S_IFREG:
124                 inode->i_mapping->a_ops = &btrfs_aops;
125                 inode->i_fop = &btrfs_file_operations;
126                 inode->i_op = &btrfs_file_inode_operations;
127                 break;
128         case S_IFDIR:
129                 inode->i_fop = &btrfs_dir_file_operations;
130                 if (root == root->fs_info->tree_root)
131                         inode->i_op = &btrfs_dir_ro_inode_operations;
132                 else
133                         inode->i_op = &btrfs_dir_inode_operations;
134                 break;
135         case S_IFLNK:
136                 inode->i_op = &btrfs_symlink_inode_operations;
137                 inode->i_mapping->a_ops = &btrfs_symlink_aops;
138                 break;
139         }
140         return;
141
142 make_bad:
143         btrfs_release_path(root, path);
144         btrfs_free_path(path);
145         mutex_unlock(&root->fs_info->fs_mutex);
146         make_bad_inode(inode);
147 }
148
149 static void fill_inode_item(struct btrfs_inode_item *item,
150                             struct inode *inode)
151 {
152         btrfs_set_inode_uid(item, inode->i_uid);
153         btrfs_set_inode_gid(item, inode->i_gid);
154         btrfs_set_inode_size(item, inode->i_size);
155         btrfs_set_inode_mode(item, inode->i_mode);
156         btrfs_set_inode_nlink(item, inode->i_nlink);
157         btrfs_set_timespec_sec(&item->atime, inode->i_atime.tv_sec);
158         btrfs_set_timespec_nsec(&item->atime, inode->i_atime.tv_nsec);
159         btrfs_set_timespec_sec(&item->mtime, inode->i_mtime.tv_sec);
160         btrfs_set_timespec_nsec(&item->mtime, inode->i_mtime.tv_nsec);
161         btrfs_set_timespec_sec(&item->ctime, inode->i_ctime.tv_sec);
162         btrfs_set_timespec_nsec(&item->ctime, inode->i_ctime.tv_nsec);
163         btrfs_set_inode_nblocks(item, inode->i_blocks);
164         btrfs_set_inode_generation(item, inode->i_generation);
165         btrfs_set_inode_block_group(item,
166                                     BTRFS_I(inode)->block_group->key.objectid);
167 }
168
169 static int btrfs_update_inode(struct btrfs_trans_handle *trans,
170                               struct btrfs_root *root,
171                               struct inode *inode)
172 {
173         struct btrfs_inode_item *inode_item;
174         struct btrfs_path *path;
175         int ret;
176
177         path = btrfs_alloc_path();
178         BUG_ON(!path);
179         ret = btrfs_lookup_inode(trans, root, path,
180                                  &BTRFS_I(inode)->location, 1);
181         if (ret) {
182                 if (ret > 0)
183                         ret = -ENOENT;
184                 goto failed;
185         }
186
187         inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
188                                   path->slots[0],
189                                   struct btrfs_inode_item);
190
191         fill_inode_item(inode_item, inode);
192         btrfs_mark_buffer_dirty(path->nodes[0]);
193         ret = 0;
194 failed:
195         btrfs_release_path(root, path);
196         btrfs_free_path(path);
197         return ret;
198 }
199
200
201 static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
202                               struct btrfs_root *root,
203                               struct inode *dir,
204                               struct dentry *dentry)
205 {
206         struct btrfs_path *path;
207         const char *name = dentry->d_name.name;
208         int name_len = dentry->d_name.len;
209         int ret = 0;
210         u64 objectid;
211         struct btrfs_dir_item *di;
212
213         path = btrfs_alloc_path();
214         if (!path) {
215                 ret = -ENOMEM;
216                 goto err;
217         }
218
219         di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
220                                     name, name_len, -1);
221         if (IS_ERR(di)) {
222                 ret = PTR_ERR(di);
223                 goto err;
224         }
225         if (!di) {
226                 ret = -ENOENT;
227                 goto err;
228         }
229         objectid = btrfs_disk_key_objectid(&di->location);
230         ret = btrfs_delete_one_dir_name(trans, root, path, di);
231         if (ret)
232                 goto err;
233         btrfs_release_path(root, path);
234
235         di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
236                                          objectid, name, name_len, -1);
237         if (IS_ERR(di)) {
238                 ret = PTR_ERR(di);
239                 goto err;
240         }
241         if (!di) {
242                 ret = -ENOENT;
243                 goto err;
244         }
245         ret = btrfs_delete_one_dir_name(trans, root, path, di);
246
247         dentry->d_inode->i_ctime = dir->i_ctime;
248 err:
249         btrfs_free_path(path);
250         if (!ret) {
251                 dir->i_size -= name_len * 2;
252                 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
253                 btrfs_update_inode(trans, root, dir);
254                 drop_nlink(dentry->d_inode);
255                 ret = btrfs_update_inode(trans, root, dentry->d_inode);
256                 dir->i_sb->s_dirt = 1;
257         }
258         return ret;
259 }
260
261 static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
262 {
263         struct btrfs_root *root;
264         struct btrfs_trans_handle *trans;
265         int ret;
266
267         root = BTRFS_I(dir)->root;
268         mutex_lock(&root->fs_info->fs_mutex);
269         trans = btrfs_start_transaction(root, 1);
270         btrfs_set_trans_block_group(trans, dir);
271         ret = btrfs_unlink_trans(trans, root, dir, dentry);
272         btrfs_end_transaction(trans, root);
273         mutex_unlock(&root->fs_info->fs_mutex);
274         btrfs_btree_balance_dirty(root);
275         return ret;
276 }
277
278 static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
279 {
280         struct inode *inode = dentry->d_inode;
281         int err;
282         int ret;
283         struct btrfs_root *root = BTRFS_I(dir)->root;
284         struct btrfs_path *path;
285         struct btrfs_key key;
286         struct btrfs_trans_handle *trans;
287         struct btrfs_key found_key;
288         int found_type;
289         struct btrfs_leaf *leaf;
290         char *goodnames = "..";
291
292         path = btrfs_alloc_path();
293         BUG_ON(!path);
294         mutex_lock(&root->fs_info->fs_mutex);
295         trans = btrfs_start_transaction(root, 1);
296         btrfs_set_trans_block_group(trans, dir);
297         key.objectid = inode->i_ino;
298         key.offset = (u64)-1;
299         key.flags = (u32)-1;
300         while(1) {
301                 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
302                 if (ret < 0) {
303                         err = ret;
304                         goto out;
305                 }
306                 BUG_ON(ret == 0);
307                 if (path->slots[0] == 0) {
308                         err = -ENOENT;
309                         goto out;
310                 }
311                 path->slots[0]--;
312                 leaf = btrfs_buffer_leaf(path->nodes[0]);
313                 btrfs_disk_key_to_cpu(&found_key,
314                                       &leaf->items[path->slots[0]].key);
315                 found_type = btrfs_key_type(&found_key);
316                 if (found_key.objectid != inode->i_ino) {
317                         err = -ENOENT;
318                         goto out;
319                 }
320                 if ((found_type != BTRFS_DIR_ITEM_KEY &&
321                      found_type != BTRFS_DIR_INDEX_KEY) ||
322                     (!btrfs_match_dir_item_name(root, path, goodnames, 2) &&
323                     !btrfs_match_dir_item_name(root, path, goodnames, 1))) {
324                         err = -ENOTEMPTY;
325                         goto out;
326                 }
327                 ret = btrfs_del_item(trans, root, path);
328                 BUG_ON(ret);
329
330                 if (found_type == BTRFS_DIR_ITEM_KEY && found_key.offset == 1)
331                         break;
332                 btrfs_release_path(root, path);
333         }
334         ret = 0;
335         btrfs_release_path(root, path);
336
337         /* now the directory is empty */
338         err = btrfs_unlink_trans(trans, root, dir, dentry);
339         if (!err) {
340                 inode->i_size = 0;
341         }
342 out:
343         btrfs_release_path(root, path);
344         btrfs_free_path(path);
345         mutex_unlock(&root->fs_info->fs_mutex);
346         ret = btrfs_end_transaction(trans, root);
347         btrfs_btree_balance_dirty(root);
348         if (ret && !err)
349                 err = ret;
350         return err;
351 }
352
353 static int btrfs_free_inode(struct btrfs_trans_handle *trans,
354                             struct btrfs_root *root,
355                             struct inode *inode)
356 {
357         struct btrfs_path *path;
358         int ret;
359
360         clear_inode(inode);
361
362         path = btrfs_alloc_path();
363         BUG_ON(!path);
364         ret = btrfs_lookup_inode(trans, root, path,
365                                  &BTRFS_I(inode)->location, -1);
366         if (ret > 0)
367                 ret = -ENOENT;
368         if (!ret)
369                 ret = btrfs_del_item(trans, root, path);
370         btrfs_free_path(path);
371         return ret;
372 }
373
374 /*
375  * truncates go from a high offset to a low offset.  So, walk
376  * from hi to lo in the node and issue readas.  Stop when you find
377  * keys from a different objectid
378  */
379 static void reada_truncate(struct btrfs_root *root, struct btrfs_path *path,
380                            u64 objectid)
381 {
382         struct btrfs_node *node;
383         int i;
384         int nritems;
385         u64 item_objectid;
386         u64 blocknr;
387         int slot;
388         int ret;
389
390         if (!path->nodes[1])
391                 return;
392         node = btrfs_buffer_node(path->nodes[1]);
393         slot = path->slots[1];
394         if (slot == 0)
395                 return;
396         nritems = btrfs_header_nritems(&node->header);
397         for (i = slot - 1; i >= 0; i--) {
398                 item_objectid = btrfs_disk_key_objectid(&node->ptrs[i].key);
399                 if (item_objectid != objectid)
400                         break;
401                 blocknr = btrfs_node_blockptr(node, i);
402                 ret = readahead_tree_block(root, blocknr);
403                 if (ret)
404                         break;
405         }
406 }
407
408 /*
409  * this can truncate away extent items, csum items and directory items.
410  * It starts at a high offset and removes keys until it can't find
411  * any higher than i_size.
412  *
413  * csum items that cross the new i_size are truncated to the new size
414  * as well.
415  */
416 static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
417                                    struct btrfs_root *root,
418                                    struct inode *inode)
419 {
420         int ret;
421         struct btrfs_path *path;
422         struct btrfs_key key;
423         struct btrfs_disk_key *found_key;
424         u32 found_type;
425         struct btrfs_leaf *leaf;
426         struct btrfs_file_extent_item *fi;
427         u64 extent_start = 0;
428         u64 extent_num_blocks = 0;
429         u64 item_end = 0;
430         int found_extent;
431         int del_item;
432
433         path = btrfs_alloc_path();
434         BUG_ON(!path);
435         /* FIXME, add redo link to tree so we don't leak on crash */
436         key.objectid = inode->i_ino;
437         key.offset = (u64)-1;
438         key.flags = (u32)-1;
439         while(1) {
440                 btrfs_init_path(path);
441                 fi = NULL;
442                 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
443                 if (ret < 0) {
444                         goto error;
445                 }
446                 if (ret > 0) {
447                         BUG_ON(path->slots[0] == 0);
448                         path->slots[0]--;
449                 }
450                 reada_truncate(root, path, inode->i_ino);
451                 leaf = btrfs_buffer_leaf(path->nodes[0]);
452                 found_key = &leaf->items[path->slots[0]].key;
453                 found_type = btrfs_disk_key_type(found_key);
454
455                 if (btrfs_disk_key_objectid(found_key) != inode->i_ino)
456                         break;
457                 if (found_type != BTRFS_CSUM_ITEM_KEY &&
458                     found_type != BTRFS_DIR_ITEM_KEY &&
459                     found_type != BTRFS_DIR_INDEX_KEY &&
460                     found_type != BTRFS_EXTENT_DATA_KEY)
461                         break;
462
463                 item_end = btrfs_disk_key_offset(found_key);
464                 if (found_type == BTRFS_EXTENT_DATA_KEY) {
465                         fi = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
466                                             path->slots[0],
467                                             struct btrfs_file_extent_item);
468                         if (btrfs_file_extent_type(fi) !=
469                             BTRFS_FILE_EXTENT_INLINE) {
470                                 item_end += btrfs_file_extent_num_blocks(fi) <<
471                                                 inode->i_blkbits;
472                         }
473                 }
474                 if (found_type == BTRFS_CSUM_ITEM_KEY) {
475                         ret = btrfs_csum_truncate(trans, root, path,
476                                                   inode->i_size);
477                         BUG_ON(ret);
478                 }
479                 if (item_end < inode->i_size) {
480                         if (found_type) {
481                                 btrfs_set_key_type(&key, found_type - 1);
482                                 continue;
483                         }
484                         break;
485                 }
486                 if (btrfs_disk_key_offset(found_key) >= inode->i_size)
487                         del_item = 1;
488                 else
489                         del_item = 0;
490                 found_extent = 0;
491
492                 /* FIXME, shrink the extent if the ref count is only 1 */
493                 if (found_type == BTRFS_EXTENT_DATA_KEY &&
494                            btrfs_file_extent_type(fi) !=
495                            BTRFS_FILE_EXTENT_INLINE) {
496                         u64 num_dec;
497                         if (!del_item) {
498                                 u64 orig_num_blocks =
499                                         btrfs_file_extent_num_blocks(fi);
500                                 extent_num_blocks = inode->i_size -
501                                         btrfs_disk_key_offset(found_key) +
502                                         root->blocksize - 1;
503                                 extent_num_blocks >>= inode->i_blkbits;
504                                 btrfs_set_file_extent_num_blocks(fi,
505                                                          extent_num_blocks);
506                                 inode->i_blocks -= (orig_num_blocks -
507                                         extent_num_blocks) << 3;
508                                 btrfs_mark_buffer_dirty(path->nodes[0]);
509                         } else {
510                                 extent_start =
511                                         btrfs_file_extent_disk_blocknr(fi);
512                                 extent_num_blocks =
513                                         btrfs_file_extent_disk_num_blocks(fi);
514                                 /* FIXME blocksize != 4096 */
515                                 num_dec = btrfs_file_extent_num_blocks(fi) << 3;
516                                 if (extent_start != 0) {
517                                         found_extent = 1;
518                                         inode->i_blocks -= num_dec;
519                                 }
520                         }
521                 }
522                 if (del_item) {
523                         ret = btrfs_del_item(trans, root, path);
524                         if (ret)
525                                 goto error;
526                 } else {
527                         break;
528                 }
529                 btrfs_release_path(root, path);
530                 if (found_extent) {
531                         ret = btrfs_free_extent(trans, root, extent_start,
532                                                 extent_num_blocks, 0);
533                         BUG_ON(ret);
534                 }
535         }
536         ret = 0;
537 error:
538         btrfs_release_path(root, path);
539         btrfs_free_path(path);
540         inode->i_sb->s_dirt = 1;
541         return ret;
542 }
543
544 /*
545  * taken from block_truncate_page, but does cow as it zeros out
546  * any bytes left in the last page in the file.
547  */
548 static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
549 {
550         struct inode *inode = mapping->host;
551         unsigned blocksize = 1 << inode->i_blkbits;
552         pgoff_t index = from >> PAGE_CACHE_SHIFT;
553         unsigned offset = from & (PAGE_CACHE_SIZE-1);
554         struct page *page;
555         char *kaddr;
556         int ret = 0;
557         struct btrfs_root *root = BTRFS_I(inode)->root;
558         u64 alloc_hint = 0;
559         struct btrfs_key ins;
560         struct btrfs_trans_handle *trans;
561
562         if ((offset & (blocksize - 1)) == 0)
563                 goto out;
564
565         ret = -ENOMEM;
566         page = grab_cache_page(mapping, index);
567         if (!page)
568                 goto out;
569
570         if (!PageUptodate(page)) {
571                 ret = btrfs_readpage(NULL, page);
572                 lock_page(page);
573                 if (!PageUptodate(page)) {
574                         ret = -EIO;
575                         goto out;
576                 }
577         }
578         mutex_lock(&root->fs_info->fs_mutex);
579         trans = btrfs_start_transaction(root, 1);
580         btrfs_set_trans_block_group(trans, inode);
581
582         ret = btrfs_drop_extents(trans, root, inode,
583                                  page->index << PAGE_CACHE_SHIFT,
584                                  (page->index + 1) << PAGE_CACHE_SHIFT,
585                                  &alloc_hint);
586         if (ret)
587                 goto out;
588         ret = btrfs_alloc_extent(trans, root, inode->i_ino, 1,
589                                  alloc_hint, (u64)-1, &ins, 1);
590         if (ret)
591                 goto out;
592         ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
593                                        page->index << PAGE_CACHE_SHIFT,
594                                        ins.objectid, 1, 1);
595         if (ret)
596                 goto out;
597         SetPageChecked(page);
598         kaddr = kmap(page);
599         memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
600         flush_dcache_page(page);
601         ret = btrfs_csum_file_block(trans, root, inode->i_ino,
602                               page->index << PAGE_CACHE_SHIFT,
603                               kaddr, PAGE_CACHE_SIZE);
604         kunmap(page);
605         btrfs_end_transaction(trans, root);
606         mutex_unlock(&root->fs_info->fs_mutex);
607
608         set_page_dirty(page);
609         unlock_page(page);
610         page_cache_release(page);
611 out:
612         return ret;
613 }
614
615 static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
616 {
617         struct inode *inode = dentry->d_inode;
618         int err;
619
620         err = inode_change_ok(inode, attr);
621         if (err)
622                 return err;
623
624         if (S_ISREG(inode->i_mode) &&
625             attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
626                 struct btrfs_trans_handle *trans;
627                 struct btrfs_root *root = BTRFS_I(inode)->root;
628                 u64 mask = root->blocksize - 1;
629                 u64 pos = (inode->i_size + mask) & ~mask;
630                 u64 hole_size;
631
632                 if (attr->ia_size <= pos)
633                         goto out;
634
635                 btrfs_truncate_page(inode->i_mapping, inode->i_size);
636
637                 hole_size = (attr->ia_size - pos + mask) & ~mask;
638                 hole_size >>= inode->i_blkbits;
639
640                 mutex_lock(&root->fs_info->fs_mutex);
641                 trans = btrfs_start_transaction(root, 1);
642                 btrfs_set_trans_block_group(trans, inode);
643                 err = btrfs_insert_file_extent(trans, root, inode->i_ino,
644                                                pos, 0, 0, hole_size);
645                 btrfs_end_transaction(trans, root);
646                 mutex_unlock(&root->fs_info->fs_mutex);
647                 if (err)
648                         return err;
649         }
650 out:
651         err = inode_setattr(inode, attr);
652
653         return err;
654 }
655 void btrfs_delete_inode(struct inode *inode)
656 {
657         struct btrfs_trans_handle *trans;
658         struct btrfs_root *root = BTRFS_I(inode)->root;
659         int ret;
660
661         truncate_inode_pages(&inode->i_data, 0);
662         if (is_bad_inode(inode)) {
663                 goto no_delete;
664         }
665         inode->i_size = 0;
666         mutex_lock(&root->fs_info->fs_mutex);
667         trans = btrfs_start_transaction(root, 1);
668         btrfs_set_trans_block_group(trans, inode);
669         ret = btrfs_truncate_in_trans(trans, root, inode);
670         if (ret)
671                 goto no_delete_lock;
672         ret = btrfs_free_inode(trans, root, inode);
673         if (ret)
674                 goto no_delete_lock;
675         btrfs_end_transaction(trans, root);
676         mutex_unlock(&root->fs_info->fs_mutex);
677         btrfs_btree_balance_dirty(root);
678         return;
679
680 no_delete_lock:
681         btrfs_end_transaction(trans, root);
682         mutex_unlock(&root->fs_info->fs_mutex);
683         btrfs_btree_balance_dirty(root);
684 no_delete:
685         clear_inode(inode);
686 }
687
688 /*
689  * this returns the key found in the dir entry in the location pointer.
690  * If no dir entries were found, location->objectid is 0.
691  */
692 static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
693                                struct btrfs_key *location)
694 {
695         const char *name = dentry->d_name.name;
696         int namelen = dentry->d_name.len;
697         struct btrfs_dir_item *di;
698         struct btrfs_path *path;
699         struct btrfs_root *root = BTRFS_I(dir)->root;
700         int ret;
701
702         path = btrfs_alloc_path();
703         BUG_ON(!path);
704         di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
705                                     namelen, 0);
706         if (!di || IS_ERR(di)) {
707                 location->objectid = 0;
708                 ret = 0;
709                 goto out;
710         }
711         btrfs_disk_key_to_cpu(location, &di->location);
712 out:
713         btrfs_release_path(root, path);
714         btrfs_free_path(path);
715         return ret;
716 }
717
718 /*
719  * when we hit a tree root in a directory, the btrfs part of the inode
720  * needs to be changed to reflect the root directory of the tree root.  This
721  * is kind of like crossing a mount point.
722  */
723 static int fixup_tree_root_location(struct btrfs_root *root,
724                              struct btrfs_key *location,
725                              struct btrfs_root **sub_root)
726 {
727         struct btrfs_path *path;
728         struct btrfs_root_item *ri;
729
730         if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
731                 return 0;
732         if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
733                 return 0;
734
735         path = btrfs_alloc_path();
736         BUG_ON(!path);
737         mutex_lock(&root->fs_info->fs_mutex);
738
739         *sub_root = btrfs_read_fs_root(root->fs_info, location);
740         if (IS_ERR(*sub_root))
741                 return PTR_ERR(*sub_root);
742
743         ri = &(*sub_root)->root_item;
744         location->objectid = btrfs_root_dirid(ri);
745         location->flags = 0;
746         btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
747         location->offset = 0;
748
749         btrfs_free_path(path);
750         mutex_unlock(&root->fs_info->fs_mutex);
751         return 0;
752 }
753
754 static int btrfs_init_locked_inode(struct inode *inode, void *p)
755 {
756         struct btrfs_iget_args *args = p;
757         inode->i_ino = args->ino;
758         BTRFS_I(inode)->root = args->root;
759         return 0;
760 }
761
762 static int btrfs_find_actor(struct inode *inode, void *opaque)
763 {
764         struct btrfs_iget_args *args = opaque;
765         return (args->ino == inode->i_ino &&
766                 args->root == BTRFS_I(inode)->root);
767 }
768
769 struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
770                                 struct btrfs_root *root)
771 {
772         struct inode *inode;
773         struct btrfs_iget_args args;
774         args.ino = objectid;
775         args.root = root;
776
777         inode = iget5_locked(s, objectid, btrfs_find_actor,
778                              btrfs_init_locked_inode,
779                              (void *)&args);
780         return inode;
781 }
782
783 static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
784                                    struct nameidata *nd)
785 {
786         struct inode * inode;
787         struct btrfs_inode *bi = BTRFS_I(dir);
788         struct btrfs_root *root = bi->root;
789         struct btrfs_root *sub_root = root;
790         struct btrfs_key location;
791         int ret;
792
793         if (dentry->d_name.len > BTRFS_NAME_LEN)
794                 return ERR_PTR(-ENAMETOOLONG);
795         mutex_lock(&root->fs_info->fs_mutex);
796         ret = btrfs_inode_by_name(dir, dentry, &location);
797         mutex_unlock(&root->fs_info->fs_mutex);
798         if (ret < 0)
799                 return ERR_PTR(ret);
800         inode = NULL;
801         if (location.objectid) {
802                 ret = fixup_tree_root_location(root, &location, &sub_root);
803                 if (ret < 0)
804                         return ERR_PTR(ret);
805                 if (ret > 0)
806                         return ERR_PTR(-ENOENT);
807                 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
808                                           sub_root);
809                 if (!inode)
810                         return ERR_PTR(-EACCES);
811                 if (inode->i_state & I_NEW) {
812                         /* the inode and parent dir are two different roots */
813                         if (sub_root != root) {
814                                 igrab(inode);
815                                 sub_root->inode = inode;
816                         }
817                         BTRFS_I(inode)->root = sub_root;
818                         memcpy(&BTRFS_I(inode)->location, &location,
819                                sizeof(location));
820                         btrfs_read_locked_inode(inode);
821                         unlock_new_inode(inode);
822                 }
823         }
824         return d_splice_alias(inode, dentry);
825 }
826
827 /*
828  * readahead one full node of leaves as long as their keys include
829  * the objectid supplied
830  */
831 static void reada_leaves(struct btrfs_root *root, struct btrfs_path *path,
832                          u64 objectid)
833 {
834         struct btrfs_node *node;
835         int i;
836         u32 nritems;
837         u64 item_objectid;
838         u64 blocknr;
839         int slot;
840         int ret;
841
842         if (!path->nodes[1])
843                 return;
844         node = btrfs_buffer_node(path->nodes[1]);
845         slot = path->slots[1];
846         nritems = btrfs_header_nritems(&node->header);
847         for (i = slot + 1; i < nritems; i++) {
848                 item_objectid = btrfs_disk_key_objectid(&node->ptrs[i].key);
849                 if (item_objectid != objectid)
850                         break;
851                 blocknr = btrfs_node_blockptr(node, i);
852                 ret = readahead_tree_block(root, blocknr);
853                 if (ret)
854                         break;
855         }
856 }
857 static unsigned char btrfs_filetype_table[] = {
858         DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
859 };
860
861 static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
862 {
863         struct inode *inode = filp->f_path.dentry->d_inode;
864         struct btrfs_root *root = BTRFS_I(inode)->root;
865         struct btrfs_item *item;
866         struct btrfs_dir_item *di;
867         struct btrfs_key key;
868         struct btrfs_path *path;
869         int ret;
870         u32 nritems;
871         struct btrfs_leaf *leaf;
872         int slot;
873         int advance;
874         unsigned char d_type;
875         int over = 0;
876         u32 di_cur;
877         u32 di_total;
878         u32 di_len;
879         int key_type = BTRFS_DIR_INDEX_KEY;
880
881         /* FIXME, use a real flag for deciding about the key type */
882         if (root->fs_info->tree_root == root)
883                 key_type = BTRFS_DIR_ITEM_KEY;
884         mutex_lock(&root->fs_info->fs_mutex);
885         key.objectid = inode->i_ino;
886         key.flags = 0;
887         btrfs_set_key_type(&key, key_type);
888         key.offset = filp->f_pos;
889         path = btrfs_alloc_path();
890         ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
891         if (ret < 0)
892                 goto err;
893         advance = 0;
894         reada_leaves(root, path, inode->i_ino);
895         while(1) {
896                 leaf = btrfs_buffer_leaf(path->nodes[0]);
897                 nritems = btrfs_header_nritems(&leaf->header);
898                 slot = path->slots[0];
899                 if (advance || slot >= nritems) {
900                         if (slot >= nritems -1) {
901                                 reada_leaves(root, path, inode->i_ino);
902                                 ret = btrfs_next_leaf(root, path);
903                                 if (ret)
904                                         break;
905                                 leaf = btrfs_buffer_leaf(path->nodes[0]);
906                                 nritems = btrfs_header_nritems(&leaf->header);
907                                 slot = path->slots[0];
908                         } else {
909                                 slot++;
910                                 path->slots[0]++;
911                         }
912                 }
913                 advance = 1;
914                 item = leaf->items + slot;
915                 if (btrfs_disk_key_objectid(&item->key) != key.objectid)
916                         break;
917                 if (btrfs_disk_key_type(&item->key) != key_type)
918                         break;
919                 if (btrfs_disk_key_offset(&item->key) < filp->f_pos)
920                         continue;
921                 filp->f_pos = btrfs_disk_key_offset(&item->key);
922                 advance = 1;
923                 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
924                 di_cur = 0;
925                 di_total = btrfs_item_size(leaf->items + slot);
926                 while(di_cur < di_total) {
927                         d_type = btrfs_filetype_table[btrfs_dir_type(di)];
928                         over = filldir(dirent, (const char *)(di + 1),
929                                        btrfs_dir_name_len(di),
930                                        btrfs_disk_key_offset(&item->key),
931                                        btrfs_disk_key_objectid(&di->location),
932                                        d_type);
933                         if (over)
934                                 goto nopos;
935                         di_len = btrfs_dir_name_len(di) + sizeof(*di);
936                         di_cur += di_len;
937                         di = (struct btrfs_dir_item *)((char *)di + di_len);
938                 }
939         }
940         filp->f_pos++;
941 nopos:
942         ret = 0;
943 err:
944         btrfs_release_path(root, path);
945         btrfs_free_path(path);
946         mutex_unlock(&root->fs_info->fs_mutex);
947         return ret;
948 }
949
950 int btrfs_write_inode(struct inode *inode, int wait)
951 {
952         struct btrfs_root *root = BTRFS_I(inode)->root;
953         struct btrfs_trans_handle *trans;
954         int ret = 0;
955
956         if (wait) {
957                 mutex_lock(&root->fs_info->fs_mutex);
958                 trans = btrfs_start_transaction(root, 1);
959                 btrfs_set_trans_block_group(trans, inode);
960                 ret = btrfs_commit_transaction(trans, root);
961                 mutex_unlock(&root->fs_info->fs_mutex);
962         }
963         return ret;
964 }
965
966 /*
967  * This is somewhat expensive, updating the tree every time the
968  * inode changes.  But, it is most likely to find the inode in cache.
969  * FIXME, needs more benchmarking...there are no reasons other than performance
970  * to keep or drop this code.
971  */
972 void btrfs_dirty_inode(struct inode *inode)
973 {
974         struct btrfs_root *root = BTRFS_I(inode)->root;
975         struct btrfs_trans_handle *trans;
976
977         mutex_lock(&root->fs_info->fs_mutex);
978         trans = btrfs_start_transaction(root, 1);
979         btrfs_set_trans_block_group(trans, inode);
980         btrfs_update_inode(trans, root, inode);
981         btrfs_end_transaction(trans, root);
982         mutex_unlock(&root->fs_info->fs_mutex);
983 }
984
985 static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
986                                      struct btrfs_root *root,
987                                      u64 objectid,
988                                      struct btrfs_block_group_cache *group,
989                                      int mode)
990 {
991         struct inode *inode;
992         struct btrfs_inode_item inode_item;
993         struct btrfs_key *location;
994         int ret;
995         int owner;
996
997         inode = new_inode(root->fs_info->sb);
998         if (!inode)
999                 return ERR_PTR(-ENOMEM);
1000
1001         BTRFS_I(inode)->root = root;
1002         if (mode & S_IFDIR)
1003                 owner = 0;
1004         else
1005                 owner = 1;
1006         group = btrfs_find_block_group(root, group, 0, 0, owner);
1007         BTRFS_I(inode)->block_group = group;
1008
1009         inode->i_uid = current->fsuid;
1010         inode->i_gid = current->fsgid;
1011         inode->i_mode = mode;
1012         inode->i_ino = objectid;
1013         inode->i_blocks = 0;
1014         inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1015         fill_inode_item(&inode_item, inode);
1016         location = &BTRFS_I(inode)->location;
1017         location->objectid = objectid;
1018         location->flags = 0;
1019         location->offset = 0;
1020         btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1021
1022         ret = btrfs_insert_inode(trans, root, objectid, &inode_item);
1023         if (ret)
1024                 return ERR_PTR(ret);
1025         insert_inode_hash(inode);
1026         return inode;
1027 }
1028
1029 static inline u8 btrfs_inode_type(struct inode *inode)
1030 {
1031         return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
1032 }
1033
1034 static int btrfs_add_link(struct btrfs_trans_handle *trans,
1035                             struct dentry *dentry, struct inode *inode)
1036 {
1037         int ret;
1038         struct btrfs_key key;
1039         struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
1040         struct inode *parent_inode;
1041         key.objectid = inode->i_ino;
1042         key.flags = 0;
1043         btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1044         key.offset = 0;
1045
1046         ret = btrfs_insert_dir_item(trans, root,
1047                                     dentry->d_name.name, dentry->d_name.len,
1048                                     dentry->d_parent->d_inode->i_ino,
1049                                     &key, btrfs_inode_type(inode));
1050         if (ret == 0) {
1051                 parent_inode = dentry->d_parent->d_inode;
1052                 parent_inode->i_size += dentry->d_name.len * 2;
1053                 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
1054                 ret = btrfs_update_inode(trans, root,
1055                                          dentry->d_parent->d_inode);
1056         }
1057         return ret;
1058 }
1059
1060 static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
1061                             struct dentry *dentry, struct inode *inode)
1062 {
1063         int err = btrfs_add_link(trans, dentry, inode);
1064         if (!err) {
1065                 d_instantiate(dentry, inode);
1066                 return 0;
1067         }
1068         if (err > 0)
1069                 err = -EEXIST;
1070         return err;
1071 }
1072
1073 static int btrfs_create(struct inode *dir, struct dentry *dentry,
1074                         int mode, struct nameidata *nd)
1075 {
1076         struct btrfs_trans_handle *trans;
1077         struct btrfs_root *root = BTRFS_I(dir)->root;
1078         struct inode *inode;
1079         int err;
1080         int drop_inode = 0;
1081         u64 objectid;
1082
1083         mutex_lock(&root->fs_info->fs_mutex);
1084         trans = btrfs_start_transaction(root, 1);
1085         btrfs_set_trans_block_group(trans, dir);
1086
1087         err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1088         if (err) {
1089                 err = -ENOSPC;
1090                 goto out_unlock;
1091         }
1092
1093         inode = btrfs_new_inode(trans, root, objectid,
1094                                 BTRFS_I(dir)->block_group, mode);
1095         err = PTR_ERR(inode);
1096         if (IS_ERR(inode))
1097                 goto out_unlock;
1098
1099         btrfs_set_trans_block_group(trans, inode);
1100         err = btrfs_add_nondir(trans, dentry, inode);
1101         if (err)
1102                 drop_inode = 1;
1103         else {
1104                 inode->i_mapping->a_ops = &btrfs_aops;
1105                 inode->i_fop = &btrfs_file_operations;
1106                 inode->i_op = &btrfs_file_inode_operations;
1107         }
1108         dir->i_sb->s_dirt = 1;
1109         btrfs_update_inode_block_group(trans, inode);
1110         btrfs_update_inode_block_group(trans, dir);
1111 out_unlock:
1112         btrfs_end_transaction(trans, root);
1113         mutex_unlock(&root->fs_info->fs_mutex);
1114
1115         if (drop_inode) {
1116                 inode_dec_link_count(inode);
1117                 iput(inode);
1118         }
1119         btrfs_btree_balance_dirty(root);
1120         return err;
1121 }
1122
1123 static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
1124                       struct dentry *dentry)
1125 {
1126         struct btrfs_trans_handle *trans;
1127         struct btrfs_root *root = BTRFS_I(dir)->root;
1128         struct inode *inode = old_dentry->d_inode;
1129         int err;
1130         int drop_inode = 0;
1131
1132         if (inode->i_nlink == 0)
1133                 return -ENOENT;
1134
1135         inc_nlink(inode);
1136         mutex_lock(&root->fs_info->fs_mutex);
1137         trans = btrfs_start_transaction(root, 1);
1138         btrfs_set_trans_block_group(trans, dir);
1139         atomic_inc(&inode->i_count);
1140         err = btrfs_add_nondir(trans, dentry, inode);
1141         if (err)
1142                 drop_inode = 1;
1143         dir->i_sb->s_dirt = 1;
1144         btrfs_update_inode_block_group(trans, dir);
1145         err = btrfs_update_inode(trans, root, inode);
1146         if (err)
1147                 drop_inode = 1;
1148
1149         btrfs_end_transaction(trans, root);
1150         mutex_unlock(&root->fs_info->fs_mutex);
1151
1152         if (drop_inode) {
1153                 inode_dec_link_count(inode);
1154                 iput(inode);
1155         }
1156         btrfs_btree_balance_dirty(root);
1157         return err;
1158 }
1159
1160 static int btrfs_make_empty_dir(struct btrfs_trans_handle *trans,
1161                                 struct btrfs_root *root,
1162                                 u64 objectid, u64 dirid)
1163 {
1164         int ret;
1165         char buf[2];
1166         struct btrfs_key key;
1167
1168         buf[0] = '.';
1169         buf[1] = '.';
1170
1171         key.objectid = objectid;
1172         key.offset = 0;
1173         key.flags = 0;
1174         btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1175
1176         ret = btrfs_insert_dir_item(trans, root, buf, 1, objectid,
1177                                     &key, BTRFS_FT_DIR);
1178         if (ret)
1179                 goto error;
1180         key.objectid = dirid;
1181         ret = btrfs_insert_dir_item(trans, root, buf, 2, objectid,
1182                                     &key, BTRFS_FT_DIR);
1183         if (ret)
1184                 goto error;
1185 error:
1186         return ret;
1187 }
1188
1189 static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1190 {
1191         struct inode *inode;
1192         struct btrfs_trans_handle *trans;
1193         struct btrfs_root *root = BTRFS_I(dir)->root;
1194         int err = 0;
1195         int drop_on_err = 0;
1196         u64 objectid;
1197
1198         mutex_lock(&root->fs_info->fs_mutex);
1199         trans = btrfs_start_transaction(root, 1);
1200         btrfs_set_trans_block_group(trans, dir);
1201         if (IS_ERR(trans)) {
1202                 err = PTR_ERR(trans);
1203                 goto out_unlock;
1204         }
1205
1206         err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1207         if (err) {
1208                 err = -ENOSPC;
1209                 goto out_unlock;
1210         }
1211
1212         inode = btrfs_new_inode(trans, root, objectid,
1213                                 BTRFS_I(dir)->block_group, S_IFDIR | mode);
1214         if (IS_ERR(inode)) {
1215                 err = PTR_ERR(inode);
1216                 goto out_fail;
1217         }
1218         drop_on_err = 1;
1219         inode->i_op = &btrfs_dir_inode_operations;
1220         inode->i_fop = &btrfs_dir_file_operations;
1221         btrfs_set_trans_block_group(trans, inode);
1222
1223         err = btrfs_make_empty_dir(trans, root, inode->i_ino, dir->i_ino);
1224         if (err)
1225                 goto out_fail;
1226
1227         inode->i_size = 6;
1228         err = btrfs_update_inode(trans, root, inode);
1229         if (err)
1230                 goto out_fail;
1231         err = btrfs_add_link(trans, dentry, inode);
1232         if (err)
1233                 goto out_fail;
1234         d_instantiate(dentry, inode);
1235         drop_on_err = 0;
1236         dir->i_sb->s_dirt = 1;
1237         btrfs_update_inode_block_group(trans, inode);
1238         btrfs_update_inode_block_group(trans, dir);
1239
1240 out_fail:
1241         btrfs_end_transaction(trans, root);
1242 out_unlock:
1243         mutex_unlock(&root->fs_info->fs_mutex);
1244         if (drop_on_err)
1245                 iput(inode);
1246         btrfs_btree_balance_dirty(root);
1247         return err;
1248 }
1249
1250 /*
1251  * FIBMAP and others want to pass in a fake buffer head.  They need to
1252  * use BTRFS_GET_BLOCK_NO_DIRECT to make sure we don't try to memcpy
1253  * any packed file data into the fake bh
1254  */
1255 #define BTRFS_GET_BLOCK_NO_CREATE 0
1256 #define BTRFS_GET_BLOCK_CREATE 1
1257 #define BTRFS_GET_BLOCK_NO_DIRECT 2
1258
1259 /*
1260  * FIXME create==1 doe not work.
1261  */
1262 static int btrfs_get_block_lock(struct inode *inode, sector_t iblock,
1263                                 struct buffer_head *result, int create)
1264 {
1265         int ret;
1266         int err = 0;
1267         u64 blocknr;
1268         u64 extent_start = 0;
1269         u64 extent_end = 0;
1270         u64 objectid = inode->i_ino;
1271         u32 found_type;
1272         u64 alloc_hint = 0;
1273         struct btrfs_path *path;
1274         struct btrfs_root *root = BTRFS_I(inode)->root;
1275         struct btrfs_file_extent_item *item;
1276         struct btrfs_leaf *leaf;
1277         struct btrfs_disk_key *found_key;
1278         struct btrfs_trans_handle *trans = NULL;
1279
1280         path = btrfs_alloc_path();
1281         BUG_ON(!path);
1282         if (create & BTRFS_GET_BLOCK_CREATE) {
1283                 /*
1284                  * danger!, this only works if the page is properly up
1285                  * to date somehow
1286                  */
1287                 trans = btrfs_start_transaction(root, 1);
1288                 if (!trans) {
1289                         err = -ENOMEM;
1290                         goto out;
1291                 }
1292                 ret = btrfs_drop_extents(trans, root, inode,
1293                                          iblock << inode->i_blkbits,
1294                                          (iblock + 1) << inode->i_blkbits,
1295                                          &alloc_hint);
1296                 BUG_ON(ret);
1297         }
1298
1299         ret = btrfs_lookup_file_extent(NULL, root, path,
1300                                        objectid,
1301                                        iblock << inode->i_blkbits, 0);
1302         if (ret < 0) {
1303                 err = ret;
1304                 goto out;
1305         }
1306
1307         if (ret != 0) {
1308                 if (path->slots[0] == 0) {
1309                         btrfs_release_path(root, path);
1310                         goto not_found;
1311                 }
1312                 path->slots[0]--;
1313         }
1314
1315         item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
1316                               struct btrfs_file_extent_item);
1317         leaf = btrfs_buffer_leaf(path->nodes[0]);
1318         blocknr = btrfs_file_extent_disk_blocknr(item);
1319         blocknr += btrfs_file_extent_offset(item);
1320
1321         /* are we inside the extent that was found? */
1322         found_key = &leaf->items[path->slots[0]].key;
1323         found_type = btrfs_disk_key_type(found_key);
1324         if (btrfs_disk_key_objectid(found_key) != objectid ||
1325             found_type != BTRFS_EXTENT_DATA_KEY) {
1326                 extent_end = 0;
1327                 extent_start = 0;
1328                 goto not_found;
1329         }
1330         found_type = btrfs_file_extent_type(item);
1331         extent_start = btrfs_disk_key_offset(&leaf->items[path->slots[0]].key);
1332         if (found_type == BTRFS_FILE_EXTENT_REG) {
1333                 extent_start = extent_start >> inode->i_blkbits;
1334                 extent_end = extent_start + btrfs_file_extent_num_blocks(item);
1335                 err = 0;
1336                 if (btrfs_file_extent_disk_blocknr(item) == 0)
1337                         goto out;
1338                 if (iblock >= extent_start && iblock < extent_end) {
1339                         btrfs_map_bh_to_logical(root, result, blocknr +
1340                                                 iblock - extent_start);
1341                         goto out;
1342                 }
1343         } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
1344                 char *ptr;
1345                 char *map;
1346                 u32 size;
1347
1348                 if (create & BTRFS_GET_BLOCK_NO_DIRECT) {
1349                         err = -EINVAL;
1350                         goto out;
1351                 }
1352                 size = btrfs_file_extent_inline_len(leaf->items +
1353                                                     path->slots[0]);
1354                 extent_end = (extent_start + size) >> inode->i_blkbits;
1355                 extent_start >>= inode->i_blkbits;
1356                 if (iblock < extent_start || iblock > extent_end) {
1357                         goto not_found;
1358                 }
1359                 ptr = btrfs_file_extent_inline_start(item);
1360                 map = kmap(result->b_page);
1361                 memcpy(map, ptr, size);
1362                 memset(map + size, 0, PAGE_CACHE_SIZE - size);
1363                 flush_dcache_page(result->b_page);
1364                 kunmap(result->b_page);
1365                 set_buffer_uptodate(result);
1366                 SetPageChecked(result->b_page);
1367                 btrfs_map_bh_to_logical(root, result, 0);
1368         }
1369 not_found:
1370         if (create & BTRFS_GET_BLOCK_CREATE) {
1371                 struct btrfs_key ins;
1372                 ret = btrfs_alloc_extent(trans, root, inode->i_ino,
1373                                          1, alloc_hint, (u64)-1,
1374                                          &ins, 1);
1375                 if (ret) {
1376                         err = ret;
1377                         goto out;
1378                 }
1379                 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
1380                                                iblock << inode->i_blkbits,
1381                                                ins.objectid, ins.offset,
1382                                                ins.offset);
1383                 if (ret) {
1384                         err = ret;
1385                         goto out;
1386                 }
1387                 btrfs_map_bh_to_logical(root, result, ins.objectid);
1388         }
1389 out:
1390         if (trans) {
1391                 ret = btrfs_end_transaction(trans, root);
1392                 if (!err)
1393                         err = ret;
1394         }
1395         btrfs_free_path(path);
1396         return err;
1397 }
1398
1399 int btrfs_get_block(struct inode *inode, sector_t iblock,
1400                     struct buffer_head *result, int create)
1401 {
1402         int err;
1403         struct btrfs_root *root = BTRFS_I(inode)->root;
1404         mutex_lock(&root->fs_info->fs_mutex);
1405         err = btrfs_get_block_lock(inode, iblock, result, create);
1406         mutex_unlock(&root->fs_info->fs_mutex);
1407         return err;
1408 }
1409
1410 static int btrfs_get_block_csum(struct inode *inode, sector_t iblock,
1411                                 struct buffer_head *result, int create)
1412 {
1413         int ret;
1414         struct btrfs_root *root = BTRFS_I(inode)->root;
1415         struct page *page = result->b_page;
1416         u64 offset = (page->index << PAGE_CACHE_SHIFT) + bh_offset(result);
1417         struct btrfs_csum_item *item;
1418         struct btrfs_path *path = NULL;
1419
1420         mutex_lock(&root->fs_info->fs_mutex);
1421         ret = btrfs_get_block_lock(inode, iblock, result, create);
1422         if (ret)
1423                 goto out;
1424
1425         path = btrfs_alloc_path();
1426         item = btrfs_lookup_csum(NULL, root, path, inode->i_ino, offset, 0);
1427         if (IS_ERR(item)) {
1428                 ret = PTR_ERR(item);
1429                 /* a csum that isn't present is a preallocated region. */
1430                 if (ret == -ENOENT || ret == -EFBIG)
1431                         ret = 0;
1432                 result->b_private = NULL;
1433                 goto out;
1434         }
1435         memcpy((char *)&result->b_private, &item->csum, BTRFS_CRC32_SIZE);
1436 out:
1437         if (path)
1438                 btrfs_free_path(path);
1439         mutex_unlock(&root->fs_info->fs_mutex);
1440         return ret;
1441 }
1442
1443 static int btrfs_get_block_bmap(struct inode *inode, sector_t iblock,
1444                            struct buffer_head *result, int create)
1445 {
1446         struct btrfs_root *root = BTRFS_I(inode)->root;
1447         mutex_lock(&root->fs_info->fs_mutex);
1448         btrfs_get_block_lock(inode, iblock, result, BTRFS_GET_BLOCK_NO_DIRECT);
1449         mutex_unlock(&root->fs_info->fs_mutex);
1450         return 0;
1451 }
1452
1453 static sector_t btrfs_bmap(struct address_space *as, sector_t block)
1454 {
1455         return generic_block_bmap(as, block, btrfs_get_block_bmap);
1456 }
1457
1458 static int btrfs_prepare_write(struct file *file, struct page *page,
1459                                unsigned from, unsigned to)
1460 {
1461         return block_prepare_write(page, from, to, btrfs_get_block);
1462 }
1463
1464 static void buffer_io_error(struct buffer_head *bh)
1465 {
1466         char b[BDEVNAME_SIZE];
1467
1468         printk(KERN_ERR "Buffer I/O error on device %s, logical block %Lu\n",
1469                         bdevname(bh->b_bdev, b),
1470                         (unsigned long long)bh->b_blocknr);
1471 }
1472
1473 /*
1474  * I/O completion handler for block_read_full_page() - pages
1475  * which come unlocked at the end of I/O.
1476  */
1477 static void btrfs_end_buffer_async_read(struct buffer_head *bh, int uptodate)
1478 {
1479         unsigned long flags;
1480         struct buffer_head *first;
1481         struct buffer_head *tmp;
1482         struct page *page;
1483         int page_uptodate = 1;
1484         struct inode *inode;
1485         int ret;
1486
1487         BUG_ON(!buffer_async_read(bh));
1488
1489         page = bh->b_page;
1490         inode = page->mapping->host;
1491         if (uptodate) {
1492                 void *kaddr;
1493                 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
1494                 if (bh->b_private) {
1495                         char csum[BTRFS_CRC32_SIZE];
1496                         kaddr = kmap_atomic(page, KM_IRQ0);
1497                         ret = btrfs_csum_data(root, kaddr + bh_offset(bh),
1498                                               bh->b_size, csum);
1499                         BUG_ON(ret);
1500                         if (memcmp(csum, &bh->b_private, BTRFS_CRC32_SIZE)) {
1501                                 u64 offset;
1502                                 offset = (page->index << PAGE_CACHE_SHIFT) +
1503                                         bh_offset(bh);
1504                                 printk("btrfs csum failed ino %lu off %llu\n",
1505                                        page->mapping->host->i_ino,
1506                                        (unsigned long long)offset);
1507                                 memset(kaddr + bh_offset(bh), 1, bh->b_size);
1508                                 flush_dcache_page(page);
1509                         }
1510                         kunmap_atomic(kaddr, KM_IRQ0);
1511                 }
1512                 set_buffer_uptodate(bh);
1513         } else {
1514                 clear_buffer_uptodate(bh);
1515                 if (printk_ratelimit())
1516                         buffer_io_error(bh);
1517                 SetPageError(page);
1518         }
1519
1520         /*
1521          * Be _very_ careful from here on. Bad things can happen if
1522          * two buffer heads end IO at almost the same time and both
1523          * decide that the page is now completely done.
1524          */
1525         first = page_buffers(page);
1526         local_irq_save(flags);
1527         bit_spin_lock(BH_Uptodate_Lock, &first->b_state);
1528         clear_buffer_async_read(bh);
1529         unlock_buffer(bh);
1530         tmp = bh;
1531         do {
1532                 if (!buffer_uptodate(tmp))
1533                         page_uptodate = 0;
1534                 if (buffer_async_read(tmp)) {
1535                         BUG_ON(!buffer_locked(tmp));
1536                         goto still_busy;
1537                 }
1538                 tmp = tmp->b_this_page;
1539         } while (tmp != bh);
1540         bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
1541         local_irq_restore(flags);
1542
1543         /*
1544          * If none of the buffers had errors and they are all
1545          * uptodate then we can set the page uptodate.
1546          */
1547         if (page_uptodate && !PageError(page))
1548                 SetPageUptodate(page);
1549         unlock_page(page);
1550         return;
1551
1552 still_busy:
1553         bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
1554         local_irq_restore(flags);
1555         return;
1556 }
1557
1558 /*
1559  * Generic "read page" function for block devices that have the normal
1560  * get_block functionality. This is most of the block device filesystems.
1561  * Reads the page asynchronously --- the unlock_buffer() and
1562  * set/clear_buffer_uptodate() functions propagate buffer state into the
1563  * page struct once IO has completed.
1564  */
1565 int btrfs_readpage(struct file *file, struct page *page)
1566 {
1567         struct inode *inode = page->mapping->host;
1568         sector_t iblock, lblock;
1569         struct buffer_head *bh, *head, *arr[MAX_BUF_PER_PAGE];
1570         unsigned int blocksize;
1571         int nr, i;
1572         int fully_mapped = 1;
1573
1574         BUG_ON(!PageLocked(page));
1575         blocksize = 1 << inode->i_blkbits;
1576         if (!page_has_buffers(page))
1577                 create_empty_buffers(page, blocksize, 0);
1578         head = page_buffers(page);
1579
1580         iblock = (sector_t)page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
1581         lblock = (i_size_read(inode)+blocksize-1) >> inode->i_blkbits;
1582         bh = head;
1583         nr = 0;
1584         i = 0;
1585
1586         do {
1587                 if (buffer_uptodate(bh))
1588                         continue;
1589
1590                 if (!buffer_mapped(bh)) {
1591                         int err = 0;
1592
1593                         fully_mapped = 0;
1594                         if (iblock < lblock) {
1595                                 WARN_ON(bh->b_size != blocksize);
1596                                 err = btrfs_get_block_csum(inode, iblock,
1597                                                            bh, 0);
1598                                 if (err)
1599                                         SetPageError(page);
1600                         }
1601                         if (!buffer_mapped(bh)) {
1602                                 void *kaddr = kmap_atomic(page, KM_USER0);
1603                                 memset(kaddr + i * blocksize, 0, blocksize);
1604                                 flush_dcache_page(page);
1605                                 kunmap_atomic(kaddr, KM_USER0);
1606                                 if (!err)
1607                                         set_buffer_uptodate(bh);
1608                                 continue;
1609                         }
1610                         /*
1611                          * get_block() might have updated the buffer
1612                          * synchronously
1613                          */
1614                         if (buffer_uptodate(bh))
1615                                 continue;
1616                 }
1617                 arr[nr++] = bh;
1618         } while (i++, iblock++, (bh = bh->b_this_page) != head);
1619
1620         if (fully_mapped)
1621                 SetPageMappedToDisk(page);
1622
1623         if (!nr) {
1624                 /*
1625                  * All buffers are uptodate - we can set the page uptodate
1626                  * as well. But not if get_block() returned an error.
1627                  */
1628                 if (!PageError(page))
1629                         SetPageUptodate(page);
1630                 unlock_page(page);
1631                 return 0;
1632         }
1633
1634         /* Stage two: lock the buffers */
1635         for (i = 0; i < nr; i++) {
1636                 bh = arr[i];
1637                 lock_buffer(bh);
1638                 bh->b_end_io = btrfs_end_buffer_async_read;
1639                 set_buffer_async_read(bh);
1640         }
1641
1642         /*
1643          * Stage 3: start the IO.  Check for uptodateness
1644          * inside the buffer lock in case another process reading
1645          * the underlying blockdev brought it uptodate (the sct fix).
1646          */
1647         for (i = 0; i < nr; i++) {
1648                 bh = arr[i];
1649                 if (buffer_uptodate(bh))
1650                         btrfs_end_buffer_async_read(bh, 1);
1651                 else
1652                         submit_bh(READ, bh);
1653         }
1654         return 0;
1655 }
1656
1657 /*
1658  * Aside from a tiny bit of packed file data handling, this is the
1659  * same as the generic code.
1660  *
1661  * While block_write_full_page is writing back the dirty buffers under
1662  * the page lock, whoever dirtied the buffers may decide to clean them
1663  * again at any time.  We handle that by only looking at the buffer
1664  * state inside lock_buffer().
1665  *
1666  * If block_write_full_page() is called for regular writeback
1667  * (wbc->sync_mode == WB_SYNC_NONE) then it will redirty a page which has a
1668  * locked buffer.   This only can happen if someone has written the buffer
1669  * directly, with submit_bh().  At the address_space level PageWriteback
1670  * prevents this contention from occurring.
1671  */
1672 static int __btrfs_write_full_page(struct inode *inode, struct page *page,
1673                                    struct writeback_control *wbc)
1674 {
1675         int err;
1676         sector_t block;
1677         sector_t last_block;
1678         struct buffer_head *bh, *head;
1679         const unsigned blocksize = 1 << inode->i_blkbits;
1680         int nr_underway = 0;
1681         struct btrfs_root *root = BTRFS_I(inode)->root;
1682
1683         BUG_ON(!PageLocked(page));
1684
1685         last_block = (i_size_read(inode) - 1) >> inode->i_blkbits;
1686
1687         /* no csumming allowed when from PF_MEMALLOC */
1688         if (current->flags & PF_MEMALLOC) {
1689                 redirty_page_for_writepage(wbc, page);
1690                 unlock_page(page);
1691                 return 0;
1692         }
1693
1694         if (!page_has_buffers(page)) {
1695                 create_empty_buffers(page, blocksize,
1696                                         (1 << BH_Dirty)|(1 << BH_Uptodate));
1697         }
1698
1699         /*
1700          * Be very careful.  We have no exclusion from __set_page_dirty_buffers
1701          * here, and the (potentially unmapped) buffers may become dirty at
1702          * any time.  If a buffer becomes dirty here after we've inspected it
1703          * then we just miss that fact, and the page stays dirty.
1704          *
1705          * Buffers outside i_size may be dirtied by __set_page_dirty_buffers;
1706          * handle that here by just cleaning them.
1707          */
1708
1709         block = (sector_t)page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
1710         head = page_buffers(page);
1711         bh = head;
1712
1713         /*
1714          * Get all the dirty buffers mapped to disk addresses and
1715          * handle any aliases from the underlying blockdev's mapping.
1716          */
1717         do {
1718                 if (block > last_block) {
1719                         /*
1720                          * mapped buffers outside i_size will occur, because
1721                          * this page can be outside i_size when there is a
1722                          * truncate in progress.
1723                          */
1724                         /*
1725                          * The buffer was zeroed by block_write_full_page()
1726                          */
1727                         clear_buffer_dirty(bh);
1728                         set_buffer_uptodate(bh);
1729                 } else if (!buffer_mapped(bh) && buffer_dirty(bh)) {
1730                         WARN_ON(bh->b_size != blocksize);
1731                         err = btrfs_get_block(inode, block, bh, 0);
1732                         if (err) {
1733                                 goto recover;
1734                         }
1735                         if (buffer_new(bh)) {
1736                                 /* blockdev mappings never come here */
1737                                 clear_buffer_new(bh);
1738                         }
1739                 }
1740                 bh = bh->b_this_page;
1741                 block++;
1742         } while (bh != head);
1743
1744         do {
1745                 if (!buffer_mapped(bh))
1746                         continue;
1747                 /*
1748                  * If it's a fully non-blocking write attempt and we cannot
1749                  * lock the buffer then redirty the page.  Note that this can
1750                  * potentially cause a busy-wait loop from pdflush and kswapd
1751                  * activity, but those code paths have their own higher-level
1752                  * throttling.
1753                  */
1754                 if (wbc->sync_mode != WB_SYNC_NONE || !wbc->nonblocking) {
1755                         lock_buffer(bh);
1756                 } else if (test_set_buffer_locked(bh)) {
1757                         redirty_page_for_writepage(wbc, page);
1758                         continue;
1759                 }
1760                 if (test_clear_buffer_dirty(bh) && bh->b_blocknr != 0) {
1761                         struct btrfs_trans_handle *trans;
1762                         int ret;
1763                         u64 off = page->index << PAGE_CACHE_SHIFT;
1764                         char *kaddr;
1765
1766                         off += bh_offset(bh);
1767                         mutex_lock(&root->fs_info->fs_mutex);
1768                         trans = btrfs_start_transaction(root, 1);
1769                         btrfs_set_trans_block_group(trans, inode);
1770                         kaddr = kmap(page);
1771                         btrfs_csum_file_block(trans, root, inode->i_ino,
1772                                                     off, kaddr + bh_offset(bh),
1773                                                     bh->b_size);
1774                         kunmap(page);
1775                         ret = btrfs_end_transaction(trans, root);
1776                         BUG_ON(ret);
1777                         mutex_unlock(&root->fs_info->fs_mutex);
1778                         mark_buffer_async_write(bh);
1779                 } else {
1780                         unlock_buffer(bh);
1781                 }
1782         } while ((bh = bh->b_this_page) != head);
1783
1784         /*
1785          * The page and its buffers are protected by PageWriteback(), so we can
1786          * drop the bh refcounts early.
1787          */
1788         BUG_ON(PageWriteback(page));
1789         set_page_writeback(page);
1790
1791         do {
1792                 struct buffer_head *next = bh->b_this_page;
1793                 if (buffer_async_write(bh)) {
1794                         submit_bh(WRITE, bh);
1795                         nr_underway++;
1796                 }
1797                 bh = next;
1798         } while (bh != head);
1799         unlock_page(page);
1800
1801         err = 0;
1802 done:
1803         if (nr_underway == 0) {
1804                 /*
1805                  * The page was marked dirty, but the buffers were
1806                  * clean.  Someone wrote them back by hand with
1807                  * ll_rw_block/submit_bh.  A rare case.
1808                  */
1809                 int uptodate = 1;
1810                 do {
1811                         if (!buffer_uptodate(bh)) {
1812                                 uptodate = 0;
1813                                 break;
1814                         }
1815                         bh = bh->b_this_page;
1816                 } while (bh != head);
1817                 if (uptodate)
1818                         SetPageUptodate(page);
1819                 end_page_writeback(page);
1820         }
1821         return err;
1822
1823 recover:
1824         /*
1825          * ENOSPC, or some other error.  We may already have added some
1826          * blocks to the file, so we need to write these out to avoid
1827          * exposing stale data.
1828          * The page is currently locked and not marked for writeback
1829          */
1830         bh = head;
1831         /* Recovery: lock and submit the mapped buffers */
1832         do {
1833                 if (buffer_mapped(bh) && buffer_dirty(bh)) {
1834                         lock_buffer(bh);
1835                         mark_buffer_async_write(bh);
1836                 } else {
1837                         /*
1838                          * The buffer may have been set dirty during
1839                          * attachment to a dirty page.
1840                          */
1841                         clear_buffer_dirty(bh);
1842                 }
1843         } while ((bh = bh->b_this_page) != head);
1844         SetPageError(page);
1845         BUG_ON(PageWriteback(page));
1846         set_page_writeback(page);
1847         do {
1848                 struct buffer_head *next = bh->b_this_page;
1849                 if (buffer_async_write(bh)) {
1850                         clear_buffer_dirty(bh);
1851                         submit_bh(WRITE, bh);
1852                         nr_underway++;
1853                 }
1854                 bh = next;
1855         } while (bh != head);
1856         unlock_page(page);
1857         goto done;
1858 }
1859
1860 static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
1861 {
1862         struct inode * const inode = page->mapping->host;
1863         loff_t i_size = i_size_read(inode);
1864         const pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT;
1865         unsigned offset;
1866         void *kaddr;
1867
1868         /* Is the page fully inside i_size? */
1869         if (page->index < end_index)
1870                 return __btrfs_write_full_page(inode, page, wbc);
1871
1872         /* Is the page fully outside i_size? (truncate in progress) */
1873         offset = i_size & (PAGE_CACHE_SIZE-1);
1874         if (page->index >= end_index+1 || !offset) {
1875                 /*
1876                  * The page may have dirty, unmapped buffers.  For example,
1877                  * they may have been added in ext3_writepage().  Make them
1878                  * freeable here, so the page does not leak.
1879                  */
1880                 block_invalidatepage(page, 0);
1881                 unlock_page(page);
1882                 return 0; /* don't care */
1883         }
1884
1885         /*
1886          * The page straddles i_size.  It must be zeroed out on each and every
1887          * writepage invokation because it may be mmapped.  "A file is mapped
1888          * in multiples of the page size.  For a file that is not a multiple of
1889          * the  page size, the remaining memory is zeroed when mapped, and
1890          * writes to that region are not written out to the file."
1891          */
1892         kaddr = kmap_atomic(page, KM_USER0);
1893         memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
1894         flush_dcache_page(page);
1895         kunmap_atomic(kaddr, KM_USER0);
1896         return __btrfs_write_full_page(inode, page, wbc);
1897 }
1898
1899 /*
1900  * btrfs_page_mkwrite() is not allowed to change the file size as it gets
1901  * called from a page fault handler when a page is first dirtied. Hence we must
1902  * be careful to check for EOF conditions here. We set the page up correctly
1903  * for a written page which means we get ENOSPC checking when writing into
1904  * holes and correct delalloc and unwritten extent mapping on filesystems that
1905  * support these features.
1906  *
1907  * We are not allowed to take the i_mutex here so we have to play games to
1908  * protect against truncate races as the page could now be beyond EOF.  Because
1909  * vmtruncate() writes the inode size before removing pages, once we have the
1910  * page lock we can determine safely if the page is beyond EOF. If it is not
1911  * beyond EOF, then the page is guaranteed safe against truncation until we
1912  * unlock the page.
1913  */
1914 int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
1915 {
1916         struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
1917         unsigned long end;
1918         loff_t size;
1919         int ret = -EINVAL;
1920
1921         lock_page(page);
1922         wait_on_page_writeback(page);
1923         size = i_size_read(inode);
1924         if ((page->mapping != inode->i_mapping) ||
1925             ((page->index << PAGE_CACHE_SHIFT) > size)) {
1926                 /* page got truncated out from underneath us */
1927                 goto out_unlock;
1928         }
1929
1930         /* page is wholly or partially inside EOF */
1931         if (((page->index + 1) << PAGE_CACHE_SHIFT) > size)
1932                 end = size & ~PAGE_CACHE_MASK;
1933         else
1934                 end = PAGE_CACHE_SIZE;
1935
1936         ret = btrfs_prepare_write(NULL, page, 0, end);
1937         if (!ret)
1938                 ret = btrfs_commit_write(NULL, page, 0, end);
1939
1940 out_unlock:
1941         unlock_page(page);
1942         return ret;
1943 }
1944
1945 static void btrfs_truncate(struct inode *inode)
1946 {
1947         struct btrfs_root *root = BTRFS_I(inode)->root;
1948         int ret;
1949         struct btrfs_trans_handle *trans;
1950
1951         if (!S_ISREG(inode->i_mode))
1952                 return;
1953         if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
1954                 return;
1955
1956         btrfs_truncate_page(inode->i_mapping, inode->i_size);
1957
1958         mutex_lock(&root->fs_info->fs_mutex);
1959         trans = btrfs_start_transaction(root, 1);
1960         btrfs_set_trans_block_group(trans, inode);
1961
1962         /* FIXME, add redo link to tree so we don't leak on crash */
1963         ret = btrfs_truncate_in_trans(trans, root, inode);
1964         btrfs_update_inode(trans, root, inode);
1965         ret = btrfs_end_transaction(trans, root);
1966         BUG_ON(ret);
1967         mutex_unlock(&root->fs_info->fs_mutex);
1968         btrfs_btree_balance_dirty(root);
1969 }
1970
1971 int btrfs_commit_write(struct file *file, struct page *page,
1972                        unsigned from, unsigned to)
1973 {
1974         struct inode *inode = page->mapping->host;
1975         struct buffer_head *bh;
1976         loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
1977
1978         SetPageUptodate(page);
1979         bh = page_buffers(page);
1980         set_buffer_uptodate(bh);
1981         if (buffer_mapped(bh) && bh->b_blocknr != 0) {
1982                 set_page_dirty(page);
1983         }
1984         if (pos > inode->i_size) {
1985                 i_size_write(inode, pos);
1986                 mark_inode_dirty(inode);
1987         }
1988         return 0;
1989 }
1990
1991 static int create_subvol(struct btrfs_root *root, char *name, int namelen)
1992 {
1993         struct btrfs_trans_handle *trans;
1994         struct btrfs_key key;
1995         struct btrfs_root_item root_item;
1996         struct btrfs_inode_item *inode_item;
1997         struct buffer_head *subvol;
1998         struct btrfs_leaf *leaf;
1999         struct btrfs_root *new_root;
2000         struct inode *inode;
2001         struct inode *dir;
2002         int ret;
2003         int err;
2004         u64 objectid;
2005         u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
2006
2007         mutex_lock(&root->fs_info->fs_mutex);
2008         trans = btrfs_start_transaction(root, 1);
2009         BUG_ON(!trans);
2010
2011         subvol = btrfs_alloc_free_block(trans, root, 0);
2012         if (IS_ERR(subvol))
2013                 return PTR_ERR(subvol);
2014         leaf = btrfs_buffer_leaf(subvol);
2015         btrfs_set_header_nritems(&leaf->header, 0);
2016         btrfs_set_header_level(&leaf->header, 0);
2017         btrfs_set_header_blocknr(&leaf->header, bh_blocknr(subvol));
2018         btrfs_set_header_generation(&leaf->header, trans->transid);
2019         btrfs_set_header_owner(&leaf->header, root->root_key.objectid);
2020         memcpy(leaf->header.fsid, root->fs_info->disk_super->fsid,
2021                sizeof(leaf->header.fsid));
2022         btrfs_mark_buffer_dirty(subvol);
2023
2024         inode_item = &root_item.inode;
2025         memset(inode_item, 0, sizeof(*inode_item));
2026         btrfs_set_inode_generation(inode_item, 1);
2027         btrfs_set_inode_size(inode_item, 3);
2028         btrfs_set_inode_nlink(inode_item, 1);
2029         btrfs_set_inode_nblocks(inode_item, 1);
2030         btrfs_set_inode_mode(inode_item, S_IFDIR | 0755);
2031
2032         btrfs_set_root_blocknr(&root_item, bh_blocknr(subvol));
2033         btrfs_set_root_refs(&root_item, 1);
2034         memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
2035         root_item.drop_level = 0;
2036         brelse(subvol);
2037         subvol = NULL;
2038
2039         ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
2040                                        0, &objectid);
2041         if (ret)
2042                 goto fail;
2043
2044         btrfs_set_root_dirid(&root_item, new_dirid);
2045
2046         key.objectid = objectid;
2047         key.offset = 1;
2048         key.flags = 0;
2049         btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
2050         ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
2051                                 &root_item);
2052         if (ret)
2053                 goto fail;
2054
2055         /*
2056          * insert the directory item
2057          */
2058         key.offset = (u64)-1;
2059         dir = root->fs_info->sb->s_root->d_inode;
2060         ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
2061                                     name, namelen, dir->i_ino, &key,
2062                                     BTRFS_FT_DIR);
2063         if (ret)
2064                 goto fail;
2065
2066         ret = btrfs_commit_transaction(trans, root);
2067         if (ret)
2068                 goto fail_commit;
2069
2070         new_root = btrfs_read_fs_root(root->fs_info, &key);
2071         BUG_ON(!new_root);
2072
2073         trans = btrfs_start_transaction(new_root, 1);
2074         BUG_ON(!trans);
2075
2076         inode = btrfs_new_inode(trans, new_root, new_dirid,
2077                                 BTRFS_I(dir)->block_group, S_IFDIR | 0700);
2078         if (IS_ERR(inode))
2079                 goto fail;
2080         inode->i_op = &btrfs_dir_inode_operations;
2081         inode->i_fop = &btrfs_dir_file_operations;
2082         new_root->inode = inode;
2083
2084         ret = btrfs_make_empty_dir(trans, new_root, new_dirid, new_dirid);
2085         if (ret)
2086                 goto fail;
2087
2088         inode->i_nlink = 1;
2089         inode->i_size = 6;
2090         ret = btrfs_update_inode(trans, new_root, inode);
2091         if (ret)
2092                 goto fail;
2093 fail:
2094         err = btrfs_commit_transaction(trans, root);
2095         if (err && !ret)
2096                 ret = err;
2097 fail_commit:
2098         mutex_unlock(&root->fs_info->fs_mutex);
2099         btrfs_btree_balance_dirty(root);
2100         return ret;
2101 }
2102
2103 static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
2104 {
2105         struct btrfs_trans_handle *trans;
2106         struct btrfs_key key;
2107         struct btrfs_root_item new_root_item;
2108         int ret;
2109         int err;
2110         u64 objectid;
2111
2112         if (!root->ref_cows)
2113                 return -EINVAL;
2114
2115         mutex_lock(&root->fs_info->fs_mutex);
2116         trans = btrfs_start_transaction(root, 1);
2117         BUG_ON(!trans);
2118
2119         ret = btrfs_update_inode(trans, root, root->inode);
2120         if (ret)
2121                 goto fail;
2122
2123         ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
2124                                        0, &objectid);
2125         if (ret)
2126                 goto fail;
2127
2128         memcpy(&new_root_item, &root->root_item,
2129                sizeof(new_root_item));
2130
2131         key.objectid = objectid;
2132         key.offset = 1;
2133         key.flags = 0;
2134         btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
2135         btrfs_set_root_blocknr(&new_root_item, bh_blocknr(root->node));
2136
2137         ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
2138                                 &new_root_item);
2139         if (ret)
2140                 goto fail;
2141
2142         /*
2143          * insert the directory item
2144          */
2145         key.offset = (u64)-1;
2146         ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
2147                                     name, namelen,
2148                                     root->fs_info->sb->s_root->d_inode->i_ino,
2149                                     &key, BTRFS_FT_DIR);
2150
2151         if (ret)
2152                 goto fail;
2153
2154         ret = btrfs_inc_root_ref(trans, root);
2155         if (ret)
2156                 goto fail;
2157
2158 fail:
2159         err = btrfs_commit_transaction(trans, root);
2160         if (err && !ret)
2161                 ret = err;
2162         mutex_unlock(&root->fs_info->fs_mutex);
2163         btrfs_btree_balance_dirty(root);
2164         return ret;
2165 }
2166
2167 int btrfs_ioctl(struct inode *inode, struct file *filp, unsigned int
2168                 cmd, unsigned long arg)
2169 {
2170         struct btrfs_root *root = BTRFS_I(inode)->root;
2171         struct btrfs_ioctl_vol_args vol_args;
2172         int ret = 0;
2173         struct btrfs_dir_item *di;
2174         int namelen;
2175         struct btrfs_path *path;
2176         u64 root_dirid;
2177
2178         switch (cmd) {
2179         case BTRFS_IOC_SNAP_CREATE:
2180                 if (copy_from_user(&vol_args,
2181                                    (struct btrfs_ioctl_vol_args __user *)arg,
2182                                    sizeof(vol_args)))
2183                         return -EFAULT;
2184                 namelen = strlen(vol_args.name);
2185                 if (namelen > BTRFS_VOL_NAME_MAX)
2186                         return -EINVAL;
2187                 if (strchr(vol_args.name, '/'))
2188                         return -EINVAL;
2189                 path = btrfs_alloc_path();
2190                 if (!path)
2191                         return -ENOMEM;
2192                 root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
2193                 mutex_lock(&root->fs_info->fs_mutex);
2194                 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
2195                                     path, root_dirid,
2196                                     vol_args.name, namelen, 0);
2197                 mutex_unlock(&root->fs_info->fs_mutex);
2198                 btrfs_free_path(path);
2199                 if (di && !IS_ERR(di))
2200                         return -EEXIST;
2201                 if (IS_ERR(di))
2202                         return PTR_ERR(di);
2203
2204                 if (root == root->fs_info->tree_root)
2205                         ret = create_subvol(root, vol_args.name, namelen);
2206                 else
2207                         ret = create_snapshot(root, vol_args.name, namelen);
2208                 break;
2209         default:
2210                 return -ENOTTY;
2211         }
2212         return ret;
2213 }
2214
2215 #ifdef CONFIG_COMPAT
2216 long btrfs_compat_ioctl(struct file *file, unsigned int cmd,
2217                                unsigned long arg)
2218 {
2219         struct inode *inode = file->f_path.dentry->d_inode;
2220         int ret;
2221         lock_kernel();
2222         ret = btrfs_ioctl(inode, file, cmd, (unsigned long) compat_ptr(arg));
2223         unlock_kernel();
2224         return ret;
2225
2226 }
2227 #endif
2228
2229 /*
2230  * Called inside transaction, so use GFP_NOFS
2231  */
2232 struct inode *btrfs_alloc_inode(struct super_block *sb)
2233 {
2234         struct btrfs_inode *ei;
2235
2236         ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
2237         if (!ei)
2238                 return NULL;
2239         return &ei->vfs_inode;
2240 }
2241
2242 void btrfs_destroy_inode(struct inode *inode)
2243 {
2244         WARN_ON(!list_empty(&inode->i_dentry));
2245         WARN_ON(inode->i_data.nrpages);
2246
2247         kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
2248 }
2249
2250 static void init_once(void * foo, struct kmem_cache * cachep,
2251                       unsigned long flags)
2252 {
2253         struct btrfs_inode *ei = (struct btrfs_inode *) foo;
2254
2255         inode_init_once(&ei->vfs_inode);
2256 }
2257
2258 void btrfs_destroy_cachep(void)
2259 {
2260         if (btrfs_inode_cachep)
2261                 kmem_cache_destroy(btrfs_inode_cachep);
2262         if (btrfs_trans_handle_cachep)
2263                 kmem_cache_destroy(btrfs_trans_handle_cachep);
2264         if (btrfs_transaction_cachep)
2265                 kmem_cache_destroy(btrfs_transaction_cachep);
2266         if (btrfs_bit_radix_cachep)
2267                 kmem_cache_destroy(btrfs_bit_radix_cachep);
2268         if (btrfs_path_cachep)
2269                 kmem_cache_destroy(btrfs_path_cachep);
2270 }
2271
2272 int btrfs_init_cachep(void)
2273 {
2274         btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache",
2275                                              sizeof(struct btrfs_inode),
2276                                              0, (SLAB_RECLAIM_ACCOUNT|
2277                                                 SLAB_MEM_SPREAD),
2278                                              init_once, NULL);
2279         if (!btrfs_inode_cachep)
2280                 goto fail;
2281         btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache",
2282                                              sizeof(struct btrfs_trans_handle),
2283                                              0, (SLAB_RECLAIM_ACCOUNT|
2284                                                 SLAB_MEM_SPREAD),
2285                                              NULL, NULL);
2286         if (!btrfs_trans_handle_cachep)
2287                 goto fail;
2288         btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache",
2289                                              sizeof(struct btrfs_transaction),
2290                                              0, (SLAB_RECLAIM_ACCOUNT|
2291                                                 SLAB_MEM_SPREAD),
2292                                              NULL, NULL);
2293         if (!btrfs_transaction_cachep)
2294                 goto fail;
2295         btrfs_path_cachep = kmem_cache_create("btrfs_path_cache",
2296                                              sizeof(struct btrfs_transaction),
2297                                              0, (SLAB_RECLAIM_ACCOUNT|
2298                                                 SLAB_MEM_SPREAD),
2299                                              NULL, NULL);
2300         if (!btrfs_path_cachep)
2301                 goto fail;
2302         btrfs_bit_radix_cachep = kmem_cache_create("btrfs_radix",
2303                                              256,
2304                                              0, (SLAB_RECLAIM_ACCOUNT|
2305                                                 SLAB_MEM_SPREAD |
2306                                                 SLAB_DESTROY_BY_RCU),
2307                                              NULL, NULL);
2308         if (!btrfs_bit_radix_cachep)
2309                 goto fail;
2310         return 0;
2311 fail:
2312         btrfs_destroy_cachep();
2313         return -ENOMEM;
2314 }
2315
2316 static int btrfs_getattr(struct vfsmount *mnt,
2317                          struct dentry *dentry, struct kstat *stat)
2318 {
2319         struct inode *inode = dentry->d_inode;
2320         generic_fillattr(inode, stat);
2321         stat->blksize = 256 * 1024;
2322         return 0;
2323 }
2324
2325 static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
2326                            struct inode * new_dir,struct dentry *new_dentry)
2327 {
2328         struct btrfs_trans_handle *trans;
2329         struct btrfs_root *root = BTRFS_I(old_dir)->root;
2330         struct inode *new_inode = new_dentry->d_inode;
2331         struct inode *old_inode = old_dentry->d_inode;
2332         struct timespec ctime = CURRENT_TIME;
2333         struct btrfs_path *path;
2334         struct btrfs_dir_item *di;
2335         int ret;
2336
2337         if (S_ISDIR(old_inode->i_mode) && new_inode &&
2338             new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
2339                 return -ENOTEMPTY;
2340         }
2341         mutex_lock(&root->fs_info->fs_mutex);
2342         trans = btrfs_start_transaction(root, 1);
2343         btrfs_set_trans_block_group(trans, new_dir);
2344         path = btrfs_alloc_path();
2345         if (!path) {
2346                 ret = -ENOMEM;
2347                 goto out_fail;
2348         }
2349
2350         old_dentry->d_inode->i_nlink++;
2351         old_dir->i_ctime = old_dir->i_mtime = ctime;
2352         new_dir->i_ctime = new_dir->i_mtime = ctime;
2353         old_inode->i_ctime = ctime;
2354         if (S_ISDIR(old_inode->i_mode) && old_dir != new_dir) {
2355                 struct btrfs_key *location = &BTRFS_I(new_dir)->location;
2356                 u64 old_parent_oid;
2357                 di = btrfs_lookup_dir_item(trans, root, path, old_inode->i_ino,
2358                                            "..", 2, -1);
2359                 if (IS_ERR(di)) {
2360                         ret = PTR_ERR(di);
2361                         goto out_fail;
2362                 }
2363                 if (!di) {
2364                         ret = -ENOENT;
2365                         goto out_fail;
2366                 }
2367                 old_parent_oid = btrfs_disk_key_objectid(&di->location);
2368                 ret = btrfs_del_item(trans, root, path);
2369                 if (ret) {
2370                         goto out_fail;
2371                 }
2372                 btrfs_release_path(root, path);
2373
2374                 di = btrfs_lookup_dir_index_item(trans, root, path,
2375                                                  old_inode->i_ino,
2376                                                  old_parent_oid,
2377                                                  "..", 2, -1);
2378                 if (IS_ERR(di)) {
2379                         ret = PTR_ERR(di);
2380                         goto out_fail;
2381                 }
2382                 if (!di) {
2383                         ret = -ENOENT;
2384                         goto out_fail;
2385                 }
2386                 ret = btrfs_del_item(trans, root, path);
2387                 if (ret) {
2388                         goto out_fail;
2389                 }
2390                 btrfs_release_path(root, path);
2391
2392                 ret = btrfs_insert_dir_item(trans, root, "..", 2,
2393                                             old_inode->i_ino, location,
2394                                             BTRFS_FT_DIR);
2395                 if (ret)
2396                         goto out_fail;
2397         }
2398
2399
2400         ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
2401         if (ret)
2402                 goto out_fail;
2403
2404         if (new_inode) {
2405                 new_inode->i_ctime = CURRENT_TIME;
2406                 ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry);
2407                 if (ret)
2408                         goto out_fail;
2409                 if (S_ISDIR(new_inode->i_mode))
2410                         clear_nlink(new_inode);
2411                 else
2412                         drop_nlink(new_inode);
2413                 ret = btrfs_update_inode(trans, root, new_inode);
2414                 if (ret)
2415                         goto out_fail;
2416         }
2417         ret = btrfs_add_link(trans, new_dentry, old_inode);
2418         if (ret)
2419                 goto out_fail;
2420
2421 out_fail:
2422         btrfs_free_path(path);
2423         btrfs_end_transaction(trans, root);
2424         mutex_unlock(&root->fs_info->fs_mutex);
2425         return ret;
2426 }
2427
2428 static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
2429                          const char *symname)
2430 {
2431         struct btrfs_trans_handle *trans;
2432         struct btrfs_root *root = BTRFS_I(dir)->root;
2433         struct btrfs_path *path;
2434         struct btrfs_key key;
2435         struct inode *inode;
2436         int err;
2437         int drop_inode = 0;
2438         u64 objectid;
2439         int name_len;
2440         int datasize;
2441         char *ptr;
2442         struct btrfs_file_extent_item *ei;
2443
2444         name_len = strlen(symname) + 1;
2445         if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
2446                 return -ENAMETOOLONG;
2447         mutex_lock(&root->fs_info->fs_mutex);
2448         trans = btrfs_start_transaction(root, 1);
2449         btrfs_set_trans_block_group(trans, dir);
2450
2451         err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2452         if (err) {
2453                 err = -ENOSPC;
2454                 goto out_unlock;
2455         }
2456
2457         inode = btrfs_new_inode(trans, root, objectid,
2458                                 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO);
2459         err = PTR_ERR(inode);
2460         if (IS_ERR(inode))
2461                 goto out_unlock;
2462
2463         btrfs_set_trans_block_group(trans, inode);
2464         err = btrfs_add_nondir(trans, dentry, inode);
2465         if (err)
2466                 drop_inode = 1;
2467         else {
2468                 inode->i_mapping->a_ops = &btrfs_aops;
2469                 inode->i_fop = &btrfs_file_operations;
2470                 inode->i_op = &btrfs_file_inode_operations;
2471         }
2472         dir->i_sb->s_dirt = 1;
2473         btrfs_update_inode_block_group(trans, inode);
2474         btrfs_update_inode_block_group(trans, dir);
2475         if (drop_inode)
2476                 goto out_unlock;
2477
2478         path = btrfs_alloc_path();
2479         BUG_ON(!path);
2480         key.objectid = inode->i_ino;
2481         key.offset = 0;
2482         key.flags = 0;
2483         btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
2484         datasize = btrfs_file_extent_calc_inline_size(name_len);
2485         err = btrfs_insert_empty_item(trans, root, path, &key,
2486                                       datasize);
2487         if (err) {
2488                 drop_inode = 1;
2489                 goto out_unlock;
2490         }
2491         ei = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
2492                path->slots[0], struct btrfs_file_extent_item);
2493         btrfs_set_file_extent_generation(ei, trans->transid);
2494         btrfs_set_file_extent_type(ei,
2495                                    BTRFS_FILE_EXTENT_INLINE);
2496         ptr = btrfs_file_extent_inline_start(ei);
2497         btrfs_memcpy(root, path->nodes[0]->b_data,
2498                      ptr, symname, name_len);
2499         btrfs_mark_buffer_dirty(path->nodes[0]);
2500         btrfs_free_path(path);
2501         inode->i_op = &btrfs_symlink_inode_operations;
2502         inode->i_mapping->a_ops = &btrfs_symlink_aops;
2503         inode->i_size = name_len - 1;
2504         err = btrfs_update_inode(trans, root, inode);
2505         if (err)
2506                 drop_inode = 1;
2507
2508 out_unlock:
2509         btrfs_end_transaction(trans, root);
2510         mutex_unlock(&root->fs_info->fs_mutex);
2511         if (drop_inode) {
2512                 inode_dec_link_count(inode);
2513                 iput(inode);
2514         }
2515         btrfs_btree_balance_dirty(root);
2516         return err;
2517 }
2518
2519 static struct inode_operations btrfs_dir_inode_operations = {
2520         .lookup         = btrfs_lookup,
2521         .create         = btrfs_create,
2522         .unlink         = btrfs_unlink,
2523         .link           = btrfs_link,
2524         .mkdir          = btrfs_mkdir,
2525         .rmdir          = btrfs_rmdir,
2526         .rename         = btrfs_rename,
2527         .symlink        = btrfs_symlink,
2528         .setattr        = btrfs_setattr,
2529 };
2530
2531 static struct inode_operations btrfs_dir_ro_inode_operations = {
2532         .lookup         = btrfs_lookup,
2533 };
2534
2535 static struct file_operations btrfs_dir_file_operations = {
2536         .llseek         = generic_file_llseek,
2537         .read           = generic_read_dir,
2538         .readdir        = btrfs_readdir,
2539         .ioctl          = btrfs_ioctl,
2540 #ifdef CONFIG_COMPAT
2541         .compat_ioctl   = btrfs_compat_ioctl,
2542 #endif
2543 };
2544
2545 static struct address_space_operations btrfs_aops = {
2546         .readpage       = btrfs_readpage,
2547         .writepage      = btrfs_writepage,
2548         .sync_page      = block_sync_page,
2549         .prepare_write  = btrfs_prepare_write,
2550         .commit_write   = btrfs_commit_write,
2551         .bmap           = btrfs_bmap,
2552 };
2553
2554 static struct address_space_operations btrfs_symlink_aops = {
2555         .readpage       = btrfs_readpage,
2556         .writepage      = btrfs_writepage,
2557 };
2558
2559 static struct inode_operations btrfs_file_inode_operations = {
2560         .truncate       = btrfs_truncate,
2561         .getattr        = btrfs_getattr,
2562         .setattr        = btrfs_setattr,
2563 };
2564
2565 static struct inode_operations btrfs_symlink_inode_operations = {
2566         .readlink       = generic_readlink,
2567         .follow_link    = page_follow_link_light,
2568         .put_link       = page_put_link,
2569 };