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