Commit | Line | Data |
---|---|---|
2e635a27 | 1 | #include <linux/module.h> |
e20d96d6 | 2 | #include <linux/buffer_head.h> |
2e635a27 CM |
3 | #include <linux/fs.h> |
4 | #include <linux/pagemap.h> | |
5 | #include <linux/highmem.h> | |
6 | #include <linux/time.h> | |
7 | #include <linux/init.h> | |
8 | #include <linux/string.h> | |
9 | #include <linux/smp_lock.h> | |
10 | #include <linux/backing-dev.h> | |
dee26a9f | 11 | #include <linux/mpage.h> |
2e635a27 | 12 | #include "ctree.h" |
e20d96d6 | 13 | #include "disk-io.h" |
d5719762 | 14 | #include "transaction.h" |
2e635a27 CM |
15 | |
16 | #define BTRFS_SUPER_MAGIC 0x9123682E | |
e20d96d6 CM |
17 | |
18 | static struct inode_operations btrfs_dir_inode_operations; | |
19 | static struct super_operations btrfs_super_ops; | |
20 | static struct file_operations btrfs_dir_file_operations; | |
dee26a9f CM |
21 | static struct inode_operations btrfs_file_inode_operations; |
22 | static struct address_space_operations btrfs_aops; | |
23 | static struct file_operations btrfs_file_operations; | |
e20d96d6 | 24 | |
e20d96d6 | 25 | static void btrfs_read_locked_inode(struct inode *inode) |
2e635a27 | 26 | { |
e20d96d6 CM |
27 | struct btrfs_path path; |
28 | struct btrfs_inode_item *inode_item; | |
29 | struct btrfs_root *root = btrfs_sb(inode->i_sb); | |
30 | int ret; | |
f4b9aa8d | 31 | |
e20d96d6 | 32 | btrfs_init_path(&path); |
f4b9aa8d CM |
33 | mutex_lock(&root->fs_info->fs_mutex); |
34 | ||
e20d96d6 CM |
35 | ret = btrfs_lookup_inode(NULL, root, &path, inode->i_ino, 0); |
36 | if (ret) { | |
78fae27e | 37 | btrfs_release_path(root, &path); |
f4b9aa8d CM |
38 | mutex_unlock(&root->fs_info->fs_mutex); |
39 | make_bad_inode(inode); | |
e20d96d6 CM |
40 | return; |
41 | } | |
42 | inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path.nodes[0]), | |
43 | path.slots[0], | |
44 | struct btrfs_inode_item); | |
2e635a27 | 45 | |
e20d96d6 CM |
46 | inode->i_mode = btrfs_inode_mode(inode_item); |
47 | inode->i_nlink = btrfs_inode_nlink(inode_item); | |
48 | inode->i_uid = btrfs_inode_uid(inode_item); | |
49 | inode->i_gid = btrfs_inode_gid(inode_item); | |
50 | inode->i_size = btrfs_inode_size(inode_item); | |
51 | inode->i_atime.tv_sec = btrfs_timespec_sec(&inode_item->atime); | |
52 | inode->i_atime.tv_nsec = btrfs_timespec_nsec(&inode_item->atime); | |
53 | inode->i_mtime.tv_sec = btrfs_timespec_sec(&inode_item->mtime); | |
54 | inode->i_mtime.tv_nsec = btrfs_timespec_nsec(&inode_item->mtime); | |
55 | inode->i_ctime.tv_sec = btrfs_timespec_sec(&inode_item->ctime); | |
56 | inode->i_ctime.tv_nsec = btrfs_timespec_nsec(&inode_item->ctime); | |
57 | inode->i_blocks = btrfs_inode_nblocks(inode_item); | |
58 | inode->i_generation = btrfs_inode_generation(inode_item); | |
e20d96d6 | 59 | btrfs_release_path(root, &path); |
f4b9aa8d | 60 | mutex_unlock(&root->fs_info->fs_mutex); |
e20d96d6 CM |
61 | switch (inode->i_mode & S_IFMT) { |
62 | #if 0 | |
63 | default: | |
64 | init_special_inode(inode, inode->i_mode, | |
65 | btrfs_inode_rdev(inode_item)); | |
66 | break; | |
67 | #endif | |
68 | case S_IFREG: | |
dee26a9f CM |
69 | inode->i_mapping->a_ops = &btrfs_aops; |
70 | inode->i_fop = &btrfs_file_operations; | |
71 | inode->i_op = &btrfs_file_inode_operations; | |
e20d96d6 CM |
72 | break; |
73 | case S_IFDIR: | |
e20d96d6 CM |
74 | inode->i_op = &btrfs_dir_inode_operations; |
75 | inode->i_fop = &btrfs_dir_file_operations; | |
76 | break; | |
77 | case S_IFLNK: | |
e20d96d6 CM |
78 | // inode->i_op = &page_symlink_inode_operations; |
79 | break; | |
2e635a27 | 80 | } |
e20d96d6 | 81 | return; |
2e635a27 CM |
82 | } |
83 | ||
134e9731 CM |
84 | static int btrfs_unlink(struct inode *dir, struct dentry *dentry) |
85 | { | |
86 | struct btrfs_path path; | |
87 | struct btrfs_root *root; | |
88 | struct btrfs_trans_handle *trans; | |
89 | const char *name = dentry->d_name.name; | |
90 | int name_len = dentry->d_name.len; | |
91 | int ret; | |
92 | u64 objectid; | |
93 | struct btrfs_dir_item *di; | |
94 | ||
95 | btrfs_init_path(&path); | |
96 | root = btrfs_sb(dir->i_sb); | |
97 | mutex_lock(&root->fs_info->fs_mutex); | |
98 | trans = btrfs_start_transaction(root, 1); | |
99 | ||
100 | ret = btrfs_lookup_dir_item(trans, root, &path, dir->i_ino, | |
101 | name, name_len, -1); | |
102 | if (ret < 0) | |
103 | goto err; | |
104 | if (ret > 0) { | |
105 | ret = -ENOENT; | |
106 | goto err; | |
107 | } | |
108 | di = btrfs_item_ptr(btrfs_buffer_leaf(path.nodes[0]), path.slots[0], | |
109 | struct btrfs_dir_item); | |
110 | objectid = btrfs_dir_objectid(di); | |
111 | ||
112 | ret = btrfs_del_item(trans, root, &path); | |
113 | BUG_ON(ret); | |
114 | dentry->d_inode->i_ctime = dir->i_ctime; | |
115 | err: | |
116 | btrfs_release_path(root, &path); | |
117 | btrfs_end_transaction(trans, root); | |
118 | mutex_unlock(&root->fs_info->fs_mutex); | |
119 | if (ret == 0) | |
120 | inode_dec_link_count(dentry->d_inode); | |
121 | return ret; | |
122 | } | |
123 | ||
124 | static int btrfs_free_inode(struct btrfs_trans_handle *trans, | |
125 | struct btrfs_root *root, | |
126 | struct inode *inode) | |
127 | { | |
128 | u64 objectid = inode->i_ino; | |
129 | struct btrfs_path path; | |
130 | struct btrfs_inode_map_item *map; | |
131 | struct btrfs_key stat_data_key; | |
132 | int ret; | |
133 | clear_inode(inode); | |
134 | btrfs_init_path(&path); | |
135 | ret = btrfs_lookup_inode_map(trans, root, &path, objectid, -1); | |
136 | if (ret) { | |
137 | if (ret > 0) | |
138 | ret = -ENOENT; | |
139 | btrfs_release_path(root, &path); | |
140 | goto error; | |
141 | } | |
142 | map = btrfs_item_ptr(btrfs_buffer_leaf(path.nodes[0]), path.slots[0], | |
143 | struct btrfs_inode_map_item); | |
144 | btrfs_disk_key_to_cpu(&stat_data_key, &map->key); | |
145 | ret = btrfs_del_item(trans, root->fs_info->inode_root, &path); | |
146 | BUG_ON(ret); | |
147 | btrfs_release_path(root, &path); | |
148 | btrfs_init_path(&path); | |
149 | ||
150 | ret = btrfs_lookup_inode(trans, root, &path, objectid, -1); | |
151 | BUG_ON(ret); | |
152 | ret = btrfs_del_item(trans, root, &path); | |
153 | BUG_ON(ret); | |
154 | btrfs_release_path(root, &path); | |
155 | error: | |
156 | return ret; | |
157 | } | |
158 | ||
f4b9aa8d CM |
159 | static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans, |
160 | struct btrfs_root *root, | |
161 | struct inode *inode) | |
162 | { | |
163 | int ret; | |
164 | struct btrfs_path path; | |
165 | struct btrfs_key key; | |
166 | struct btrfs_disk_key *found_key; | |
167 | struct btrfs_leaf *leaf; | |
168 | struct btrfs_file_extent_item *fi; | |
169 | u64 extent_start; | |
170 | u64 extent_num_blocks; | |
171 | ||
172 | /* FIXME, add redo link to tree so we don't leak on crash */ | |
173 | key.objectid = inode->i_ino; | |
174 | key.offset = (u64)-1; | |
175 | key.flags = 0; | |
176 | btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY); | |
177 | while(1) { | |
178 | btrfs_init_path(&path); | |
179 | ret = btrfs_search_slot(trans, root, &key, &path, -1, 1); | |
180 | if (ret < 0) { | |
181 | btrfs_release_path(root, &path); | |
182 | goto error; | |
183 | } | |
184 | if (ret > 0) { | |
185 | BUG_ON(path.slots[0] == 0); | |
186 | path.slots[0]--; | |
187 | } | |
188 | leaf = btrfs_buffer_leaf(path.nodes[0]); | |
189 | found_key = &leaf->items[path.slots[0]].key; | |
190 | if (btrfs_disk_key_objectid(found_key) != inode->i_ino) | |
191 | break; | |
192 | if (btrfs_disk_key_type(found_key) != BTRFS_EXTENT_DATA_KEY) | |
193 | break; | |
194 | if (btrfs_disk_key_offset(found_key) < inode->i_size) | |
195 | break; | |
196 | /* FIXME: add extent truncation */ | |
197 | if (btrfs_disk_key_offset(found_key) < inode->i_size) | |
198 | break; | |
199 | fi = btrfs_item_ptr(btrfs_buffer_leaf(path.nodes[0]), | |
200 | path.slots[0], | |
201 | struct btrfs_file_extent_item); | |
202 | extent_start = btrfs_file_extent_disk_blocknr(fi); | |
203 | extent_num_blocks = btrfs_file_extent_disk_num_blocks(fi); | |
204 | key.offset = btrfs_disk_key_offset(found_key) - 1; | |
205 | ret = btrfs_del_item(trans, root, &path); | |
206 | BUG_ON(ret); | |
207 | inode->i_blocks -= btrfs_file_extent_num_blocks(fi) >> 9; | |
208 | btrfs_release_path(root, &path); | |
209 | ret = btrfs_free_extent(trans, root, extent_start, | |
210 | extent_num_blocks, 0); | |
211 | BUG_ON(ret); | |
212 | if (btrfs_disk_key_offset(found_key) == 0) | |
213 | break; | |
214 | } | |
215 | btrfs_release_path(root, &path); | |
216 | ret = 0; | |
217 | error: | |
218 | return ret; | |
219 | } | |
220 | ||
134e9731 CM |
221 | static void btrfs_delete_inode(struct inode *inode) |
222 | { | |
223 | struct btrfs_trans_handle *trans; | |
224 | struct btrfs_root *root = btrfs_sb(inode->i_sb); | |
f4b9aa8d CM |
225 | int ret; |
226 | ||
134e9731 CM |
227 | truncate_inode_pages(&inode->i_data, 0); |
228 | if (is_bad_inode(inode)) { | |
229 | goto no_delete; | |
230 | } | |
231 | inode->i_size = 0; | |
134e9731 CM |
232 | mutex_lock(&root->fs_info->fs_mutex); |
233 | trans = btrfs_start_transaction(root, 1); | |
f4b9aa8d CM |
234 | if (S_ISREG(inode->i_mode)) { |
235 | ret = btrfs_truncate_in_trans(trans, root, inode); | |
236 | BUG_ON(ret); | |
237 | } | |
134e9731 CM |
238 | btrfs_free_inode(trans, root, inode); |
239 | btrfs_end_transaction(trans, root); | |
240 | mutex_unlock(&root->fs_info->fs_mutex); | |
241 | return; | |
242 | no_delete: | |
243 | clear_inode(inode); | |
244 | } | |
245 | ||
e20d96d6 CM |
246 | static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry, |
247 | ino_t *ino) | |
248 | { | |
249 | const char *name = dentry->d_name.name; | |
250 | int namelen = dentry->d_name.len; | |
251 | struct btrfs_dir_item *di; | |
252 | struct btrfs_path path; | |
253 | struct btrfs_root *root = btrfs_sb(dir->i_sb); | |
254 | int ret; | |
255 | ||
256 | btrfs_init_path(&path); | |
257 | ret = btrfs_lookup_dir_item(NULL, root, &path, dir->i_ino, name, | |
258 | namelen, 0); | |
7f5c1516 | 259 | if (ret || !btrfs_match_dir_item_name(root, &path, name, namelen)) { |
e20d96d6 CM |
260 | *ino = 0; |
261 | goto out; | |
262 | } | |
263 | di = btrfs_item_ptr(btrfs_buffer_leaf(path.nodes[0]), path.slots[0], | |
264 | struct btrfs_dir_item); | |
265 | *ino = btrfs_dir_objectid(di); | |
266 | out: | |
267 | btrfs_release_path(root, &path); | |
268 | return ret; | |
269 | } | |
270 | ||
271 | static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry, | |
272 | struct nameidata *nd) | |
273 | { | |
274 | struct inode * inode; | |
275 | ino_t ino; | |
276 | int ret; | |
277 | ||
278 | if (dentry->d_name.len > BTRFS_NAME_LEN) | |
279 | return ERR_PTR(-ENAMETOOLONG); | |
280 | ||
281 | ret = btrfs_inode_by_name(dir, dentry, &ino); | |
282 | if (ret < 0) | |
283 | return ERR_PTR(ret); | |
284 | inode = NULL; | |
285 | if (ino) { | |
e20d96d6 CM |
286 | inode = iget(dir->i_sb, ino); |
287 | if (!inode) | |
288 | return ERR_PTR(-EACCES); | |
289 | } | |
290 | return d_splice_alias(inode, dentry); | |
291 | } | |
292 | ||
293 | static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir) | |
294 | { | |
295 | struct inode *inode = filp->f_path.dentry->d_inode; | |
296 | struct btrfs_root *root = btrfs_sb(inode->i_sb); | |
297 | struct btrfs_item *item; | |
298 | struct btrfs_dir_item *di; | |
299 | struct btrfs_key key; | |
300 | struct btrfs_path path; | |
301 | int ret; | |
302 | u32 nritems; | |
303 | struct btrfs_leaf *leaf; | |
304 | int slot; | |
305 | int advance; | |
306 | unsigned char d_type = DT_UNKNOWN; | |
7f5c1516 | 307 | int over = 0; |
e20d96d6 CM |
308 | |
309 | key.objectid = inode->i_ino; | |
e20d96d6 CM |
310 | key.flags = 0; |
311 | btrfs_set_key_type(&key, BTRFS_DIR_ITEM_KEY); | |
312 | key.offset = filp->f_pos; | |
313 | btrfs_init_path(&path); | |
314 | ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0); | |
315 | if (ret < 0) { | |
316 | goto err; | |
317 | } | |
7f5c1516 | 318 | advance = 0; |
e20d96d6 CM |
319 | while(1) { |
320 | leaf = btrfs_buffer_leaf(path.nodes[0]); | |
321 | nritems = btrfs_header_nritems(&leaf->header); | |
322 | slot = path.slots[0]; | |
dee26a9f CM |
323 | if (advance || slot >= nritems) { |
324 | if (slot >= nritems -1) { | |
e20d96d6 CM |
325 | ret = btrfs_next_leaf(root, &path); |
326 | if (ret) | |
327 | break; | |
328 | leaf = btrfs_buffer_leaf(path.nodes[0]); | |
329 | nritems = btrfs_header_nritems(&leaf->header); | |
330 | slot = path.slots[0]; | |
e20d96d6 CM |
331 | } else { |
332 | slot++; | |
333 | path.slots[0]++; | |
334 | } | |
335 | } | |
336 | advance = 1; | |
337 | item = leaf->items + slot; | |
e20d96d6 CM |
338 | if (btrfs_disk_key_objectid(&item->key) != key.objectid) |
339 | break; | |
340 | if (btrfs_disk_key_type(&item->key) != BTRFS_DIR_ITEM_KEY) | |
341 | continue; | |
7f5c1516 CM |
342 | if (btrfs_disk_key_offset(&item->key) < filp->f_pos) |
343 | continue; | |
dee26a9f CM |
344 | |
345 | advance = 1; | |
e20d96d6 | 346 | di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); |
e20d96d6 CM |
347 | over = filldir(dirent, (const char *)(di + 1), |
348 | btrfs_dir_name_len(di), | |
349 | btrfs_disk_key_offset(&item->key), | |
350 | btrfs_dir_objectid(di), d_type); | |
7f5c1516 CM |
351 | if (over) { |
352 | filp->f_pos = btrfs_disk_key_offset(&item->key); | |
e20d96d6 | 353 | break; |
7f5c1516 | 354 | } |
e20d96d6 CM |
355 | filp->f_pos = btrfs_disk_key_offset(&item->key) + 1; |
356 | } | |
e20d96d6 CM |
357 | ret = 0; |
358 | err: | |
359 | btrfs_release_path(root, &path); | |
360 | return ret; | |
361 | } | |
362 | ||
363 | static void btrfs_put_super (struct super_block * sb) | |
364 | { | |
365 | struct btrfs_root *root = btrfs_sb(sb); | |
366 | int ret; | |
367 | ||
368 | ret = close_ctree(root); | |
369 | if (ret) { | |
370 | printk("close ctree returns %d\n", ret); | |
371 | } | |
372 | sb->s_fs_info = NULL; | |
373 | } | |
2e635a27 CM |
374 | |
375 | static int btrfs_fill_super(struct super_block * sb, void * data, int silent) | |
376 | { | |
377 | struct inode * inode; | |
e20d96d6 CM |
378 | struct dentry * root_dentry; |
379 | struct btrfs_super_block *disk_super; | |
380 | struct buffer_head *bh; | |
381 | struct btrfs_root *root; | |
2e635a27 CM |
382 | |
383 | sb->s_maxbytes = MAX_LFS_FILESIZE; | |
384 | sb->s_blocksize = PAGE_CACHE_SIZE; | |
385 | sb->s_blocksize_bits = PAGE_CACHE_SHIFT; | |
386 | sb->s_magic = BTRFS_SUPER_MAGIC; | |
e20d96d6 | 387 | sb->s_op = &btrfs_super_ops; |
2e635a27 | 388 | sb->s_time_gran = 1; |
e20d96d6 CM |
389 | |
390 | bh = sb_bread(sb, BTRFS_SUPER_INFO_OFFSET / sb->s_blocksize); | |
391 | if (!bh) { | |
392 | printk("btrfs: unable to read on disk super\n"); | |
393 | return -EIO; | |
394 | } | |
395 | disk_super = (struct btrfs_super_block *)bh->b_data; | |
396 | root = open_ctree(sb, bh, disk_super); | |
397 | sb->s_fs_info = root; | |
398 | if (!root) { | |
399 | printk("btrfs: open_ctree failed\n"); | |
400 | return -EIO; | |
401 | } | |
402 | printk("read in super total blocks %Lu root %Lu\n", | |
403 | btrfs_super_total_blocks(disk_super), | |
404 | btrfs_super_root_dir(disk_super)); | |
405 | ||
406 | inode = iget_locked(sb, btrfs_super_root_dir(disk_super)); | |
2e635a27 CM |
407 | if (!inode) |
408 | return -ENOMEM; | |
e20d96d6 CM |
409 | if (inode->i_state & I_NEW) { |
410 | btrfs_read_locked_inode(inode); | |
411 | unlock_new_inode(inode); | |
412 | } | |
2e635a27 | 413 | |
e20d96d6 CM |
414 | root_dentry = d_alloc_root(inode); |
415 | if (!root_dentry) { | |
2e635a27 CM |
416 | iput(inode); |
417 | return -ENOMEM; | |
418 | } | |
e20d96d6 CM |
419 | sb->s_root = root_dentry; |
420 | ||
2e635a27 CM |
421 | return 0; |
422 | } | |
423 | ||
d5719762 CM |
424 | static void fill_inode_item(struct btrfs_inode_item *item, |
425 | struct inode *inode) | |
426 | { | |
427 | btrfs_set_inode_uid(item, inode->i_uid); | |
428 | btrfs_set_inode_gid(item, inode->i_gid); | |
429 | btrfs_set_inode_size(item, inode->i_size); | |
430 | btrfs_set_inode_mode(item, inode->i_mode); | |
431 | btrfs_set_inode_nlink(item, inode->i_nlink); | |
432 | btrfs_set_timespec_sec(&item->atime, inode->i_atime.tv_sec); | |
433 | btrfs_set_timespec_nsec(&item->atime, inode->i_atime.tv_nsec); | |
434 | btrfs_set_timespec_sec(&item->mtime, inode->i_mtime.tv_sec); | |
435 | btrfs_set_timespec_nsec(&item->mtime, inode->i_mtime.tv_nsec); | |
436 | btrfs_set_timespec_sec(&item->ctime, inode->i_ctime.tv_sec); | |
437 | btrfs_set_timespec_nsec(&item->ctime, inode->i_ctime.tv_nsec); | |
438 | btrfs_set_inode_nblocks(item, inode->i_blocks); | |
439 | btrfs_set_inode_generation(item, inode->i_generation); | |
440 | } | |
441 | ||
4730a4bc CM |
442 | static int btrfs_update_inode(struct btrfs_trans_handle *trans, |
443 | struct btrfs_root *root, | |
444 | struct inode *inode) | |
445 | { | |
446 | struct btrfs_inode_item *inode_item; | |
447 | struct btrfs_path path; | |
448 | int ret; | |
449 | ||
450 | btrfs_init_path(&path); | |
451 | ||
452 | ret = btrfs_lookup_inode(trans, root, &path, inode->i_ino, 1); | |
453 | if (ret) { | |
454 | if (ret > 0) | |
455 | ret = -ENOENT; | |
456 | goto failed; | |
457 | } | |
458 | ||
459 | inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path.nodes[0]), | |
460 | path.slots[0], | |
461 | struct btrfs_inode_item); | |
462 | ||
463 | fill_inode_item(inode_item, inode); | |
464 | mark_buffer_dirty(path.nodes[0]); | |
465 | failed: | |
466 | btrfs_release_path(root, &path); | |
467 | return 0; | |
468 | } | |
469 | ||
470 | static int btrfs_write_inode(struct inode *inode, int wait) | |
471 | { | |
472 | struct btrfs_root *root = btrfs_sb(inode->i_sb); | |
473 | struct btrfs_trans_handle *trans; | |
474 | int ret; | |
475 | ||
476 | mutex_lock(&root->fs_info->fs_mutex); | |
477 | trans = btrfs_start_transaction(root, 1); | |
478 | ret = btrfs_update_inode(trans, root, inode); | |
479 | if (wait) | |
480 | btrfs_commit_transaction(trans, root); | |
481 | else | |
482 | btrfs_end_transaction(trans, root); | |
483 | mutex_unlock(&root->fs_info->fs_mutex); | |
484 | return ret; | |
485 | } | |
486 | ||
d5719762 CM |
487 | static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans, |
488 | struct inode *dir, int mode) | |
489 | { | |
490 | struct inode *inode; | |
491 | struct btrfs_inode_item inode_item; | |
492 | struct btrfs_root *root = btrfs_sb(dir->i_sb); | |
493 | struct btrfs_key key; | |
494 | int ret; | |
495 | u64 objectid; | |
496 | ||
497 | inode = new_inode(dir->i_sb); | |
498 | if (!inode) | |
499 | return ERR_PTR(-ENOMEM); | |
500 | ||
501 | ret = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid); | |
502 | BUG_ON(ret); | |
503 | ||
504 | inode->i_uid = current->fsuid; | |
505 | inode->i_gid = current->fsgid; | |
506 | inode->i_mode = mode; | |
507 | inode->i_ino = objectid; | |
508 | inode->i_blocks = 0; | |
509 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME_SEC; | |
510 | fill_inode_item(&inode_item, inode); | |
511 | ||
d5719762 CM |
512 | key.objectid = objectid; |
513 | key.flags = 0; | |
514 | key.offset = 0; | |
515 | btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY); | |
516 | ret = btrfs_insert_inode_map(trans, root, objectid, &key); | |
517 | BUG_ON(ret); | |
518 | ||
519 | ret = btrfs_insert_inode(trans, root, objectid, &inode_item); | |
520 | BUG_ON(ret); | |
521 | ||
522 | insert_inode_hash(inode); | |
d5719762 CM |
523 | return inode; |
524 | } | |
525 | ||
526 | static int btrfs_add_link(struct btrfs_trans_handle *trans, | |
527 | struct dentry *dentry, struct inode *inode) | |
528 | { | |
529 | int ret; | |
530 | ret = btrfs_insert_dir_item(trans, btrfs_sb(inode->i_sb), | |
531 | dentry->d_name.name, dentry->d_name.len, | |
532 | dentry->d_parent->d_inode->i_ino, | |
533 | inode->i_ino, 0); | |
4730a4bc CM |
534 | if (ret == 0) { |
535 | dentry->d_parent->d_inode->i_size += dentry->d_name.len; | |
536 | ret = btrfs_update_inode(trans, btrfs_sb(inode->i_sb), | |
537 | dentry->d_parent->d_inode); | |
538 | } | |
539 | ||
d5719762 CM |
540 | return ret; |
541 | } | |
542 | ||
543 | static int btrfs_add_nondir(struct btrfs_trans_handle *trans, | |
544 | struct dentry *dentry, struct inode *inode) | |
545 | { | |
546 | int err = btrfs_add_link(trans, dentry, inode); | |
547 | if (!err) { | |
548 | d_instantiate(dentry, inode); | |
549 | return 0; | |
550 | } | |
d5719762 CM |
551 | return err; |
552 | } | |
553 | ||
554 | static int btrfs_create(struct inode *dir, struct dentry *dentry, | |
555 | int mode, struct nameidata *nd) | |
556 | { | |
557 | struct btrfs_trans_handle *trans; | |
558 | struct btrfs_root *root = btrfs_sb(dir->i_sb); | |
559 | struct inode *inode; | |
560 | int err; | |
134e9731 | 561 | int drop_inode = 0; |
d5719762 | 562 | |
d561c025 | 563 | mutex_lock(&root->fs_info->fs_mutex); |
d5719762 CM |
564 | trans = btrfs_start_transaction(root, 1); |
565 | inode = btrfs_new_inode(trans, dir, mode); | |
566 | err = PTR_ERR(inode); | |
567 | if (IS_ERR(inode)) | |
d561c025 | 568 | goto out_unlock; |
d5719762 CM |
569 | // FIXME mark the inode dirty |
570 | err = btrfs_add_nondir(trans, dentry, inode); | |
134e9731 CM |
571 | if (err) |
572 | drop_inode = 1; | |
dee26a9f CM |
573 | else { |
574 | inode->i_mapping->a_ops = &btrfs_aops; | |
575 | inode->i_fop = &btrfs_file_operations; | |
576 | inode->i_op = &btrfs_file_inode_operations; | |
577 | } | |
d5719762 CM |
578 | dir->i_sb->s_dirt = 1; |
579 | btrfs_end_transaction(trans, root); | |
d561c025 CM |
580 | out_unlock: |
581 | mutex_unlock(&root->fs_info->fs_mutex); | |
134e9731 CM |
582 | if (drop_inode) { |
583 | inode_dec_link_count(inode); | |
584 | iput(inode); | |
585 | } | |
d5719762 CM |
586 | return err; |
587 | } | |
588 | ||
f7922033 CM |
589 | static int btrfs_make_empty_dir(struct btrfs_trans_handle *trans, |
590 | struct inode *inode, struct inode *dir) | |
591 | { | |
592 | struct btrfs_root *root = btrfs_sb(inode->i_sb); | |
593 | int ret; | |
594 | char buf[2]; | |
595 | buf[0] = '.'; | |
596 | buf[1] = '.'; | |
597 | ||
598 | ret = btrfs_insert_dir_item(trans, root, buf, 1, inode->i_ino, | |
599 | inode->i_ino, 1); | |
600 | if (ret) | |
601 | goto error; | |
602 | ret = btrfs_insert_dir_item(trans, root, buf, 2, inode->i_ino, | |
603 | dir->i_ino, 1); | |
4730a4bc CM |
604 | if (ret) |
605 | goto error; | |
606 | inode->i_size = 3; | |
607 | ret = btrfs_update_inode(trans, root, inode); | |
f7922033 CM |
608 | error: |
609 | return ret; | |
610 | } | |
611 | ||
612 | static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode) | |
613 | { | |
614 | struct inode *inode; | |
615 | struct btrfs_trans_handle *trans; | |
616 | struct btrfs_root *root = btrfs_sb(dir->i_sb); | |
617 | int err = 0; | |
618 | int drop_on_err = 0; | |
619 | ||
620 | mutex_lock(&root->fs_info->fs_mutex); | |
621 | trans = btrfs_start_transaction(root, 1); | |
622 | if (IS_ERR(trans)) { | |
623 | err = PTR_ERR(trans); | |
624 | goto out_unlock; | |
625 | } | |
626 | inode = btrfs_new_inode(trans, dir, S_IFDIR | mode); | |
627 | if (IS_ERR(inode)) { | |
628 | err = PTR_ERR(inode); | |
629 | goto out_fail; | |
630 | } | |
631 | drop_on_err = 1; | |
632 | inode->i_op = &btrfs_dir_inode_operations; | |
633 | inode->i_fop = &btrfs_dir_file_operations; | |
634 | ||
635 | err = btrfs_make_empty_dir(trans, inode, dir); | |
636 | if (err) | |
637 | goto out_fail; | |
638 | err = btrfs_add_link(trans, dentry, inode); | |
639 | if (err) | |
640 | goto out_fail; | |
641 | d_instantiate(dentry, inode); | |
f7922033 CM |
642 | drop_on_err = 0; |
643 | ||
644 | out_fail: | |
645 | btrfs_end_transaction(trans, root); | |
646 | out_unlock: | |
647 | mutex_unlock(&root->fs_info->fs_mutex); | |
648 | if (drop_on_err) | |
649 | iput(inode); | |
650 | return err; | |
651 | } | |
652 | ||
d5719762 CM |
653 | static int btrfs_sync_fs(struct super_block *sb, int wait) |
654 | { | |
655 | struct btrfs_trans_handle *trans; | |
656 | struct btrfs_root *root; | |
657 | int ret; | |
df2ce34c | 658 | |
d5719762 | 659 | sb->s_dirt = 0; |
d561c025 CM |
660 | if (!wait) { |
661 | filemap_flush(sb->s_bdev->bd_inode->i_mapping); | |
662 | return 0; | |
663 | } | |
664 | filemap_write_and_wait(sb->s_bdev->bd_inode->i_mapping); | |
df2ce34c | 665 | |
d5719762 | 666 | root = btrfs_sb(sb); |
d561c025 | 667 | mutex_lock(&root->fs_info->fs_mutex); |
d5719762 CM |
668 | trans = btrfs_start_transaction(root, 1); |
669 | ret = btrfs_commit_transaction(trans, root); | |
670 | sb->s_dirt = 0; | |
671 | BUG_ON(ret); | |
672 | printk("btrfs sync_fs\n"); | |
d561c025 | 673 | mutex_unlock(&root->fs_info->fs_mutex); |
d5719762 CM |
674 | return 0; |
675 | } | |
676 | ||
dee26a9f CM |
677 | static int btrfs_get_block(struct inode *inode, sector_t iblock, |
678 | struct buffer_head *result, int create) | |
679 | { | |
680 | int ret; | |
681 | int err = 0; | |
682 | u64 blocknr; | |
683 | u64 extent_start = 0; | |
684 | u64 extent_end = 0; | |
685 | u64 objectid = inode->i_ino; | |
686 | struct btrfs_path path; | |
687 | struct btrfs_root *root = btrfs_sb(inode->i_sb); | |
688 | struct btrfs_trans_handle *trans = NULL; | |
689 | struct btrfs_file_extent_item *item; | |
690 | struct btrfs_leaf *leaf; | |
691 | struct btrfs_disk_key *found_key; | |
692 | ||
693 | btrfs_init_path(&path); | |
694 | mutex_lock(&root->fs_info->fs_mutex); | |
695 | if (create) | |
696 | trans = btrfs_start_transaction(root, 1); | |
697 | ||
698 | ||
699 | ret = btrfs_lookup_file_extent(trans, root, &path, | |
9773a788 CM |
700 | inode->i_ino, |
701 | iblock << inode->i_blkbits, 0); | |
dee26a9f CM |
702 | if (ret < 0) { |
703 | btrfs_release_path(root, &path); | |
704 | err = ret; | |
705 | goto out; | |
706 | } | |
707 | ||
708 | if (ret != 0) { | |
709 | if (path.slots[0] == 0) { | |
710 | btrfs_release_path(root, &path); | |
711 | goto allocate; | |
712 | } | |
713 | path.slots[0]--; | |
714 | } | |
715 | ||
716 | item = btrfs_item_ptr(btrfs_buffer_leaf(path.nodes[0]), path.slots[0], | |
717 | struct btrfs_file_extent_item); | |
718 | leaf = btrfs_buffer_leaf(path.nodes[0]); | |
719 | blocknr = btrfs_file_extent_disk_blocknr(item); | |
720 | blocknr += btrfs_file_extent_offset(item); | |
721 | ||
722 | /* exact match found, use it */ | |
723 | if (ret == 0) { | |
724 | err = 0; | |
725 | map_bh(result, inode->i_sb, blocknr); | |
726 | btrfs_release_path(root, &path); | |
727 | goto out; | |
728 | } | |
729 | ||
730 | /* are we inside the extent that was found? */ | |
731 | found_key = &leaf->items[path.slots[0]].key; | |
732 | if (btrfs_disk_key_objectid(found_key) != objectid || | |
733 | btrfs_disk_key_type(found_key) != BTRFS_EXTENT_DATA_KEY) { | |
734 | extent_end = 0; | |
735 | extent_start = 0; | |
736 | btrfs_release_path(root, &path); | |
737 | goto allocate; | |
738 | } | |
739 | ||
740 | extent_start = btrfs_disk_key_offset(&leaf->items[path.slots[0]].key); | |
9773a788 | 741 | extent_start = extent_start >> inode->i_blkbits; |
dee26a9f CM |
742 | extent_start += btrfs_file_extent_offset(item); |
743 | extent_end = extent_start + btrfs_file_extent_num_blocks(item); | |
744 | btrfs_release_path(root, &path); | |
745 | if (iblock >= extent_start && iblock < extent_end) { | |
746 | err = 0; | |
747 | map_bh(result, inode->i_sb, blocknr + iblock - extent_start); | |
748 | goto out; | |
749 | } | |
750 | allocate: | |
751 | /* ok, create a new extent */ | |
752 | if (!create) { | |
753 | err = 0; | |
754 | goto out; | |
755 | } | |
9773a788 CM |
756 | ret = btrfs_alloc_file_extent(trans, root, objectid, |
757 | iblock << inode->i_blkbits, | |
dee26a9f CM |
758 | 1, extent_end, &blocknr); |
759 | if (ret) { | |
760 | err = ret; | |
761 | goto out; | |
762 | } | |
f4b9aa8d CM |
763 | inode->i_blocks += inode->i_sb->s_blocksize >> 9; |
764 | set_buffer_new(result); | |
dee26a9f CM |
765 | map_bh(result, inode->i_sb, blocknr); |
766 | ||
767 | out: | |
768 | if (trans) | |
769 | btrfs_end_transaction(trans, root); | |
770 | mutex_unlock(&root->fs_info->fs_mutex); | |
771 | return err; | |
772 | } | |
773 | ||
774 | static int btrfs_prepare_write(struct file *file, struct page *page, | |
775 | unsigned from, unsigned to) | |
776 | { | |
6407bf6d | 777 | return nobh_prepare_write(page, from, to, btrfs_get_block); |
dee26a9f CM |
778 | } |
779 | ||
d561c025 CM |
780 | static void btrfs_write_super(struct super_block *sb) |
781 | { | |
782 | btrfs_sync_fs(sb, 1); | |
783 | } | |
784 | ||
dee26a9f CM |
785 | static int btrfs_readpage(struct file *file, struct page *page) |
786 | { | |
787 | return mpage_readpage(page, btrfs_get_block); | |
788 | } | |
789 | ||
790 | static int btrfs_readpages(struct file *file, struct address_space *mapping, | |
791 | struct list_head *pages, unsigned nr_pages) | |
792 | { | |
793 | return mpage_readpages(mapping, pages, nr_pages, btrfs_get_block); | |
794 | } | |
795 | ||
796 | static int btrfs_writepage(struct page *page, struct writeback_control *wbc) | |
797 | { | |
6407bf6d | 798 | return nobh_writepage(page, btrfs_get_block, wbc); |
dee26a9f | 799 | } |
d561c025 | 800 | |
f4b9aa8d CM |
801 | static void btrfs_truncate(struct inode *inode) |
802 | { | |
803 | struct btrfs_root *root = btrfs_sb(inode->i_sb); | |
804 | int ret; | |
805 | struct btrfs_trans_handle *trans; | |
806 | ||
807 | if (!S_ISREG(inode->i_mode)) | |
808 | return; | |
809 | if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) | |
810 | return; | |
811 | ||
812 | nobh_truncate_page(inode->i_mapping, inode->i_size); | |
813 | ||
814 | /* FIXME, add redo link to tree so we don't leak on crash */ | |
815 | mutex_lock(&root->fs_info->fs_mutex); | |
816 | trans = btrfs_start_transaction(root, 1); | |
817 | ret = btrfs_truncate_in_trans(trans, root, inode); | |
818 | BUG_ON(ret); | |
819 | ret = btrfs_end_transaction(trans, root); | |
820 | BUG_ON(ret); | |
821 | mutex_unlock(&root->fs_info->fs_mutex); | |
822 | mark_inode_dirty(inode); | |
823 | } | |
824 | ||
2e635a27 CM |
825 | static int btrfs_get_sb(struct file_system_type *fs_type, |
826 | int flags, const char *dev_name, void *data, struct vfsmount *mnt) | |
827 | { | |
828 | return get_sb_bdev(fs_type, flags, dev_name, data, | |
829 | btrfs_fill_super, mnt); | |
830 | } | |
831 | ||
832 | static struct file_system_type btrfs_fs_type = { | |
833 | .owner = THIS_MODULE, | |
834 | .name = "btrfs", | |
835 | .get_sb = btrfs_get_sb, | |
836 | .kill_sb = kill_block_super, | |
837 | .fs_flags = FS_REQUIRES_DEV, | |
838 | }; | |
839 | ||
e20d96d6 CM |
840 | static struct super_operations btrfs_super_ops = { |
841 | .statfs = simple_statfs, | |
134e9731 | 842 | .delete_inode = btrfs_delete_inode, |
e20d96d6 CM |
843 | .put_super = btrfs_put_super, |
844 | .read_inode = btrfs_read_locked_inode, | |
d5719762 CM |
845 | .write_super = btrfs_write_super, |
846 | .sync_fs = btrfs_sync_fs, | |
4730a4bc | 847 | .write_inode = btrfs_write_inode, |
e20d96d6 CM |
848 | }; |
849 | ||
850 | static struct inode_operations btrfs_dir_inode_operations = { | |
851 | .lookup = btrfs_lookup, | |
d5719762 | 852 | .create = btrfs_create, |
134e9731 | 853 | .unlink = btrfs_unlink, |
f7922033 | 854 | .mkdir = btrfs_mkdir, |
e20d96d6 CM |
855 | }; |
856 | ||
857 | static struct file_operations btrfs_dir_file_operations = { | |
858 | .llseek = generic_file_llseek, | |
859 | .read = generic_read_dir, | |
860 | .readdir = btrfs_readdir, | |
861 | }; | |
862 | ||
dee26a9f CM |
863 | static struct address_space_operations btrfs_aops = { |
864 | .readpage = btrfs_readpage, | |
865 | .readpages = btrfs_readpages, | |
866 | .writepage = btrfs_writepage, | |
867 | .sync_page = block_sync_page, | |
868 | .prepare_write = btrfs_prepare_write, | |
6407bf6d | 869 | .commit_write = nobh_commit_write, |
dee26a9f CM |
870 | }; |
871 | ||
872 | static struct inode_operations btrfs_file_inode_operations = { | |
f4b9aa8d | 873 | .truncate = btrfs_truncate, |
dee26a9f CM |
874 | }; |
875 | ||
876 | static struct file_operations btrfs_file_operations = { | |
877 | .llseek = generic_file_llseek, | |
878 | .read = do_sync_read, | |
879 | .write = do_sync_write, | |
880 | .aio_read = generic_file_aio_read, | |
881 | .aio_write = generic_file_aio_write, | |
882 | .mmap = generic_file_mmap, | |
883 | .open = generic_file_open, | |
884 | .sendfile = generic_file_sendfile, | |
885 | .splice_read = generic_file_splice_read, | |
886 | .splice_write = generic_file_splice_write, | |
887 | }; | |
e20d96d6 | 888 | |
2e635a27 CM |
889 | static int __init init_btrfs_fs(void) |
890 | { | |
891 | printk("btrfs loaded!\n"); | |
892 | return register_filesystem(&btrfs_fs_type); | |
893 | } | |
894 | ||
895 | static void __exit exit_btrfs_fs(void) | |
896 | { | |
897 | unregister_filesystem(&btrfs_fs_type); | |
898 | printk("btrfs unloaded\n"); | |
899 | } | |
900 | ||
901 | module_init(init_btrfs_fs) | |
902 | module_exit(exit_btrfs_fs) | |
903 | ||
904 | MODULE_LICENSE("GPL"); |