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