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