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; | |
236454df | 320 | found_extent = 0; |
f254e52c | 321 | if (btrfs_disk_key_type(found_key) == BTRFS_EXTENT_DATA_KEY) { |
5caf2a00 CM |
322 | fi = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), |
323 | path->slots[0], | |
f254e52c | 324 | struct btrfs_file_extent_item); |
236454df CM |
325 | if (btrfs_file_extent_type(fi) != |
326 | BTRFS_FILE_EXTENT_INLINE) { | |
327 | extent_start = | |
328 | btrfs_file_extent_disk_blocknr(fi); | |
329 | extent_num_blocks = | |
330 | btrfs_file_extent_disk_num_blocks(fi); | |
331 | /* FIXME blocksize != 4096 */ | |
332 | inode->i_blocks -= | |
333 | btrfs_file_extent_num_blocks(fi) << 3; | |
334 | found_extent = 1; | |
335 | } | |
f254e52c | 336 | } |
5caf2a00 | 337 | ret = btrfs_del_item(trans, root, path); |
f4b9aa8d | 338 | BUG_ON(ret); |
5caf2a00 | 339 | btrfs_release_path(root, path); |
f254e52c CM |
340 | if (found_extent) { |
341 | ret = btrfs_free_extent(trans, root, extent_start, | |
342 | extent_num_blocks, 0); | |
343 | BUG_ON(ret); | |
344 | } | |
f4b9aa8d | 345 | } |
f4b9aa8d CM |
346 | ret = 0; |
347 | error: | |
5caf2a00 CM |
348 | btrfs_release_path(root, path); |
349 | btrfs_free_path(path); | |
f4b9aa8d CM |
350 | return ret; |
351 | } | |
352 | ||
134e9731 CM |
353 | static void btrfs_delete_inode(struct inode *inode) |
354 | { | |
355 | struct btrfs_trans_handle *trans; | |
d6e4a428 | 356 | struct btrfs_root *root = BTRFS_I(inode)->root; |
f4b9aa8d CM |
357 | int ret; |
358 | ||
134e9731 CM |
359 | truncate_inode_pages(&inode->i_data, 0); |
360 | if (is_bad_inode(inode)) { | |
361 | goto no_delete; | |
362 | } | |
363 | inode->i_size = 0; | |
134e9731 CM |
364 | mutex_lock(&root->fs_info->fs_mutex); |
365 | trans = btrfs_start_transaction(root, 1); | |
f4b9aa8d CM |
366 | if (S_ISREG(inode->i_mode)) { |
367 | ret = btrfs_truncate_in_trans(trans, root, inode); | |
368 | BUG_ON(ret); | |
369 | } | |
134e9731 CM |
370 | btrfs_free_inode(trans, root, inode); |
371 | btrfs_end_transaction(trans, root); | |
372 | mutex_unlock(&root->fs_info->fs_mutex); | |
373 | return; | |
374 | no_delete: | |
375 | clear_inode(inode); | |
376 | } | |
377 | ||
e20d96d6 | 378 | static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry, |
d6e4a428 | 379 | struct btrfs_key *location) |
e20d96d6 CM |
380 | { |
381 | const char *name = dentry->d_name.name; | |
382 | int namelen = dentry->d_name.len; | |
383 | struct btrfs_dir_item *di; | |
5caf2a00 | 384 | struct btrfs_path *path; |
d6e4a428 | 385 | struct btrfs_root *root = BTRFS_I(dir)->root; |
e20d96d6 CM |
386 | int ret; |
387 | ||
5caf2a00 CM |
388 | path = btrfs_alloc_path(); |
389 | BUG_ON(!path); | |
390 | btrfs_init_path(path); | |
391 | ret = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name, | |
e20d96d6 | 392 | namelen, 0); |
5caf2a00 | 393 | if (ret || !btrfs_match_dir_item_name(root, path, name, namelen)) { |
d6e4a428 | 394 | location->objectid = 0; |
2c90e5d6 | 395 | ret = 0; |
e20d96d6 CM |
396 | goto out; |
397 | } | |
5caf2a00 | 398 | di = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0], |
e20d96d6 | 399 | struct btrfs_dir_item); |
d6e4a428 | 400 | btrfs_disk_key_to_cpu(location, &di->location); |
e20d96d6 | 401 | out: |
5caf2a00 CM |
402 | btrfs_release_path(root, path); |
403 | btrfs_free_path(path); | |
e20d96d6 CM |
404 | return ret; |
405 | } | |
406 | ||
d6e4a428 CM |
407 | int fixup_tree_root_location(struct btrfs_root *root, |
408 | struct btrfs_key *location, | |
409 | struct btrfs_root **sub_root) | |
410 | { | |
411 | struct btrfs_path *path; | |
412 | struct btrfs_root_item *ri; | |
d6e4a428 CM |
413 | |
414 | if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY) | |
415 | return 0; | |
416 | if (location->objectid == BTRFS_ROOT_TREE_OBJECTID) | |
417 | return 0; | |
418 | ||
419 | path = btrfs_alloc_path(); | |
420 | BUG_ON(!path); | |
421 | mutex_lock(&root->fs_info->fs_mutex); | |
422 | ||
0f7d52f4 CM |
423 | *sub_root = btrfs_read_fs_root(root->fs_info, location); |
424 | if (IS_ERR(*sub_root)) | |
425 | return PTR_ERR(*sub_root); | |
426 | ||
427 | ri = &(*sub_root)->root_item; | |
d6e4a428 CM |
428 | location->objectid = btrfs_root_dirid(ri); |
429 | location->flags = 0; | |
430 | btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY); | |
431 | location->offset = 0; | |
0f7d52f4 | 432 | |
d6e4a428 CM |
433 | btrfs_free_path(path); |
434 | mutex_unlock(&root->fs_info->fs_mutex); | |
0f7d52f4 | 435 | return 0; |
d6e4a428 CM |
436 | } |
437 | ||
c5739bba CM |
438 | int btrfs_init_locked_inode(struct inode *inode, void *p) |
439 | { | |
440 | struct btrfs_iget_args *args = p; | |
441 | inode->i_ino = args->ino; | |
442 | BTRFS_I(inode)->root = args->root; | |
443 | return 0; | |
444 | } | |
445 | ||
446 | int btrfs_find_actor(struct inode *inode, void *opaque) | |
447 | { | |
448 | struct btrfs_iget_args *args = opaque; | |
449 | return (args->ino == inode->i_ino && | |
450 | args->root == BTRFS_I(inode)->root); | |
451 | } | |
452 | ||
453 | struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid, | |
454 | struct btrfs_root *root) | |
455 | { | |
456 | struct inode *inode; | |
457 | struct btrfs_iget_args args; | |
458 | args.ino = objectid; | |
459 | args.root = root; | |
460 | ||
461 | inode = iget5_locked(s, objectid, btrfs_find_actor, | |
462 | btrfs_init_locked_inode, | |
463 | (void *)&args); | |
464 | return inode; | |
465 | } | |
d6e4a428 | 466 | |
e20d96d6 CM |
467 | static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry, |
468 | struct nameidata *nd) | |
469 | { | |
470 | struct inode * inode; | |
d6e4a428 CM |
471 | struct btrfs_inode *bi = BTRFS_I(dir); |
472 | struct btrfs_root *root = bi->root; | |
473 | struct btrfs_root *sub_root = root; | |
474 | struct btrfs_key location; | |
e20d96d6 CM |
475 | int ret; |
476 | ||
477 | if (dentry->d_name.len > BTRFS_NAME_LEN) | |
478 | return ERR_PTR(-ENAMETOOLONG); | |
22b0ebda | 479 | mutex_lock(&root->fs_info->fs_mutex); |
d6e4a428 | 480 | ret = btrfs_inode_by_name(dir, dentry, &location); |
22b0ebda | 481 | mutex_unlock(&root->fs_info->fs_mutex); |
e20d96d6 CM |
482 | if (ret < 0) |
483 | return ERR_PTR(ret); | |
484 | inode = NULL; | |
d6e4a428 CM |
485 | if (location.objectid) { |
486 | ret = fixup_tree_root_location(root, &location, &sub_root); | |
487 | if (ret < 0) | |
488 | return ERR_PTR(ret); | |
489 | if (ret > 0) | |
490 | return ERR_PTR(-ENOENT); | |
c5739bba CM |
491 | inode = btrfs_iget_locked(dir->i_sb, location.objectid, |
492 | sub_root); | |
e20d96d6 CM |
493 | if (!inode) |
494 | return ERR_PTR(-EACCES); | |
d6e4a428 | 495 | if (inode->i_state & I_NEW) { |
0f7d52f4 | 496 | if (sub_root != root) { |
c5739bba | 497 | printk("adding new root for inode %lu root %p (found %p)\n", inode->i_ino, sub_root, BTRFS_I(inode)->root); |
0f7d52f4 CM |
498 | igrab(inode); |
499 | sub_root->inode = inode; | |
500 | } | |
d6e4a428 CM |
501 | BTRFS_I(inode)->root = sub_root; |
502 | memcpy(&BTRFS_I(inode)->location, &location, | |
503 | sizeof(location)); | |
504 | btrfs_read_locked_inode(inode); | |
505 | unlock_new_inode(inode); | |
506 | } | |
e20d96d6 CM |
507 | } |
508 | return d_splice_alias(inode, dentry); | |
509 | } | |
510 | ||
511 | static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir) | |
512 | { | |
513 | struct inode *inode = filp->f_path.dentry->d_inode; | |
d6e4a428 | 514 | struct btrfs_root *root = BTRFS_I(inode)->root; |
e20d96d6 CM |
515 | struct btrfs_item *item; |
516 | struct btrfs_dir_item *di; | |
517 | struct btrfs_key key; | |
5caf2a00 | 518 | struct btrfs_path *path; |
e20d96d6 CM |
519 | int ret; |
520 | u32 nritems; | |
521 | struct btrfs_leaf *leaf; | |
522 | int slot; | |
523 | int advance; | |
524 | unsigned char d_type = DT_UNKNOWN; | |
7f5c1516 | 525 | int over = 0; |
70b2befd | 526 | int key_type = BTRFS_DIR_ITEM_KEY; |
d6e4a428 CM |
527 | |
528 | /* FIXME, use a real flag for deciding about the key type */ | |
529 | if (root->fs_info->tree_root == root) | |
530 | key_type = BTRFS_DIR_ITEM_KEY; | |
22b0ebda | 531 | mutex_lock(&root->fs_info->fs_mutex); |
e20d96d6 | 532 | key.objectid = inode->i_ino; |
e20d96d6 | 533 | key.flags = 0; |
d6e4a428 | 534 | btrfs_set_key_type(&key, key_type); |
e20d96d6 | 535 | key.offset = filp->f_pos; |
5caf2a00 CM |
536 | path = btrfs_alloc_path(); |
537 | btrfs_init_path(path); | |
538 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
1b05da2e | 539 | if (ret < 0) |
e20d96d6 | 540 | goto err; |
7f5c1516 | 541 | advance = 0; |
e20d96d6 | 542 | while(1) { |
5caf2a00 | 543 | leaf = btrfs_buffer_leaf(path->nodes[0]); |
e20d96d6 | 544 | nritems = btrfs_header_nritems(&leaf->header); |
5caf2a00 | 545 | slot = path->slots[0]; |
dee26a9f CM |
546 | if (advance || slot >= nritems) { |
547 | if (slot >= nritems -1) { | |
5caf2a00 | 548 | ret = btrfs_next_leaf(root, path); |
e20d96d6 CM |
549 | if (ret) |
550 | break; | |
5caf2a00 | 551 | leaf = btrfs_buffer_leaf(path->nodes[0]); |
e20d96d6 | 552 | nritems = btrfs_header_nritems(&leaf->header); |
5caf2a00 | 553 | slot = path->slots[0]; |
e20d96d6 CM |
554 | } else { |
555 | slot++; | |
5caf2a00 | 556 | path->slots[0]++; |
e20d96d6 CM |
557 | } |
558 | } | |
559 | advance = 1; | |
560 | item = leaf->items + slot; | |
e20d96d6 CM |
561 | if (btrfs_disk_key_objectid(&item->key) != key.objectid) |
562 | break; | |
d6e4a428 | 563 | if (btrfs_disk_key_type(&item->key) != key_type) |
a429e513 | 564 | break; |
7f5c1516 CM |
565 | if (btrfs_disk_key_offset(&item->key) < filp->f_pos) |
566 | continue; | |
7fcde0e3 | 567 | filp->f_pos = btrfs_disk_key_offset(&item->key); |
dee26a9f | 568 | advance = 1; |
e20d96d6 | 569 | di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); |
e20d96d6 CM |
570 | over = filldir(dirent, (const char *)(di + 1), |
571 | btrfs_dir_name_len(di), | |
572 | btrfs_disk_key_offset(&item->key), | |
d6e4a428 | 573 | btrfs_disk_key_objectid(&di->location), d_type); |
7fcde0e3 CM |
574 | if (over) |
575 | goto nopos; | |
e20d96d6 | 576 | } |
7fcde0e3 CM |
577 | filp->f_pos++; |
578 | nopos: | |
e20d96d6 CM |
579 | ret = 0; |
580 | err: | |
5caf2a00 CM |
581 | btrfs_release_path(root, path); |
582 | btrfs_free_path(path); | |
22b0ebda | 583 | mutex_unlock(&root->fs_info->fs_mutex); |
e20d96d6 CM |
584 | return ret; |
585 | } | |
586 | ||
587 | static void btrfs_put_super (struct super_block * sb) | |
588 | { | |
589 | struct btrfs_root *root = btrfs_sb(sb); | |
590 | int ret; | |
591 | ||
592 | ret = close_ctree(root); | |
593 | if (ret) { | |
594 | printk("close ctree returns %d\n", ret); | |
595 | } | |
596 | sb->s_fs_info = NULL; | |
597 | } | |
2e635a27 CM |
598 | |
599 | static int btrfs_fill_super(struct super_block * sb, void * data, int silent) | |
600 | { | |
601 | struct inode * inode; | |
e20d96d6 CM |
602 | struct dentry * root_dentry; |
603 | struct btrfs_super_block *disk_super; | |
0f7d52f4 | 604 | struct btrfs_root *tree_root; |
d6e4a428 | 605 | struct btrfs_inode *bi; |
2e635a27 CM |
606 | |
607 | sb->s_maxbytes = MAX_LFS_FILESIZE; | |
2e635a27 | 608 | sb->s_magic = BTRFS_SUPER_MAGIC; |
e20d96d6 | 609 | sb->s_op = &btrfs_super_ops; |
2e635a27 | 610 | sb->s_time_gran = 1; |
e20d96d6 | 611 | |
0f7d52f4 | 612 | tree_root = open_ctree(sb); |
d98237b3 | 613 | |
0f7d52f4 | 614 | if (!tree_root) { |
e20d96d6 CM |
615 | printk("btrfs: open_ctree failed\n"); |
616 | return -EIO; | |
617 | } | |
0f7d52f4 CM |
618 | sb->s_fs_info = tree_root; |
619 | disk_super = tree_root->fs_info->disk_super; | |
e20d96d6 CM |
620 | printk("read in super total blocks %Lu root %Lu\n", |
621 | btrfs_super_total_blocks(disk_super), | |
622 | btrfs_super_root_dir(disk_super)); | |
623 | ||
c5739bba CM |
624 | inode = btrfs_iget_locked(sb, btrfs_super_root_dir(disk_super), |
625 | tree_root); | |
d6e4a428 CM |
626 | bi = BTRFS_I(inode); |
627 | bi->location.objectid = inode->i_ino; | |
628 | bi->location.offset = 0; | |
629 | bi->location.flags = 0; | |
0f7d52f4 | 630 | bi->root = tree_root; |
d6e4a428 CM |
631 | btrfs_set_key_type(&bi->location, BTRFS_INODE_ITEM_KEY); |
632 | ||
2e635a27 CM |
633 | if (!inode) |
634 | return -ENOMEM; | |
e20d96d6 CM |
635 | if (inode->i_state & I_NEW) { |
636 | btrfs_read_locked_inode(inode); | |
637 | unlock_new_inode(inode); | |
638 | } | |
2e635a27 | 639 | |
e20d96d6 CM |
640 | root_dentry = d_alloc_root(inode); |
641 | if (!root_dentry) { | |
2e635a27 CM |
642 | iput(inode); |
643 | return -ENOMEM; | |
644 | } | |
e20d96d6 CM |
645 | sb->s_root = root_dentry; |
646 | ||
2e635a27 CM |
647 | return 0; |
648 | } | |
649 | ||
d5719762 CM |
650 | static void fill_inode_item(struct btrfs_inode_item *item, |
651 | struct inode *inode) | |
652 | { | |
653 | btrfs_set_inode_uid(item, inode->i_uid); | |
654 | btrfs_set_inode_gid(item, inode->i_gid); | |
655 | btrfs_set_inode_size(item, inode->i_size); | |
656 | btrfs_set_inode_mode(item, inode->i_mode); | |
657 | btrfs_set_inode_nlink(item, inode->i_nlink); | |
658 | btrfs_set_timespec_sec(&item->atime, inode->i_atime.tv_sec); | |
659 | btrfs_set_timespec_nsec(&item->atime, inode->i_atime.tv_nsec); | |
660 | btrfs_set_timespec_sec(&item->mtime, inode->i_mtime.tv_sec); | |
661 | btrfs_set_timespec_nsec(&item->mtime, inode->i_mtime.tv_nsec); | |
662 | btrfs_set_timespec_sec(&item->ctime, inode->i_ctime.tv_sec); | |
663 | btrfs_set_timespec_nsec(&item->ctime, inode->i_ctime.tv_nsec); | |
664 | btrfs_set_inode_nblocks(item, inode->i_blocks); | |
665 | btrfs_set_inode_generation(item, inode->i_generation); | |
666 | } | |
667 | ||
4730a4bc CM |
668 | static int btrfs_update_inode(struct btrfs_trans_handle *trans, |
669 | struct btrfs_root *root, | |
670 | struct inode *inode) | |
671 | { | |
672 | struct btrfs_inode_item *inode_item; | |
5caf2a00 | 673 | struct btrfs_path *path; |
4730a4bc CM |
674 | int ret; |
675 | ||
5caf2a00 CM |
676 | path = btrfs_alloc_path(); |
677 | BUG_ON(!path); | |
678 | btrfs_init_path(path); | |
d6e4a428 CM |
679 | ret = btrfs_lookup_inode(trans, root, path, |
680 | &BTRFS_I(inode)->location, 1); | |
4730a4bc CM |
681 | if (ret) { |
682 | if (ret > 0) | |
683 | ret = -ENOENT; | |
684 | goto failed; | |
685 | } | |
686 | ||
5caf2a00 CM |
687 | inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), |
688 | path->slots[0], | |
4730a4bc CM |
689 | struct btrfs_inode_item); |
690 | ||
691 | fill_inode_item(inode_item, inode); | |
5caf2a00 | 692 | btrfs_mark_buffer_dirty(path->nodes[0]); |
1b05da2e | 693 | ret = 0; |
4730a4bc | 694 | failed: |
5caf2a00 CM |
695 | btrfs_release_path(root, path); |
696 | btrfs_free_path(path); | |
1b05da2e | 697 | return ret; |
4730a4bc CM |
698 | } |
699 | ||
700 | static int btrfs_write_inode(struct inode *inode, int wait) | |
701 | { | |
d6e4a428 | 702 | struct btrfs_root *root = BTRFS_I(inode)->root; |
4730a4bc CM |
703 | struct btrfs_trans_handle *trans; |
704 | int ret; | |
705 | ||
706 | mutex_lock(&root->fs_info->fs_mutex); | |
707 | trans = btrfs_start_transaction(root, 1); | |
708 | ret = btrfs_update_inode(trans, root, inode); | |
709 | if (wait) | |
710 | btrfs_commit_transaction(trans, root); | |
711 | else | |
712 | btrfs_end_transaction(trans, root); | |
713 | mutex_unlock(&root->fs_info->fs_mutex); | |
714 | return ret; | |
715 | } | |
716 | ||
d5719762 | 717 | static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans, |
2619ba1f CM |
718 | struct btrfs_root *root, |
719 | u64 objectid, int mode) | |
d5719762 CM |
720 | { |
721 | struct inode *inode; | |
722 | struct btrfs_inode_item inode_item; | |
1b05da2e | 723 | struct btrfs_key *location; |
d5719762 | 724 | int ret; |
d5719762 | 725 | |
2619ba1f | 726 | inode = new_inode(root->fs_info->sb); |
d5719762 CM |
727 | if (!inode) |
728 | return ERR_PTR(-ENOMEM); | |
729 | ||
2619ba1f | 730 | BTRFS_I(inode)->root = root; |
d5719762 CM |
731 | |
732 | inode->i_uid = current->fsuid; | |
733 | inode->i_gid = current->fsgid; | |
734 | inode->i_mode = mode; | |
735 | inode->i_ino = objectid; | |
736 | inode->i_blocks = 0; | |
c5739bba | 737 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; |
d5719762 | 738 | fill_inode_item(&inode_item, inode); |
1b05da2e CM |
739 | location = &BTRFS_I(inode)->location; |
740 | location->objectid = objectid; | |
741 | location->flags = 0; | |
742 | location->offset = 0; | |
743 | btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY); | |
d5719762 CM |
744 | |
745 | ret = btrfs_insert_inode(trans, root, objectid, &inode_item); | |
746 | BUG_ON(ret); | |
747 | ||
748 | insert_inode_hash(inode); | |
d5719762 CM |
749 | return inode; |
750 | } | |
751 | ||
752 | static int btrfs_add_link(struct btrfs_trans_handle *trans, | |
753 | struct dentry *dentry, struct inode *inode) | |
754 | { | |
755 | int ret; | |
d6e4a428 CM |
756 | struct btrfs_key key; |
757 | struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root; | |
758 | key.objectid = inode->i_ino; | |
759 | key.flags = 0; | |
760 | btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY); | |
761 | key.offset = 0; | |
762 | ||
763 | ret = btrfs_insert_dir_item(trans, root, | |
d5719762 CM |
764 | dentry->d_name.name, dentry->d_name.len, |
765 | dentry->d_parent->d_inode->i_ino, | |
d6e4a428 | 766 | &key, 0); |
4730a4bc | 767 | if (ret == 0) { |
5f26f772 | 768 | dentry->d_parent->d_inode->i_size += dentry->d_name.len * 2; |
d6e4a428 | 769 | ret = btrfs_update_inode(trans, root, |
4730a4bc CM |
770 | dentry->d_parent->d_inode); |
771 | } | |
d5719762 CM |
772 | return ret; |
773 | } | |
774 | ||
775 | static int btrfs_add_nondir(struct btrfs_trans_handle *trans, | |
776 | struct dentry *dentry, struct inode *inode) | |
777 | { | |
778 | int err = btrfs_add_link(trans, dentry, inode); | |
779 | if (!err) { | |
780 | d_instantiate(dentry, inode); | |
781 | return 0; | |
782 | } | |
2c90e5d6 CM |
783 | if (err > 0) |
784 | err = -EEXIST; | |
d5719762 CM |
785 | return err; |
786 | } | |
787 | ||
788 | static int btrfs_create(struct inode *dir, struct dentry *dentry, | |
789 | int mode, struct nameidata *nd) | |
790 | { | |
791 | struct btrfs_trans_handle *trans; | |
d6e4a428 | 792 | struct btrfs_root *root = BTRFS_I(dir)->root; |
d5719762 CM |
793 | struct inode *inode; |
794 | int err; | |
134e9731 | 795 | int drop_inode = 0; |
2619ba1f | 796 | u64 objectid; |
d5719762 | 797 | |
d561c025 | 798 | mutex_lock(&root->fs_info->fs_mutex); |
d5719762 | 799 | trans = btrfs_start_transaction(root, 1); |
2619ba1f CM |
800 | |
801 | err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid); | |
802 | if (err) { | |
803 | err = -ENOSPC; | |
804 | goto out_unlock; | |
805 | } | |
806 | ||
807 | inode = btrfs_new_inode(trans, root, objectid, mode); | |
d5719762 CM |
808 | err = PTR_ERR(inode); |
809 | if (IS_ERR(inode)) | |
d561c025 | 810 | goto out_unlock; |
d5719762 CM |
811 | // FIXME mark the inode dirty |
812 | err = btrfs_add_nondir(trans, dentry, inode); | |
134e9731 CM |
813 | if (err) |
814 | drop_inode = 1; | |
dee26a9f CM |
815 | else { |
816 | inode->i_mapping->a_ops = &btrfs_aops; | |
817 | inode->i_fop = &btrfs_file_operations; | |
818 | inode->i_op = &btrfs_file_inode_operations; | |
819 | } | |
d5719762 | 820 | dir->i_sb->s_dirt = 1; |
d561c025 | 821 | out_unlock: |
22b0ebda | 822 | btrfs_end_transaction(trans, root); |
d561c025 | 823 | mutex_unlock(&root->fs_info->fs_mutex); |
2c90e5d6 | 824 | |
134e9731 CM |
825 | if (drop_inode) { |
826 | inode_dec_link_count(inode); | |
827 | iput(inode); | |
828 | } | |
d5719762 CM |
829 | return err; |
830 | } | |
831 | ||
f7922033 | 832 | static int btrfs_make_empty_dir(struct btrfs_trans_handle *trans, |
2619ba1f CM |
833 | struct btrfs_root *root, |
834 | u64 objectid, u64 dirid) | |
f7922033 | 835 | { |
f7922033 CM |
836 | int ret; |
837 | char buf[2]; | |
d6e4a428 CM |
838 | struct btrfs_key key; |
839 | ||
f7922033 CM |
840 | buf[0] = '.'; |
841 | buf[1] = '.'; | |
842 | ||
2619ba1f | 843 | key.objectid = objectid; |
d6e4a428 CM |
844 | key.offset = 0; |
845 | key.flags = 0; | |
846 | btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY); | |
847 | ||
2619ba1f | 848 | ret = btrfs_insert_dir_item(trans, root, buf, 1, objectid, |
d6e4a428 | 849 | &key, 1); |
f7922033 CM |
850 | if (ret) |
851 | goto error; | |
2619ba1f CM |
852 | key.objectid = dirid; |
853 | ret = btrfs_insert_dir_item(trans, root, buf, 2, objectid, | |
d6e4a428 | 854 | &key, 1); |
4730a4bc CM |
855 | if (ret) |
856 | goto error; | |
f7922033 CM |
857 | error: |
858 | return ret; | |
859 | } | |
860 | ||
861 | static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode) | |
862 | { | |
863 | struct inode *inode; | |
864 | struct btrfs_trans_handle *trans; | |
d6e4a428 | 865 | struct btrfs_root *root = BTRFS_I(dir)->root; |
f7922033 CM |
866 | int err = 0; |
867 | int drop_on_err = 0; | |
2619ba1f | 868 | u64 objectid; |
f7922033 CM |
869 | |
870 | mutex_lock(&root->fs_info->fs_mutex); | |
871 | trans = btrfs_start_transaction(root, 1); | |
872 | if (IS_ERR(trans)) { | |
873 | err = PTR_ERR(trans); | |
874 | goto out_unlock; | |
875 | } | |
2619ba1f CM |
876 | |
877 | err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid); | |
878 | if (err) { | |
879 | err = -ENOSPC; | |
880 | goto out_unlock; | |
881 | } | |
882 | ||
883 | inode = btrfs_new_inode(trans, root, objectid, S_IFDIR | mode); | |
f7922033 CM |
884 | if (IS_ERR(inode)) { |
885 | err = PTR_ERR(inode); | |
886 | goto out_fail; | |
887 | } | |
888 | drop_on_err = 1; | |
889 | inode->i_op = &btrfs_dir_inode_operations; | |
890 | inode->i_fop = &btrfs_dir_file_operations; | |
891 | ||
2619ba1f CM |
892 | err = btrfs_make_empty_dir(trans, root, inode->i_ino, dir->i_ino); |
893 | if (err) | |
894 | goto out_fail; | |
895 | ||
896 | inode->i_size = 6; | |
897 | err = btrfs_update_inode(trans, root, inode); | |
f7922033 CM |
898 | if (err) |
899 | goto out_fail; | |
900 | err = btrfs_add_link(trans, dentry, inode); | |
901 | if (err) | |
902 | goto out_fail; | |
903 | d_instantiate(dentry, inode); | |
f7922033 CM |
904 | drop_on_err = 0; |
905 | ||
906 | out_fail: | |
907 | btrfs_end_transaction(trans, root); | |
908 | out_unlock: | |
909 | mutex_unlock(&root->fs_info->fs_mutex); | |
910 | if (drop_on_err) | |
911 | iput(inode); | |
912 | return err; | |
913 | } | |
914 | ||
d5719762 CM |
915 | static int btrfs_sync_fs(struct super_block *sb, int wait) |
916 | { | |
917 | struct btrfs_trans_handle *trans; | |
918 | struct btrfs_root *root; | |
919 | int ret; | |
d98237b3 | 920 | root = btrfs_sb(sb); |
df2ce34c | 921 | |
d5719762 | 922 | sb->s_dirt = 0; |
d561c025 | 923 | if (!wait) { |
7cfcc17e | 924 | filemap_flush(root->fs_info->btree_inode->i_mapping); |
d561c025 CM |
925 | return 0; |
926 | } | |
7cfcc17e | 927 | filemap_write_and_wait(root->fs_info->btree_inode->i_mapping); |
d561c025 | 928 | mutex_lock(&root->fs_info->fs_mutex); |
d5719762 CM |
929 | trans = btrfs_start_transaction(root, 1); |
930 | ret = btrfs_commit_transaction(trans, root); | |
931 | sb->s_dirt = 0; | |
932 | BUG_ON(ret); | |
933 | printk("btrfs sync_fs\n"); | |
d561c025 | 934 | mutex_unlock(&root->fs_info->fs_mutex); |
d5719762 CM |
935 | return 0; |
936 | } | |
937 | ||
e8f05c45 | 938 | #if 0 |
df24a2b9 CM |
939 | static int btrfs_get_block_inline(struct inode *inode, sector_t iblock, |
940 | struct buffer_head *result, int create) | |
941 | { | |
942 | struct btrfs_root *root = btrfs_sb(inode->i_sb); | |
943 | struct btrfs_path *path; | |
944 | struct btrfs_key key; | |
945 | struct btrfs_leaf *leaf; | |
946 | int num_bytes = result->b_size; | |
947 | int item_size; | |
948 | int ret; | |
949 | u64 pos; | |
950 | char *ptr; | |
951 | int copy_size; | |
952 | int err = 0; | |
953 | char *safe_ptr; | |
954 | char *data_ptr; | |
955 | ||
956 | path = btrfs_alloc_path(); | |
957 | BUG_ON(!path); | |
958 | ||
959 | WARN_ON(create); | |
960 | if (create) { | |
961 | return 0; | |
962 | } | |
963 | pos = iblock << inode->i_blkbits; | |
964 | key.objectid = inode->i_ino; | |
965 | key.flags = 0; | |
966 | btrfs_set_key_type(&key, BTRFS_INLINE_DATA_KEY); | |
967 | ptr = kmap(result->b_page); | |
968 | safe_ptr = ptr; | |
969 | ptr += (pos & (PAGE_CACHE_SIZE -1)); | |
970 | again: | |
971 | key.offset = pos; | |
972 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
973 | if (ret) { | |
974 | if (ret < 0) | |
975 | err = ret; | |
976 | else | |
977 | err = 0; | |
978 | goto out; | |
979 | } | |
980 | leaf = btrfs_buffer_leaf(path->nodes[0]); | |
981 | item_size = btrfs_item_size(leaf->items + path->slots[0]); | |
982 | copy_size = min(num_bytes, item_size); | |
983 | data_ptr = btrfs_item_ptr(leaf, path->slots[0], char); | |
984 | WARN_ON(safe_ptr + PAGE_CACHE_SIZE < ptr + copy_size); | |
985 | memcpy(ptr, data_ptr, copy_size); | |
986 | pos += copy_size; | |
987 | num_bytes -= copy_size; | |
988 | WARN_ON(num_bytes < 0); | |
989 | ptr += copy_size; | |
990 | btrfs_release_path(root, path); | |
991 | if (num_bytes != 0) { | |
992 | if (pos >= i_size_read(inode)) | |
993 | memset(ptr, 0, num_bytes); | |
994 | else | |
995 | goto again; | |
996 | } | |
997 | set_buffer_uptodate(result); | |
998 | map_bh(result, inode->i_sb, 0); | |
999 | err = 0; | |
1000 | out: | |
1001 | btrfs_free_path(path); | |
1002 | kunmap(result->b_page); | |
1003 | return err; | |
1004 | } | |
e8f05c45 | 1005 | #endif |
df24a2b9 | 1006 | |
75dfe396 | 1007 | static int btrfs_get_block_lock(struct inode *inode, sector_t iblock, |
dee26a9f CM |
1008 | struct buffer_head *result, int create) |
1009 | { | |
1010 | int ret; | |
1011 | int err = 0; | |
1012 | u64 blocknr; | |
1013 | u64 extent_start = 0; | |
1014 | u64 extent_end = 0; | |
1015 | u64 objectid = inode->i_ino; | |
236454df | 1016 | u32 found_type; |
5caf2a00 | 1017 | struct btrfs_path *path; |
d6e4a428 | 1018 | struct btrfs_root *root = BTRFS_I(inode)->root; |
dee26a9f CM |
1019 | struct btrfs_file_extent_item *item; |
1020 | struct btrfs_leaf *leaf; | |
1021 | struct btrfs_disk_key *found_key; | |
1022 | ||
5caf2a00 CM |
1023 | path = btrfs_alloc_path(); |
1024 | BUG_ON(!path); | |
1025 | btrfs_init_path(path); | |
6567e837 | 1026 | if (create) { |
6567e837 CM |
1027 | WARN_ON(1); |
1028 | } | |
dee26a9f | 1029 | |
236454df | 1030 | ret = btrfs_lookup_file_extent(NULL, root, path, |
9773a788 | 1031 | inode->i_ino, |
236454df | 1032 | iblock << inode->i_blkbits, 0); |
dee26a9f | 1033 | if (ret < 0) { |
dee26a9f CM |
1034 | err = ret; |
1035 | goto out; | |
1036 | } | |
1037 | ||
1038 | if (ret != 0) { | |
5caf2a00 CM |
1039 | if (path->slots[0] == 0) { |
1040 | btrfs_release_path(root, path); | |
236454df | 1041 | goto out; |
dee26a9f | 1042 | } |
5caf2a00 | 1043 | path->slots[0]--; |
dee26a9f CM |
1044 | } |
1045 | ||
5caf2a00 | 1046 | item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0], |
dee26a9f | 1047 | struct btrfs_file_extent_item); |
5caf2a00 | 1048 | leaf = btrfs_buffer_leaf(path->nodes[0]); |
dee26a9f CM |
1049 | blocknr = btrfs_file_extent_disk_blocknr(item); |
1050 | blocknr += btrfs_file_extent_offset(item); | |
1051 | ||
dee26a9f | 1052 | /* are we inside the extent that was found? */ |
5caf2a00 | 1053 | found_key = &leaf->items[path->slots[0]].key; |
236454df | 1054 | found_type = btrfs_disk_key_type(found_key); |
dee26a9f | 1055 | if (btrfs_disk_key_objectid(found_key) != objectid || |
236454df | 1056 | found_type != BTRFS_EXTENT_DATA_KEY) { |
dee26a9f CM |
1057 | extent_end = 0; |
1058 | extent_start = 0; | |
5caf2a00 | 1059 | btrfs_release_path(root, path); |
dee26a9f CM |
1060 | goto out; |
1061 | } | |
236454df CM |
1062 | found_type = btrfs_file_extent_type(item); |
1063 | extent_start = btrfs_disk_key_offset(&leaf->items[path->slots[0]].key); | |
1064 | if (found_type == BTRFS_FILE_EXTENT_REG) { | |
1065 | extent_start = extent_start >> inode->i_blkbits; | |
1066 | extent_end = extent_start + btrfs_file_extent_num_blocks(item); | |
1067 | if (iblock >= extent_start && iblock < extent_end) { | |
1068 | err = 0; | |
1069 | btrfs_map_bh_to_logical(root, result, blocknr + | |
1070 | iblock - extent_start); | |
1071 | goto out; | |
1072 | } | |
1073 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { | |
1074 | char *ptr; | |
1075 | char *map; | |
1076 | u32 size; | |
1077 | size = btrfs_file_extent_inline_len(leaf->items + | |
1078 | path->slots[0]); | |
1079 | extent_end = (extent_start + size) >> inode->i_blkbits; | |
1080 | extent_start >>= inode->i_blkbits; | |
1081 | if (iblock < extent_start || iblock > extent_end) { | |
1082 | goto out; | |
1083 | } | |
1084 | ptr = btrfs_file_extent_inline_start(item); | |
1085 | map = kmap(result->b_page); | |
1086 | memcpy(map, ptr, size); | |
1087 | memset(map + size, 0, PAGE_CACHE_SIZE - size); | |
1088 | flush_dcache_page(result->b_page); | |
1089 | kunmap(result->b_page); | |
1090 | set_buffer_uptodate(result); | |
1091 | SetPageChecked(result->b_page); | |
1092 | btrfs_map_bh_to_logical(root, result, 0); | |
dee26a9f | 1093 | } |
dee26a9f | 1094 | out: |
5caf2a00 CM |
1095 | btrfs_release_path(root, path); |
1096 | btrfs_free_path(path); | |
75dfe396 CM |
1097 | return err; |
1098 | } | |
1099 | ||
1100 | static int btrfs_get_block(struct inode *inode, sector_t iblock, | |
1101 | struct buffer_head *result, int create) | |
1102 | { | |
1103 | int err; | |
d6e4a428 | 1104 | struct btrfs_root *root = BTRFS_I(inode)->root; |
75dfe396 | 1105 | mutex_lock(&root->fs_info->fs_mutex); |
e8f05c45 | 1106 | err = btrfs_get_block_lock(inode, iblock, result, create); |
dee26a9f CM |
1107 | mutex_unlock(&root->fs_info->fs_mutex); |
1108 | return err; | |
1109 | } | |
1110 | ||
1111 | static int btrfs_prepare_write(struct file *file, struct page *page, | |
1112 | unsigned from, unsigned to) | |
1113 | { | |
6407bf6d | 1114 | return nobh_prepare_write(page, from, to, btrfs_get_block); |
dee26a9f CM |
1115 | } |
1116 | ||
d561c025 CM |
1117 | static void btrfs_write_super(struct super_block *sb) |
1118 | { | |
1119 | btrfs_sync_fs(sb, 1); | |
1120 | } | |
1121 | ||
dee26a9f CM |
1122 | static int btrfs_readpage(struct file *file, struct page *page) |
1123 | { | |
1124 | return mpage_readpage(page, btrfs_get_block); | |
1125 | } | |
1126 | ||
236454df CM |
1127 | /* |
1128 | * While block_write_full_page is writing back the dirty buffers under | |
1129 | * the page lock, whoever dirtied the buffers may decide to clean them | |
1130 | * again at any time. We handle that by only looking at the buffer | |
1131 | * state inside lock_buffer(). | |
1132 | * | |
1133 | * If block_write_full_page() is called for regular writeback | |
1134 | * (wbc->sync_mode == WB_SYNC_NONE) then it will redirty a page which has a | |
1135 | * locked buffer. This only can happen if someone has written the buffer | |
1136 | * directly, with submit_bh(). At the address_space level PageWriteback | |
1137 | * prevents this contention from occurring. | |
1138 | */ | |
1139 | static int __btrfs_write_full_page(struct inode *inode, struct page *page, | |
1140 | struct writeback_control *wbc) | |
1141 | { | |
1142 | int err; | |
1143 | sector_t block; | |
1144 | sector_t last_block; | |
1145 | struct buffer_head *bh, *head; | |
1146 | const unsigned blocksize = 1 << inode->i_blkbits; | |
1147 | int nr_underway = 0; | |
1148 | ||
1149 | BUG_ON(!PageLocked(page)); | |
1150 | ||
1151 | last_block = (i_size_read(inode) - 1) >> inode->i_blkbits; | |
1152 | ||
1153 | if (!page_has_buffers(page)) { | |
1154 | create_empty_buffers(page, blocksize, | |
1155 | (1 << BH_Dirty)|(1 << BH_Uptodate)); | |
1156 | } | |
1157 | ||
1158 | /* | |
1159 | * Be very careful. We have no exclusion from __set_page_dirty_buffers | |
1160 | * here, and the (potentially unmapped) buffers may become dirty at | |
1161 | * any time. If a buffer becomes dirty here after we've inspected it | |
1162 | * then we just miss that fact, and the page stays dirty. | |
1163 | * | |
1164 | * Buffers outside i_size may be dirtied by __set_page_dirty_buffers; | |
1165 | * handle that here by just cleaning them. | |
1166 | */ | |
1167 | ||
1168 | block = (sector_t)page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits); | |
1169 | head = page_buffers(page); | |
1170 | bh = head; | |
1171 | ||
1172 | /* | |
1173 | * Get all the dirty buffers mapped to disk addresses and | |
1174 | * handle any aliases from the underlying blockdev's mapping. | |
1175 | */ | |
1176 | do { | |
1177 | if (block > last_block) { | |
1178 | /* | |
1179 | * mapped buffers outside i_size will occur, because | |
1180 | * this page can be outside i_size when there is a | |
1181 | * truncate in progress. | |
1182 | */ | |
1183 | /* | |
1184 | * The buffer was zeroed by block_write_full_page() | |
1185 | */ | |
1186 | clear_buffer_dirty(bh); | |
1187 | set_buffer_uptodate(bh); | |
1188 | } else if (!buffer_mapped(bh) && buffer_dirty(bh)) { | |
1189 | WARN_ON(bh->b_size != blocksize); | |
1190 | err = btrfs_get_block(inode, block, bh, 0); | |
1191 | if (err) | |
1192 | goto recover; | |
1193 | if (buffer_new(bh)) { | |
1194 | /* blockdev mappings never come here */ | |
1195 | clear_buffer_new(bh); | |
1196 | unmap_underlying_metadata(bh->b_bdev, | |
1197 | bh->b_blocknr); | |
1198 | } | |
1199 | } | |
1200 | bh = bh->b_this_page; | |
1201 | block++; | |
1202 | } while (bh != head); | |
1203 | ||
1204 | do { | |
1205 | if (!buffer_mapped(bh)) | |
1206 | continue; | |
1207 | /* | |
1208 | * If it's a fully non-blocking write attempt and we cannot | |
1209 | * lock the buffer then redirty the page. Note that this can | |
1210 | * potentially cause a busy-wait loop from pdflush and kswapd | |
1211 | * activity, but those code paths have their own higher-level | |
1212 | * throttling. | |
1213 | */ | |
1214 | if (wbc->sync_mode != WB_SYNC_NONE || !wbc->nonblocking) { | |
1215 | lock_buffer(bh); | |
1216 | } else if (test_set_buffer_locked(bh)) { | |
1217 | redirty_page_for_writepage(wbc, page); | |
1218 | continue; | |
1219 | } | |
1220 | if (test_clear_buffer_dirty(bh) && bh->b_blocknr != 0) { | |
1221 | mark_buffer_async_write(bh); | |
1222 | } else { | |
1223 | unlock_buffer(bh); | |
1224 | } | |
1225 | } while ((bh = bh->b_this_page) != head); | |
1226 | ||
1227 | /* | |
1228 | * The page and its buffers are protected by PageWriteback(), so we can | |
1229 | * drop the bh refcounts early. | |
1230 | */ | |
1231 | BUG_ON(PageWriteback(page)); | |
1232 | set_page_writeback(page); | |
1233 | ||
1234 | do { | |
1235 | struct buffer_head *next = bh->b_this_page; | |
1236 | if (buffer_async_write(bh)) { | |
1237 | submit_bh(WRITE, bh); | |
1238 | nr_underway++; | |
1239 | } | |
1240 | bh = next; | |
1241 | } while (bh != head); | |
1242 | unlock_page(page); | |
1243 | ||
1244 | err = 0; | |
1245 | done: | |
1246 | if (nr_underway == 0) { | |
1247 | /* | |
1248 | * The page was marked dirty, but the buffers were | |
1249 | * clean. Someone wrote them back by hand with | |
1250 | * ll_rw_block/submit_bh. A rare case. | |
1251 | */ | |
1252 | int uptodate = 1; | |
1253 | do { | |
1254 | if (!buffer_uptodate(bh)) { | |
1255 | uptodate = 0; | |
1256 | break; | |
1257 | } | |
1258 | bh = bh->b_this_page; | |
1259 | } while (bh != head); | |
1260 | if (uptodate) | |
1261 | SetPageUptodate(page); | |
1262 | end_page_writeback(page); | |
1263 | /* | |
1264 | * The page and buffer_heads can be released at any time from | |
1265 | * here on. | |
1266 | */ | |
1267 | wbc->pages_skipped++; /* We didn't write this page */ | |
1268 | } | |
1269 | return err; | |
1270 | ||
1271 | recover: | |
1272 | /* | |
1273 | * ENOSPC, or some other error. We may already have added some | |
1274 | * blocks to the file, so we need to write these out to avoid | |
1275 | * exposing stale data. | |
1276 | * The page is currently locked and not marked for writeback | |
1277 | */ | |
1278 | bh = head; | |
1279 | /* Recovery: lock and submit the mapped buffers */ | |
1280 | do { | |
1281 | if (buffer_mapped(bh) && buffer_dirty(bh)) { | |
1282 | lock_buffer(bh); | |
1283 | mark_buffer_async_write(bh); | |
1284 | } else { | |
1285 | /* | |
1286 | * The buffer may have been set dirty during | |
1287 | * attachment to a dirty page. | |
1288 | */ | |
1289 | clear_buffer_dirty(bh); | |
1290 | } | |
1291 | } while ((bh = bh->b_this_page) != head); | |
1292 | SetPageError(page); | |
1293 | BUG_ON(PageWriteback(page)); | |
1294 | set_page_writeback(page); | |
1295 | do { | |
1296 | struct buffer_head *next = bh->b_this_page; | |
1297 | if (buffer_async_write(bh)) { | |
1298 | clear_buffer_dirty(bh); | |
1299 | submit_bh(WRITE, bh); | |
1300 | nr_underway++; | |
1301 | } | |
1302 | bh = next; | |
1303 | } while (bh != head); | |
1304 | unlock_page(page); | |
1305 | goto done; | |
1306 | } | |
1307 | ||
1308 | /* | |
1309 | * The generic ->writepage function for buffer-backed address_spaces | |
1310 | */ | |
dee26a9f CM |
1311 | static int btrfs_writepage(struct page *page, struct writeback_control *wbc) |
1312 | { | |
236454df CM |
1313 | struct inode * const inode = page->mapping->host; |
1314 | loff_t i_size = i_size_read(inode); | |
1315 | const pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT; | |
1316 | unsigned offset; | |
1317 | void *kaddr; | |
1318 | ||
1319 | /* Is the page fully inside i_size? */ | |
1320 | if (page->index < end_index) | |
1321 | return __btrfs_write_full_page(inode, page, wbc); | |
1322 | ||
1323 | /* Is the page fully outside i_size? (truncate in progress) */ | |
1324 | offset = i_size & (PAGE_CACHE_SIZE-1); | |
1325 | if (page->index >= end_index+1 || !offset) { | |
1326 | /* | |
1327 | * The page may have dirty, unmapped buffers. For example, | |
1328 | * they may have been added in ext3_writepage(). Make them | |
1329 | * freeable here, so the page does not leak. | |
1330 | */ | |
1331 | block_invalidatepage(page, 0); | |
1332 | unlock_page(page); | |
1333 | return 0; /* don't care */ | |
1334 | } | |
1335 | ||
1336 | /* | |
1337 | * The page straddles i_size. It must be zeroed out on each and every | |
1338 | * writepage invokation because it may be mmapped. "A file is mapped | |
1339 | * in multiples of the page size. For a file that is not a multiple of | |
1340 | * the page size, the remaining memory is zeroed when mapped, and | |
1341 | * writes to that region are not written out to the file." | |
1342 | */ | |
1343 | kaddr = kmap_atomic(page, KM_USER0); | |
1344 | memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset); | |
1345 | flush_dcache_page(page); | |
1346 | kunmap_atomic(kaddr, KM_USER0); | |
1347 | return __btrfs_write_full_page(inode, page, wbc); | |
dee26a9f | 1348 | } |
d561c025 | 1349 | |
f4b9aa8d CM |
1350 | static void btrfs_truncate(struct inode *inode) |
1351 | { | |
d6e4a428 | 1352 | struct btrfs_root *root = BTRFS_I(inode)->root; |
f4b9aa8d CM |
1353 | int ret; |
1354 | struct btrfs_trans_handle *trans; | |
1355 | ||
1356 | if (!S_ISREG(inode->i_mode)) | |
1357 | return; | |
1358 | if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) | |
1359 | return; | |
1360 | ||
e8f05c45 | 1361 | nobh_truncate_page(inode->i_mapping, inode->i_size); |
f4b9aa8d CM |
1362 | |
1363 | /* FIXME, add redo link to tree so we don't leak on crash */ | |
1364 | mutex_lock(&root->fs_info->fs_mutex); | |
1365 | trans = btrfs_start_transaction(root, 1); | |
1366 | ret = btrfs_truncate_in_trans(trans, root, inode); | |
1367 | BUG_ON(ret); | |
1368 | ret = btrfs_end_transaction(trans, root); | |
1369 | BUG_ON(ret); | |
1370 | mutex_unlock(&root->fs_info->fs_mutex); | |
1371 | mark_inode_dirty(inode); | |
1372 | } | |
1373 | ||
236454df CM |
1374 | /* |
1375 | * Make sure any changes to nobh_commit_write() are reflected in | |
1376 | * nobh_truncate_page(), since it doesn't call commit_write(). | |
1377 | */ | |
1378 | static int btrfs_commit_write(struct file *file, struct page *page, | |
1379 | unsigned from, unsigned to) | |
1380 | { | |
1381 | struct inode *inode = page->mapping->host; | |
1382 | struct buffer_head *bh; | |
1383 | loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to; | |
1384 | ||
1385 | SetPageUptodate(page); | |
1386 | bh = page_buffers(page); | |
1387 | if (buffer_mapped(bh) && bh->b_blocknr != 0) { | |
1388 | set_page_dirty(page); | |
1389 | } | |
1390 | if (pos > inode->i_size) { | |
1391 | i_size_write(inode, pos); | |
1392 | mark_inode_dirty(inode); | |
1393 | } | |
1394 | return 0; | |
1395 | } | |
1396 | ||
75dfe396 CM |
1397 | static int btrfs_copy_from_user(loff_t pos, int num_pages, int write_bytes, |
1398 | struct page **prepared_pages, | |
1399 | const char __user * buf) | |
1400 | { | |
1401 | long page_fault = 0; | |
1402 | int i; | |
1403 | int offset = pos & (PAGE_CACHE_SIZE - 1); | |
1404 | ||
1405 | for (i = 0; i < num_pages && write_bytes > 0; i++, offset = 0) { | |
1406 | size_t count = min_t(size_t, | |
1407 | PAGE_CACHE_SIZE - offset, write_bytes); | |
1408 | struct page *page = prepared_pages[i]; | |
1409 | fault_in_pages_readable(buf, count); | |
1410 | ||
1411 | /* Copy data from userspace to the current page */ | |
1412 | kmap(page); | |
1413 | page_fault = __copy_from_user(page_address(page) + offset, | |
1414 | buf, count); | |
1415 | /* Flush processor's dcache for this page */ | |
1416 | flush_dcache_page(page); | |
1417 | kunmap(page); | |
1418 | buf += count; | |
1419 | write_bytes -= count; | |
1420 | ||
1421 | if (page_fault) | |
1422 | break; | |
1423 | } | |
1424 | return page_fault ? -EFAULT : 0; | |
1425 | } | |
1426 | ||
1427 | static void btrfs_drop_pages(struct page **pages, size_t num_pages) | |
1428 | { | |
1429 | size_t i; | |
1430 | for (i = 0; i < num_pages; i++) { | |
1431 | if (!pages[i]) | |
1432 | break; | |
1433 | unlock_page(pages[i]); | |
1434 | mark_page_accessed(pages[i]); | |
1435 | page_cache_release(pages[i]); | |
1436 | } | |
1437 | } | |
1438 | static int dirty_and_release_pages(struct btrfs_trans_handle *trans, | |
1439 | struct btrfs_root *root, | |
1440 | struct file *file, | |
1441 | struct page **pages, | |
1442 | size_t num_pages, | |
1443 | loff_t pos, | |
1444 | size_t write_bytes) | |
1445 | { | |
1446 | int i; | |
1447 | int offset; | |
1448 | int err = 0; | |
1449 | int ret; | |
1450 | int this_write; | |
f254e52c | 1451 | struct inode *inode = file->f_path.dentry->d_inode; |
236454df CM |
1452 | struct buffer_head *bh; |
1453 | struct btrfs_file_extent_item *ei; | |
75dfe396 CM |
1454 | |
1455 | for (i = 0; i < num_pages; i++) { | |
1456 | offset = pos & (PAGE_CACHE_SIZE -1); | |
1457 | this_write = min(PAGE_CACHE_SIZE - offset, write_bytes); | |
f254e52c CM |
1458 | /* FIXME, one block at a time */ |
1459 | ||
1460 | mutex_lock(&root->fs_info->fs_mutex); | |
1461 | trans = btrfs_start_transaction(root, 1); | |
236454df CM |
1462 | |
1463 | bh = page_buffers(pages[i]); | |
1464 | if (buffer_mapped(bh) && bh->b_blocknr == 0) { | |
1465 | struct btrfs_key key; | |
1466 | struct btrfs_path *path; | |
1467 | char *ptr; | |
1468 | u32 datasize; | |
1469 | ||
1470 | path = btrfs_alloc_path(); | |
1471 | BUG_ON(!path); | |
1472 | key.objectid = inode->i_ino; | |
1473 | key.offset = pages[i]->index << PAGE_CACHE_SHIFT; | |
1474 | key.flags = 0; | |
1475 | btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY); | |
1476 | BUG_ON(write_bytes >= PAGE_CACHE_SIZE); | |
1477 | datasize = offset + | |
1478 | btrfs_file_extent_calc_inline_size(write_bytes); | |
1479 | ret = btrfs_insert_empty_item(trans, root, path, &key, | |
1480 | datasize); | |
1481 | BUG_ON(ret); | |
1482 | ei = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), | |
1483 | path->slots[0], struct btrfs_file_extent_item); | |
1484 | btrfs_set_file_extent_generation(ei, trans->transid); | |
1485 | btrfs_set_file_extent_type(ei, | |
1486 | BTRFS_FILE_EXTENT_INLINE); | |
1487 | ptr = btrfs_file_extent_inline_start(ei); | |
1488 | memcpy(ptr, bh->b_data, offset + write_bytes); | |
1489 | mark_buffer_dirty(path->nodes[0]); | |
1490 | btrfs_free_path(path); | |
1491 | } else { | |
1492 | btrfs_csum_file_block(trans, root, inode->i_ino, | |
f254e52c CM |
1493 | pages[i]->index << PAGE_CACHE_SHIFT, |
1494 | kmap(pages[i]), PAGE_CACHE_SIZE); | |
236454df CM |
1495 | kunmap(pages[i]); |
1496 | } | |
f254e52c CM |
1497 | SetPageChecked(pages[i]); |
1498 | ret = btrfs_end_transaction(trans, root); | |
1499 | BUG_ON(ret); | |
1500 | mutex_unlock(&root->fs_info->fs_mutex); | |
1501 | ||
236454df | 1502 | ret = btrfs_commit_write(file, pages[i], offset, |
75dfe396 CM |
1503 | offset + this_write); |
1504 | pos += this_write; | |
1505 | if (ret) { | |
1506 | err = ret; | |
1507 | goto failed; | |
1508 | } | |
1509 | WARN_ON(this_write > write_bytes); | |
1510 | write_bytes -= this_write; | |
1511 | } | |
1512 | failed: | |
1513 | return err; | |
1514 | } | |
1515 | ||
b18c6685 CM |
1516 | static int drop_extents(struct btrfs_trans_handle *trans, |
1517 | struct btrfs_root *root, | |
1518 | struct inode *inode, | |
1519 | u64 start, u64 end) | |
1520 | { | |
1521 | int ret; | |
1522 | struct btrfs_key key; | |
1523 | struct btrfs_leaf *leaf; | |
1524 | int slot; | |
1525 | struct btrfs_file_extent_item *extent; | |
236454df | 1526 | u64 extent_end = 0; |
b18c6685 CM |
1527 | int keep; |
1528 | struct btrfs_file_extent_item old; | |
1529 | struct btrfs_path *path; | |
1530 | u64 search_start = start; | |
1531 | int bookend; | |
236454df CM |
1532 | int found_type; |
1533 | int found_extent; | |
1534 | int found_inline; | |
1535 | ||
b18c6685 CM |
1536 | path = btrfs_alloc_path(); |
1537 | if (!path) | |
1538 | return -ENOMEM; | |
a429e513 CM |
1539 | while(1) { |
1540 | btrfs_release_path(root, path); | |
1541 | ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino, | |
1542 | search_start, -1); | |
1543 | if (ret < 0) | |
70b2befd | 1544 | goto out; |
a429e513 CM |
1545 | if (ret > 0) { |
1546 | if (path->slots[0] == 0) { | |
236454df | 1547 | ret = 0; |
a429e513 CM |
1548 | goto out; |
1549 | } | |
1550 | path->slots[0]--; | |
70b2befd | 1551 | } |
b18c6685 CM |
1552 | keep = 0; |
1553 | bookend = 0; | |
236454df CM |
1554 | found_extent = 0; |
1555 | found_inline = 0; | |
1556 | extent = NULL; | |
b18c6685 CM |
1557 | leaf = btrfs_buffer_leaf(path->nodes[0]); |
1558 | slot = path->slots[0]; | |
1559 | btrfs_disk_key_to_cpu(&key, &leaf->items[slot].key); | |
b18c6685 CM |
1560 | if (key.offset >= end || key.objectid != inode->i_ino) { |
1561 | ret = 0; | |
1562 | goto out; | |
1563 | } | |
236454df CM |
1564 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) { |
1565 | ret = 0; | |
1566 | goto out; | |
1567 | } | |
1568 | extent = btrfs_item_ptr(leaf, slot, | |
1569 | struct btrfs_file_extent_item); | |
1570 | found_type = btrfs_file_extent_type(extent); | |
1571 | if (found_type == BTRFS_FILE_EXTENT_REG) { | |
1572 | extent_end = key.offset + | |
1573 | (btrfs_file_extent_num_blocks(extent) << | |
1574 | inode->i_blkbits); | |
1575 | found_extent = 1; | |
1576 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { | |
1577 | found_inline = 1; | |
1578 | extent_end = key.offset + | |
1579 | btrfs_file_extent_inline_len(leaf->items + slot); | |
1580 | } | |
1581 | ||
1582 | if (!found_extent && !found_inline) { | |
1583 | ret = 0; | |
a429e513 | 1584 | goto out; |
236454df CM |
1585 | } |
1586 | ||
1587 | if (search_start >= extent_end) { | |
1588 | ret = 0; | |
a429e513 | 1589 | goto out; |
236454df CM |
1590 | } |
1591 | ||
a429e513 | 1592 | search_start = extent_end; |
b18c6685 CM |
1593 | |
1594 | if (end < extent_end && end >= key.offset) { | |
236454df CM |
1595 | if (found_extent) { |
1596 | memcpy(&old, extent, sizeof(old)); | |
1597 | ret = btrfs_inc_extent_ref(trans, root, | |
1598 | btrfs_file_extent_disk_blocknr(&old), | |
1599 | btrfs_file_extent_disk_num_blocks(&old)); | |
1600 | BUG_ON(ret); | |
1601 | } | |
1602 | WARN_ON(found_inline); | |
b18c6685 CM |
1603 | bookend = 1; |
1604 | } | |
1605 | ||
1606 | if (start > key.offset) { | |
1607 | u64 new_num; | |
a429e513 | 1608 | u64 old_num; |
b18c6685 CM |
1609 | /* truncate existing extent */ |
1610 | keep = 1; | |
1611 | WARN_ON(start & (root->blocksize - 1)); | |
236454df CM |
1612 | if (found_extent) { |
1613 | new_num = (start - key.offset) >> | |
1614 | inode->i_blkbits; | |
1615 | old_num = btrfs_file_extent_num_blocks(extent); | |
1616 | inode->i_blocks -= (old_num - new_num) << 3; | |
1617 | btrfs_set_file_extent_num_blocks(extent, | |
1618 | new_num); | |
1619 | mark_buffer_dirty(path->nodes[0]); | |
1620 | } else { | |
1621 | WARN_ON(1); | |
1622 | /* | |
1623 | ret = btrfs_truncate_item(trans, root, path, | |
1624 | start - key.offset); | |
1625 | BUG_ON(ret); | |
1626 | */ | |
1627 | } | |
b18c6685 CM |
1628 | } |
1629 | if (!keep) { | |
236454df CM |
1630 | u64 disk_blocknr = 0; |
1631 | u64 disk_num_blocks = 0; | |
1632 | u64 extent_num_blocks = 0; | |
1633 | if (found_extent) { | |
1634 | disk_blocknr = | |
1635 | btrfs_file_extent_disk_blocknr(extent); | |
1636 | disk_num_blocks = | |
1637 | btrfs_file_extent_disk_num_blocks(extent); | |
1638 | extent_num_blocks = | |
1639 | btrfs_file_extent_num_blocks(extent); | |
1640 | } | |
b18c6685 CM |
1641 | ret = btrfs_del_item(trans, root, path); |
1642 | BUG_ON(ret); | |
1643 | btrfs_release_path(root, path); | |
236454df CM |
1644 | if (found_extent) { |
1645 | inode->i_blocks -= | |
1646 | btrfs_file_extent_num_blocks(extent) << 3; | |
1647 | ret = btrfs_free_extent(trans, root, | |
1648 | disk_blocknr, | |
1649 | disk_num_blocks, 0); | |
1650 | } | |
b18c6685 CM |
1651 | |
1652 | BUG_ON(ret); | |
1653 | if (!bookend && search_start >= end) { | |
1654 | ret = 0; | |
1655 | goto out; | |
1656 | } | |
1657 | if (!bookend) | |
a429e513 | 1658 | continue; |
b18c6685 | 1659 | } |
236454df | 1660 | if (bookend && found_extent) { |
b18c6685 CM |
1661 | /* create bookend */ |
1662 | struct btrfs_key ins; | |
b18c6685 CM |
1663 | ins.objectid = inode->i_ino; |
1664 | ins.offset = end; | |
1665 | ins.flags = 0; | |
1666 | btrfs_set_key_type(&ins, BTRFS_EXTENT_DATA_KEY); | |
1667 | ||
1668 | btrfs_release_path(root, path); | |
b18c6685 CM |
1669 | ret = btrfs_insert_empty_item(trans, root, path, &ins, |
1670 | sizeof(*extent)); | |
1671 | BUG_ON(ret); | |
1672 | extent = btrfs_item_ptr( | |
1673 | btrfs_buffer_leaf(path->nodes[0]), | |
1674 | path->slots[0], | |
1675 | struct btrfs_file_extent_item); | |
1676 | btrfs_set_file_extent_disk_blocknr(extent, | |
1677 | btrfs_file_extent_disk_blocknr(&old)); | |
1678 | btrfs_set_file_extent_disk_num_blocks(extent, | |
1679 | btrfs_file_extent_disk_num_blocks(&old)); | |
1680 | ||
1681 | btrfs_set_file_extent_offset(extent, | |
1682 | btrfs_file_extent_offset(&old) + | |
1683 | ((end - key.offset) >> inode->i_blkbits)); | |
1684 | WARN_ON(btrfs_file_extent_num_blocks(&old) < | |
1685 | (end - key.offset) >> inode->i_blkbits); | |
1686 | btrfs_set_file_extent_num_blocks(extent, | |
1687 | btrfs_file_extent_num_blocks(&old) - | |
1688 | ((end - key.offset) >> inode->i_blkbits)); | |
1689 | ||
236454df CM |
1690 | btrfs_set_file_extent_type(extent, |
1691 | BTRFS_FILE_EXTENT_REG); | |
b18c6685 CM |
1692 | btrfs_set_file_extent_generation(extent, |
1693 | btrfs_file_extent_generation(&old)); | |
b18c6685 | 1694 | btrfs_mark_buffer_dirty(path->nodes[0]); |
a429e513 CM |
1695 | inode->i_blocks += |
1696 | btrfs_file_extent_num_blocks(extent) << 3; | |
b18c6685 | 1697 | ret = 0; |
70b2befd | 1698 | goto out; |
b18c6685 | 1699 | } |
b18c6685 | 1700 | } |
b18c6685 | 1701 | out: |
b18c6685 CM |
1702 | btrfs_free_path(path); |
1703 | return ret; | |
1704 | } | |
1705 | ||
1706 | static int prepare_pages(struct btrfs_root *root, | |
75dfe396 CM |
1707 | struct file *file, |
1708 | struct page **pages, | |
1709 | size_t num_pages, | |
1710 | loff_t pos, | |
2932f3ec CM |
1711 | unsigned long first_index, |
1712 | unsigned long last_index, | |
6567e837 CM |
1713 | size_t write_bytes, |
1714 | u64 alloc_extent_start) | |
75dfe396 CM |
1715 | { |
1716 | int i; | |
1717 | unsigned long index = pos >> PAGE_CACHE_SHIFT; | |
1718 | struct inode *inode = file->f_path.dentry->d_inode; | |
1719 | int offset; | |
1720 | int err = 0; | |
75dfe396 | 1721 | int this_write; |
6567e837 CM |
1722 | struct buffer_head *bh; |
1723 | struct buffer_head *head; | |
75dfe396 CM |
1724 | loff_t isize = i_size_read(inode); |
1725 | ||
1726 | memset(pages, 0, num_pages * sizeof(struct page *)); | |
1727 | ||
1728 | for (i = 0; i < num_pages; i++) { | |
1729 | pages[i] = grab_cache_page(inode->i_mapping, index + i); | |
1730 | if (!pages[i]) { | |
1731 | err = -ENOMEM; | |
1732 | goto failed_release; | |
1733 | } | |
1734 | offset = pos & (PAGE_CACHE_SIZE -1); | |
1735 | this_write = min(PAGE_CACHE_SIZE - offset, write_bytes); | |
6567e837 CM |
1736 | create_empty_buffers(pages[i], root->fs_info->sb->s_blocksize, |
1737 | (1 << BH_Uptodate)); | |
1738 | head = page_buffers(pages[i]); | |
1739 | bh = head; | |
1740 | do { | |
1741 | err = btrfs_map_bh_to_logical(root, bh, | |
1742 | alloc_extent_start); | |
1743 | BUG_ON(err); | |
1744 | if (err) | |
1745 | goto failed_truncate; | |
1746 | bh = bh->b_this_page; | |
236454df CM |
1747 | if (alloc_extent_start) |
1748 | alloc_extent_start++; | |
6567e837 | 1749 | } while (bh != head); |
75dfe396 | 1750 | pos += this_write; |
75dfe396 CM |
1751 | WARN_ON(this_write > write_bytes); |
1752 | write_bytes -= this_write; | |
1753 | } | |
1754 | return 0; | |
1755 | ||
1756 | failed_release: | |
1757 | btrfs_drop_pages(pages, num_pages); | |
1758 | return err; | |
1759 | ||
1760 | failed_truncate: | |
1761 | btrfs_drop_pages(pages, num_pages); | |
1762 | if (pos > isize) | |
1763 | vmtruncate(inode, isize); | |
1764 | return err; | |
1765 | } | |
1766 | ||
1767 | static ssize_t btrfs_file_write(struct file *file, const char __user *buf, | |
1768 | size_t count, loff_t *ppos) | |
1769 | { | |
1770 | loff_t pos; | |
1771 | size_t num_written = 0; | |
1772 | int err = 0; | |
1773 | int ret = 0; | |
75dfe396 | 1774 | struct inode *inode = file->f_path.dentry->d_inode; |
d6e4a428 | 1775 | struct btrfs_root *root = BTRFS_I(inode)->root; |
b18c6685 | 1776 | struct page *pages[8]; |
a429e513 | 1777 | struct page *pinned[2] = { NULL, NULL }; |
2932f3ec CM |
1778 | unsigned long first_index; |
1779 | unsigned long last_index; | |
6567e837 CM |
1780 | u64 start_pos; |
1781 | u64 num_blocks; | |
1782 | u64 alloc_extent_start; | |
6567e837 | 1783 | struct btrfs_trans_handle *trans; |
b18c6685 | 1784 | struct btrfs_key ins; |
75dfe396 CM |
1785 | |
1786 | if (file->f_flags & O_DIRECT) | |
1787 | return -EINVAL; | |
1788 | pos = *ppos; | |
75dfe396 CM |
1789 | vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE); |
1790 | current->backing_dev_info = inode->i_mapping->backing_dev_info; | |
1791 | err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode)); | |
1792 | if (err) | |
1793 | goto out; | |
1794 | if (count == 0) | |
1795 | goto out; | |
1796 | err = remove_suid(file->f_path.dentry); | |
1797 | if (err) | |
1798 | goto out; | |
1799 | file_update_time(file); | |
a429e513 CM |
1800 | |
1801 | start_pos = pos & ~((u64)PAGE_CACHE_SIZE - 1); | |
1802 | num_blocks = (count + pos - start_pos + root->blocksize - 1) >> | |
1803 | inode->i_blkbits; | |
1804 | ||
75dfe396 | 1805 | mutex_lock(&inode->i_mutex); |
2932f3ec CM |
1806 | first_index = pos >> PAGE_CACHE_SHIFT; |
1807 | last_index = (pos + count) >> PAGE_CACHE_SHIFT; | |
6567e837 | 1808 | |
a429e513 CM |
1809 | if ((first_index << PAGE_CACHE_SHIFT) < inode->i_size && |
1810 | (pos & (PAGE_CACHE_SIZE - 1))) { | |
1811 | pinned[0] = grab_cache_page(inode->i_mapping, first_index); | |
1812 | if (!PageUptodate(pinned[0])) { | |
1813 | ret = mpage_readpage(pinned[0], btrfs_get_block); | |
1814 | BUG_ON(ret); | |
1815 | } else { | |
1816 | unlock_page(pinned[0]); | |
1817 | } | |
1818 | } | |
1819 | if (first_index != last_index && | |
1820 | (last_index << PAGE_CACHE_SHIFT) < inode->i_size && | |
1821 | (count & (PAGE_CACHE_SIZE - 1))) { | |
1822 | pinned[1] = grab_cache_page(inode->i_mapping, last_index); | |
1823 | if (!PageUptodate(pinned[1])) { | |
1824 | ret = mpage_readpage(pinned[1], btrfs_get_block); | |
1825 | BUG_ON(ret); | |
1826 | } else { | |
1827 | unlock_page(pinned[1]); | |
1828 | } | |
1829 | } | |
1830 | ||
6567e837 CM |
1831 | mutex_lock(&root->fs_info->fs_mutex); |
1832 | trans = btrfs_start_transaction(root, 1); | |
1833 | if (!trans) { | |
1834 | err = -ENOMEM; | |
b18c6685 | 1835 | mutex_unlock(&root->fs_info->fs_mutex); |
6567e837 CM |
1836 | goto out_unlock; |
1837 | } | |
a429e513 CM |
1838 | /* FIXME blocksize != 4096 */ |
1839 | inode->i_blocks += num_blocks << 3; | |
b18c6685 | 1840 | if (start_pos < inode->i_size) { |
a429e513 | 1841 | /* FIXME blocksize != pagesize */ |
b18c6685 CM |
1842 | ret = drop_extents(trans, root, inode, |
1843 | start_pos, | |
1844 | (pos + count + root->blocksize -1) & | |
a429e513 | 1845 | ~((u64)root->blocksize - 1)); |
236454df | 1846 | BUG_ON(ret); |
b18c6685 | 1847 | } |
236454df CM |
1848 | if (inode->i_size >= PAGE_CACHE_SIZE || pos + count < inode->i_size || |
1849 | pos + count - start_pos > BTRFS_MAX_INLINE_DATA_SIZE(root)) { | |
1850 | ret = btrfs_alloc_extent(trans, root, num_blocks, 1, | |
b18c6685 | 1851 | (u64)-1, &ins); |
236454df CM |
1852 | BUG_ON(ret); |
1853 | ret = btrfs_insert_file_extent(trans, root, inode->i_ino, | |
b18c6685 | 1854 | start_pos, ins.objectid, ins.offset); |
236454df CM |
1855 | BUG_ON(ret); |
1856 | } else { | |
1857 | ins.offset = 0; | |
1858 | ins.objectid = 0; | |
1859 | } | |
6567e837 | 1860 | BUG_ON(ret); |
b18c6685 CM |
1861 | alloc_extent_start = ins.objectid; |
1862 | ret = btrfs_end_transaction(trans, root); | |
6567e837 CM |
1863 | mutex_unlock(&root->fs_info->fs_mutex); |
1864 | ||
75dfe396 CM |
1865 | while(count > 0) { |
1866 | size_t offset = pos & (PAGE_CACHE_SIZE - 1); | |
1867 | size_t write_bytes = min(count, PAGE_CACHE_SIZE - offset); | |
1868 | size_t num_pages = (write_bytes + PAGE_CACHE_SIZE - 1) >> | |
1869 | PAGE_CACHE_SHIFT; | |
b18c6685 CM |
1870 | |
1871 | memset(pages, 0, sizeof(pages)); | |
1872 | ret = prepare_pages(root, file, pages, num_pages, | |
6567e837 CM |
1873 | pos, first_index, last_index, |
1874 | write_bytes, alloc_extent_start); | |
75dfe396 | 1875 | BUG_ON(ret); |
b18c6685 | 1876 | |
6567e837 | 1877 | /* FIXME blocks != pagesize */ |
236454df CM |
1878 | if (alloc_extent_start) |
1879 | alloc_extent_start += num_pages; | |
75dfe396 CM |
1880 | ret = btrfs_copy_from_user(pos, num_pages, |
1881 | write_bytes, pages, buf); | |
1882 | BUG_ON(ret); | |
1883 | ||
f254e52c | 1884 | ret = dirty_and_release_pages(NULL, root, file, pages, |
70b2befd | 1885 | num_pages, pos, write_bytes); |
75dfe396 CM |
1886 | BUG_ON(ret); |
1887 | btrfs_drop_pages(pages, num_pages); | |
1888 | ||
75dfe396 CM |
1889 | buf += write_bytes; |
1890 | count -= write_bytes; | |
1891 | pos += write_bytes; | |
1892 | num_written += write_bytes; | |
1893 | ||
1894 | balance_dirty_pages_ratelimited(inode->i_mapping); | |
1895 | cond_resched(); | |
1896 | } | |
6567e837 | 1897 | out_unlock: |
75dfe396 CM |
1898 | mutex_unlock(&inode->i_mutex); |
1899 | out: | |
a429e513 CM |
1900 | if (pinned[0]) |
1901 | page_cache_release(pinned[0]); | |
1902 | if (pinned[1]) | |
1903 | page_cache_release(pinned[1]); | |
75dfe396 CM |
1904 | *ppos = pos; |
1905 | current->backing_dev_info = NULL; | |
a429e513 | 1906 | mark_inode_dirty(inode); |
75dfe396 CM |
1907 | return num_written ? num_written : err; |
1908 | } | |
1909 | ||
e8f05c45 | 1910 | #if 0 |
df24a2b9 CM |
1911 | static ssize_t inline_one_page(struct btrfs_root *root, struct inode *inode, |
1912 | struct page *page, loff_t pos, | |
1913 | size_t offset, size_t write_bytes) | |
1914 | { | |
1915 | struct btrfs_path *path; | |
1916 | struct btrfs_trans_handle *trans; | |
1917 | struct btrfs_key key; | |
1918 | struct btrfs_leaf *leaf; | |
1919 | struct btrfs_key found_key; | |
1920 | int ret; | |
1921 | size_t copy_size = 0; | |
1922 | char *dst = NULL; | |
1923 | int err = 0; | |
1924 | size_t num_written = 0; | |
1925 | ||
1926 | path = btrfs_alloc_path(); | |
1927 | BUG_ON(!path); | |
1928 | mutex_lock(&root->fs_info->fs_mutex); | |
1929 | trans = btrfs_start_transaction(root, 1); | |
1930 | key.objectid = inode->i_ino; | |
1931 | key.flags = 0; | |
1932 | btrfs_set_key_type(&key, BTRFS_INLINE_DATA_KEY); | |
1933 | ||
1934 | again: | |
1935 | key.offset = pos; | |
1936 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); | |
1937 | if (ret < 0) { | |
1938 | err = ret; | |
1939 | goto out; | |
1940 | } | |
1941 | if (ret == 0) { | |
1942 | leaf = btrfs_buffer_leaf(path->nodes[0]); | |
1943 | btrfs_disk_key_to_cpu(&found_key, | |
1944 | &leaf->items[path->slots[0]].key); | |
1945 | copy_size = btrfs_item_size(leaf->items + path->slots[0]); | |
1946 | dst = btrfs_item_ptr(leaf, path->slots[0], char); | |
1947 | copy_size = min(write_bytes, copy_size); | |
1948 | goto copyit; | |
1949 | } else { | |
1950 | int slot = path->slots[0]; | |
1951 | if (slot > 0) { | |
1952 | slot--; | |
1953 | } | |
1954 | // FIXME find max key | |
1955 | leaf = btrfs_buffer_leaf(path->nodes[0]); | |
1956 | btrfs_disk_key_to_cpu(&found_key, | |
1957 | &leaf->items[slot].key); | |
1958 | if (found_key.objectid != inode->i_ino) | |
1959 | goto insert; | |
1960 | if (btrfs_key_type(&found_key) != BTRFS_INLINE_DATA_KEY) | |
1961 | goto insert; | |
1962 | copy_size = btrfs_item_size(leaf->items + slot); | |
1963 | if (found_key.offset + copy_size <= pos) | |
1964 | goto insert; | |
1965 | dst = btrfs_item_ptr(leaf, path->slots[0], char); | |
1966 | dst += pos - found_key.offset; | |
1967 | copy_size = copy_size - (pos - found_key.offset); | |
1968 | BUG_ON(copy_size < 0); | |
1969 | copy_size = min(write_bytes, copy_size); | |
1970 | WARN_ON(copy_size == 0); | |
1971 | goto copyit; | |
1972 | } | |
1973 | insert: | |
1974 | btrfs_release_path(root, path); | |
d4dbff95 CM |
1975 | copy_size = min(write_bytes, |
1976 | (size_t)BTRFS_LEAF_DATA_SIZE(root) - | |
1977 | sizeof(struct btrfs_item) * 4); | |
df24a2b9 CM |
1978 | ret = btrfs_insert_empty_item(trans, root, path, &key, copy_size); |
1979 | BUG_ON(ret); | |
1980 | dst = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), | |
1981 | path->slots[0], char); | |
1982 | copyit: | |
1983 | WARN_ON(copy_size == 0); | |
1984 | WARN_ON(dst + copy_size > | |
1985 | btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), | |
1986 | path->slots[0], char) + | |
1987 | btrfs_item_size(btrfs_buffer_leaf(path->nodes[0])->items + | |
1988 | path->slots[0])); | |
1989 | btrfs_memcpy(root, path->nodes[0]->b_data, dst, | |
1990 | page_address(page) + offset, copy_size); | |
1991 | mark_buffer_dirty(path->nodes[0]); | |
1992 | btrfs_release_path(root, path); | |
1993 | pos += copy_size; | |
1994 | offset += copy_size; | |
1995 | num_written += copy_size; | |
1996 | write_bytes -= copy_size; | |
1997 | if (write_bytes) | |
1998 | goto again; | |
1999 | out: | |
2000 | btrfs_free_path(path); | |
2001 | ret = btrfs_end_transaction(trans, root); | |
2002 | BUG_ON(ret); | |
2003 | mutex_unlock(&root->fs_info->fs_mutex); | |
2004 | return num_written ? num_written : err; | |
2005 | } | |
2006 | ||
2007 | static ssize_t btrfs_file_inline_write(struct file *file, | |
2008 | const char __user *buf, | |
2009 | size_t count, loff_t *ppos) | |
2010 | { | |
2011 | loff_t pos; | |
2012 | size_t num_written = 0; | |
2013 | int err = 0; | |
2014 | int ret = 0; | |
2015 | struct inode *inode = file->f_path.dentry->d_inode; | |
d6e4a428 | 2016 | struct btrfs_root *root = BTRFS_I(inode)->root; |
df24a2b9 CM |
2017 | unsigned long page_index; |
2018 | ||
2019 | if (file->f_flags & O_DIRECT) | |
2020 | return -EINVAL; | |
2021 | pos = *ppos; | |
2022 | ||
2023 | vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE); | |
2024 | current->backing_dev_info = inode->i_mapping->backing_dev_info; | |
2025 | err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode)); | |
2026 | if (err) | |
2027 | goto out; | |
2028 | if (count == 0) | |
2029 | goto out; | |
2030 | err = remove_suid(file->f_path.dentry); | |
2031 | if (err) | |
2032 | goto out; | |
2033 | file_update_time(file); | |
2034 | mutex_lock(&inode->i_mutex); | |
2035 | while(count > 0) { | |
2036 | size_t offset = pos & (PAGE_CACHE_SIZE - 1); | |
2037 | size_t write_bytes = min(count, PAGE_CACHE_SIZE - offset); | |
2038 | struct page *page; | |
2039 | ||
2040 | page_index = pos >> PAGE_CACHE_SHIFT; | |
2041 | page = grab_cache_page(inode->i_mapping, page_index); | |
2042 | if (!PageUptodate(page)) { | |
2043 | ret = mpage_readpage(page, btrfs_get_block); | |
2044 | BUG_ON(ret); | |
2045 | lock_page(page); | |
2046 | } | |
2047 | ret = btrfs_copy_from_user(pos, 1, | |
2048 | write_bytes, &page, buf); | |
2049 | BUG_ON(ret); | |
2050 | write_bytes = inline_one_page(root, inode, page, pos, | |
2051 | offset, write_bytes); | |
2052 | SetPageUptodate(page); | |
2053 | if (write_bytes > 0 && pos + write_bytes > inode->i_size) { | |
2054 | i_size_write(inode, pos + write_bytes); | |
2055 | mark_inode_dirty(inode); | |
2056 | } | |
2057 | page_cache_release(page); | |
2058 | unlock_page(page); | |
2059 | if (write_bytes < 0) | |
2060 | goto out_unlock; | |
2061 | buf += write_bytes; | |
2062 | count -= write_bytes; | |
2063 | pos += write_bytes; | |
2064 | num_written += write_bytes; | |
2065 | ||
2066 | balance_dirty_pages_ratelimited(inode->i_mapping); | |
2067 | cond_resched(); | |
2068 | } | |
2069 | out_unlock: | |
2070 | mutex_unlock(&inode->i_mutex); | |
2071 | out: | |
2072 | *ppos = pos; | |
2073 | current->backing_dev_info = NULL; | |
2074 | return num_written ? num_written : err; | |
2075 | } | |
e8f05c45 | 2076 | #endif |
df24a2b9 | 2077 | |
f254e52c CM |
2078 | static int btrfs_read_actor(read_descriptor_t *desc, struct page *page, |
2079 | unsigned long offset, unsigned long size) | |
2080 | { | |
2081 | char *kaddr; | |
2082 | unsigned long left, count = desc->count; | |
d6e4a428 | 2083 | struct inode *inode = page->mapping->host; |
f254e52c CM |
2084 | |
2085 | if (size > count) | |
2086 | size = count; | |
2087 | ||
2088 | if (!PageChecked(page)) { | |
2089 | /* FIXME, do it per block */ | |
d6e4a428 | 2090 | struct btrfs_root *root = BTRFS_I(inode)->root; |
236454df | 2091 | |
f254e52c | 2092 | int ret = btrfs_csum_verify_file_block(root, |
236454df CM |
2093 | page->mapping->host->i_ino, |
2094 | page->index << PAGE_CACHE_SHIFT, | |
2095 | kmap(page), PAGE_CACHE_SIZE); | |
f254e52c CM |
2096 | if (ret) { |
2097 | printk("failed to verify ino %lu page %lu\n", | |
2098 | page->mapping->host->i_ino, | |
2099 | page->index); | |
2100 | memset(page_address(page), 0, PAGE_CACHE_SIZE); | |
2101 | } | |
2102 | SetPageChecked(page); | |
2103 | kunmap(page); | |
2104 | } | |
2105 | /* | |
2106 | * Faults on the destination of a read are common, so do it before | |
2107 | * taking the kmap. | |
2108 | */ | |
2109 | if (!fault_in_pages_writeable(desc->arg.buf, size)) { | |
2110 | kaddr = kmap_atomic(page, KM_USER0); | |
2111 | left = __copy_to_user_inatomic(desc->arg.buf, | |
2112 | kaddr + offset, size); | |
2113 | kunmap_atomic(kaddr, KM_USER0); | |
2114 | if (left == 0) | |
2115 | goto success; | |
2116 | } | |
2117 | ||
2118 | /* Do it the slow way */ | |
2119 | kaddr = kmap(page); | |
2120 | left = __copy_to_user(desc->arg.buf, kaddr + offset, size); | |
2121 | kunmap(page); | |
2122 | ||
2123 | if (left) { | |
2124 | size -= left; | |
2125 | desc->error = -EFAULT; | |
2126 | } | |
2127 | success: | |
2128 | desc->count = count - size; | |
2129 | desc->written += size; | |
2130 | desc->arg.buf += size; | |
2131 | return size; | |
2132 | } | |
2133 | ||
2134 | /** | |
2135 | * btrfs_file_aio_read - filesystem read routine | |
2136 | * @iocb: kernel I/O control block | |
2137 | * @iov: io vector request | |
2138 | * @nr_segs: number of segments in the iovec | |
2139 | * @pos: current file position | |
2140 | */ | |
2141 | static ssize_t btrfs_file_aio_read(struct kiocb *iocb, const struct iovec *iov, | |
2142 | unsigned long nr_segs, loff_t pos) | |
2143 | { | |
2144 | struct file *filp = iocb->ki_filp; | |
2145 | ssize_t retval; | |
2146 | unsigned long seg; | |
2147 | size_t count; | |
2148 | loff_t *ppos = &iocb->ki_pos; | |
2149 | ||
2150 | count = 0; | |
2151 | for (seg = 0; seg < nr_segs; seg++) { | |
2152 | const struct iovec *iv = &iov[seg]; | |
2153 | ||
2154 | /* | |
2155 | * If any segment has a negative length, or the cumulative | |
2156 | * length ever wraps negative then return -EINVAL. | |
2157 | */ | |
2158 | count += iv->iov_len; | |
2159 | if (unlikely((ssize_t)(count|iv->iov_len) < 0)) | |
2160 | return -EINVAL; | |
2161 | if (access_ok(VERIFY_WRITE, iv->iov_base, iv->iov_len)) | |
2162 | continue; | |
2163 | if (seg == 0) | |
2164 | return -EFAULT; | |
2165 | nr_segs = seg; | |
2166 | count -= iv->iov_len; /* This segment is no good */ | |
2167 | break; | |
2168 | } | |
2169 | retval = 0; | |
2170 | if (count) { | |
2171 | for (seg = 0; seg < nr_segs; seg++) { | |
2172 | read_descriptor_t desc; | |
2173 | ||
2174 | desc.written = 0; | |
2175 | desc.arg.buf = iov[seg].iov_base; | |
2176 | desc.count = iov[seg].iov_len; | |
2177 | if (desc.count == 0) | |
2178 | continue; | |
2179 | desc.error = 0; | |
2180 | do_generic_file_read(filp, ppos, &desc, | |
2181 | btrfs_read_actor); | |
2182 | retval += desc.written; | |
2183 | if (desc.error) { | |
2184 | retval = retval ?: desc.error; | |
2185 | break; | |
2186 | } | |
2187 | } | |
2188 | } | |
2189 | return retval; | |
2190 | } | |
2191 | ||
2619ba1f CM |
2192 | static int create_subvol(struct btrfs_root *root, char *name, int namelen) |
2193 | { | |
2194 | struct btrfs_trans_handle *trans; | |
2195 | struct btrfs_key key; | |
2196 | struct btrfs_root_item root_item; | |
2197 | struct btrfs_inode_item *inode_item; | |
2198 | struct buffer_head *subvol; | |
2199 | struct btrfs_leaf *leaf; | |
2200 | struct btrfs_root *new_root; | |
2201 | struct inode *inode; | |
2202 | int ret; | |
2203 | u64 objectid; | |
2204 | u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID; | |
2205 | ||
2206 | mutex_lock(&root->fs_info->fs_mutex); | |
2207 | trans = btrfs_start_transaction(root, 1); | |
2208 | BUG_ON(!trans); | |
2209 | ||
2210 | subvol = btrfs_alloc_free_block(trans, root); | |
2211 | leaf = btrfs_buffer_leaf(subvol); | |
2212 | btrfs_set_header_nritems(&leaf->header, 0); | |
2213 | btrfs_set_header_level(&leaf->header, 0); | |
7eccb903 | 2214 | btrfs_set_header_blocknr(&leaf->header, bh_blocknr(subvol)); |
2619ba1f CM |
2215 | btrfs_set_header_generation(&leaf->header, trans->transid); |
2216 | memcpy(leaf->header.fsid, root->fs_info->disk_super->fsid, | |
2217 | sizeof(leaf->header.fsid)); | |
2218 | ||
2219 | inode_item = &root_item.inode; | |
2220 | memset(inode_item, 0, sizeof(*inode_item)); | |
2221 | btrfs_set_inode_generation(inode_item, 1); | |
2222 | btrfs_set_inode_size(inode_item, 3); | |
2223 | btrfs_set_inode_nlink(inode_item, 1); | |
2224 | btrfs_set_inode_nblocks(inode_item, 1); | |
2225 | btrfs_set_inode_mode(inode_item, S_IFDIR | 0755); | |
2226 | ||
7eccb903 | 2227 | btrfs_set_root_blocknr(&root_item, bh_blocknr(subvol)); |
2619ba1f CM |
2228 | btrfs_set_root_refs(&root_item, 1); |
2229 | ||
2230 | mark_buffer_dirty(subvol); | |
2231 | brelse(subvol); | |
2232 | subvol = NULL; | |
2233 | ||
2234 | ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root, | |
2235 | 0, &objectid); | |
2236 | BUG_ON(ret); | |
2237 | ||
2238 | btrfs_set_root_dirid(&root_item, new_dirid); | |
2239 | ||
2240 | key.objectid = objectid; | |
2241 | key.offset = 1; | |
2242 | key.flags = 0; | |
2243 | btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY); | |
2244 | ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key, | |
2245 | &root_item); | |
2246 | BUG_ON(ret); | |
2247 | ||
2248 | /* | |
2249 | * insert the directory item | |
2250 | */ | |
2251 | key.offset = (u64)-1; | |
2252 | ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root, | |
2253 | name, namelen, | |
2254 | root->fs_info->sb->s_root->d_inode->i_ino, | |
2255 | &key, 0); | |
2256 | BUG_ON(ret); | |
2257 | ||
2258 | ret = btrfs_commit_transaction(trans, root); | |
2259 | BUG_ON(ret); | |
2260 | ||
2261 | new_root = btrfs_read_fs_root(root->fs_info, &key); | |
2262 | BUG_ON(!new_root); | |
2263 | ||
2264 | trans = btrfs_start_transaction(new_root, 1); | |
2265 | BUG_ON(!trans); | |
2266 | ||
2267 | inode = btrfs_new_inode(trans, new_root, new_dirid, S_IFDIR | 0700); | |
2268 | inode->i_op = &btrfs_dir_inode_operations; | |
2269 | inode->i_fop = &btrfs_dir_file_operations; | |
2270 | ||
2271 | ret = btrfs_make_empty_dir(trans, new_root, new_dirid, new_dirid); | |
2272 | BUG_ON(ret); | |
2273 | ||
2274 | inode->i_nlink = 1; | |
2275 | inode->i_size = 6; | |
2276 | ret = btrfs_update_inode(trans, new_root, inode); | |
2277 | BUG_ON(ret); | |
2278 | ||
2279 | ret = btrfs_commit_transaction(trans, new_root); | |
2280 | BUG_ON(ret); | |
2281 | ||
2282 | iput(inode); | |
2283 | ||
2284 | mutex_unlock(&root->fs_info->fs_mutex); | |
2285 | return 0; | |
2286 | } | |
2287 | ||
c5739bba CM |
2288 | static int create_snapshot(struct btrfs_root *root, char *name, int namelen) |
2289 | { | |
2290 | struct btrfs_trans_handle *trans; | |
2291 | struct btrfs_key key; | |
2292 | struct btrfs_root_item new_root_item; | |
2293 | int ret; | |
2294 | u64 objectid; | |
2295 | ||
2619ba1f CM |
2296 | if (!root->ref_cows) |
2297 | return -EINVAL; | |
2298 | ||
c5739bba CM |
2299 | mutex_lock(&root->fs_info->fs_mutex); |
2300 | trans = btrfs_start_transaction(root, 1); | |
2301 | BUG_ON(!trans); | |
2302 | ||
2303 | ret = btrfs_update_inode(trans, root, root->inode); | |
2304 | BUG_ON(ret); | |
2305 | ||
1b05da2e CM |
2306 | ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root, |
2307 | 0, &objectid); | |
c5739bba CM |
2308 | BUG_ON(ret); |
2309 | ||
c5739bba CM |
2310 | memcpy(&new_root_item, &root->root_item, |
2311 | sizeof(new_root_item)); | |
2312 | ||
c5739bba CM |
2313 | key.objectid = objectid; |
2314 | key.offset = 1; | |
2315 | key.flags = 0; | |
2316 | btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY); | |
7eccb903 | 2317 | btrfs_set_root_blocknr(&new_root_item, bh_blocknr(root->node)); |
c5739bba CM |
2318 | |
2319 | ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key, | |
2320 | &new_root_item); | |
2321 | BUG_ON(ret); | |
2322 | ||
c5739bba CM |
2323 | /* |
2324 | * insert the directory item | |
2325 | */ | |
2326 | key.offset = (u64)-1; | |
2327 | ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root, | |
2328 | name, namelen, | |
2329 | root->fs_info->sb->s_root->d_inode->i_ino, | |
2330 | &key, 0); | |
2331 | ||
2332 | BUG_ON(ret); | |
2333 | ||
2334 | ret = btrfs_inc_root_ref(trans, root); | |
2335 | BUG_ON(ret); | |
2336 | ||
2337 | ret = btrfs_commit_transaction(trans, root); | |
2338 | BUG_ON(ret); | |
2339 | mutex_unlock(&root->fs_info->fs_mutex); | |
2340 | return 0; | |
2341 | } | |
2342 | ||
8352d8a4 CM |
2343 | static int add_disk(struct btrfs_root *root, char *name, int namelen) |
2344 | { | |
2345 | struct block_device *bdev; | |
2346 | struct btrfs_path *path; | |
2347 | struct super_block *sb = root->fs_info->sb; | |
2348 | struct btrfs_root *dev_root = root->fs_info->dev_root; | |
2349 | struct btrfs_trans_handle *trans; | |
2350 | struct btrfs_device_item *dev_item; | |
2351 | struct btrfs_key key; | |
2352 | u16 item_size; | |
2353 | u64 num_blocks; | |
2354 | u64 new_blocks; | |
b4100d64 | 2355 | u64 device_id; |
8352d8a4 | 2356 | int ret; |
b4100d64 | 2357 | |
8352d8a4 CM |
2358 | printk("adding disk %s\n", name); |
2359 | path = btrfs_alloc_path(); | |
2360 | if (!path) | |
2361 | return -ENOMEM; | |
2362 | num_blocks = btrfs_super_total_blocks(root->fs_info->disk_super); | |
2363 | bdev = open_bdev_excl(name, O_RDWR, sb); | |
2364 | if (IS_ERR(bdev)) { | |
2365 | ret = PTR_ERR(bdev); | |
2366 | printk("open bdev excl failed ret %d\n", ret); | |
2367 | goto out_nolock; | |
2368 | } | |
2369 | set_blocksize(bdev, sb->s_blocksize); | |
2370 | new_blocks = bdev->bd_inode->i_size >> sb->s_blocksize_bits; | |
2371 | key.objectid = num_blocks; | |
2372 | key.offset = new_blocks; | |
2373 | key.flags = 0; | |
2374 | btrfs_set_key_type(&key, BTRFS_DEV_ITEM_KEY); | |
2375 | ||
2376 | mutex_lock(&dev_root->fs_info->fs_mutex); | |
2377 | trans = btrfs_start_transaction(dev_root, 1); | |
2378 | item_size = sizeof(*dev_item) + namelen; | |
2379 | printk("insert empty on %Lu %Lu %u size %d\n", num_blocks, new_blocks, key.flags, item_size); | |
2380 | ret = btrfs_insert_empty_item(trans, dev_root, path, &key, item_size); | |
2381 | if (ret) { | |
2382 | printk("insert failed %d\n", ret); | |
2383 | close_bdev_excl(bdev); | |
2384 | if (ret > 0) | |
2385 | ret = -EEXIST; | |
2386 | goto out; | |
2387 | } | |
2388 | dev_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), | |
2389 | path->slots[0], struct btrfs_device_item); | |
2390 | btrfs_set_device_pathlen(dev_item, namelen); | |
2391 | memcpy(dev_item + 1, name, namelen); | |
b4100d64 CM |
2392 | |
2393 | device_id = btrfs_super_last_device_id(root->fs_info->disk_super) + 1; | |
2394 | btrfs_set_super_last_device_id(root->fs_info->disk_super, device_id); | |
2395 | btrfs_set_device_id(dev_item, device_id); | |
8352d8a4 CM |
2396 | mark_buffer_dirty(path->nodes[0]); |
2397 | ||
b4100d64 CM |
2398 | ret = btrfs_insert_dev_radix(root, bdev, device_id, num_blocks, |
2399 | new_blocks); | |
8352d8a4 CM |
2400 | |
2401 | if (!ret) { | |
2402 | btrfs_set_super_total_blocks(root->fs_info->disk_super, | |
2403 | num_blocks + new_blocks); | |
2404 | i_size_write(root->fs_info->btree_inode, | |
2405 | (num_blocks + new_blocks) << | |
2406 | root->fs_info->btree_inode->i_blkbits); | |
2407 | } | |
2408 | ||
2409 | out: | |
2410 | ret = btrfs_commit_transaction(trans, dev_root); | |
2411 | BUG_ON(ret); | |
2412 | mutex_unlock(&root->fs_info->fs_mutex); | |
2413 | out_nolock: | |
2414 | btrfs_free_path(path); | |
2415 | ||
2416 | return ret; | |
2417 | } | |
2418 | ||
c5739bba CM |
2419 | static int btrfs_ioctl(struct inode *inode, struct file *filp, unsigned int |
2420 | cmd, unsigned long arg) | |
2421 | { | |
2422 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
2423 | struct btrfs_ioctl_vol_args vol_args; | |
8352d8a4 | 2424 | int ret = 0; |
c5739bba | 2425 | int namelen; |
2619ba1f CM |
2426 | struct btrfs_path *path; |
2427 | u64 root_dirid; | |
c5739bba | 2428 | |
c5739bba CM |
2429 | switch (cmd) { |
2430 | case BTRFS_IOC_SNAP_CREATE: | |
2431 | if (copy_from_user(&vol_args, | |
2432 | (struct btrfs_ioctl_vol_args __user *)arg, | |
2433 | sizeof(vol_args))) | |
2434 | return -EFAULT; | |
2435 | namelen = strlen(vol_args.name); | |
2436 | if (namelen > BTRFS_VOL_NAME_MAX) | |
2437 | return -EINVAL; | |
2619ba1f CM |
2438 | path = btrfs_alloc_path(); |
2439 | if (!path) | |
2440 | return -ENOMEM; | |
2d13d8d0 | 2441 | root_dirid = root->fs_info->sb->s_root->d_inode->i_ino, |
2619ba1f CM |
2442 | mutex_lock(&root->fs_info->fs_mutex); |
2443 | ret = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root, | |
2444 | path, root_dirid, | |
2445 | vol_args.name, namelen, 0); | |
2446 | mutex_unlock(&root->fs_info->fs_mutex); | |
2d13d8d0 | 2447 | btrfs_free_path(path); |
2619ba1f CM |
2448 | if (ret == 0) |
2449 | return -EEXIST; | |
2450 | ||
2451 | if (root == root->fs_info->tree_root) | |
2452 | ret = create_subvol(root, vol_args.name, namelen); | |
2453 | else | |
2454 | ret = create_snapshot(root, vol_args.name, namelen); | |
c5739bba CM |
2455 | WARN_ON(ret); |
2456 | break; | |
8352d8a4 CM |
2457 | case BTRFS_IOC_ADD_DISK: |
2458 | if (copy_from_user(&vol_args, | |
2459 | (struct btrfs_ioctl_vol_args __user *)arg, | |
2460 | sizeof(vol_args))) | |
2461 | return -EFAULT; | |
2462 | namelen = strlen(vol_args.name); | |
2463 | if (namelen > BTRFS_VOL_NAME_MAX) | |
2464 | return -EINVAL; | |
2465 | vol_args.name[namelen] = '\0'; | |
2466 | ret = add_disk(root, vol_args.name, namelen); | |
2467 | break; | |
c5739bba CM |
2468 | default: |
2469 | return -ENOTTY; | |
2470 | } | |
8352d8a4 | 2471 | return ret; |
c5739bba CM |
2472 | } |
2473 | ||
2c90e5d6 CM |
2474 | static struct kmem_cache *btrfs_inode_cachep; |
2475 | struct kmem_cache *btrfs_trans_handle_cachep; | |
2476 | struct kmem_cache *btrfs_transaction_cachep; | |
2477 | struct kmem_cache *btrfs_bit_radix_cachep; | |
2478 | struct kmem_cache *btrfs_path_cachep; | |
2479 | ||
2480 | /* | |
2481 | * Called inside transaction, so use GFP_NOFS | |
2482 | */ | |
2483 | static struct inode *btrfs_alloc_inode(struct super_block *sb) | |
2484 | { | |
2485 | struct btrfs_inode *ei; | |
2486 | ||
2487 | ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS); | |
2488 | if (!ei) | |
2489 | return NULL; | |
2c90e5d6 CM |
2490 | return &ei->vfs_inode; |
2491 | } | |
2492 | ||
2493 | static void btrfs_destroy_inode(struct inode *inode) | |
2494 | { | |
2c90e5d6 | 2495 | WARN_ON(!list_empty(&inode->i_dentry)); |
2c90e5d6 CM |
2496 | WARN_ON(inode->i_data.nrpages); |
2497 | ||
2c90e5d6 CM |
2498 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
2499 | } | |
2500 | ||
2501 | static void init_once(void * foo, struct kmem_cache * cachep, | |
2502 | unsigned long flags) | |
2503 | { | |
2504 | struct btrfs_inode *ei = (struct btrfs_inode *) foo; | |
2505 | ||
2506 | if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) == | |
2507 | SLAB_CTOR_CONSTRUCTOR) { | |
2508 | inode_init_once(&ei->vfs_inode); | |
2509 | } | |
2510 | } | |
2511 | ||
2512 | static int init_inodecache(void) | |
2513 | { | |
2514 | btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache", | |
2515 | sizeof(struct btrfs_inode), | |
2516 | 0, (SLAB_RECLAIM_ACCOUNT| | |
2517 | SLAB_MEM_SPREAD), | |
2518 | init_once, NULL); | |
2519 | btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache", | |
2520 | sizeof(struct btrfs_trans_handle), | |
2521 | 0, (SLAB_RECLAIM_ACCOUNT| | |
2522 | SLAB_MEM_SPREAD), | |
2523 | NULL, NULL); | |
2524 | btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache", | |
2525 | sizeof(struct btrfs_transaction), | |
2526 | 0, (SLAB_RECLAIM_ACCOUNT| | |
2527 | SLAB_MEM_SPREAD), | |
2528 | NULL, NULL); | |
2529 | btrfs_path_cachep = kmem_cache_create("btrfs_path_cache", | |
2530 | sizeof(struct btrfs_transaction), | |
2531 | 0, (SLAB_RECLAIM_ACCOUNT| | |
2532 | SLAB_MEM_SPREAD), | |
2533 | NULL, NULL); | |
2534 | btrfs_bit_radix_cachep = kmem_cache_create("btrfs_radix", | |
2535 | 256, | |
2536 | 0, (SLAB_RECLAIM_ACCOUNT| | |
2537 | SLAB_MEM_SPREAD | | |
2538 | SLAB_DESTROY_BY_RCU), | |
2539 | NULL, NULL); | |
2540 | if (btrfs_inode_cachep == NULL || btrfs_trans_handle_cachep == NULL || | |
2541 | btrfs_transaction_cachep == NULL || btrfs_bit_radix_cachep == NULL) | |
2542 | return -ENOMEM; | |
2543 | return 0; | |
2544 | } | |
2545 | ||
2546 | static void destroy_inodecache(void) | |
2547 | { | |
2548 | kmem_cache_destroy(btrfs_inode_cachep); | |
2549 | kmem_cache_destroy(btrfs_trans_handle_cachep); | |
2550 | kmem_cache_destroy(btrfs_transaction_cachep); | |
2551 | kmem_cache_destroy(btrfs_bit_radix_cachep); | |
2552 | kmem_cache_destroy(btrfs_path_cachep); | |
2553 | } | |
2554 | ||
2e635a27 CM |
2555 | static int btrfs_get_sb(struct file_system_type *fs_type, |
2556 | int flags, const char *dev_name, void *data, struct vfsmount *mnt) | |
2557 | { | |
2558 | return get_sb_bdev(fs_type, flags, dev_name, data, | |
2559 | btrfs_fill_super, mnt); | |
2560 | } | |
2561 | ||
236454df CM |
2562 | |
2563 | static int btrfs_getattr(struct vfsmount *mnt, | |
2564 | struct dentry *dentry, struct kstat *stat) | |
2565 | { | |
2566 | struct inode *inode = dentry->d_inode; | |
2567 | generic_fillattr(inode, stat); | |
2568 | stat->blksize = 256 * 1024; | |
2569 | return 0; | |
2570 | } | |
2571 | ||
2e635a27 CM |
2572 | static struct file_system_type btrfs_fs_type = { |
2573 | .owner = THIS_MODULE, | |
2574 | .name = "btrfs", | |
2575 | .get_sb = btrfs_get_sb, | |
2576 | .kill_sb = kill_block_super, | |
2577 | .fs_flags = FS_REQUIRES_DEV, | |
2578 | }; | |
2579 | ||
e20d96d6 CM |
2580 | static struct super_operations btrfs_super_ops = { |
2581 | .statfs = simple_statfs, | |
134e9731 | 2582 | .delete_inode = btrfs_delete_inode, |
e20d96d6 CM |
2583 | .put_super = btrfs_put_super, |
2584 | .read_inode = btrfs_read_locked_inode, | |
d5719762 CM |
2585 | .write_super = btrfs_write_super, |
2586 | .sync_fs = btrfs_sync_fs, | |
4730a4bc | 2587 | .write_inode = btrfs_write_inode, |
2c90e5d6 CM |
2588 | .alloc_inode = btrfs_alloc_inode, |
2589 | .destroy_inode = btrfs_destroy_inode, | |
e20d96d6 CM |
2590 | }; |
2591 | ||
2592 | static struct inode_operations btrfs_dir_inode_operations = { | |
2593 | .lookup = btrfs_lookup, | |
d5719762 | 2594 | .create = btrfs_create, |
134e9731 | 2595 | .unlink = btrfs_unlink, |
f7922033 | 2596 | .mkdir = btrfs_mkdir, |
5f443fd2 | 2597 | .rmdir = btrfs_rmdir, |
e20d96d6 CM |
2598 | }; |
2599 | ||
d6e4a428 CM |
2600 | static struct inode_operations btrfs_dir_ro_inode_operations = { |
2601 | .lookup = btrfs_lookup, | |
2602 | }; | |
2603 | ||
e20d96d6 CM |
2604 | static struct file_operations btrfs_dir_file_operations = { |
2605 | .llseek = generic_file_llseek, | |
2606 | .read = generic_read_dir, | |
2607 | .readdir = btrfs_readdir, | |
c5739bba | 2608 | .ioctl = btrfs_ioctl, |
e20d96d6 CM |
2609 | }; |
2610 | ||
dee26a9f CM |
2611 | static struct address_space_operations btrfs_aops = { |
2612 | .readpage = btrfs_readpage, | |
dee26a9f CM |
2613 | .writepage = btrfs_writepage, |
2614 | .sync_page = block_sync_page, | |
2615 | .prepare_write = btrfs_prepare_write, | |
75dfe396 | 2616 | .commit_write = btrfs_commit_write, |
dee26a9f CM |
2617 | }; |
2618 | ||
2619 | static struct inode_operations btrfs_file_inode_operations = { | |
f4b9aa8d | 2620 | .truncate = btrfs_truncate, |
236454df | 2621 | .getattr = btrfs_getattr, |
dee26a9f CM |
2622 | }; |
2623 | ||
2624 | static struct file_operations btrfs_file_operations = { | |
2625 | .llseek = generic_file_llseek, | |
2626 | .read = do_sync_read, | |
e8f05c45 CM |
2627 | .aio_read = btrfs_file_aio_read, |
2628 | .write = btrfs_file_write, | |
dee26a9f CM |
2629 | .mmap = generic_file_mmap, |
2630 | .open = generic_file_open, | |
c5739bba | 2631 | .ioctl = btrfs_ioctl, |
dee26a9f | 2632 | }; |
e20d96d6 | 2633 | |
2e635a27 CM |
2634 | static int __init init_btrfs_fs(void) |
2635 | { | |
2c90e5d6 | 2636 | int err; |
2e635a27 | 2637 | printk("btrfs loaded!\n"); |
2c90e5d6 CM |
2638 | err = init_inodecache(); |
2639 | if (err) | |
2640 | return err; | |
d6e4a428 CM |
2641 | kset_set_kset_s(&btrfs_subsys, fs_subsys); |
2642 | err = subsystem_register(&btrfs_subsys); | |
2643 | if (err) | |
2644 | goto out; | |
2e635a27 | 2645 | return register_filesystem(&btrfs_fs_type); |
d6e4a428 CM |
2646 | out: |
2647 | destroy_inodecache(); | |
2648 | return err; | |
2e635a27 CM |
2649 | } |
2650 | ||
2651 | static void __exit exit_btrfs_fs(void) | |
2652 | { | |
2c90e5d6 | 2653 | destroy_inodecache(); |
2e635a27 | 2654 | unregister_filesystem(&btrfs_fs_type); |
d6e4a428 | 2655 | subsystem_unregister(&btrfs_subsys); |
2e635a27 CM |
2656 | printk("btrfs unloaded\n"); |
2657 | } | |
2658 | ||
2659 | module_init(init_btrfs_fs) | |
2660 | module_exit(exit_btrfs_fs) | |
2661 | ||
2662 | MODULE_LICENSE("GPL"); |