Commit | Line | Data |
---|---|---|
b3b94faa DT |
1 | /* |
2 | * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved. | |
3 | * Copyright (C) 2004-2005 Red Hat, Inc. All rights reserved. | |
4 | * | |
5 | * This copyrighted material is made available to anyone wishing to use, | |
6 | * modify, copy, or redistribute it subject to the terms and conditions | |
7 | * of the GNU General Public License v.2. | |
8 | */ | |
9 | ||
10 | #include <linux/sched.h> | |
11 | #include <linux/slab.h> | |
12 | #include <linux/spinlock.h> | |
13 | #include <linux/completion.h> | |
14 | #include <linux/buffer_head.h> | |
15 | #include <asm/semaphore.h> | |
16 | ||
17 | #include "gfs2.h" | |
18 | #include "bmap.h" | |
19 | #include "glock.h" | |
20 | #include "inode.h" | |
21 | #include "jdata.h" | |
22 | #include "meta_io.h" | |
23 | #include "page.h" | |
24 | #include "quota.h" | |
25 | #include "rgrp.h" | |
26 | #include "trans.h" | |
27 | ||
28 | /* This doesn't need to be that large as max 64 bit pointers in a 4k | |
29 | * block is 512, so __u16 is fine for that. It saves stack space to | |
30 | * keep it small. | |
31 | */ | |
32 | struct metapath { | |
33 | __u16 mp_list[GFS2_MAX_META_HEIGHT]; | |
34 | }; | |
35 | ||
36 | typedef int (*block_call_t) (struct gfs2_inode *ip, struct buffer_head *dibh, | |
37 | struct buffer_head *bh, uint64_t *top, | |
38 | uint64_t *bottom, unsigned int height, | |
39 | void *data); | |
40 | ||
41 | struct strip_mine { | |
42 | int sm_first; | |
43 | unsigned int sm_height; | |
44 | }; | |
45 | ||
46 | /** | |
47 | * @gfs2_unstuffer_sync - Synchronously unstuff a dinode | |
48 | * @ip: | |
49 | * @dibh: | |
50 | * @block: | |
51 | * @private: | |
52 | * | |
53 | * Cheat and use a metadata buffer instead of a data page. | |
54 | * | |
55 | * Returns: errno | |
56 | */ | |
57 | ||
58 | int gfs2_unstuffer_sync(struct gfs2_inode *ip, struct buffer_head *dibh, | |
59 | uint64_t block, void *private) | |
60 | { | |
61 | struct buffer_head *bh; | |
62 | int error; | |
63 | ||
64 | bh = gfs2_meta_new(ip->i_gl, block); | |
65 | ||
66 | gfs2_buffer_copy_tail(bh, 0, dibh, sizeof(struct gfs2_dinode)); | |
67 | ||
68 | set_buffer_dirty(bh); | |
69 | error = sync_dirty_buffer(bh); | |
70 | ||
71 | brelse(bh); | |
72 | ||
73 | return error; | |
74 | } | |
75 | ||
76 | /** | |
77 | * gfs2_unstuff_dinode - Unstuff a dinode when the data has grown too big | |
78 | * @ip: The GFS2 inode to unstuff | |
79 | * @unstuffer: the routine that handles unstuffing a non-zero length file | |
80 | * @private: private data for the unstuffer | |
81 | * | |
82 | * This routine unstuffs a dinode and returns it to a "normal" state such | |
83 | * that the height can be grown in the traditional way. | |
84 | * | |
85 | * Returns: errno | |
86 | */ | |
87 | ||
88 | int gfs2_unstuff_dinode(struct gfs2_inode *ip, gfs2_unstuffer_t unstuffer, | |
89 | void *private) | |
90 | { | |
91 | struct buffer_head *bh, *dibh; | |
92 | uint64_t block = 0; | |
93 | int journaled = gfs2_is_jdata(ip); | |
94 | int error; | |
95 | ||
96 | down_write(&ip->i_rw_mutex); | |
97 | ||
98 | error = gfs2_meta_inode_buffer(ip, &dibh); | |
99 | if (error) | |
100 | goto out; | |
101 | ||
102 | if (ip->i_di.di_size) { | |
103 | /* Get a free block, fill it with the stuffed data, | |
104 | and write it out to disk */ | |
105 | ||
106 | if (journaled) { | |
107 | block = gfs2_alloc_meta(ip); | |
108 | ||
109 | error = gfs2_jdata_get_buffer(ip, block, 1, &bh); | |
110 | if (error) | |
111 | goto out_brelse; | |
112 | gfs2_buffer_copy_tail(bh, | |
113 | sizeof(struct gfs2_meta_header), | |
114 | dibh, sizeof(struct gfs2_dinode)); | |
115 | brelse(bh); | |
116 | } else { | |
117 | block = gfs2_alloc_data(ip); | |
118 | ||
119 | error = unstuffer(ip, dibh, block, private); | |
120 | if (error) | |
121 | goto out_brelse; | |
122 | } | |
123 | } | |
124 | ||
125 | /* Set up the pointer to the new block */ | |
126 | ||
127 | gfs2_trans_add_bh(ip->i_gl, dibh); | |
128 | ||
129 | gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode)); | |
130 | ||
131 | if (ip->i_di.di_size) { | |
132 | *(uint64_t *)(dibh->b_data + sizeof(struct gfs2_dinode)) = cpu_to_be64(block); | |
133 | ip->i_di.di_blocks++; | |
134 | } | |
135 | ||
136 | ip->i_di.di_height = 1; | |
137 | ||
138 | gfs2_dinode_out(&ip->i_di, dibh->b_data); | |
139 | ||
140 | out_brelse: | |
141 | brelse(dibh); | |
142 | ||
143 | out: | |
144 | up_write(&ip->i_rw_mutex); | |
145 | ||
146 | return error; | |
147 | } | |
148 | ||
149 | /** | |
150 | * calc_tree_height - Calculate the height of a metadata tree | |
151 | * @ip: The GFS2 inode | |
152 | * @size: The proposed size of the file | |
153 | * | |
154 | * Work out how tall a metadata tree needs to be in order to accommodate a | |
155 | * file of a particular size. If size is less than the current size of | |
156 | * the inode, then the current size of the inode is used instead of the | |
157 | * supplied one. | |
158 | * | |
159 | * Returns: the height the tree should be | |
160 | */ | |
161 | ||
162 | static unsigned int calc_tree_height(struct gfs2_inode *ip, uint64_t size) | |
163 | { | |
164 | struct gfs2_sbd *sdp = ip->i_sbd; | |
165 | uint64_t *arr; | |
166 | unsigned int max, height; | |
167 | ||
168 | if (ip->i_di.di_size > size) | |
169 | size = ip->i_di.di_size; | |
170 | ||
171 | if (gfs2_is_jdata(ip)) { | |
172 | arr = sdp->sd_jheightsize; | |
173 | max = sdp->sd_max_jheight; | |
174 | } else { | |
175 | arr = sdp->sd_heightsize; | |
176 | max = sdp->sd_max_height; | |
177 | } | |
178 | ||
179 | for (height = 0; height < max; height++) | |
180 | if (arr[height] >= size) | |
181 | break; | |
182 | ||
183 | return height; | |
184 | } | |
185 | ||
186 | /** | |
187 | * build_height - Build a metadata tree of the requested height | |
188 | * @ip: The GFS2 inode | |
189 | * @height: The height to build to | |
190 | * | |
191 | * This routine makes sure that the metadata tree is tall enough to hold | |
192 | * "size" bytes of data. | |
193 | * | |
194 | * Returns: errno | |
195 | */ | |
196 | ||
197 | static int build_height(struct gfs2_inode *ip, int height) | |
198 | { | |
199 | struct gfs2_sbd *sdp = ip->i_sbd; | |
200 | struct buffer_head *bh, *dibh; | |
201 | uint64_t block = 0, *bp; | |
202 | unsigned int x; | |
203 | int new_block; | |
204 | int error; | |
205 | ||
206 | while (ip->i_di.di_height < height) { | |
207 | error = gfs2_meta_inode_buffer(ip, &dibh); | |
208 | if (error) | |
209 | return error; | |
210 | ||
211 | new_block = 0; | |
212 | bp = (uint64_t *)(dibh->b_data + sizeof(struct gfs2_dinode)); | |
213 | for (x = 0; x < sdp->sd_diptrs; x++, bp++) | |
214 | if (*bp) { | |
215 | new_block = 1; | |
216 | break; | |
217 | } | |
218 | ||
219 | if (new_block) { | |
220 | /* Get a new block, fill it with the old direct | |
221 | pointers, and write it out */ | |
222 | ||
223 | block = gfs2_alloc_meta(ip); | |
224 | ||
225 | bh = gfs2_meta_new(ip->i_gl, block); | |
226 | gfs2_trans_add_bh(ip->i_gl, bh); | |
227 | gfs2_metatype_set(bh, | |
228 | GFS2_METATYPE_IN, | |
229 | GFS2_FORMAT_IN); | |
230 | gfs2_buffer_copy_tail(bh, | |
231 | sizeof(struct gfs2_meta_header), | |
232 | dibh, sizeof(struct gfs2_dinode)); | |
233 | ||
234 | brelse(bh); | |
235 | } | |
236 | ||
237 | /* Set up the new direct pointer and write it out to disk */ | |
238 | ||
239 | gfs2_trans_add_bh(ip->i_gl, dibh); | |
240 | ||
241 | gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode)); | |
242 | ||
243 | if (new_block) { | |
244 | *(uint64_t *)(dibh->b_data + sizeof(struct gfs2_dinode)) = cpu_to_be64(block); | |
245 | ip->i_di.di_blocks++; | |
246 | } | |
247 | ||
248 | ip->i_di.di_height++; | |
249 | ||
250 | gfs2_dinode_out(&ip->i_di, dibh->b_data); | |
251 | brelse(dibh); | |
252 | } | |
253 | ||
254 | return 0; | |
255 | } | |
256 | ||
257 | /** | |
258 | * find_metapath - Find path through the metadata tree | |
259 | * @ip: The inode pointer | |
260 | * @mp: The metapath to return the result in | |
261 | * @block: The disk block to look up | |
262 | * | |
263 | * This routine returns a struct metapath structure that defines a path | |
264 | * through the metadata of inode "ip" to get to block "block". | |
265 | * | |
266 | * Example: | |
267 | * Given: "ip" is a height 3 file, "offset" is 101342453, and this is a | |
268 | * filesystem with a blocksize of 4096. | |
269 | * | |
270 | * find_metapath() would return a struct metapath structure set to: | |
271 | * mp_offset = 101342453, mp_height = 3, mp_list[0] = 0, mp_list[1] = 48, | |
272 | * and mp_list[2] = 165. | |
273 | * | |
274 | * That means that in order to get to the block containing the byte at | |
275 | * offset 101342453, we would load the indirect block pointed to by pointer | |
276 | * 0 in the dinode. We would then load the indirect block pointed to by | |
277 | * pointer 48 in that indirect block. We would then load the data block | |
278 | * pointed to by pointer 165 in that indirect block. | |
279 | * | |
280 | * ---------------------------------------- | |
281 | * | Dinode | | | |
282 | * | | 4| | |
283 | * | |0 1 2 3 4 5 9| | |
284 | * | | 6| | |
285 | * ---------------------------------------- | |
286 | * | | |
287 | * | | |
288 | * V | |
289 | * ---------------------------------------- | |
290 | * | Indirect Block | | |
291 | * | 5| | |
292 | * | 4 4 4 4 4 5 5 1| | |
293 | * |0 5 6 7 8 9 0 1 2| | |
294 | * ---------------------------------------- | |
295 | * | | |
296 | * | | |
297 | * V | |
298 | * ---------------------------------------- | |
299 | * | Indirect Block | | |
300 | * | 1 1 1 1 1 5| | |
301 | * | 6 6 6 6 6 1| | |
302 | * |0 3 4 5 6 7 2| | |
303 | * ---------------------------------------- | |
304 | * | | |
305 | * | | |
306 | * V | |
307 | * ---------------------------------------- | |
308 | * | Data block containing offset | | |
309 | * | 101342453 | | |
310 | * | | | |
311 | * | | | |
312 | * ---------------------------------------- | |
313 | * | |
314 | */ | |
315 | ||
316 | static void find_metapath(struct gfs2_inode *ip, uint64_t block, struct metapath *mp) | |
317 | { | |
318 | struct gfs2_sbd *sdp = ip->i_sbd; | |
319 | uint64_t b = block; | |
320 | unsigned int i; | |
321 | ||
322 | for (i = ip->i_di.di_height; i--;) | |
323 | mp->mp_list[i] = (__u16)do_div(b, sdp->sd_inptrs); | |
324 | ||
325 | } | |
326 | ||
327 | /** | |
328 | * metapointer - Return pointer to start of metadata in a buffer | |
329 | * @bh: The buffer | |
330 | * @height: The metadata height (0 = dinode) | |
331 | * @mp: The metapath | |
332 | * | |
333 | * Return a pointer to the block number of the next height of the metadata | |
334 | * tree given a buffer containing the pointer to the current height of the | |
335 | * metadata tree. | |
336 | */ | |
337 | ||
338 | static inline uint64_t *metapointer(struct buffer_head *bh, | |
339 | unsigned int height, struct metapath *mp) | |
340 | { | |
341 | unsigned int head_size = (height > 0) ? | |
342 | sizeof(struct gfs2_meta_header) : sizeof(struct gfs2_dinode); | |
343 | ||
344 | return ((uint64_t *)(bh->b_data + head_size)) + mp->mp_list[height]; | |
345 | } | |
346 | ||
347 | /** | |
348 | * lookup_block - Get the next metadata block in metadata tree | |
349 | * @ip: The GFS2 inode | |
350 | * @bh: Buffer containing the pointers to metadata blocks | |
351 | * @height: The height of the tree (0 = dinode) | |
352 | * @mp: The metapath | |
353 | * @create: Non-zero if we may create a new meatdata block | |
354 | * @new: Used to indicate if we did create a new metadata block | |
355 | * @block: the returned disk block number | |
356 | * | |
357 | * Given a metatree, complete to a particular height, checks to see if the next | |
358 | * height of the tree exists. If not the next height of the tree is created. | |
359 | * The block number of the next height of the metadata tree is returned. | |
360 | * | |
361 | */ | |
362 | ||
363 | static void lookup_block(struct gfs2_inode *ip, struct buffer_head *bh, | |
364 | unsigned int height, struct metapath *mp, int create, | |
365 | int *new, uint64_t *block) | |
366 | { | |
367 | uint64_t *ptr = metapointer(bh, height, mp); | |
368 | ||
369 | if (*ptr) { | |
370 | *block = be64_to_cpu(*ptr); | |
371 | return; | |
372 | } | |
373 | ||
374 | *block = 0; | |
375 | ||
376 | if (!create) | |
377 | return; | |
378 | ||
379 | if (height == ip->i_di.di_height - 1 && | |
380 | !gfs2_is_jdata(ip)) | |
381 | *block = gfs2_alloc_data(ip); | |
382 | else | |
383 | *block = gfs2_alloc_meta(ip); | |
384 | ||
385 | gfs2_trans_add_bh(ip->i_gl, bh); | |
386 | ||
387 | *ptr = cpu_to_be64(*block); | |
388 | ip->i_di.di_blocks++; | |
389 | ||
390 | *new = 1; | |
391 | } | |
392 | ||
393 | /** | |
394 | * gfs2_block_map - Map a block from an inode to a disk block | |
395 | * @ip: The GFS2 inode | |
396 | * @lblock: The logical block number | |
397 | * @new: Value/Result argument (1 = may create/did create new blocks) | |
398 | * @dblock: the disk block number of the start of an extent | |
399 | * @extlen: the size of the extent | |
400 | * | |
401 | * Find the block number on the current device which corresponds to an | |
402 | * inode's block. If the block had to be created, "new" will be set. | |
403 | * | |
404 | * Returns: errno | |
405 | */ | |
406 | ||
407 | int gfs2_block_map(struct gfs2_inode *ip, uint64_t lblock, int *new, | |
408 | uint64_t *dblock, uint32_t *extlen) | |
409 | { | |
410 | struct gfs2_sbd *sdp = ip->i_sbd; | |
411 | struct buffer_head *bh; | |
412 | struct metapath mp; | |
413 | int create = *new; | |
414 | unsigned int bsize; | |
415 | unsigned int height; | |
416 | unsigned int end_of_metadata; | |
417 | unsigned int x; | |
418 | int error = 0; | |
419 | ||
420 | *new = 0; | |
421 | *dblock = 0; | |
422 | if (extlen) | |
423 | *extlen = 0; | |
424 | ||
425 | if (create) | |
426 | down_write(&ip->i_rw_mutex); | |
427 | else | |
428 | down_read(&ip->i_rw_mutex); | |
429 | ||
430 | if (gfs2_assert_warn(sdp, !gfs2_is_stuffed(ip))) | |
431 | goto out; | |
432 | ||
433 | bsize = (gfs2_is_jdata(ip)) ? sdp->sd_jbsize : sdp->sd_sb.sb_bsize; | |
434 | ||
435 | height = calc_tree_height(ip, (lblock + 1) * bsize); | |
436 | if (ip->i_di.di_height < height) { | |
437 | if (!create) | |
438 | goto out; | |
439 | ||
440 | error = build_height(ip, height); | |
441 | if (error) | |
442 | goto out; | |
443 | } | |
444 | ||
445 | find_metapath(ip, lblock, &mp); | |
446 | end_of_metadata = ip->i_di.di_height - 1; | |
447 | ||
448 | error = gfs2_meta_inode_buffer(ip, &bh); | |
449 | if (error) | |
450 | goto out; | |
451 | ||
452 | for (x = 0; x < end_of_metadata; x++) { | |
453 | lookup_block(ip, bh, x, &mp, create, new, dblock); | |
454 | brelse(bh); | |
455 | if (!*dblock) | |
456 | goto out; | |
457 | ||
458 | error = gfs2_meta_indirect_buffer(ip, x+1, *dblock, *new, &bh); | |
459 | if (error) | |
460 | goto out; | |
461 | } | |
462 | ||
463 | lookup_block(ip, bh, end_of_metadata, &mp, create, new, dblock); | |
464 | ||
465 | if (extlen && *dblock) { | |
466 | *extlen = 1; | |
467 | ||
468 | if (!*new) { | |
469 | uint64_t tmp_dblock; | |
470 | int tmp_new; | |
471 | unsigned int nptrs; | |
472 | ||
473 | nptrs = (end_of_metadata) ? sdp->sd_inptrs : | |
474 | sdp->sd_diptrs; | |
475 | ||
476 | while (++mp.mp_list[end_of_metadata] < nptrs) { | |
477 | lookup_block(ip, bh, end_of_metadata, &mp, | |
478 | 0, &tmp_new, &tmp_dblock); | |
479 | ||
480 | if (*dblock + *extlen != tmp_dblock) | |
481 | break; | |
482 | ||
483 | (*extlen)++; | |
484 | } | |
485 | } | |
486 | } | |
487 | ||
488 | brelse(bh); | |
489 | ||
490 | if (*new) { | |
491 | error = gfs2_meta_inode_buffer(ip, &bh); | |
492 | if (!error) { | |
493 | gfs2_trans_add_bh(ip->i_gl, bh); | |
494 | gfs2_dinode_out(&ip->i_di, bh->b_data); | |
495 | brelse(bh); | |
496 | } | |
497 | } | |
498 | ||
499 | out: | |
500 | if (create) | |
501 | up_write(&ip->i_rw_mutex); | |
502 | else | |
503 | up_read(&ip->i_rw_mutex); | |
504 | ||
505 | return error; | |
506 | } | |
507 | ||
508 | /** | |
509 | * recursive_scan - recursively scan through the end of a file | |
510 | * @ip: the inode | |
511 | * @dibh: the dinode buffer | |
512 | * @mp: the path through the metadata to the point to start | |
513 | * @height: the height the recursion is at | |
514 | * @block: the indirect block to look at | |
515 | * @first: 1 if this is the first block | |
516 | * @bc: the call to make for each piece of metadata | |
517 | * @data: data opaque to this function to pass to @bc | |
518 | * | |
519 | * When this is first called @height and @block should be zero and | |
520 | * @first should be 1. | |
521 | * | |
522 | * Returns: errno | |
523 | */ | |
524 | ||
525 | static int recursive_scan(struct gfs2_inode *ip, struct buffer_head *dibh, | |
526 | struct metapath *mp, unsigned int height, | |
527 | uint64_t block, int first, block_call_t bc, | |
528 | void *data) | |
529 | { | |
530 | struct gfs2_sbd *sdp = ip->i_sbd; | |
531 | struct buffer_head *bh = NULL; | |
532 | uint64_t *top, *bottom; | |
533 | uint64_t bn; | |
534 | int error; | |
535 | int mh_size = sizeof(struct gfs2_meta_header); | |
536 | ||
537 | if (!height) { | |
538 | error = gfs2_meta_inode_buffer(ip, &bh); | |
539 | if (error) | |
540 | return error; | |
541 | dibh = bh; | |
542 | ||
543 | top = (uint64_t *)(bh->b_data + sizeof(struct gfs2_dinode)) + | |
544 | mp->mp_list[0]; | |
545 | bottom = (uint64_t *)(bh->b_data + sizeof(struct gfs2_dinode)) + | |
546 | sdp->sd_diptrs; | |
547 | } else { | |
548 | error = gfs2_meta_indirect_buffer(ip, height, block, 0, &bh); | |
549 | if (error) | |
550 | return error; | |
551 | ||
552 | top = (uint64_t *)(bh->b_data + mh_size) + | |
553 | ((first) ? mp->mp_list[height] : 0); | |
554 | ||
555 | bottom = (uint64_t *)(bh->b_data + mh_size) + sdp->sd_inptrs; | |
556 | } | |
557 | ||
558 | error = bc(ip, dibh, bh, top, bottom, height, data); | |
559 | if (error) | |
560 | goto out; | |
561 | ||
562 | if (height < ip->i_di.di_height - 1) | |
563 | for (; top < bottom; top++, first = 0) { | |
564 | if (!*top) | |
565 | continue; | |
566 | ||
567 | bn = be64_to_cpu(*top); | |
568 | ||
569 | error = recursive_scan(ip, dibh, mp, height + 1, bn, | |
570 | first, bc, data); | |
571 | if (error) | |
572 | break; | |
573 | } | |
574 | ||
575 | out: | |
576 | brelse(bh); | |
577 | ||
578 | return error; | |
579 | } | |
580 | ||
581 | /** | |
582 | * do_strip - Look for a layer a particular layer of the file and strip it off | |
583 | * @ip: the inode | |
584 | * @dibh: the dinode buffer | |
585 | * @bh: A buffer of pointers | |
586 | * @top: The first pointer in the buffer | |
587 | * @bottom: One more than the last pointer | |
588 | * @height: the height this buffer is at | |
589 | * @data: a pointer to a struct strip_mine | |
590 | * | |
591 | * Returns: errno | |
592 | */ | |
593 | ||
594 | static int do_strip(struct gfs2_inode *ip, struct buffer_head *dibh, | |
595 | struct buffer_head *bh, uint64_t *top, uint64_t *bottom, | |
596 | unsigned int height, void *data) | |
597 | { | |
598 | struct strip_mine *sm = (struct strip_mine *)data; | |
599 | struct gfs2_sbd *sdp = ip->i_sbd; | |
600 | struct gfs2_rgrp_list rlist; | |
601 | uint64_t bn, bstart; | |
602 | uint32_t blen; | |
603 | uint64_t *p; | |
604 | unsigned int rg_blocks = 0; | |
605 | int metadata; | |
606 | unsigned int revokes = 0; | |
607 | int x; | |
608 | int error; | |
609 | ||
610 | if (!*top) | |
611 | sm->sm_first = 0; | |
612 | ||
613 | if (height != sm->sm_height) | |
614 | return 0; | |
615 | ||
616 | if (sm->sm_first) { | |
617 | top++; | |
618 | sm->sm_first = 0; | |
619 | } | |
620 | ||
621 | metadata = (height != ip->i_di.di_height - 1) || gfs2_is_jdata(ip); | |
622 | if (metadata) | |
623 | revokes = (height) ? sdp->sd_inptrs : sdp->sd_diptrs; | |
624 | ||
625 | error = gfs2_rindex_hold(sdp, &ip->i_alloc.al_ri_gh); | |
626 | if (error) | |
627 | return error; | |
628 | ||
629 | memset(&rlist, 0, sizeof(struct gfs2_rgrp_list)); | |
630 | bstart = 0; | |
631 | blen = 0; | |
632 | ||
633 | for (p = top; p < bottom; p++) { | |
634 | if (!*p) | |
635 | continue; | |
636 | ||
637 | bn = be64_to_cpu(*p); | |
638 | ||
639 | if (bstart + blen == bn) | |
640 | blen++; | |
641 | else { | |
642 | if (bstart) | |
643 | gfs2_rlist_add(sdp, &rlist, bstart); | |
644 | ||
645 | bstart = bn; | |
646 | blen = 1; | |
647 | } | |
648 | } | |
649 | ||
650 | if (bstart) | |
651 | gfs2_rlist_add(sdp, &rlist, bstart); | |
652 | else | |
653 | goto out; /* Nothing to do */ | |
654 | ||
655 | gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE, 0); | |
656 | ||
657 | for (x = 0; x < rlist.rl_rgrps; x++) { | |
658 | struct gfs2_rgrpd *rgd; | |
659 | rgd = get_gl2rgd(rlist.rl_ghs[x].gh_gl); | |
660 | rg_blocks += rgd->rd_ri.ri_length; | |
661 | } | |
662 | ||
663 | error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs); | |
664 | if (error) | |
665 | goto out_rlist; | |
666 | ||
667 | error = gfs2_trans_begin(sdp, rg_blocks + RES_DINODE + | |
668 | RES_INDIRECT + RES_STATFS + RES_QUOTA, | |
669 | revokes); | |
670 | if (error) | |
671 | goto out_rg_gunlock; | |
672 | ||
673 | down_write(&ip->i_rw_mutex); | |
674 | ||
675 | gfs2_trans_add_bh(ip->i_gl, dibh); | |
676 | gfs2_trans_add_bh(ip->i_gl, bh); | |
677 | ||
678 | bstart = 0; | |
679 | blen = 0; | |
680 | ||
681 | for (p = top; p < bottom; p++) { | |
682 | if (!*p) | |
683 | continue; | |
684 | ||
685 | bn = be64_to_cpu(*p); | |
686 | ||
687 | if (bstart + blen == bn) | |
688 | blen++; | |
689 | else { | |
690 | if (bstart) { | |
691 | if (metadata) | |
692 | gfs2_free_meta(ip, bstart, blen); | |
693 | else | |
694 | gfs2_free_data(ip, bstart, blen); | |
695 | } | |
696 | ||
697 | bstart = bn; | |
698 | blen = 1; | |
699 | } | |
700 | ||
701 | *p = 0; | |
702 | if (!ip->i_di.di_blocks) | |
703 | gfs2_consist_inode(ip); | |
704 | ip->i_di.di_blocks--; | |
705 | } | |
706 | if (bstart) { | |
707 | if (metadata) | |
708 | gfs2_free_meta(ip, bstart, blen); | |
709 | else | |
710 | gfs2_free_data(ip, bstart, blen); | |
711 | } | |
712 | ||
713 | ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds(); | |
714 | ||
715 | gfs2_dinode_out(&ip->i_di, dibh->b_data); | |
716 | ||
717 | up_write(&ip->i_rw_mutex); | |
718 | ||
719 | gfs2_trans_end(sdp); | |
720 | ||
721 | out_rg_gunlock: | |
722 | gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs); | |
723 | ||
724 | out_rlist: | |
725 | gfs2_rlist_free(&rlist); | |
726 | ||
727 | out: | |
728 | gfs2_glock_dq_uninit(&ip->i_alloc.al_ri_gh); | |
729 | ||
730 | return error; | |
731 | } | |
732 | ||
733 | /** | |
734 | * do_grow - Make a file look bigger than it is | |
735 | * @ip: the inode | |
736 | * @size: the size to set the file to | |
737 | * | |
738 | * Called with an exclusive lock on @ip. | |
739 | * | |
740 | * Returns: errno | |
741 | */ | |
742 | ||
743 | static int do_grow(struct gfs2_inode *ip, uint64_t size) | |
744 | { | |
745 | struct gfs2_sbd *sdp = ip->i_sbd; | |
746 | struct gfs2_alloc *al; | |
747 | struct buffer_head *dibh; | |
748 | unsigned int h; | |
749 | int error; | |
750 | ||
751 | al = gfs2_alloc_get(ip); | |
752 | ||
753 | error = gfs2_quota_lock(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE); | |
754 | if (error) | |
755 | goto out; | |
756 | ||
757 | error = gfs2_quota_check(ip, ip->i_di.di_uid, ip->i_di.di_gid); | |
758 | if (error) | |
759 | goto out_gunlock_q; | |
760 | ||
761 | al->al_requested = sdp->sd_max_height + RES_DATA; | |
762 | ||
763 | error = gfs2_inplace_reserve(ip); | |
764 | if (error) | |
765 | goto out_gunlock_q; | |
766 | ||
767 | error = gfs2_trans_begin(sdp, | |
768 | sdp->sd_max_height + al->al_rgd->rd_ri.ri_length + | |
769 | RES_JDATA + RES_DINODE + RES_STATFS + RES_QUOTA, 0); | |
770 | if (error) | |
771 | goto out_ipres; | |
772 | ||
773 | if (size > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode)) { | |
774 | if (gfs2_is_stuffed(ip)) { | |
775 | error = gfs2_unstuff_dinode(ip, gfs2_unstuffer_page, | |
776 | NULL); | |
777 | if (error) | |
778 | goto out_end_trans; | |
779 | } | |
780 | ||
781 | h = calc_tree_height(ip, size); | |
782 | if (ip->i_di.di_height < h) { | |
783 | down_write(&ip->i_rw_mutex); | |
784 | error = build_height(ip, h); | |
785 | up_write(&ip->i_rw_mutex); | |
786 | if (error) | |
787 | goto out_end_trans; | |
788 | } | |
789 | } | |
790 | ||
791 | ip->i_di.di_size = size; | |
792 | ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds(); | |
793 | ||
794 | error = gfs2_meta_inode_buffer(ip, &dibh); | |
795 | if (error) | |
796 | goto out_end_trans; | |
797 | ||
798 | gfs2_trans_add_bh(ip->i_gl, dibh); | |
799 | gfs2_dinode_out(&ip->i_di, dibh->b_data); | |
800 | brelse(dibh); | |
801 | ||
802 | out_end_trans: | |
803 | gfs2_trans_end(sdp); | |
804 | ||
805 | out_ipres: | |
806 | gfs2_inplace_release(ip); | |
807 | ||
808 | out_gunlock_q: | |
809 | gfs2_quota_unlock(ip); | |
810 | ||
811 | out: | |
812 | gfs2_alloc_put(ip); | |
813 | ||
814 | return error; | |
815 | } | |
816 | ||
817 | static int truncator_journaled(struct gfs2_inode *ip, uint64_t size) | |
818 | { | |
819 | uint64_t lbn, dbn; | |
820 | uint32_t off; | |
821 | struct buffer_head *bh; | |
822 | int new = 0; | |
823 | int error; | |
824 | ||
825 | lbn = size; | |
826 | off = do_div(lbn, ip->i_sbd->sd_jbsize); | |
827 | ||
828 | error = gfs2_block_map(ip, lbn, &new, &dbn, NULL); | |
829 | if (error || !dbn) | |
830 | return error; | |
831 | ||
832 | error = gfs2_jdata_get_buffer(ip, dbn, 0, &bh); | |
833 | if (error) | |
834 | return error; | |
835 | ||
836 | gfs2_trans_add_bh(ip->i_gl, bh); | |
837 | gfs2_buffer_clear_tail(bh, sizeof(struct gfs2_meta_header) + off); | |
838 | ||
839 | brelse(bh); | |
840 | ||
841 | return 0; | |
842 | } | |
843 | ||
844 | static int trunc_start(struct gfs2_inode *ip, uint64_t size, | |
845 | gfs2_truncator_t truncator) | |
846 | { | |
847 | struct gfs2_sbd *sdp = ip->i_sbd; | |
848 | struct buffer_head *dibh; | |
849 | int journaled = gfs2_is_jdata(ip); | |
850 | int error; | |
851 | ||
852 | error = gfs2_trans_begin(sdp, | |
853 | RES_DINODE + ((journaled) ? RES_JDATA : 0), 0); | |
854 | if (error) | |
855 | return error; | |
856 | ||
857 | error = gfs2_meta_inode_buffer(ip, &dibh); | |
858 | if (error) | |
859 | goto out; | |
860 | ||
861 | if (gfs2_is_stuffed(ip)) { | |
862 | ip->i_di.di_size = size; | |
863 | ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds(); | |
864 | gfs2_trans_add_bh(ip->i_gl, dibh); | |
865 | gfs2_dinode_out(&ip->i_di, dibh->b_data); | |
866 | gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode) + size); | |
867 | error = 1; | |
868 | ||
869 | } else { | |
870 | if (journaled) { | |
871 | uint64_t junk = size; | |
872 | /* we're just interested in the modulus */ | |
873 | if (do_div(junk, sdp->sd_jbsize)) | |
874 | error = truncator_journaled(ip, size); | |
875 | } else if (size & (uint64_t)(sdp->sd_sb.sb_bsize - 1)) | |
876 | error = truncator(ip, size); | |
877 | ||
878 | if (!error) { | |
879 | ip->i_di.di_size = size; | |
880 | ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds(); | |
881 | ip->i_di.di_flags |= GFS2_DIF_TRUNC_IN_PROG; | |
882 | gfs2_trans_add_bh(ip->i_gl, dibh); | |
883 | gfs2_dinode_out(&ip->i_di, dibh->b_data); | |
884 | } | |
885 | } | |
886 | ||
887 | brelse(dibh); | |
888 | ||
889 | out: | |
890 | gfs2_trans_end(sdp); | |
891 | ||
892 | return error; | |
893 | } | |
894 | ||
895 | static int trunc_dealloc(struct gfs2_inode *ip, uint64_t size) | |
896 | { | |
897 | unsigned int height = ip->i_di.di_height; | |
898 | uint64_t lblock; | |
899 | struct metapath mp; | |
900 | int error; | |
901 | ||
902 | if (!size) | |
903 | lblock = 0; | |
904 | else if (gfs2_is_jdata(ip)) { | |
905 | lblock = size - 1; | |
906 | do_div(lblock, ip->i_sbd->sd_jbsize); | |
907 | } else | |
908 | lblock = (size - 1) >> ip->i_sbd->sd_sb.sb_bsize_shift; | |
909 | ||
910 | find_metapath(ip, lblock, &mp); | |
911 | gfs2_alloc_get(ip); | |
912 | ||
913 | error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE); | |
914 | if (error) | |
915 | goto out; | |
916 | ||
917 | while (height--) { | |
918 | struct strip_mine sm; | |
919 | sm.sm_first = !!size; | |
920 | sm.sm_height = height; | |
921 | ||
922 | error = recursive_scan(ip, NULL, &mp, 0, 0, 1, do_strip, &sm); | |
923 | if (error) | |
924 | break; | |
925 | } | |
926 | ||
927 | gfs2_quota_unhold(ip); | |
928 | ||
929 | out: | |
930 | gfs2_alloc_put(ip); | |
931 | return error; | |
932 | } | |
933 | ||
934 | static int trunc_end(struct gfs2_inode *ip) | |
935 | { | |
936 | struct gfs2_sbd *sdp = ip->i_sbd; | |
937 | struct buffer_head *dibh; | |
938 | int error; | |
939 | ||
940 | error = gfs2_trans_begin(sdp, RES_DINODE, 0); | |
941 | if (error) | |
942 | return error; | |
943 | ||
944 | down_write(&ip->i_rw_mutex); | |
945 | ||
946 | error = gfs2_meta_inode_buffer(ip, &dibh); | |
947 | if (error) | |
948 | goto out; | |
949 | ||
950 | if (!ip->i_di.di_size) { | |
951 | ip->i_di.di_height = 0; | |
952 | ip->i_di.di_goal_meta = | |
953 | ip->i_di.di_goal_data = | |
954 | ip->i_num.no_addr; | |
955 | gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode)); | |
956 | } | |
957 | ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds(); | |
958 | ip->i_di.di_flags &= ~GFS2_DIF_TRUNC_IN_PROG; | |
959 | ||
960 | gfs2_trans_add_bh(ip->i_gl, dibh); | |
961 | gfs2_dinode_out(&ip->i_di, dibh->b_data); | |
962 | brelse(dibh); | |
963 | ||
964 | out: | |
965 | up_write(&ip->i_rw_mutex); | |
966 | ||
967 | gfs2_trans_end(sdp); | |
968 | ||
969 | return error; | |
970 | } | |
971 | ||
972 | /** | |
973 | * do_shrink - make a file smaller | |
974 | * @ip: the inode | |
975 | * @size: the size to make the file | |
976 | * @truncator: function to truncate the last partial block | |
977 | * | |
978 | * Called with an exclusive lock on @ip. | |
979 | * | |
980 | * Returns: errno | |
981 | */ | |
982 | ||
983 | static int do_shrink(struct gfs2_inode *ip, uint64_t size, | |
984 | gfs2_truncator_t truncator) | |
985 | { | |
986 | int error; | |
987 | ||
988 | error = trunc_start(ip, size, truncator); | |
989 | if (error < 0) | |
990 | return error; | |
991 | if (error > 0) | |
992 | return 0; | |
993 | ||
994 | error = trunc_dealloc(ip, size); | |
995 | if (!error) | |
996 | error = trunc_end(ip); | |
997 | ||
998 | return error; | |
999 | } | |
1000 | ||
1001 | /** | |
1002 | * gfs2_truncatei - make a file a give size | |
1003 | * @ip: the inode | |
1004 | * @size: the size to make the file | |
1005 | * @truncator: function to truncate the last partial block | |
1006 | * | |
1007 | * The file size can grow, shrink, or stay the same size. | |
1008 | * | |
1009 | * Returns: errno | |
1010 | */ | |
1011 | ||
1012 | int gfs2_truncatei(struct gfs2_inode *ip, uint64_t size, | |
1013 | gfs2_truncator_t truncator) | |
1014 | { | |
1015 | int error; | |
1016 | ||
1017 | if (gfs2_assert_warn(ip->i_sbd, S_ISREG(ip->i_di.di_mode))) | |
1018 | return -EINVAL; | |
1019 | ||
1020 | if (size > ip->i_di.di_size) | |
1021 | error = do_grow(ip, size); | |
1022 | else | |
1023 | error = do_shrink(ip, size, truncator); | |
1024 | ||
1025 | return error; | |
1026 | } | |
1027 | ||
1028 | int gfs2_truncatei_resume(struct gfs2_inode *ip) | |
1029 | { | |
1030 | int error; | |
1031 | error = trunc_dealloc(ip, ip->i_di.di_size); | |
1032 | if (!error) | |
1033 | error = trunc_end(ip); | |
1034 | return error; | |
1035 | } | |
1036 | ||
1037 | int gfs2_file_dealloc(struct gfs2_inode *ip) | |
1038 | { | |
1039 | return trunc_dealloc(ip, 0); | |
1040 | } | |
1041 | ||
1042 | /** | |
1043 | * gfs2_write_calc_reserv - calculate number of blocks needed to write to a file | |
1044 | * @ip: the file | |
1045 | * @len: the number of bytes to be written to the file | |
1046 | * @data_blocks: returns the number of data blocks required | |
1047 | * @ind_blocks: returns the number of indirect blocks required | |
1048 | * | |
1049 | */ | |
1050 | ||
1051 | void gfs2_write_calc_reserv(struct gfs2_inode *ip, unsigned int len, | |
1052 | unsigned int *data_blocks, unsigned int *ind_blocks) | |
1053 | { | |
1054 | struct gfs2_sbd *sdp = ip->i_sbd; | |
1055 | unsigned int tmp; | |
1056 | ||
1057 | if (gfs2_is_jdata(ip)) { | |
1058 | *data_blocks = DIV_RU(len, sdp->sd_jbsize) + 2; | |
1059 | *ind_blocks = 3 * (sdp->sd_max_jheight - 1); | |
1060 | } else { | |
1061 | *data_blocks = (len >> sdp->sd_sb.sb_bsize_shift) + 3; | |
1062 | *ind_blocks = 3 * (sdp->sd_max_height - 1); | |
1063 | } | |
1064 | ||
1065 | for (tmp = *data_blocks; tmp > sdp->sd_diptrs;) { | |
1066 | tmp = DIV_RU(tmp, sdp->sd_inptrs); | |
1067 | *ind_blocks += tmp; | |
1068 | } | |
1069 | } | |
1070 | ||
1071 | /** | |
1072 | * gfs2_write_alloc_required - figure out if a write will require an allocation | |
1073 | * @ip: the file being written to | |
1074 | * @offset: the offset to write to | |
1075 | * @len: the number of bytes being written | |
1076 | * @alloc_required: set to 1 if an alloc is required, 0 otherwise | |
1077 | * | |
1078 | * Returns: errno | |
1079 | */ | |
1080 | ||
1081 | int gfs2_write_alloc_required(struct gfs2_inode *ip, uint64_t offset, | |
1082 | unsigned int len, int *alloc_required) | |
1083 | { | |
1084 | struct gfs2_sbd *sdp = ip->i_sbd; | |
1085 | uint64_t lblock, lblock_stop, dblock; | |
1086 | uint32_t extlen; | |
1087 | int new = 0; | |
1088 | int error = 0; | |
1089 | ||
1090 | *alloc_required = 0; | |
1091 | ||
1092 | if (!len) | |
1093 | return 0; | |
1094 | ||
1095 | if (gfs2_is_stuffed(ip)) { | |
1096 | if (offset + len > | |
1097 | sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode)) | |
1098 | *alloc_required = 1; | |
1099 | return 0; | |
1100 | } | |
1101 | ||
1102 | if (gfs2_is_jdata(ip)) { | |
1103 | unsigned int bsize = sdp->sd_jbsize; | |
1104 | lblock = offset; | |
1105 | do_div(lblock, bsize); | |
1106 | lblock_stop = offset + len + bsize - 1; | |
1107 | do_div(lblock_stop, bsize); | |
1108 | } else { | |
1109 | unsigned int shift = sdp->sd_sb.sb_bsize_shift; | |
1110 | lblock = offset >> shift; | |
1111 | lblock_stop = (offset + len + sdp->sd_sb.sb_bsize - 1) >> shift; | |
1112 | } | |
1113 | ||
1114 | for (; lblock < lblock_stop; lblock += extlen) { | |
1115 | error = gfs2_block_map(ip, lblock, &new, &dblock, &extlen); | |
1116 | if (error) | |
1117 | return error; | |
1118 | ||
1119 | if (!dblock) { | |
1120 | *alloc_required = 1; | |
1121 | return 0; | |
1122 | } | |
1123 | } | |
1124 | ||
1125 | return 0; | |
1126 | } | |
1127 | ||
1128 | /** | |
1129 | * do_gfm - Copy out the dinode/indirect blocks of a file | |
1130 | * @ip: the file | |
1131 | * @dibh: the dinode buffer | |
1132 | * @bh: the indirect buffer we're looking at | |
1133 | * @top: the first pointer in the block | |
1134 | * @bottom: one more than the last pointer in the block | |
1135 | * @height: the height the block is at | |
1136 | * @data: a pointer to a struct gfs2_user_buffer structure | |
1137 | * | |
1138 | * If this is a journaled file, copy out the data too. | |
1139 | * | |
1140 | * Returns: errno | |
1141 | */ | |
1142 | ||
1143 | static int do_gfm(struct gfs2_inode *ip, struct buffer_head *dibh, | |
1144 | struct buffer_head *bh, uint64_t *top, uint64_t *bottom, | |
1145 | unsigned int height, void *data) | |
1146 | { | |
1147 | struct gfs2_user_buffer *ub = (struct gfs2_user_buffer *)data; | |
1148 | int error; | |
1149 | ||
1150 | error = gfs2_add_bh_to_ub(ub, bh); | |
1151 | if (error) | |
1152 | return error; | |
1153 | ||
1154 | if (!S_ISDIR(ip->i_di.di_mode) || | |
1155 | height + 1 != ip->i_di.di_height) | |
1156 | return 0; | |
1157 | ||
1158 | for (; top < bottom; top++) | |
1159 | if (*top) { | |
1160 | struct buffer_head *data_bh; | |
1161 | ||
1162 | error = gfs2_meta_read(ip->i_gl, be64_to_cpu(*top), | |
1163 | DIO_START | DIO_WAIT, | |
1164 | &data_bh); | |
1165 | if (error) | |
1166 | return error; | |
1167 | ||
1168 | error = gfs2_add_bh_to_ub(ub, data_bh); | |
1169 | ||
1170 | brelse(data_bh); | |
1171 | ||
1172 | if (error) | |
1173 | return error; | |
1174 | } | |
1175 | ||
1176 | return 0; | |
1177 | } | |
1178 | ||
1179 | /** | |
1180 | * gfs2_get_file_meta - return all the metadata for a file | |
1181 | * @ip: the file | |
1182 | * @ub: the structure representing the meta | |
1183 | * | |
1184 | * Returns: errno | |
1185 | */ | |
1186 | ||
1187 | int gfs2_get_file_meta(struct gfs2_inode *ip, struct gfs2_user_buffer *ub) | |
1188 | { | |
1189 | int error; | |
1190 | ||
1191 | if (gfs2_is_stuffed(ip)) { | |
1192 | struct buffer_head *dibh; | |
1193 | error = gfs2_meta_inode_buffer(ip, &dibh); | |
1194 | if (!error) { | |
1195 | error = gfs2_add_bh_to_ub(ub, dibh); | |
1196 | brelse(dibh); | |
1197 | } | |
1198 | } else { | |
1199 | struct metapath mp; | |
1200 | find_metapath(ip, 0, &mp); | |
1201 | error = recursive_scan(ip, NULL, &mp, 0, 0, 1, do_gfm, ub); | |
1202 | } | |
1203 | ||
1204 | return error; | |
1205 | } | |
1206 |