]>
Commit | Line | Data |
---|---|---|
d7e09d03 PT |
1 | /* |
2 | * GPL HEADER START | |
3 | * | |
4 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License version 2 only, | |
8 | * as published by the Free Software Foundation. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, but | |
11 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 | * General Public License version 2 for more details (a copy is included | |
14 | * in the LICENSE file that accompanied this code). | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * version 2 along with this program; If not, see | |
18 | * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf | |
19 | * | |
20 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, | |
21 | * CA 95054 USA or visit www.sun.com if you need additional information or | |
22 | * have any questions. | |
23 | * | |
24 | * GPL HEADER END | |
25 | */ | |
26 | /* | |
27 | * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved. | |
28 | * Use is subject to license terms. | |
29 | * | |
30 | * Copyright (c) 2011, 2012, Intel Corporation. | |
31 | */ | |
32 | /* | |
33 | * This file is part of Lustre, http://www.lustre.org/ | |
34 | * Lustre is a trademark of Sun Microsystems, Inc. | |
35 | * | |
36 | * lustre/lvfs/fsfilt_ext3.c | |
37 | * | |
38 | * Author: Andreas Dilger <adilger@clusterfs.com> | |
39 | */ | |
40 | ||
41 | #define DEBUG_SUBSYSTEM S_FILTER | |
42 | ||
43 | #include <linux/init.h> | |
44 | #include <linux/module.h> | |
45 | #include <linux/fs.h> | |
46 | #include <linux/slab.h> | |
47 | #include <linux/pagemap.h> | |
48 | #include <ldiskfs/ldiskfs_config.h> | |
49 | #include <ext4/ext4.h> | |
50 | #include <ext4/ext4_jbd2.h> | |
d7e09d03 PT |
51 | #include <linux/bitops.h> |
52 | #include <linux/quota.h> | |
53 | ||
54 | #include <linux/libcfs/libcfs.h> | |
55 | #include <lustre_fsfilt.h> | |
56 | #include <obd.h> | |
57 | #include <linux/lustre_compat25.h> | |
58 | #include <linux/lprocfs_status.h> | |
59 | ||
60 | #include <ext4/ext4_extents.h> | |
61 | ||
62 | #ifdef HAVE_EXT_PBLOCK /* Name changed to ext4_ext_pblock for kernel 2.6.35 */ | |
63 | #define ext3_ext_pblock(ex) ext_pblock((ex)) | |
64 | #endif | |
65 | ||
66 | /* for kernels 2.6.18 and later */ | |
67 | #define FSFILT_SINGLEDATA_TRANS_BLOCKS(sb) EXT3_SINGLEDATA_TRANS_BLOCKS(sb) | |
68 | ||
69 | #define fsfilt_ext3_ext_insert_extent(handle, inode, path, newext, flag) \ | |
70 | ext3_ext_insert_extent(handle, inode, path, newext, flag) | |
71 | ||
72 | #define ext3_mb_discard_inode_preallocations(inode) \ | |
73 | ext3_discard_preallocations(inode) | |
74 | ||
75 | #define fsfilt_log_start_commit(journal, tid) jbd2_log_start_commit(journal, tid) | |
76 | #define fsfilt_log_wait_commit(journal, tid) jbd2_log_wait_commit(journal, tid) | |
77 | ||
78 | static struct kmem_cache *fcb_cache; | |
79 | ||
80 | struct fsfilt_cb_data { | |
81 | struct ext4_journal_cb_entry cb_jcb; /* private data - MUST BE FIRST */ | |
82 | fsfilt_cb_t cb_func; /* MDS/OBD completion function */ | |
83 | struct obd_device *cb_obd; /* MDS/OBD completion device */ | |
84 | __u64 cb_last_rcvd; /* MDS/OST last committed operation */ | |
85 | void *cb_data; /* MDS/OST completion function data */ | |
86 | }; | |
87 | ||
88 | static char *fsfilt_ext3_get_label(struct super_block *sb) | |
89 | { | |
90 | return EXT3_SB(sb)->s_es->s_volume_name; | |
91 | } | |
92 | ||
93 | /* kernel has ext4_blocks_for_truncate since linux-3.1.1 */ | |
94 | # include <ext4/truncate.h> | |
95 | ||
96 | /* | |
97 | * We don't currently need any additional blocks for rmdir and | |
98 | * unlink transactions because we are storing the OST oa_id inside | |
99 | * the inode (which we will be changing anyways as part of this | |
100 | * transaction). | |
101 | */ | |
102 | static void *fsfilt_ext3_start(struct inode *inode, int op, void *desc_private, | |
103 | int logs) | |
104 | { | |
105 | /* For updates to the last received file */ | |
106 | int nblocks = FSFILT_SINGLEDATA_TRANS_BLOCKS(inode->i_sb); | |
107 | journal_t *journal; | |
108 | void *handle; | |
109 | ||
110 | if (current->journal_info) { | |
111 | CDEBUG(D_INODE, "increasing refcount on %p\n", | |
112 | current->journal_info); | |
113 | goto journal_start; | |
114 | } | |
115 | ||
116 | switch(op) { | |
117 | case FSFILT_OP_UNLINK: | |
118 | /* delete one file + create/update logs for each stripe */ | |
119 | nblocks += EXT3_DELETE_TRANS_BLOCKS(inode->i_sb); | |
120 | nblocks += (EXT3_INDEX_EXTRA_TRANS_BLOCKS + | |
121 | FSFILT_SINGLEDATA_TRANS_BLOCKS(inode->i_sb)) * logs; | |
122 | break; | |
123 | case FSFILT_OP_CANCEL_UNLINK: | |
124 | LASSERT(logs == 1); | |
125 | ||
126 | /* blocks for log header bitmap update OR | |
127 | * blocks for catalog header bitmap update + unlink of logs + | |
128 | * blocks for delete the inode (include blocks truncating). */ | |
129 | nblocks = (LLOG_CHUNK_SIZE >> inode->i_blkbits) + | |
130 | EXT3_DELETE_TRANS_BLOCKS(inode->i_sb) + | |
131 | ext4_blocks_for_truncate(inode) + 3; | |
132 | break; | |
133 | default: CERROR("unknown transaction start op %d\n", op); | |
134 | LBUG(); | |
135 | } | |
136 | ||
137 | LASSERT(current->journal_info == desc_private); | |
138 | journal = EXT3_SB(inode->i_sb)->s_journal; | |
139 | if (nblocks > journal->j_max_transaction_buffers) { | |
140 | CWARN("too many credits %d for op %ux%u using %d instead\n", | |
141 | nblocks, op, logs, journal->j_max_transaction_buffers); | |
142 | nblocks = journal->j_max_transaction_buffers; | |
143 | } | |
144 | ||
145 | journal_start: | |
146 | LASSERTF(nblocks > 0, "can't start %d credit transaction\n", nblocks); | |
147 | handle = ext3_journal_start(inode, nblocks); | |
148 | ||
149 | if (!IS_ERR(handle)) | |
150 | LASSERT(current->journal_info == handle); | |
151 | else | |
152 | CERROR("error starting handle for op %u (%u credits): rc %ld\n", | |
153 | op, nblocks, PTR_ERR(handle)); | |
154 | return handle; | |
155 | } | |
156 | ||
157 | static int fsfilt_ext3_commit(struct inode *inode, void *h, int force_sync) | |
158 | { | |
159 | int rc; | |
160 | handle_t *handle = h; | |
161 | ||
162 | LASSERT(current->journal_info == handle); | |
163 | if (force_sync) | |
164 | handle->h_sync = 1; /* recovery likes this */ | |
165 | ||
166 | rc = ext3_journal_stop(handle); | |
167 | ||
168 | return rc; | |
169 | } | |
170 | ||
171 | #ifndef EXT3_EXTENTS_FL | |
172 | #define EXT3_EXTENTS_FL 0x00080000 /* Inode uses extents */ | |
173 | #endif | |
174 | ||
175 | #ifndef EXT_ASSERT | |
176 | #define EXT_ASSERT(cond) BUG_ON(!(cond)) | |
177 | #endif | |
178 | ||
179 | #define EXT_GENERATION(inode) (EXT4_I(inode)->i_ext_generation) | |
180 | #define ext3_ext_base inode | |
181 | #define ext3_ext_base2inode(inode) (inode) | |
182 | #define EXT_DEPTH(inode) ext_depth(inode) | |
183 | #define fsfilt_ext3_ext_walk_space(inode, block, num, cb, cbdata) \ | |
184 | ext3_ext_walk_space(inode, block, num, cb, cbdata); | |
185 | ||
186 | struct bpointers { | |
187 | unsigned long *blocks; | |
188 | unsigned long start; | |
189 | int num; | |
190 | int init_num; | |
191 | int create; | |
192 | }; | |
193 | ||
194 | static long ext3_ext_find_goal(struct inode *inode, struct ext3_ext_path *path, | |
195 | unsigned long block, int *aflags) | |
196 | { | |
197 | struct ext3_inode_info *ei = EXT3_I(inode); | |
198 | unsigned long bg_start; | |
199 | unsigned long colour; | |
200 | int depth; | |
201 | ||
202 | if (path) { | |
203 | struct ext3_extent *ex; | |
204 | depth = path->p_depth; | |
205 | ||
206 | /* try to predict block placement */ | |
207 | if ((ex = path[depth].p_ext)) | |
208 | return ext4_ext_pblock(ex) + (block - le32_to_cpu(ex->ee_block)); | |
209 | ||
210 | /* it looks index is empty | |
211 | * try to find starting from index itself */ | |
212 | if (path[depth].p_bh) | |
213 | return path[depth].p_bh->b_blocknr; | |
214 | } | |
215 | ||
216 | /* OK. use inode's group */ | |
217 | bg_start = (ei->i_block_group * EXT3_BLOCKS_PER_GROUP(inode->i_sb)) + | |
218 | le32_to_cpu(EXT3_SB(inode->i_sb)->s_es->s_first_data_block); | |
219 | colour = (current->pid % 16) * | |
220 | (EXT3_BLOCKS_PER_GROUP(inode->i_sb) / 16); | |
221 | return bg_start + colour + block; | |
222 | } | |
223 | ||
224 | #define ll_unmap_underlying_metadata(sb, blocknr) \ | |
225 | unmap_underlying_metadata((sb)->s_bdev, blocknr) | |
226 | ||
227 | #ifndef EXT3_MB_HINT_GROUP_ALLOC | |
228 | static unsigned long new_blocks(handle_t *handle, struct ext3_ext_base *base, | |
229 | struct ext3_ext_path *path, unsigned long block, | |
230 | unsigned long *count, int *err) | |
231 | { | |
232 | unsigned long pblock, goal; | |
233 | int aflags = 0; | |
234 | struct inode *inode = ext3_ext_base2inode(base); | |
235 | ||
236 | goal = ext3_ext_find_goal(inode, path, block, &aflags); | |
237 | aflags |= 2; /* block have been already reserved */ | |
238 | pblock = ext3_mb_new_blocks(handle, inode, goal, count, aflags, err); | |
239 | return pblock; | |
240 | ||
241 | } | |
242 | #else | |
243 | static unsigned long new_blocks(handle_t *handle, struct ext3_ext_base *base, | |
244 | struct ext3_ext_path *path, unsigned long block, | |
245 | unsigned long *count, int *err) | |
246 | { | |
247 | struct inode *inode = ext3_ext_base2inode(base); | |
248 | struct ext3_allocation_request ar; | |
249 | unsigned long pblock; | |
250 | int aflags; | |
251 | ||
252 | /* find neighbour allocated blocks */ | |
253 | ar.lleft = block; | |
254 | *err = ext3_ext_search_left(base, path, &ar.lleft, &ar.pleft); | |
255 | if (*err) | |
256 | return 0; | |
257 | ar.lright = block; | |
258 | *err = ext3_ext_search_right(base, path, &ar.lright, &ar.pright); | |
259 | if (*err) | |
260 | return 0; | |
261 | ||
262 | /* allocate new block */ | |
263 | ar.goal = ext3_ext_find_goal(inode, path, block, &aflags); | |
264 | ar.inode = inode; | |
265 | ar.logical = block; | |
266 | ar.len = *count; | |
267 | ar.flags = EXT3_MB_HINT_DATA; | |
268 | pblock = ext3_mb_new_blocks(handle, &ar, err); | |
269 | *count = ar.len; | |
270 | return pblock; | |
271 | } | |
272 | #endif | |
273 | ||
274 | static int ext3_ext_new_extent_cb(struct ext3_ext_base *base, | |
275 | struct ext3_ext_path *path, | |
276 | struct ext3_ext_cache *cex, | |
277 | #ifdef HAVE_EXT_PREPARE_CB_EXTENT | |
278 | struct ext3_extent *ex, | |
279 | #endif | |
280 | void *cbdata) | |
281 | { | |
282 | struct bpointers *bp = cbdata; | |
283 | struct inode *inode = ext3_ext_base2inode(base); | |
284 | struct ext3_extent nex; | |
285 | unsigned long pblock; | |
286 | unsigned long tgen; | |
287 | int err, i; | |
288 | unsigned long count; | |
289 | handle_t *handle; | |
290 | ||
291 | #ifdef EXT3_EXT_CACHE_EXTENT | |
292 | if (cex->ec_type == EXT3_EXT_CACHE_EXTENT) | |
293 | #else | |
294 | if ((cex->ec_len != 0) && (cex->ec_start != 0)) | |
295 | #endif | |
296 | { | |
297 | err = EXT_CONTINUE; | |
298 | goto map; | |
299 | } | |
300 | ||
301 | if (bp->create == 0) { | |
302 | i = 0; | |
303 | if (cex->ec_block < bp->start) | |
304 | i = bp->start - cex->ec_block; | |
305 | if (i >= cex->ec_len) | |
306 | CERROR("nothing to do?! i = %d, e_num = %u\n", | |
307 | i, cex->ec_len); | |
308 | for (; i < cex->ec_len && bp->num; i++) { | |
309 | *(bp->blocks) = 0; | |
310 | bp->blocks++; | |
311 | bp->num--; | |
312 | bp->start++; | |
313 | } | |
314 | ||
315 | return EXT_CONTINUE; | |
316 | } | |
317 | ||
318 | tgen = EXT_GENERATION(base); | |
319 | count = ext3_ext_calc_credits_for_insert(base, path); | |
320 | ||
321 | handle = ext3_journal_start(inode, count+EXT3_ALLOC_NEEDED+1); | |
322 | if (IS_ERR(handle)) { | |
323 | return PTR_ERR(handle); | |
324 | } | |
325 | ||
326 | if (tgen != EXT_GENERATION(base)) { | |
327 | /* the tree has changed. so path can be invalid at moment */ | |
328 | ext3_journal_stop(handle); | |
329 | return EXT_REPEAT; | |
330 | } | |
331 | ||
332 | /* In 2.6.32 kernel, ext4_ext_walk_space()'s callback func is not | |
333 | * protected by i_data_sem as whole. so we patch it to store | |
334 | * generation to path and now verify the tree hasn't changed */ | |
335 | down_write((&EXT4_I(inode)->i_data_sem)); | |
336 | ||
337 | /* validate extent, make sure the extent tree does not changed */ | |
338 | if (EXT_GENERATION(base) != path[0].p_generation) { | |
339 | /* cex is invalid, try again */ | |
340 | up_write(&EXT4_I(inode)->i_data_sem); | |
341 | ext3_journal_stop(handle); | |
342 | return EXT_REPEAT; | |
343 | } | |
344 | ||
345 | count = cex->ec_len; | |
346 | pblock = new_blocks(handle, base, path, cex->ec_block, &count, &err); | |
347 | if (!pblock) | |
348 | goto out; | |
349 | EXT_ASSERT(count <= cex->ec_len); | |
350 | ||
351 | /* insert new extent */ | |
352 | nex.ee_block = cpu_to_le32(cex->ec_block); | |
353 | ext3_ext_store_pblock(&nex, pblock); | |
354 | nex.ee_len = cpu_to_le16(count); | |
355 | err = fsfilt_ext3_ext_insert_extent(handle, base, path, &nex, 0); | |
356 | if (err) { | |
357 | /* free data blocks we just allocated */ | |
358 | /* not a good idea to call discard here directly, | |
359 | * but otherwise we'd need to call it every free() */ | |
360 | #ifdef EXT3_MB_HINT_GROUP_ALLOC | |
361 | ext3_mb_discard_inode_preallocations(inode); | |
362 | #endif | |
363 | #ifdef HAVE_EXT_FREE_BLOCK_WITH_BUFFER_HEAD /* Introduced in 2.6.32-rc7 */ | |
364 | ext3_free_blocks(handle, inode, NULL, ext4_ext_pblock(&nex), | |
365 | cpu_to_le16(nex.ee_len), 0); | |
366 | #else | |
367 | ext3_free_blocks(handle, inode, ext4_ext_pblock(&nex), | |
368 | cpu_to_le16(nex.ee_len), 0); | |
369 | #endif | |
370 | goto out; | |
371 | } | |
372 | ||
373 | /* | |
374 | * Putting len of the actual extent we just inserted, | |
375 | * we are asking ext3_ext_walk_space() to continue | |
376 | * scaning after that block | |
377 | */ | |
378 | cex->ec_len = le16_to_cpu(nex.ee_len); | |
379 | cex->ec_start = ext4_ext_pblock(&nex); | |
380 | BUG_ON(le16_to_cpu(nex.ee_len) == 0); | |
381 | BUG_ON(le32_to_cpu(nex.ee_block) != cex->ec_block); | |
382 | ||
383 | out: | |
384 | up_write((&EXT4_I(inode)->i_data_sem)); | |
385 | ext3_journal_stop(handle); | |
386 | map: | |
387 | if (err >= 0) { | |
388 | /* map blocks */ | |
389 | if (bp->num == 0) { | |
390 | CERROR("hmm. why do we find this extent?\n"); | |
391 | CERROR("initial space: %lu:%u\n", | |
392 | bp->start, bp->init_num); | |
393 | #ifdef EXT3_EXT_CACHE_EXTENT | |
394 | CERROR("current extent: %u/%u/%llu %d\n", | |
395 | cex->ec_block, cex->ec_len, | |
396 | (unsigned long long)cex->ec_start, | |
397 | cex->ec_type); | |
398 | #else | |
399 | CERROR("current extent: %u/%u/%llu\n", | |
400 | cex->ec_block, cex->ec_len, | |
401 | (unsigned long long)cex->ec_start); | |
402 | #endif | |
403 | } | |
404 | i = 0; | |
405 | if (cex->ec_block < bp->start) | |
406 | i = bp->start - cex->ec_block; | |
407 | if (i >= cex->ec_len) | |
408 | CERROR("nothing to do?! i = %d, e_num = %u\n", | |
409 | i, cex->ec_len); | |
410 | for (; i < cex->ec_len && bp->num; i++) { | |
411 | *(bp->blocks) = cex->ec_start + i; | |
412 | #ifdef EXT3_EXT_CACHE_EXTENT | |
413 | if (cex->ec_type != EXT3_EXT_CACHE_EXTENT) | |
414 | #else | |
415 | if ((cex->ec_len == 0) || (cex->ec_start == 0)) | |
416 | #endif | |
417 | { | |
418 | /* unmap any possible underlying metadata from | |
419 | * the block device mapping. bug 6998. */ | |
420 | ll_unmap_underlying_metadata(inode->i_sb, | |
421 | *(bp->blocks)); | |
422 | } | |
423 | bp->blocks++; | |
424 | bp->num--; | |
425 | bp->start++; | |
426 | } | |
427 | } | |
428 | return err; | |
429 | } | |
430 | ||
431 | int fsfilt_map_nblocks(struct inode *inode, unsigned long block, | |
432 | unsigned long num, unsigned long *blocks, | |
433 | int create) | |
434 | { | |
435 | struct ext3_ext_base *base = inode; | |
436 | struct bpointers bp; | |
437 | int err; | |
438 | ||
439 | CDEBUG(D_OTHER, "blocks %lu-%lu requested for inode %u\n", | |
440 | block, block + num - 1, (unsigned) inode->i_ino); | |
441 | ||
442 | bp.blocks = blocks; | |
443 | bp.start = block; | |
444 | bp.init_num = bp.num = num; | |
445 | bp.create = create; | |
446 | ||
447 | err = fsfilt_ext3_ext_walk_space(base, block, num, | |
448 | ext3_ext_new_extent_cb, &bp); | |
449 | ext3_ext_invalidate_cache(base); | |
450 | ||
451 | return err; | |
452 | } | |
453 | ||
454 | int fsfilt_ext3_map_ext_inode_pages(struct inode *inode, struct page **page, | |
455 | int pages, unsigned long *blocks, | |
456 | int create) | |
457 | { | |
458 | int blocks_per_page = PAGE_CACHE_SIZE >> inode->i_blkbits; | |
459 | int rc = 0, i = 0; | |
460 | struct page *fp = NULL; | |
461 | int clen = 0; | |
462 | ||
463 | CDEBUG(D_OTHER, "inode %lu: map %d pages from %lu\n", | |
464 | inode->i_ino, pages, (*page)->index); | |
465 | ||
466 | /* pages are sorted already. so, we just have to find | |
467 | * contig. space and process them properly */ | |
468 | while (i < pages) { | |
469 | if (fp == NULL) { | |
470 | /* start new extent */ | |
471 | fp = *page++; | |
472 | clen = 1; | |
473 | i++; | |
474 | continue; | |
475 | } else if (fp->index + clen == (*page)->index) { | |
476 | /* continue the extent */ | |
477 | page++; | |
478 | clen++; | |
479 | i++; | |
480 | continue; | |
481 | } | |
482 | ||
483 | /* process found extent */ | |
484 | rc = fsfilt_map_nblocks(inode, fp->index * blocks_per_page, | |
485 | clen * blocks_per_page, blocks, | |
486 | create); | |
487 | if (rc) | |
488 | GOTO(cleanup, rc); | |
489 | ||
490 | /* look for next extent */ | |
491 | fp = NULL; | |
492 | blocks += blocks_per_page * clen; | |
493 | } | |
494 | ||
495 | if (fp) | |
496 | rc = fsfilt_map_nblocks(inode, fp->index * blocks_per_page, | |
497 | clen * blocks_per_page, blocks, | |
498 | create); | |
499 | cleanup: | |
500 | return rc; | |
501 | } | |
502 | ||
503 | int fsfilt_ext3_map_bm_inode_pages(struct inode *inode, struct page **page, | |
504 | int pages, unsigned long *blocks, | |
505 | int create) | |
506 | { | |
507 | int blocks_per_page = PAGE_CACHE_SIZE >> inode->i_blkbits; | |
508 | unsigned long *b; | |
509 | int rc = 0, i; | |
510 | ||
511 | for (i = 0, b = blocks; i < pages; i++, page++) { | |
512 | rc = ext3_map_inode_page(inode, *page, b, create); | |
513 | if (rc) { | |
514 | CERROR("ino %lu, blk %lu create %d: rc %d\n", | |
515 | inode->i_ino, *b, create, rc); | |
516 | break; | |
517 | } | |
518 | ||
519 | b += blocks_per_page; | |
520 | } | |
521 | return rc; | |
522 | } | |
523 | ||
524 | int fsfilt_ext3_map_inode_pages(struct inode *inode, struct page **page, | |
525 | int pages, unsigned long *blocks, | |
526 | int create, struct mutex *optional_mutex) | |
527 | { | |
528 | int rc; | |
529 | ||
530 | if (EXT3_I(inode)->i_flags & EXT3_EXTENTS_FL) { | |
531 | rc = fsfilt_ext3_map_ext_inode_pages(inode, page, pages, | |
532 | blocks, create); | |
533 | return rc; | |
534 | } | |
535 | if (optional_mutex != NULL) | |
536 | mutex_lock(optional_mutex); | |
537 | rc = fsfilt_ext3_map_bm_inode_pages(inode, page, pages, blocks, create); | |
538 | if (optional_mutex != NULL) | |
539 | mutex_unlock(optional_mutex); | |
540 | ||
541 | return rc; | |
542 | } | |
543 | ||
544 | int fsfilt_ext3_read(struct inode *inode, void *buf, int size, loff_t *offs) | |
545 | { | |
546 | unsigned long block; | |
547 | struct buffer_head *bh; | |
548 | int err, blocksize, csize, boffs, osize = size; | |
549 | ||
550 | /* prevent reading after eof */ | |
551 | spin_lock(&inode->i_lock); | |
552 | if (i_size_read(inode) < *offs + size) { | |
553 | size = i_size_read(inode) - *offs; | |
554 | spin_unlock(&inode->i_lock); | |
555 | if (size < 0) { | |
556 | CDEBUG(D_EXT2, "size %llu is too short for read @%llu\n", | |
557 | i_size_read(inode), *offs); | |
558 | return -EBADR; | |
559 | } else if (size == 0) { | |
560 | return 0; | |
561 | } | |
562 | } else { | |
563 | spin_unlock(&inode->i_lock); | |
564 | } | |
565 | ||
566 | blocksize = 1 << inode->i_blkbits; | |
567 | ||
568 | while (size > 0) { | |
569 | block = *offs >> inode->i_blkbits; | |
570 | boffs = *offs & (blocksize - 1); | |
571 | csize = min(blocksize - boffs, size); | |
572 | bh = ext3_bread(NULL, inode, block, 0, &err); | |
573 | if (!bh) { | |
574 | CERROR("can't read block: %d\n", err); | |
575 | return err; | |
576 | } | |
577 | ||
578 | memcpy(buf, bh->b_data + boffs, csize); | |
579 | brelse(bh); | |
580 | ||
581 | *offs += csize; | |
582 | buf += csize; | |
583 | size -= csize; | |
584 | } | |
585 | return osize; | |
586 | } | |
587 | EXPORT_SYMBOL(fsfilt_ext3_read); | |
588 | ||
589 | static int fsfilt_ext3_read_record(struct file * file, void *buf, | |
590 | int size, loff_t *offs) | |
591 | { | |
592 | int rc; | |
593 | rc = fsfilt_ext3_read(file->f_dentry->d_inode, buf, size, offs); | |
594 | if (rc > 0) | |
595 | rc = 0; | |
596 | return rc; | |
597 | } | |
598 | ||
599 | int fsfilt_ext3_write_handle(struct inode *inode, void *buf, int bufsize, | |
600 | loff_t *offs, handle_t *handle) | |
601 | { | |
602 | struct buffer_head *bh = NULL; | |
603 | loff_t old_size = i_size_read(inode), offset = *offs; | |
604 | loff_t new_size = i_size_read(inode); | |
605 | unsigned long block; | |
606 | int err = 0, blocksize = 1 << inode->i_blkbits, size, boffs; | |
607 | ||
608 | while (bufsize > 0) { | |
609 | if (bh != NULL) | |
610 | brelse(bh); | |
611 | ||
612 | block = offset >> inode->i_blkbits; | |
613 | boffs = offset & (blocksize - 1); | |
614 | size = min(blocksize - boffs, bufsize); | |
615 | bh = ext3_bread(handle, inode, block, 1, &err); | |
616 | if (!bh) { | |
617 | CERROR("can't read/create block: %d\n", err); | |
618 | break; | |
619 | } | |
620 | ||
621 | err = ext3_journal_get_write_access(handle, bh); | |
622 | if (err) { | |
623 | CERROR("journal_get_write_access() returned error %d\n", | |
624 | err); | |
625 | break; | |
626 | } | |
627 | LASSERT(bh->b_data + boffs + size <= bh->b_data + bh->b_size); | |
628 | memcpy(bh->b_data + boffs, buf, size); | |
629 | err = ext3_journal_dirty_metadata(handle, bh); | |
630 | if (err) { | |
631 | CERROR("journal_dirty_metadata() returned error %d\n", | |
632 | err); | |
633 | break; | |
634 | } | |
635 | if (offset + size > new_size) | |
636 | new_size = offset + size; | |
637 | offset += size; | |
638 | bufsize -= size; | |
639 | buf += size; | |
640 | } | |
641 | if (bh) | |
642 | brelse(bh); | |
643 | ||
644 | /* correct in-core and on-disk sizes */ | |
645 | if (new_size > i_size_read(inode)) { | |
646 | spin_lock(&inode->i_lock); | |
647 | if (new_size > i_size_read(inode)) | |
648 | i_size_write(inode, new_size); | |
649 | if (i_size_read(inode) > EXT3_I(inode)->i_disksize) | |
650 | EXT3_I(inode)->i_disksize = i_size_read(inode); | |
651 | if (i_size_read(inode) > old_size) { | |
652 | spin_unlock(&inode->i_lock); | |
653 | mark_inode_dirty(inode); | |
654 | } else { | |
655 | spin_unlock(&inode->i_lock); | |
656 | } | |
657 | } | |
658 | ||
659 | if (err == 0) | |
660 | *offs = offset; | |
661 | return err; | |
662 | } | |
663 | EXPORT_SYMBOL(fsfilt_ext3_write_handle); | |
664 | ||
665 | static int fsfilt_ext3_write_record(struct file *file, void *buf, int bufsize, | |
666 | loff_t *offs, int force_sync) | |
667 | { | |
668 | struct inode *inode = file->f_dentry->d_inode; | |
669 | handle_t *handle; | |
670 | int err, block_count = 0, blocksize; | |
671 | ||
672 | /* Determine how many transaction credits are needed */ | |
673 | blocksize = 1 << inode->i_blkbits; | |
674 | block_count = (*offs & (blocksize - 1)) + bufsize; | |
675 | block_count = (block_count + blocksize - 1) >> inode->i_blkbits; | |
676 | ||
677 | handle = ext3_journal_start(inode, | |
678 | block_count * EXT3_DATA_TRANS_BLOCKS(inode->i_sb) + 2); | |
679 | if (IS_ERR(handle)) { | |
680 | CERROR("can't start transaction for %d blocks (%d bytes)\n", | |
681 | block_count * EXT3_DATA_TRANS_BLOCKS(inode->i_sb) + 2, | |
682 | bufsize); | |
683 | return PTR_ERR(handle); | |
684 | } | |
685 | ||
686 | err = fsfilt_ext3_write_handle(inode, buf, bufsize, offs, handle); | |
687 | ||
688 | if (!err && force_sync) | |
689 | handle->h_sync = 1; /* recovery likes this */ | |
690 | ||
691 | ext3_journal_stop(handle); | |
692 | ||
693 | return err; | |
694 | } | |
695 | ||
696 | static int fsfilt_ext3_setup(struct super_block *sb) | |
697 | { | |
698 | if (!EXT3_HAS_COMPAT_FEATURE(sb, | |
699 | EXT3_FEATURE_COMPAT_HAS_JOURNAL)) { | |
700 | CERROR("ext3 mounted without journal\n"); | |
701 | return -EINVAL; | |
702 | } | |
703 | ||
704 | #ifdef S_PDIROPS | |
705 | CWARN("Enabling PDIROPS\n"); | |
706 | set_opt(EXT3_SB(sb)->s_mount_opt, PDIROPS); | |
707 | sb->s_flags |= S_PDIROPS; | |
708 | #endif | |
709 | if (!EXT3_HAS_COMPAT_FEATURE(sb, EXT3_FEATURE_COMPAT_DIR_INDEX)) | |
710 | CWARN("filesystem doesn't have dir_index feature enabled\n"); | |
711 | return 0; | |
712 | } | |
713 | static struct fsfilt_operations fsfilt_ext3_ops = { | |
714 | .fs_type = "ext3", | |
715 | .fs_owner = THIS_MODULE, | |
716 | .fs_getlabel = fsfilt_ext3_get_label, | |
717 | .fs_start = fsfilt_ext3_start, | |
718 | .fs_commit = fsfilt_ext3_commit, | |
719 | .fs_map_inode_pages = fsfilt_ext3_map_inode_pages, | |
720 | .fs_write_record = fsfilt_ext3_write_record, | |
721 | .fs_read_record = fsfilt_ext3_read_record, | |
722 | .fs_setup = fsfilt_ext3_setup, | |
723 | }; | |
724 | ||
725 | static int __init fsfilt_ext3_init(void) | |
726 | { | |
727 | int rc; | |
728 | ||
729 | fcb_cache = kmem_cache_create("fsfilt_ext3_fcb", | |
730 | sizeof(struct fsfilt_cb_data), 0, 0); | |
731 | if (!fcb_cache) { | |
732 | CERROR("error allocating fsfilt journal callback cache\n"); | |
733 | GOTO(out, rc = -ENOMEM); | |
734 | } | |
735 | ||
736 | rc = fsfilt_register_ops(&fsfilt_ext3_ops); | |
737 | ||
738 | if (rc) { | |
739 | int err = kmem_cache_destroy(fcb_cache); | |
740 | LASSERTF(err == 0, "error destroying new cache: rc %d\n", err); | |
741 | } | |
742 | out: | |
743 | return rc; | |
744 | } | |
745 | ||
746 | static void __exit fsfilt_ext3_exit(void) | |
747 | { | |
748 | int rc; | |
749 | ||
750 | fsfilt_unregister_ops(&fsfilt_ext3_ops); | |
751 | rc = kmem_cache_destroy(fcb_cache); | |
752 | LASSERTF(rc == 0, "couldn't destroy fcb_cache slab\n"); | |
753 | } | |
754 | ||
755 | module_init(fsfilt_ext3_init); | |
756 | module_exit(fsfilt_ext3_exit); | |
757 | ||
758 | MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>"); | |
759 | MODULE_DESCRIPTION("Lustre ext3 Filesystem Helper v0.1"); | |
760 | MODULE_LICENSE("GPL"); |