Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * linux/fs/ext2/inode.c | |
3 | * | |
4 | * Copyright (C) 1992, 1993, 1994, 1995 | |
5 | * Remy Card (card@masi.ibp.fr) | |
6 | * Laboratoire MASI - Institut Blaise Pascal | |
7 | * Universite Pierre et Marie Curie (Paris VI) | |
8 | * | |
9 | * from | |
10 | * | |
11 | * linux/fs/minix/inode.c | |
12 | * | |
13 | * Copyright (C) 1991, 1992 Linus Torvalds | |
14 | * | |
15 | * Goal-directed block allocation by Stephen Tweedie | |
16 | * (sct@dcs.ed.ac.uk), 1993, 1998 | |
17 | * Big-endian to little-endian byte-swapping/bitmaps by | |
18 | * David S. Miller (davem@caip.rutgers.edu), 1995 | |
19 | * 64-bit file support on 64-bit platforms by Jakub Jelinek | |
20 | * (jj@sunsite.ms.mff.cuni.cz) | |
21 | * | |
22 | * Assorted race fixes, rewrite of ext2_get_block() by Al Viro, 2000 | |
23 | */ | |
24 | ||
25 | #include <linux/smp_lock.h> | |
26 | #include <linux/time.h> | |
27 | #include <linux/highuid.h> | |
28 | #include <linux/pagemap.h> | |
29 | #include <linux/quotaops.h> | |
30 | #include <linux/module.h> | |
31 | #include <linux/writeback.h> | |
32 | #include <linux/buffer_head.h> | |
33 | #include <linux/mpage.h> | |
68c9d702 | 34 | #include <linux/fiemap.h> |
8d6d0c4d | 35 | #include <linux/namei.h> |
1da177e4 LT |
36 | #include "ext2.h" |
37 | #include "acl.h" | |
6d79125b | 38 | #include "xip.h" |
1da177e4 LT |
39 | |
40 | MODULE_AUTHOR("Remy Card and others"); | |
41 | MODULE_DESCRIPTION("Second Extended Filesystem"); | |
42 | MODULE_LICENSE("GPL"); | |
43 | ||
1da177e4 LT |
44 | /* |
45 | * Test whether an inode is a fast symlink. | |
46 | */ | |
47 | static inline int ext2_inode_is_fast_symlink(struct inode *inode) | |
48 | { | |
49 | int ea_blocks = EXT2_I(inode)->i_file_acl ? | |
50 | (inode->i_sb->s_blocksize >> 9) : 0; | |
51 | ||
52 | return (S_ISLNK(inode->i_mode) && | |
53 | inode->i_blocks - ea_blocks == 0); | |
54 | } | |
55 | ||
56 | /* | |
57 | * Called at the last iput() if i_nlink is zero. | |
58 | */ | |
59 | void ext2_delete_inode (struct inode * inode) | |
60 | { | |
fef26658 MF |
61 | truncate_inode_pages(&inode->i_data, 0); |
62 | ||
1da177e4 LT |
63 | if (is_bad_inode(inode)) |
64 | goto no_delete; | |
65 | EXT2_I(inode)->i_dtime = get_seconds(); | |
66 | mark_inode_dirty(inode); | |
e1740a46 | 67 | ext2_write_inode(inode, inode_needs_sync(inode)); |
1da177e4 LT |
68 | |
69 | inode->i_size = 0; | |
70 | if (inode->i_blocks) | |
71 | ext2_truncate (inode); | |
72 | ext2_free_inode (inode); | |
73 | ||
74 | return; | |
75 | no_delete: | |
76 | clear_inode(inode); /* We must guarantee clearing of inode... */ | |
77 | } | |
78 | ||
1da177e4 LT |
79 | typedef struct { |
80 | __le32 *p; | |
81 | __le32 key; | |
82 | struct buffer_head *bh; | |
83 | } Indirect; | |
84 | ||
85 | static inline void add_chain(Indirect *p, struct buffer_head *bh, __le32 *v) | |
86 | { | |
87 | p->key = *(p->p = v); | |
88 | p->bh = bh; | |
89 | } | |
90 | ||
91 | static inline int verify_chain(Indirect *from, Indirect *to) | |
92 | { | |
93 | while (from <= to && from->key == *from->p) | |
94 | from++; | |
95 | return (from > to); | |
96 | } | |
97 | ||
98 | /** | |
99 | * ext2_block_to_path - parse the block number into array of offsets | |
100 | * @inode: inode in question (we are only interested in its superblock) | |
101 | * @i_block: block number to be parsed | |
102 | * @offsets: array to store the offsets in | |
103 | * @boundary: set this non-zero if the referred-to block is likely to be | |
104 | * followed (on disk) by an indirect block. | |
105 | * To store the locations of file's data ext2 uses a data structure common | |
106 | * for UNIX filesystems - tree of pointers anchored in the inode, with | |
107 | * data blocks at leaves and indirect blocks in intermediate nodes. | |
108 | * This function translates the block number into path in that tree - | |
109 | * return value is the path length and @offsets[n] is the offset of | |
110 | * pointer to (n+1)th node in the nth one. If @block is out of range | |
111 | * (negative or too large) warning is printed and zero returned. | |
112 | * | |
113 | * Note: function doesn't find node addresses, so no IO is needed. All | |
114 | * we need to know is the capacity of indirect blocks (taken from the | |
115 | * inode->i_sb). | |
116 | */ | |
117 | ||
118 | /* | |
119 | * Portability note: the last comparison (check that we fit into triple | |
120 | * indirect block) is spelled differently, because otherwise on an | |
121 | * architecture with 32-bit longs and 8Kb pages we might get into trouble | |
122 | * if our filesystem had 8Kb blocks. We might use long long, but that would | |
123 | * kill us on x86. Oh, well, at least the sign propagation does not matter - | |
124 | * i_block would have to be negative in the very beginning, so we would not | |
125 | * get there at all. | |
126 | */ | |
127 | ||
128 | static int ext2_block_to_path(struct inode *inode, | |
129 | long i_block, int offsets[4], int *boundary) | |
130 | { | |
131 | int ptrs = EXT2_ADDR_PER_BLOCK(inode->i_sb); | |
132 | int ptrs_bits = EXT2_ADDR_PER_BLOCK_BITS(inode->i_sb); | |
133 | const long direct_blocks = EXT2_NDIR_BLOCKS, | |
134 | indirect_blocks = ptrs, | |
135 | double_blocks = (1 << (ptrs_bits * 2)); | |
136 | int n = 0; | |
137 | int final = 0; | |
138 | ||
139 | if (i_block < 0) { | |
140 | ext2_warning (inode->i_sb, "ext2_block_to_path", "block < 0"); | |
141 | } else if (i_block < direct_blocks) { | |
142 | offsets[n++] = i_block; | |
143 | final = direct_blocks; | |
144 | } else if ( (i_block -= direct_blocks) < indirect_blocks) { | |
145 | offsets[n++] = EXT2_IND_BLOCK; | |
146 | offsets[n++] = i_block; | |
147 | final = ptrs; | |
148 | } else if ((i_block -= indirect_blocks) < double_blocks) { | |
149 | offsets[n++] = EXT2_DIND_BLOCK; | |
150 | offsets[n++] = i_block >> ptrs_bits; | |
151 | offsets[n++] = i_block & (ptrs - 1); | |
152 | final = ptrs; | |
153 | } else if (((i_block -= double_blocks) >> (ptrs_bits * 2)) < ptrs) { | |
154 | offsets[n++] = EXT2_TIND_BLOCK; | |
155 | offsets[n++] = i_block >> (ptrs_bits * 2); | |
156 | offsets[n++] = (i_block >> ptrs_bits) & (ptrs - 1); | |
157 | offsets[n++] = i_block & (ptrs - 1); | |
158 | final = ptrs; | |
159 | } else { | |
160 | ext2_warning (inode->i_sb, "ext2_block_to_path", "block > big"); | |
161 | } | |
162 | if (boundary) | |
a686cd89 MB |
163 | *boundary = final - 1 - (i_block & (ptrs - 1)); |
164 | ||
1da177e4 LT |
165 | return n; |
166 | } | |
167 | ||
168 | /** | |
169 | * ext2_get_branch - read the chain of indirect blocks leading to data | |
170 | * @inode: inode in question | |
171 | * @depth: depth of the chain (1 - direct pointer, etc.) | |
172 | * @offsets: offsets of pointers in inode/indirect blocks | |
173 | * @chain: place to store the result | |
174 | * @err: here we store the error value | |
175 | * | |
176 | * Function fills the array of triples <key, p, bh> and returns %NULL | |
177 | * if everything went OK or the pointer to the last filled triple | |
178 | * (incomplete one) otherwise. Upon the return chain[i].key contains | |
179 | * the number of (i+1)-th block in the chain (as it is stored in memory, | |
180 | * i.e. little-endian 32-bit), chain[i].p contains the address of that | |
181 | * number (it points into struct inode for i==0 and into the bh->b_data | |
182 | * for i>0) and chain[i].bh points to the buffer_head of i-th indirect | |
183 | * block for i>0 and NULL for i==0. In other words, it holds the block | |
184 | * numbers of the chain, addresses they were taken from (and where we can | |
185 | * verify that chain did not change) and buffer_heads hosting these | |
186 | * numbers. | |
187 | * | |
188 | * Function stops when it stumbles upon zero pointer (absent block) | |
189 | * (pointer to last triple returned, *@err == 0) | |
190 | * or when it gets an IO error reading an indirect block | |
191 | * (ditto, *@err == -EIO) | |
192 | * or when it notices that chain had been changed while it was reading | |
193 | * (ditto, *@err == -EAGAIN) | |
194 | * or when it reads all @depth-1 indirect blocks successfully and finds | |
195 | * the whole chain, all way to the data (returns %NULL, *err == 0). | |
196 | */ | |
2557c31c JA |
197 | static Indirect *ext2_get_branch(struct inode *inode, int depth, |
198 | int *offsets, Indirect chain[4], | |
199 | int *err, struct wait_bit_queue *wait) | |
1da177e4 LT |
200 | { |
201 | struct super_block *sb = inode->i_sb; | |
202 | Indirect *p = chain; | |
203 | struct buffer_head *bh; | |
204 | ||
205 | *err = 0; | |
206 | /* i_data is not going away, no lock needed */ | |
207 | add_chain (chain, NULL, EXT2_I(inode)->i_data + *offsets); | |
208 | if (!p->key) | |
209 | goto no_block; | |
210 | while (--depth) { | |
2557c31c JA |
211 | bh = sb_bread_async(sb, le32_to_cpu(p->key), wait); |
212 | if (!bh || IS_ERR(bh)) | |
1da177e4 LT |
213 | goto failure; |
214 | read_lock(&EXT2_I(inode)->i_meta_lock); | |
215 | if (!verify_chain(chain, p)) | |
216 | goto changed; | |
217 | add_chain(++p, bh, (__le32*)bh->b_data + *++offsets); | |
218 | read_unlock(&EXT2_I(inode)->i_meta_lock); | |
219 | if (!p->key) | |
220 | goto no_block; | |
221 | } | |
222 | return NULL; | |
223 | ||
224 | changed: | |
225 | read_unlock(&EXT2_I(inode)->i_meta_lock); | |
226 | brelse(bh); | |
227 | *err = -EAGAIN; | |
228 | goto no_block; | |
229 | failure: | |
2557c31c JA |
230 | if (IS_ERR(bh)) |
231 | *err = PTR_ERR(bh); | |
232 | else | |
233 | *err = -EIO; | |
1da177e4 LT |
234 | no_block: |
235 | return p; | |
236 | } | |
237 | ||
238 | /** | |
239 | * ext2_find_near - find a place for allocation with sufficient locality | |
240 | * @inode: owner | |
241 | * @ind: descriptor of indirect block. | |
242 | * | |
1cc8dcf5 | 243 | * This function returns the preferred place for block allocation. |
1da177e4 LT |
244 | * It is used when heuristic for sequential allocation fails. |
245 | * Rules are: | |
246 | * + if there is a block to the left of our position - allocate near it. | |
247 | * + if pointer will live in indirect block - allocate near that block. | |
248 | * + if pointer will live in inode - allocate in the same cylinder group. | |
249 | * | |
250 | * In the latter case we colour the starting block by the callers PID to | |
251 | * prevent it from clashing with concurrent allocations for a different inode | |
252 | * in the same block group. The PID is used here so that functionally related | |
253 | * files will be close-by on-disk. | |
254 | * | |
255 | * Caller must make sure that @ind is valid and will stay that way. | |
256 | */ | |
257 | ||
4c8b3125 | 258 | static ext2_fsblk_t ext2_find_near(struct inode *inode, Indirect *ind) |
1da177e4 LT |
259 | { |
260 | struct ext2_inode_info *ei = EXT2_I(inode); | |
261 | __le32 *start = ind->bh ? (__le32 *) ind->bh->b_data : ei->i_data; | |
262 | __le32 *p; | |
4c8b3125 AM |
263 | ext2_fsblk_t bg_start; |
264 | ext2_fsblk_t colour; | |
1da177e4 LT |
265 | |
266 | /* Try to find previous block */ | |
267 | for (p = ind->p - 1; p >= start; p--) | |
268 | if (*p) | |
269 | return le32_to_cpu(*p); | |
270 | ||
271 | /* No such thing, so let's try location of indirect block */ | |
272 | if (ind->bh) | |
273 | return ind->bh->b_blocknr; | |
274 | ||
275 | /* | |
276 | * It is going to be refered from inode itself? OK, just put it into | |
277 | * the same cylinder group then. | |
278 | */ | |
24097d12 | 279 | bg_start = ext2_group_first_block_no(inode->i_sb, ei->i_block_group); |
1da177e4 LT |
280 | colour = (current->pid % 16) * |
281 | (EXT2_BLOCKS_PER_GROUP(inode->i_sb) / 16); | |
282 | return bg_start + colour; | |
283 | } | |
284 | ||
285 | /** | |
1cc8dcf5 | 286 | * ext2_find_goal - find a preferred place for allocation. |
1da177e4 LT |
287 | * @inode: owner |
288 | * @block: block we want | |
1da177e4 | 289 | * @partial: pointer to the last triple within a chain |
1da177e4 | 290 | * |
a686cd89 | 291 | * Returns preferred place for a block (the goal). |
1da177e4 LT |
292 | */ |
293 | ||
4c8b3125 AM |
294 | static inline ext2_fsblk_t ext2_find_goal(struct inode *inode, long block, |
295 | Indirect *partial) | |
1da177e4 | 296 | { |
a686cd89 MB |
297 | struct ext2_block_alloc_info *block_i; |
298 | ||
299 | block_i = EXT2_I(inode)->i_block_alloc_info; | |
300 | ||
301 | /* | |
302 | * try the heuristic for sequential allocation, | |
303 | * failing that at least try to get decent locality. | |
304 | */ | |
305 | if (block_i && (block == block_i->last_alloc_logical_block + 1) | |
306 | && (block_i->last_alloc_physical_block != 0)) { | |
307 | return block_i->last_alloc_physical_block + 1; | |
308 | } | |
309 | ||
310 | return ext2_find_near(inode, partial); | |
311 | } | |
312 | ||
313 | /** | |
314 | * ext2_blks_to_allocate: Look up the block map and count the number | |
315 | * of direct blocks need to be allocated for the given branch. | |
316 | * | |
317 | * @branch: chain of indirect blocks | |
318 | * @k: number of blocks need for indirect blocks | |
319 | * @blks: number of data blocks to be mapped. | |
320 | * @blocks_to_boundary: the offset in the indirect block | |
321 | * | |
322 | * return the total number of blocks to be allocate, including the | |
323 | * direct and indirect blocks. | |
324 | */ | |
325 | static int | |
326 | ext2_blks_to_allocate(Indirect * branch, int k, unsigned long blks, | |
327 | int blocks_to_boundary) | |
328 | { | |
329 | unsigned long count = 0; | |
330 | ||
331 | /* | |
332 | * Simple case, [t,d]Indirect block(s) has not allocated yet | |
333 | * then it's clear blocks on that path have not allocated | |
334 | */ | |
335 | if (k > 0) { | |
336 | /* right now don't hanel cross boundary allocation */ | |
337 | if (blks < blocks_to_boundary + 1) | |
338 | count += blks; | |
339 | else | |
340 | count += blocks_to_boundary + 1; | |
341 | return count; | |
1da177e4 | 342 | } |
a686cd89 MB |
343 | |
344 | count++; | |
345 | while (count < blks && count <= blocks_to_boundary | |
346 | && le32_to_cpu(*(branch[0].p + count)) == 0) { | |
347 | count++; | |
348 | } | |
349 | return count; | |
350 | } | |
351 | ||
352 | /** | |
353 | * ext2_alloc_blocks: multiple allocate blocks needed for a branch | |
354 | * @indirect_blks: the number of blocks need to allocate for indirect | |
355 | * blocks | |
356 | * | |
357 | * @new_blocks: on return it will store the new block numbers for | |
358 | * the indirect blocks(if needed) and the first direct block, | |
359 | * @blks: on return it will store the total number of allocated | |
360 | * direct blocks | |
361 | */ | |
362 | static int ext2_alloc_blocks(struct inode *inode, | |
363 | ext2_fsblk_t goal, int indirect_blks, int blks, | |
364 | ext2_fsblk_t new_blocks[4], int *err) | |
365 | { | |
366 | int target, i; | |
367 | unsigned long count = 0; | |
368 | int index = 0; | |
369 | ext2_fsblk_t current_block = 0; | |
370 | int ret = 0; | |
371 | ||
372 | /* | |
373 | * Here we try to allocate the requested multiple blocks at once, | |
374 | * on a best-effort basis. | |
375 | * To build a branch, we should allocate blocks for | |
376 | * the indirect blocks(if not allocated yet), and at least | |
377 | * the first direct block of this branch. That's the | |
378 | * minimum number of blocks need to allocate(required) | |
379 | */ | |
380 | target = blks + indirect_blks; | |
381 | ||
382 | while (1) { | |
383 | count = target; | |
384 | /* allocating blocks for indirect blocks and direct blocks */ | |
385 | current_block = ext2_new_blocks(inode,goal,&count,err); | |
386 | if (*err) | |
387 | goto failed_out; | |
388 | ||
389 | target -= count; | |
390 | /* allocate blocks for indirect blocks */ | |
391 | while (index < indirect_blks && count) { | |
392 | new_blocks[index++] = current_block++; | |
393 | count--; | |
394 | } | |
395 | ||
396 | if (count > 0) | |
397 | break; | |
398 | } | |
399 | ||
400 | /* save the new block number for the first direct block */ | |
401 | new_blocks[index] = current_block; | |
402 | ||
403 | /* total number of blocks allocated for direct blocks */ | |
404 | ret = count; | |
405 | *err = 0; | |
406 | return ret; | |
407 | failed_out: | |
408 | for (i = 0; i <index; i++) | |
409 | ext2_free_blocks(inode, new_blocks[i], 1); | |
410 | return ret; | |
1da177e4 LT |
411 | } |
412 | ||
413 | /** | |
414 | * ext2_alloc_branch - allocate and set up a chain of blocks. | |
415 | * @inode: owner | |
416 | * @num: depth of the chain (number of blocks to allocate) | |
417 | * @offsets: offsets (in the blocks) to store the pointers to next. | |
418 | * @branch: place to store the chain in. | |
419 | * | |
420 | * This function allocates @num blocks, zeroes out all but the last one, | |
421 | * links them into chain and (if we are synchronous) writes them to disk. | |
422 | * In other words, it prepares a branch that can be spliced onto the | |
423 | * inode. It stores the information about that chain in the branch[], in | |
424 | * the same format as ext2_get_branch() would do. We are calling it after | |
425 | * we had read the existing part of chain and partial points to the last | |
426 | * triple of that (one with zero ->key). Upon the exit we have the same | |
427 | * picture as after the successful ext2_get_block(), excpet that in one | |
428 | * place chain is disconnected - *branch->p is still zero (we did not | |
429 | * set the last link), but branch->key contains the number that should | |
430 | * be placed into *branch->p to fill that gap. | |
431 | * | |
432 | * If allocation fails we free all blocks we've allocated (and forget | |
433 | * their buffer_heads) and return the error value the from failed | |
434 | * ext2_alloc_block() (normally -ENOSPC). Otherwise we set the chain | |
435 | * as described above and return 0. | |
436 | */ | |
437 | ||
438 | static int ext2_alloc_branch(struct inode *inode, | |
a686cd89 MB |
439 | int indirect_blks, int *blks, ext2_fsblk_t goal, |
440 | int *offsets, Indirect *branch) | |
1da177e4 LT |
441 | { |
442 | int blocksize = inode->i_sb->s_blocksize; | |
a686cd89 MB |
443 | int i, n = 0; |
444 | int err = 0; | |
445 | struct buffer_head *bh; | |
446 | int num; | |
447 | ext2_fsblk_t new_blocks[4]; | |
448 | ext2_fsblk_t current_block; | |
449 | ||
450 | num = ext2_alloc_blocks(inode, goal, indirect_blks, | |
451 | *blks, new_blocks, &err); | |
452 | if (err) | |
453 | return err; | |
454 | ||
455 | branch[0].key = cpu_to_le32(new_blocks[0]); | |
456 | /* | |
457 | * metadata blocks and data blocks are allocated. | |
458 | */ | |
459 | for (n = 1; n <= indirect_blks; n++) { | |
1da177e4 | 460 | /* |
a686cd89 MB |
461 | * Get buffer_head for parent block, zero it out |
462 | * and set the pointer to new one, then send | |
463 | * parent to disk. | |
1da177e4 | 464 | */ |
a686cd89 MB |
465 | bh = sb_getblk(inode->i_sb, new_blocks[n-1]); |
466 | branch[n].bh = bh; | |
1da177e4 LT |
467 | lock_buffer(bh); |
468 | memset(bh->b_data, 0, blocksize); | |
1da177e4 | 469 | branch[n].p = (__le32 *) bh->b_data + offsets[n]; |
a686cd89 | 470 | branch[n].key = cpu_to_le32(new_blocks[n]); |
1da177e4 | 471 | *branch[n].p = branch[n].key; |
a686cd89 MB |
472 | if ( n == indirect_blks) { |
473 | current_block = new_blocks[n]; | |
474 | /* | |
475 | * End of chain, update the last new metablock of | |
476 | * the chain to point to the new allocated | |
477 | * data blocks numbers | |
478 | */ | |
479 | for (i=1; i < num; i++) | |
480 | *(branch[n].p + i) = cpu_to_le32(++current_block); | |
481 | } | |
1da177e4 LT |
482 | set_buffer_uptodate(bh); |
483 | unlock_buffer(bh); | |
484 | mark_buffer_dirty_inode(bh, inode); | |
485 | /* We used to sync bh here if IS_SYNC(inode). | |
a2a735ad | 486 | * But we now rely upon generic_write_sync() |
1da177e4 LT |
487 | * and b_inode_buffers. But not for directories. |
488 | */ | |
489 | if (S_ISDIR(inode->i_mode) && IS_DIRSYNC(inode)) | |
490 | sync_dirty_buffer(bh); | |
1da177e4 | 491 | } |
a686cd89 | 492 | *blks = num; |
1da177e4 LT |
493 | return err; |
494 | } | |
495 | ||
496 | /** | |
a686cd89 MB |
497 | * ext2_splice_branch - splice the allocated branch onto inode. |
498 | * @inode: owner | |
499 | * @block: (logical) number of block we are adding | |
a686cd89 MB |
500 | * @where: location of missing link |
501 | * @num: number of indirect blocks we are adding | |
502 | * @blks: number of direct blocks we are adding | |
1da177e4 | 503 | * |
a686cd89 MB |
504 | * This function fills the missing link and does all housekeeping needed in |
505 | * inode (->i_blocks, etc.). In case of success we end up with the full | |
506 | * chain to new block and return 0. | |
1da177e4 | 507 | */ |
a686cd89 MB |
508 | static void ext2_splice_branch(struct inode *inode, |
509 | long block, Indirect *where, int num, int blks) | |
1da177e4 | 510 | { |
1da177e4 | 511 | int i; |
a686cd89 MB |
512 | struct ext2_block_alloc_info *block_i; |
513 | ext2_fsblk_t current_block; | |
1da177e4 | 514 | |
a686cd89 | 515 | block_i = EXT2_I(inode)->i_block_alloc_info; |
1da177e4 | 516 | |
a686cd89 | 517 | /* XXX LOCKING probably should have i_meta_lock ?*/ |
1da177e4 LT |
518 | /* That's it */ |
519 | ||
520 | *where->p = where->key; | |
1da177e4 | 521 | |
a686cd89 MB |
522 | /* |
523 | * Update the host buffer_head or inode to point to more just allocated | |
524 | * direct blocks blocks | |
525 | */ | |
526 | if (num == 0 && blks > 1) { | |
527 | current_block = le32_to_cpu(where->key) + 1; | |
528 | for (i = 1; i < blks; i++) | |
529 | *(where->p + i ) = cpu_to_le32(current_block++); | |
530 | } | |
1da177e4 | 531 | |
a686cd89 MB |
532 | /* |
533 | * update the most recently allocated logical & physical block | |
534 | * in i_block_alloc_info, to assist find the proper goal block for next | |
535 | * allocation | |
536 | */ | |
537 | if (block_i) { | |
538 | block_i->last_alloc_logical_block = block + blks - 1; | |
539 | block_i->last_alloc_physical_block = | |
540 | le32_to_cpu(where[num].key) + blks - 1; | |
541 | } | |
1da177e4 | 542 | |
a686cd89 | 543 | /* We are done with atomic stuff, now do the rest of housekeeping */ |
1da177e4 LT |
544 | |
545 | /* had we spliced it onto indirect block? */ | |
546 | if (where->bh) | |
547 | mark_buffer_dirty_inode(where->bh, inode); | |
548 | ||
a686cd89 | 549 | inode->i_ctime = CURRENT_TIME_SEC; |
1da177e4 | 550 | mark_inode_dirty(inode); |
1da177e4 LT |
551 | } |
552 | ||
553 | /* | |
554 | * Allocation strategy is simple: if we have to allocate something, we will | |
555 | * have to go the whole way to leaf. So let's do it before attaching anything | |
556 | * to tree, set linkage between the newborn blocks, write them if sync is | |
557 | * required, recheck the path, free and repeat if check fails, otherwise | |
558 | * set the last missing link (that will protect us from any truncate-generated | |
559 | * removals - all blocks on the path are immune now) and possibly force the | |
560 | * write on the parent block. | |
561 | * That has a nice additional property: no special recovery from the failed | |
562 | * allocations is needed - we simply release blocks and do not touch anything | |
563 | * reachable from inode. | |
a686cd89 MB |
564 | * |
565 | * `handle' can be NULL if create == 0. | |
566 | * | |
a686cd89 MB |
567 | * return > 0, # of blocks mapped or allocated. |
568 | * return = 0, if plain lookup failed. | |
569 | * return < 0, error case. | |
1da177e4 | 570 | */ |
2557c31c JA |
571 | static int ext2_get_blocks(struct inode *inode, sector_t iblock, |
572 | unsigned long maxblocks, | |
573 | struct buffer_head *bh_result, int create, | |
574 | struct wait_bit_queue *wait) | |
1da177e4 LT |
575 | { |
576 | int err = -EIO; | |
577 | int offsets[4]; | |
578 | Indirect chain[4]; | |
579 | Indirect *partial; | |
a686cd89 MB |
580 | ext2_fsblk_t goal; |
581 | int indirect_blks; | |
582 | int blocks_to_boundary = 0; | |
583 | int depth; | |
584 | struct ext2_inode_info *ei = EXT2_I(inode); | |
585 | int count = 0; | |
586 | ext2_fsblk_t first_block = 0; | |
1da177e4 | 587 | |
a686cd89 | 588 | depth = ext2_block_to_path(inode,iblock,offsets,&blocks_to_boundary); |
1da177e4 | 589 | |
a686cd89 MB |
590 | if (depth == 0) |
591 | return (err); | |
1da177e4 | 592 | |
2557c31c | 593 | partial = ext2_get_branch(inode, depth, offsets, chain, &err, wait); |
1da177e4 LT |
594 | /* Simplest case - block found, no allocation needed */ |
595 | if (!partial) { | |
a686cd89 MB |
596 | first_block = le32_to_cpu(chain[depth - 1].key); |
597 | clear_buffer_new(bh_result); /* What's this do? */ | |
598 | count++; | |
599 | /*map more blocks*/ | |
600 | while (count < maxblocks && count <= blocks_to_boundary) { | |
601 | ext2_fsblk_t blk; | |
602 | ||
316cb4ef | 603 | if (!verify_chain(chain, chain + depth - 1)) { |
a686cd89 MB |
604 | /* |
605 | * Indirect block might be removed by | |
606 | * truncate while we were reading it. | |
607 | * Handling of that case: forget what we've | |
608 | * got now, go to reread. | |
609 | */ | |
316cb4ef | 610 | err = -EAGAIN; |
a686cd89 | 611 | count = 0; |
316cb4ef | 612 | break; |
a686cd89 MB |
613 | } |
614 | blk = le32_to_cpu(*(chain[depth-1].p + count)); | |
615 | if (blk == first_block + count) | |
616 | count++; | |
617 | else | |
618 | break; | |
619 | } | |
316cb4ef JK |
620 | if (err != -EAGAIN) |
621 | goto got_it; | |
1da177e4 LT |
622 | } |
623 | ||
624 | /* Next simple case - plain lookup or failed read of indirect block */ | |
2557c31c | 625 | if (!create || err == -EIO || err == -EIOCBRETRY) |
a686cd89 MB |
626 | goto cleanup; |
627 | ||
628 | mutex_lock(&ei->truncate_mutex); | |
316cb4ef JK |
629 | /* |
630 | * If the indirect block is missing while we are reading | |
631 | * the chain(ext3_get_branch() returns -EAGAIN err), or | |
632 | * if the chain has been changed after we grab the semaphore, | |
633 | * (either because another process truncated this branch, or | |
634 | * another get_block allocated this branch) re-grab the chain to see if | |
635 | * the request block has been allocated or not. | |
636 | * | |
637 | * Since we already block the truncate/other get_block | |
638 | * at this point, we will have the current copy of the chain when we | |
639 | * splice the branch into the tree. | |
640 | */ | |
641 | if (err == -EAGAIN || !verify_chain(chain, partial)) { | |
642 | while (partial > chain) { | |
643 | brelse(partial->bh); | |
644 | partial--; | |
645 | } | |
2557c31c JA |
646 | partial = ext2_get_branch(inode, depth, offsets, chain, &err, |
647 | wait); | |
316cb4ef JK |
648 | if (!partial) { |
649 | count++; | |
650 | mutex_unlock(&ei->truncate_mutex); | |
651 | if (err) | |
652 | goto cleanup; | |
653 | clear_buffer_new(bh_result); | |
654 | goto got_it; | |
655 | } | |
656 | } | |
1da177e4 LT |
657 | |
658 | /* | |
a686cd89 MB |
659 | * Okay, we need to do block allocation. Lazily initialize the block |
660 | * allocation info here if necessary | |
661 | */ | |
662 | if (S_ISREG(inode->i_mode) && (!ei->i_block_alloc_info)) | |
663 | ext2_init_block_alloc_info(inode); | |
1da177e4 | 664 | |
fb01bfda | 665 | goal = ext2_find_goal(inode, iblock, partial); |
1da177e4 | 666 | |
a686cd89 MB |
667 | /* the number of blocks need to allocate for [d,t]indirect blocks */ |
668 | indirect_blks = (chain + depth) - partial - 1; | |
669 | /* | |
670 | * Next look up the indirect map to count the totoal number of | |
671 | * direct blocks to allocate for this branch. | |
672 | */ | |
673 | count = ext2_blks_to_allocate(partial, indirect_blks, | |
674 | maxblocks, blocks_to_boundary); | |
675 | /* | |
676 | * XXX ???? Block out ext2_truncate while we alter the tree | |
677 | */ | |
678 | err = ext2_alloc_branch(inode, indirect_blks, &count, goal, | |
679 | offsets + (partial - chain), partial); | |
680 | ||
681 | if (err) { | |
682 | mutex_unlock(&ei->truncate_mutex); | |
1da177e4 | 683 | goto cleanup; |
a686cd89 | 684 | } |
1da177e4 | 685 | |
6d79125b CO |
686 | if (ext2_use_xip(inode->i_sb)) { |
687 | /* | |
688 | * we need to clear the block | |
689 | */ | |
690 | err = ext2_clear_xip_target (inode, | |
691 | le32_to_cpu(chain[depth-1].key)); | |
a686cd89 MB |
692 | if (err) { |
693 | mutex_unlock(&ei->truncate_mutex); | |
6d79125b | 694 | goto cleanup; |
a686cd89 | 695 | } |
6d79125b CO |
696 | } |
697 | ||
a686cd89 MB |
698 | ext2_splice_branch(inode, iblock, partial, indirect_blks, count); |
699 | mutex_unlock(&ei->truncate_mutex); | |
1da177e4 | 700 | set_buffer_new(bh_result); |
a686cd89 MB |
701 | got_it: |
702 | map_bh(bh_result, inode->i_sb, le32_to_cpu(chain[depth-1].key)); | |
703 | if (count > blocks_to_boundary) | |
704 | set_buffer_boundary(bh_result); | |
705 | err = count; | |
706 | /* Clean up and exit */ | |
707 | partial = chain + depth - 1; /* the whole chain */ | |
708 | cleanup: | |
709 | while (partial > chain) { | |
710 | brelse(partial->bh); | |
711 | partial--; | |
712 | } | |
713 | return err; | |
1da177e4 LT |
714 | } |
715 | ||
a686cd89 MB |
716 | int ext2_get_block(struct inode *inode, sector_t iblock, struct buffer_head *bh_result, int create) |
717 | { | |
718 | unsigned max_blocks = bh_result->b_size >> inode->i_blkbits; | |
719 | int ret = ext2_get_blocks(inode, iblock, max_blocks, | |
2557c31c | 720 | bh_result, create, NULL); |
a686cd89 MB |
721 | if (ret > 0) { |
722 | bh_result->b_size = (ret << inode->i_blkbits); | |
723 | ret = 0; | |
724 | } | |
725 | return ret; | |
726 | ||
727 | } | |
728 | ||
2557c31c JA |
729 | int ext2_get_block_async(struct inode *inode, sector_t iblock, |
730 | struct buffer_head *bh_result, int create) | |
731 | { | |
732 | unsigned max_blocks = bh_result->b_size >> inode->i_blkbits; | |
733 | int ret = ext2_get_blocks(inode, iblock, max_blocks, | |
734 | bh_result, create, current->io_wait); | |
735 | if (ret > 0) { | |
736 | bh_result->b_size = (ret << inode->i_blkbits); | |
737 | ret = 0; | |
738 | } | |
739 | return ret; | |
740 | ||
741 | } | |
68c9d702 JB |
742 | int ext2_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
743 | u64 start, u64 len) | |
744 | { | |
745 | return generic_block_fiemap(inode, fieinfo, start, len, | |
746 | ext2_get_block); | |
747 | } | |
748 | ||
1da177e4 LT |
749 | static int ext2_writepage(struct page *page, struct writeback_control *wbc) |
750 | { | |
751 | return block_write_full_page(page, ext2_get_block, wbc); | |
752 | } | |
753 | ||
754 | static int ext2_readpage(struct file *file, struct page *page) | |
755 | { | |
756 | return mpage_readpage(page, ext2_get_block); | |
757 | } | |
758 | ||
759 | static int | |
760 | ext2_readpages(struct file *file, struct address_space *mapping, | |
761 | struct list_head *pages, unsigned nr_pages) | |
762 | { | |
763 | return mpage_readpages(mapping, pages, nr_pages, ext2_get_block); | |
764 | } | |
765 | ||
f34fb6ec NP |
766 | int __ext2_write_begin(struct file *file, struct address_space *mapping, |
767 | loff_t pos, unsigned len, unsigned flags, | |
768 | struct page **pagep, void **fsdata) | |
1da177e4 | 769 | { |
f34fb6ec | 770 | return block_write_begin(file, mapping, pos, len, flags, pagep, fsdata, |
2557c31c | 771 | ext2_get_block_async); |
1da177e4 LT |
772 | } |
773 | ||
774 | static int | |
f34fb6ec NP |
775 | ext2_write_begin(struct file *file, struct address_space *mapping, |
776 | loff_t pos, unsigned len, unsigned flags, | |
777 | struct page **pagep, void **fsdata) | |
1da177e4 | 778 | { |
f34fb6ec NP |
779 | *pagep = NULL; |
780 | return __ext2_write_begin(file, mapping, pos, len, flags, pagep,fsdata); | |
1da177e4 LT |
781 | } |
782 | ||
03158cd7 NP |
783 | static int |
784 | ext2_nobh_write_begin(struct file *file, struct address_space *mapping, | |
785 | loff_t pos, unsigned len, unsigned flags, | |
786 | struct page **pagep, void **fsdata) | |
787 | { | |
788 | /* | |
789 | * Dir-in-pagecache still uses ext2_write_begin. Would have to rework | |
790 | * directory handling code to pass around offsets rather than struct | |
791 | * pages in order to make this work easily. | |
792 | */ | |
793 | return nobh_write_begin(file, mapping, pos, len, flags, pagep, fsdata, | |
2557c31c | 794 | ext2_get_block_async); |
03158cd7 NP |
795 | } |
796 | ||
1da177e4 LT |
797 | static int ext2_nobh_writepage(struct page *page, |
798 | struct writeback_control *wbc) | |
799 | { | |
800 | return nobh_writepage(page, ext2_get_block, wbc); | |
801 | } | |
802 | ||
803 | static sector_t ext2_bmap(struct address_space *mapping, sector_t block) | |
804 | { | |
805 | return generic_block_bmap(mapping,block,ext2_get_block); | |
806 | } | |
807 | ||
1da177e4 LT |
808 | static ssize_t |
809 | ext2_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov, | |
810 | loff_t offset, unsigned long nr_segs) | |
811 | { | |
812 | struct file *file = iocb->ki_filp; | |
813 | struct inode *inode = file->f_mapping->host; | |
814 | ||
815 | return blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov, | |
1d8fa7a2 | 816 | offset, nr_segs, ext2_get_block, NULL); |
1da177e4 LT |
817 | } |
818 | ||
819 | static int | |
820 | ext2_writepages(struct address_space *mapping, struct writeback_control *wbc) | |
821 | { | |
822 | return mpage_writepages(mapping, wbc, ext2_get_block); | |
823 | } | |
824 | ||
f5e54d6e | 825 | const struct address_space_operations ext2_aops = { |
1da177e4 LT |
826 | .readpage = ext2_readpage, |
827 | .readpages = ext2_readpages, | |
828 | .writepage = ext2_writepage, | |
829 | .sync_page = block_sync_page, | |
f34fb6ec NP |
830 | .write_begin = ext2_write_begin, |
831 | .write_end = generic_write_end, | |
1da177e4 LT |
832 | .bmap = ext2_bmap, |
833 | .direct_IO = ext2_direct_IO, | |
834 | .writepages = ext2_writepages, | |
e965f963 | 835 | .migratepage = buffer_migrate_page, |
8ab22b9a | 836 | .is_partially_uptodate = block_is_partially_uptodate, |
aa261f54 | 837 | .error_remove_page = generic_error_remove_page, |
1da177e4 LT |
838 | }; |
839 | ||
f5e54d6e | 840 | const struct address_space_operations ext2_aops_xip = { |
6d79125b | 841 | .bmap = ext2_bmap, |
70688e4d | 842 | .get_xip_mem = ext2_get_xip_mem, |
6d79125b CO |
843 | }; |
844 | ||
f5e54d6e | 845 | const struct address_space_operations ext2_nobh_aops = { |
1da177e4 LT |
846 | .readpage = ext2_readpage, |
847 | .readpages = ext2_readpages, | |
848 | .writepage = ext2_nobh_writepage, | |
849 | .sync_page = block_sync_page, | |
03158cd7 NP |
850 | .write_begin = ext2_nobh_write_begin, |
851 | .write_end = nobh_write_end, | |
1da177e4 LT |
852 | .bmap = ext2_bmap, |
853 | .direct_IO = ext2_direct_IO, | |
854 | .writepages = ext2_writepages, | |
e965f963 | 855 | .migratepage = buffer_migrate_page, |
aa261f54 | 856 | .error_remove_page = generic_error_remove_page, |
1da177e4 LT |
857 | }; |
858 | ||
859 | /* | |
860 | * Probably it should be a library function... search for first non-zero word | |
861 | * or memcmp with zero_page, whatever is better for particular architecture. | |
862 | * Linus? | |
863 | */ | |
864 | static inline int all_zeroes(__le32 *p, __le32 *q) | |
865 | { | |
866 | while (p < q) | |
867 | if (*p++) | |
868 | return 0; | |
869 | return 1; | |
870 | } | |
871 | ||
872 | /** | |
873 | * ext2_find_shared - find the indirect blocks for partial truncation. | |
874 | * @inode: inode in question | |
875 | * @depth: depth of the affected branch | |
876 | * @offsets: offsets of pointers in that branch (see ext2_block_to_path) | |
877 | * @chain: place to store the pointers to partial indirect blocks | |
878 | * @top: place to the (detached) top of branch | |
879 | * | |
880 | * This is a helper function used by ext2_truncate(). | |
881 | * | |
882 | * When we do truncate() we may have to clean the ends of several indirect | |
883 | * blocks but leave the blocks themselves alive. Block is partially | |
884 | * truncated if some data below the new i_size is refered from it (and | |
885 | * it is on the path to the first completely truncated data block, indeed). | |
886 | * We have to free the top of that path along with everything to the right | |
887 | * of the path. Since no allocation past the truncation point is possible | |
888 | * until ext2_truncate() finishes, we may safely do the latter, but top | |
889 | * of branch may require special attention - pageout below the truncation | |
890 | * point might try to populate it. | |
891 | * | |
892 | * We atomically detach the top of branch from the tree, store the block | |
893 | * number of its root in *@top, pointers to buffer_heads of partially | |
894 | * truncated blocks - in @chain[].bh and pointers to their last elements | |
895 | * that should not be removed - in @chain[].p. Return value is the pointer | |
896 | * to last filled element of @chain. | |
897 | * | |
898 | * The work left to caller to do the actual freeing of subtrees: | |
899 | * a) free the subtree starting from *@top | |
900 | * b) free the subtrees whose roots are stored in | |
901 | * (@chain[i].p+1 .. end of @chain[i].bh->b_data) | |
902 | * c) free the subtrees growing from the inode past the @chain[0].p | |
903 | * (no partially truncated stuff there). | |
904 | */ | |
905 | ||
906 | static Indirect *ext2_find_shared(struct inode *inode, | |
907 | int depth, | |
908 | int offsets[4], | |
909 | Indirect chain[4], | |
910 | __le32 *top) | |
911 | { | |
912 | Indirect *partial, *p; | |
913 | int k, err; | |
914 | ||
915 | *top = 0; | |
916 | for (k = depth; k > 1 && !offsets[k-1]; k--) | |
917 | ; | |
2557c31c | 918 | partial = ext2_get_branch(inode, k, offsets, chain, &err, NULL); |
1da177e4 LT |
919 | if (!partial) |
920 | partial = chain + k-1; | |
921 | /* | |
922 | * If the branch acquired continuation since we've looked at it - | |
923 | * fine, it should all survive and (new) top doesn't belong to us. | |
924 | */ | |
925 | write_lock(&EXT2_I(inode)->i_meta_lock); | |
926 | if (!partial->key && *partial->p) { | |
927 | write_unlock(&EXT2_I(inode)->i_meta_lock); | |
928 | goto no_top; | |
929 | } | |
930 | for (p=partial; p>chain && all_zeroes((__le32*)p->bh->b_data,p->p); p--) | |
931 | ; | |
932 | /* | |
933 | * OK, we've found the last block that must survive. The rest of our | |
934 | * branch should be detached before unlocking. However, if that rest | |
935 | * of branch is all ours and does not grow immediately from the inode | |
936 | * it's easier to cheat and just decrement partial->p. | |
937 | */ | |
938 | if (p == chain + k - 1 && p > chain) { | |
939 | p->p--; | |
940 | } else { | |
941 | *top = *p->p; | |
942 | *p->p = 0; | |
943 | } | |
944 | write_unlock(&EXT2_I(inode)->i_meta_lock); | |
945 | ||
946 | while(partial > p) | |
947 | { | |
948 | brelse(partial->bh); | |
949 | partial--; | |
950 | } | |
951 | no_top: | |
952 | return partial; | |
953 | } | |
954 | ||
955 | /** | |
956 | * ext2_free_data - free a list of data blocks | |
957 | * @inode: inode we are dealing with | |
958 | * @p: array of block numbers | |
959 | * @q: points immediately past the end of array | |
960 | * | |
961 | * We are freeing all blocks refered from that array (numbers are | |
962 | * stored as little-endian 32-bit) and updating @inode->i_blocks | |
963 | * appropriately. | |
964 | */ | |
965 | static inline void ext2_free_data(struct inode *inode, __le32 *p, __le32 *q) | |
966 | { | |
967 | unsigned long block_to_free = 0, count = 0; | |
968 | unsigned long nr; | |
969 | ||
970 | for ( ; p < q ; p++) { | |
971 | nr = le32_to_cpu(*p); | |
972 | if (nr) { | |
973 | *p = 0; | |
974 | /* accumulate blocks to free if they're contiguous */ | |
975 | if (count == 0) | |
976 | goto free_this; | |
977 | else if (block_to_free == nr - count) | |
978 | count++; | |
979 | else { | |
980 | mark_inode_dirty(inode); | |
981 | ext2_free_blocks (inode, block_to_free, count); | |
982 | free_this: | |
983 | block_to_free = nr; | |
984 | count = 1; | |
985 | } | |
986 | } | |
987 | } | |
988 | if (count > 0) { | |
989 | mark_inode_dirty(inode); | |
990 | ext2_free_blocks (inode, block_to_free, count); | |
991 | } | |
992 | } | |
993 | ||
994 | /** | |
995 | * ext2_free_branches - free an array of branches | |
996 | * @inode: inode we are dealing with | |
997 | * @p: array of block numbers | |
998 | * @q: pointer immediately past the end of array | |
999 | * @depth: depth of the branches to free | |
1000 | * | |
1001 | * We are freeing all blocks refered from these branches (numbers are | |
1002 | * stored as little-endian 32-bit) and updating @inode->i_blocks | |
1003 | * appropriately. | |
1004 | */ | |
1005 | static void ext2_free_branches(struct inode *inode, __le32 *p, __le32 *q, int depth) | |
1006 | { | |
1007 | struct buffer_head * bh; | |
1008 | unsigned long nr; | |
1009 | ||
1010 | if (depth--) { | |
1011 | int addr_per_block = EXT2_ADDR_PER_BLOCK(inode->i_sb); | |
1012 | for ( ; p < q ; p++) { | |
1013 | nr = le32_to_cpu(*p); | |
1014 | if (!nr) | |
1015 | continue; | |
1016 | *p = 0; | |
1017 | bh = sb_bread(inode->i_sb, nr); | |
1018 | /* | |
1019 | * A read failure? Report error and clear slot | |
1020 | * (should be rare). | |
1021 | */ | |
1022 | if (!bh) { | |
1023 | ext2_error(inode->i_sb, "ext2_free_branches", | |
1024 | "Read failure, inode=%ld, block=%ld", | |
1025 | inode->i_ino, nr); | |
1026 | continue; | |
1027 | } | |
1028 | ext2_free_branches(inode, | |
1029 | (__le32*)bh->b_data, | |
1030 | (__le32*)bh->b_data + addr_per_block, | |
1031 | depth); | |
1032 | bforget(bh); | |
1033 | ext2_free_blocks(inode, nr, 1); | |
1034 | mark_inode_dirty(inode); | |
1035 | } | |
1036 | } else | |
1037 | ext2_free_data(inode, p, q); | |
1038 | } | |
1039 | ||
a686cd89 | 1040 | void ext2_truncate(struct inode *inode) |
1da177e4 LT |
1041 | { |
1042 | __le32 *i_data = EXT2_I(inode)->i_data; | |
a686cd89 | 1043 | struct ext2_inode_info *ei = EXT2_I(inode); |
1da177e4 LT |
1044 | int addr_per_block = EXT2_ADDR_PER_BLOCK(inode->i_sb); |
1045 | int offsets[4]; | |
1046 | Indirect chain[4]; | |
1047 | Indirect *partial; | |
1048 | __le32 nr = 0; | |
1049 | int n; | |
1050 | long iblock; | |
1051 | unsigned blocksize; | |
1052 | ||
1053 | if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || | |
1054 | S_ISLNK(inode->i_mode))) | |
1055 | return; | |
1056 | if (ext2_inode_is_fast_symlink(inode)) | |
1057 | return; | |
1058 | if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) | |
1059 | return; | |
1060 | ||
1da177e4 LT |
1061 | blocksize = inode->i_sb->s_blocksize; |
1062 | iblock = (inode->i_size + blocksize-1) | |
1063 | >> EXT2_BLOCK_SIZE_BITS(inode->i_sb); | |
1064 | ||
6d79125b CO |
1065 | if (mapping_is_xip(inode->i_mapping)) |
1066 | xip_truncate_page(inode->i_mapping, inode->i_size); | |
1067 | else if (test_opt(inode->i_sb, NOBH)) | |
03158cd7 NP |
1068 | nobh_truncate_page(inode->i_mapping, |
1069 | inode->i_size, ext2_get_block); | |
1da177e4 LT |
1070 | else |
1071 | block_truncate_page(inode->i_mapping, | |
1072 | inode->i_size, ext2_get_block); | |
1073 | ||
1074 | n = ext2_block_to_path(inode, iblock, offsets, NULL); | |
1075 | if (n == 0) | |
1076 | return; | |
1077 | ||
a686cd89 MB |
1078 | /* |
1079 | * From here we block out all ext2_get_block() callers who want to | |
1080 | * modify the block allocation tree. | |
1081 | */ | |
1082 | mutex_lock(&ei->truncate_mutex); | |
1083 | ||
1da177e4 LT |
1084 | if (n == 1) { |
1085 | ext2_free_data(inode, i_data+offsets[0], | |
1086 | i_data + EXT2_NDIR_BLOCKS); | |
1087 | goto do_indirects; | |
1088 | } | |
1089 | ||
1090 | partial = ext2_find_shared(inode, n, offsets, chain, &nr); | |
1091 | /* Kill the top of shared branch (already detached) */ | |
1092 | if (nr) { | |
1093 | if (partial == chain) | |
1094 | mark_inode_dirty(inode); | |
1095 | else | |
1096 | mark_buffer_dirty_inode(partial->bh, inode); | |
1097 | ext2_free_branches(inode, &nr, &nr+1, (chain+n-1) - partial); | |
1098 | } | |
1099 | /* Clear the ends of indirect blocks on the shared branch */ | |
1100 | while (partial > chain) { | |
1101 | ext2_free_branches(inode, | |
1102 | partial->p + 1, | |
1103 | (__le32*)partial->bh->b_data+addr_per_block, | |
1104 | (chain+n-1) - partial); | |
1105 | mark_buffer_dirty_inode(partial->bh, inode); | |
1106 | brelse (partial->bh); | |
1107 | partial--; | |
1108 | } | |
1109 | do_indirects: | |
1110 | /* Kill the remaining (whole) subtrees */ | |
1111 | switch (offsets[0]) { | |
1112 | default: | |
1113 | nr = i_data[EXT2_IND_BLOCK]; | |
1114 | if (nr) { | |
1115 | i_data[EXT2_IND_BLOCK] = 0; | |
1116 | mark_inode_dirty(inode); | |
1117 | ext2_free_branches(inode, &nr, &nr+1, 1); | |
1118 | } | |
1119 | case EXT2_IND_BLOCK: | |
1120 | nr = i_data[EXT2_DIND_BLOCK]; | |
1121 | if (nr) { | |
1122 | i_data[EXT2_DIND_BLOCK] = 0; | |
1123 | mark_inode_dirty(inode); | |
1124 | ext2_free_branches(inode, &nr, &nr+1, 2); | |
1125 | } | |
1126 | case EXT2_DIND_BLOCK: | |
1127 | nr = i_data[EXT2_TIND_BLOCK]; | |
1128 | if (nr) { | |
1129 | i_data[EXT2_TIND_BLOCK] = 0; | |
1130 | mark_inode_dirty(inode); | |
1131 | ext2_free_branches(inode, &nr, &nr+1, 3); | |
1132 | } | |
1133 | case EXT2_TIND_BLOCK: | |
1134 | ; | |
1135 | } | |
a686cd89 MB |
1136 | |
1137 | ext2_discard_reservation(inode); | |
1138 | ||
1139 | mutex_unlock(&ei->truncate_mutex); | |
1da177e4 LT |
1140 | inode->i_mtime = inode->i_ctime = CURRENT_TIME_SEC; |
1141 | if (inode_needs_sync(inode)) { | |
1142 | sync_mapping_buffers(inode->i_mapping); | |
1143 | ext2_sync_inode (inode); | |
1144 | } else { | |
1145 | mark_inode_dirty(inode); | |
1146 | } | |
1147 | } | |
1148 | ||
1149 | static struct ext2_inode *ext2_get_inode(struct super_block *sb, ino_t ino, | |
1150 | struct buffer_head **p) | |
1151 | { | |
1152 | struct buffer_head * bh; | |
1153 | unsigned long block_group; | |
1154 | unsigned long block; | |
1155 | unsigned long offset; | |
1156 | struct ext2_group_desc * gdp; | |
1157 | ||
1158 | *p = NULL; | |
1159 | if ((ino != EXT2_ROOT_INO && ino < EXT2_FIRST_INO(sb)) || | |
1160 | ino > le32_to_cpu(EXT2_SB(sb)->s_es->s_inodes_count)) | |
1161 | goto Einval; | |
1162 | ||
1163 | block_group = (ino - 1) / EXT2_INODES_PER_GROUP(sb); | |
ef2fb679 | 1164 | gdp = ext2_get_group_desc(sb, block_group, NULL); |
1da177e4 LT |
1165 | if (!gdp) |
1166 | goto Egdp; | |
1167 | /* | |
1168 | * Figure out the offset within the block group inode table | |
1169 | */ | |
1170 | offset = ((ino - 1) % EXT2_INODES_PER_GROUP(sb)) * EXT2_INODE_SIZE(sb); | |
1171 | block = le32_to_cpu(gdp->bg_inode_table) + | |
1172 | (offset >> EXT2_BLOCK_SIZE_BITS(sb)); | |
1173 | if (!(bh = sb_bread(sb, block))) | |
1174 | goto Eio; | |
1175 | ||
1176 | *p = bh; | |
1177 | offset &= (EXT2_BLOCK_SIZE(sb) - 1); | |
1178 | return (struct ext2_inode *) (bh->b_data + offset); | |
1179 | ||
1180 | Einval: | |
1181 | ext2_error(sb, "ext2_get_inode", "bad inode number: %lu", | |
1182 | (unsigned long) ino); | |
1183 | return ERR_PTR(-EINVAL); | |
1184 | Eio: | |
1185 | ext2_error(sb, "ext2_get_inode", | |
1186 | "unable to read inode block - inode=%lu, block=%lu", | |
1187 | (unsigned long) ino, block); | |
1188 | Egdp: | |
1189 | return ERR_PTR(-EIO); | |
1190 | } | |
1191 | ||
1192 | void ext2_set_inode_flags(struct inode *inode) | |
1193 | { | |
1194 | unsigned int flags = EXT2_I(inode)->i_flags; | |
1195 | ||
1196 | inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC); | |
1197 | if (flags & EXT2_SYNC_FL) | |
1198 | inode->i_flags |= S_SYNC; | |
1199 | if (flags & EXT2_APPEND_FL) | |
1200 | inode->i_flags |= S_APPEND; | |
1201 | if (flags & EXT2_IMMUTABLE_FL) | |
1202 | inode->i_flags |= S_IMMUTABLE; | |
1203 | if (flags & EXT2_NOATIME_FL) | |
1204 | inode->i_flags |= S_NOATIME; | |
1205 | if (flags & EXT2_DIRSYNC_FL) | |
1206 | inode->i_flags |= S_DIRSYNC; | |
1207 | } | |
1208 | ||
4f99ed67 JK |
1209 | /* Propagate flags from i_flags to EXT2_I(inode)->i_flags */ |
1210 | void ext2_get_inode_flags(struct ext2_inode_info *ei) | |
1211 | { | |
1212 | unsigned int flags = ei->vfs_inode.i_flags; | |
1213 | ||
1214 | ei->i_flags &= ~(EXT2_SYNC_FL|EXT2_APPEND_FL| | |
1215 | EXT2_IMMUTABLE_FL|EXT2_NOATIME_FL|EXT2_DIRSYNC_FL); | |
1216 | if (flags & S_SYNC) | |
1217 | ei->i_flags |= EXT2_SYNC_FL; | |
1218 | if (flags & S_APPEND) | |
1219 | ei->i_flags |= EXT2_APPEND_FL; | |
1220 | if (flags & S_IMMUTABLE) | |
1221 | ei->i_flags |= EXT2_IMMUTABLE_FL; | |
1222 | if (flags & S_NOATIME) | |
1223 | ei->i_flags |= EXT2_NOATIME_FL; | |
1224 | if (flags & S_DIRSYNC) | |
1225 | ei->i_flags |= EXT2_DIRSYNC_FL; | |
1226 | } | |
1227 | ||
52fcf703 | 1228 | struct inode *ext2_iget (struct super_block *sb, unsigned long ino) |
1da177e4 | 1229 | { |
52fcf703 | 1230 | struct ext2_inode_info *ei; |
1da177e4 | 1231 | struct buffer_head * bh; |
52fcf703 DH |
1232 | struct ext2_inode *raw_inode; |
1233 | struct inode *inode; | |
1234 | long ret = -EIO; | |
1da177e4 LT |
1235 | int n; |
1236 | ||
52fcf703 DH |
1237 | inode = iget_locked(sb, ino); |
1238 | if (!inode) | |
1239 | return ERR_PTR(-ENOMEM); | |
1240 | if (!(inode->i_state & I_NEW)) | |
1241 | return inode; | |
1242 | ||
1243 | ei = EXT2_I(inode); | |
a686cd89 MB |
1244 | ei->i_block_alloc_info = NULL; |
1245 | ||
52fcf703 DH |
1246 | raw_inode = ext2_get_inode(inode->i_sb, ino, &bh); |
1247 | if (IS_ERR(raw_inode)) { | |
1248 | ret = PTR_ERR(raw_inode); | |
1da177e4 | 1249 | goto bad_inode; |
52fcf703 | 1250 | } |
1da177e4 LT |
1251 | |
1252 | inode->i_mode = le16_to_cpu(raw_inode->i_mode); | |
1253 | inode->i_uid = (uid_t)le16_to_cpu(raw_inode->i_uid_low); | |
1254 | inode->i_gid = (gid_t)le16_to_cpu(raw_inode->i_gid_low); | |
1255 | if (!(test_opt (inode->i_sb, NO_UID32))) { | |
1256 | inode->i_uid |= le16_to_cpu(raw_inode->i_uid_high) << 16; | |
1257 | inode->i_gid |= le16_to_cpu(raw_inode->i_gid_high) << 16; | |
1258 | } | |
1259 | inode->i_nlink = le16_to_cpu(raw_inode->i_links_count); | |
1260 | inode->i_size = le32_to_cpu(raw_inode->i_size); | |
4d7bf11d MR |
1261 | inode->i_atime.tv_sec = (signed)le32_to_cpu(raw_inode->i_atime); |
1262 | inode->i_ctime.tv_sec = (signed)le32_to_cpu(raw_inode->i_ctime); | |
1263 | inode->i_mtime.tv_sec = (signed)le32_to_cpu(raw_inode->i_mtime); | |
1da177e4 LT |
1264 | inode->i_atime.tv_nsec = inode->i_mtime.tv_nsec = inode->i_ctime.tv_nsec = 0; |
1265 | ei->i_dtime = le32_to_cpu(raw_inode->i_dtime); | |
1266 | /* We now have enough fields to check if the inode was active or not. | |
1267 | * This is needed because nfsd might try to access dead inodes | |
1268 | * the test is that same one that e2fsck uses | |
1269 | * NeilBrown 1999oct15 | |
1270 | */ | |
1271 | if (inode->i_nlink == 0 && (inode->i_mode == 0 || ei->i_dtime)) { | |
1272 | /* this inode is deleted */ | |
1273 | brelse (bh); | |
52fcf703 | 1274 | ret = -ESTALE; |
1da177e4 LT |
1275 | goto bad_inode; |
1276 | } | |
1da177e4 LT |
1277 | inode->i_blocks = le32_to_cpu(raw_inode->i_blocks); |
1278 | ei->i_flags = le32_to_cpu(raw_inode->i_flags); | |
1279 | ei->i_faddr = le32_to_cpu(raw_inode->i_faddr); | |
1280 | ei->i_frag_no = raw_inode->i_frag; | |
1281 | ei->i_frag_size = raw_inode->i_fsize; | |
1282 | ei->i_file_acl = le32_to_cpu(raw_inode->i_file_acl); | |
1283 | ei->i_dir_acl = 0; | |
1284 | if (S_ISREG(inode->i_mode)) | |
1285 | inode->i_size |= ((__u64)le32_to_cpu(raw_inode->i_size_high)) << 32; | |
1286 | else | |
1287 | ei->i_dir_acl = le32_to_cpu(raw_inode->i_dir_acl); | |
1288 | ei->i_dtime = 0; | |
1289 | inode->i_generation = le32_to_cpu(raw_inode->i_generation); | |
1290 | ei->i_state = 0; | |
1da177e4 LT |
1291 | ei->i_block_group = (ino - 1) / EXT2_INODES_PER_GROUP(inode->i_sb); |
1292 | ei->i_dir_start_lookup = 0; | |
1293 | ||
1294 | /* | |
1295 | * NOTE! The in-memory inode i_data array is in little-endian order | |
1296 | * even on big-endian machines: we do NOT byteswap the block numbers! | |
1297 | */ | |
1298 | for (n = 0; n < EXT2_N_BLOCKS; n++) | |
1299 | ei->i_data[n] = raw_inode->i_block[n]; | |
1300 | ||
1301 | if (S_ISREG(inode->i_mode)) { | |
1302 | inode->i_op = &ext2_file_inode_operations; | |
6d79125b CO |
1303 | if (ext2_use_xip(inode->i_sb)) { |
1304 | inode->i_mapping->a_ops = &ext2_aops_xip; | |
1305 | inode->i_fop = &ext2_xip_file_operations; | |
1306 | } else if (test_opt(inode->i_sb, NOBH)) { | |
1da177e4 | 1307 | inode->i_mapping->a_ops = &ext2_nobh_aops; |
6d79125b CO |
1308 | inode->i_fop = &ext2_file_operations; |
1309 | } else { | |
1da177e4 | 1310 | inode->i_mapping->a_ops = &ext2_aops; |
6d79125b CO |
1311 | inode->i_fop = &ext2_file_operations; |
1312 | } | |
1da177e4 LT |
1313 | } else if (S_ISDIR(inode->i_mode)) { |
1314 | inode->i_op = &ext2_dir_inode_operations; | |
1315 | inode->i_fop = &ext2_dir_operations; | |
1316 | if (test_opt(inode->i_sb, NOBH)) | |
1317 | inode->i_mapping->a_ops = &ext2_nobh_aops; | |
1318 | else | |
1319 | inode->i_mapping->a_ops = &ext2_aops; | |
1320 | } else if (S_ISLNK(inode->i_mode)) { | |
8d6d0c4d | 1321 | if (ext2_inode_is_fast_symlink(inode)) { |
1da177e4 | 1322 | inode->i_op = &ext2_fast_symlink_inode_operations; |
8d6d0c4d DG |
1323 | nd_terminate_link(ei->i_data, inode->i_size, |
1324 | sizeof(ei->i_data) - 1); | |
1325 | } else { | |
1da177e4 LT |
1326 | inode->i_op = &ext2_symlink_inode_operations; |
1327 | if (test_opt(inode->i_sb, NOBH)) | |
1328 | inode->i_mapping->a_ops = &ext2_nobh_aops; | |
1329 | else | |
1330 | inode->i_mapping->a_ops = &ext2_aops; | |
1331 | } | |
1332 | } else { | |
1333 | inode->i_op = &ext2_special_inode_operations; | |
1334 | if (raw_inode->i_block[0]) | |
1335 | init_special_inode(inode, inode->i_mode, | |
1336 | old_decode_dev(le32_to_cpu(raw_inode->i_block[0]))); | |
1337 | else | |
1338 | init_special_inode(inode, inode->i_mode, | |
1339 | new_decode_dev(le32_to_cpu(raw_inode->i_block[1]))); | |
1340 | } | |
1341 | brelse (bh); | |
1342 | ext2_set_inode_flags(inode); | |
52fcf703 DH |
1343 | unlock_new_inode(inode); |
1344 | return inode; | |
1da177e4 LT |
1345 | |
1346 | bad_inode: | |
52fcf703 DH |
1347 | iget_failed(inode); |
1348 | return ERR_PTR(ret); | |
1da177e4 LT |
1349 | } |
1350 | ||
e1740a46 | 1351 | int ext2_write_inode(struct inode *inode, int do_sync) |
1da177e4 LT |
1352 | { |
1353 | struct ext2_inode_info *ei = EXT2_I(inode); | |
1354 | struct super_block *sb = inode->i_sb; | |
1355 | ino_t ino = inode->i_ino; | |
1356 | uid_t uid = inode->i_uid; | |
1357 | gid_t gid = inode->i_gid; | |
1358 | struct buffer_head * bh; | |
1359 | struct ext2_inode * raw_inode = ext2_get_inode(sb, ino, &bh); | |
1360 | int n; | |
1361 | int err = 0; | |
1362 | ||
1363 | if (IS_ERR(raw_inode)) | |
1364 | return -EIO; | |
1365 | ||
1366 | /* For fields not not tracking in the in-memory inode, | |
1367 | * initialise them to zero for new inodes. */ | |
1368 | if (ei->i_state & EXT2_STATE_NEW) | |
1369 | memset(raw_inode, 0, EXT2_SB(sb)->s_inode_size); | |
1370 | ||
4f99ed67 | 1371 | ext2_get_inode_flags(ei); |
1da177e4 LT |
1372 | raw_inode->i_mode = cpu_to_le16(inode->i_mode); |
1373 | if (!(test_opt(sb, NO_UID32))) { | |
1374 | raw_inode->i_uid_low = cpu_to_le16(low_16_bits(uid)); | |
1375 | raw_inode->i_gid_low = cpu_to_le16(low_16_bits(gid)); | |
1376 | /* | |
1377 | * Fix up interoperability with old kernels. Otherwise, old inodes get | |
1378 | * re-used with the upper 16 bits of the uid/gid intact | |
1379 | */ | |
1380 | if (!ei->i_dtime) { | |
1381 | raw_inode->i_uid_high = cpu_to_le16(high_16_bits(uid)); | |
1382 | raw_inode->i_gid_high = cpu_to_le16(high_16_bits(gid)); | |
1383 | } else { | |
1384 | raw_inode->i_uid_high = 0; | |
1385 | raw_inode->i_gid_high = 0; | |
1386 | } | |
1387 | } else { | |
1388 | raw_inode->i_uid_low = cpu_to_le16(fs_high2lowuid(uid)); | |
1389 | raw_inode->i_gid_low = cpu_to_le16(fs_high2lowgid(gid)); | |
1390 | raw_inode->i_uid_high = 0; | |
1391 | raw_inode->i_gid_high = 0; | |
1392 | } | |
1393 | raw_inode->i_links_count = cpu_to_le16(inode->i_nlink); | |
1394 | raw_inode->i_size = cpu_to_le32(inode->i_size); | |
1395 | raw_inode->i_atime = cpu_to_le32(inode->i_atime.tv_sec); | |
1396 | raw_inode->i_ctime = cpu_to_le32(inode->i_ctime.tv_sec); | |
1397 | raw_inode->i_mtime = cpu_to_le32(inode->i_mtime.tv_sec); | |
1398 | ||
1399 | raw_inode->i_blocks = cpu_to_le32(inode->i_blocks); | |
1400 | raw_inode->i_dtime = cpu_to_le32(ei->i_dtime); | |
1401 | raw_inode->i_flags = cpu_to_le32(ei->i_flags); | |
1402 | raw_inode->i_faddr = cpu_to_le32(ei->i_faddr); | |
1403 | raw_inode->i_frag = ei->i_frag_no; | |
1404 | raw_inode->i_fsize = ei->i_frag_size; | |
1405 | raw_inode->i_file_acl = cpu_to_le32(ei->i_file_acl); | |
1406 | if (!S_ISREG(inode->i_mode)) | |
1407 | raw_inode->i_dir_acl = cpu_to_le32(ei->i_dir_acl); | |
1408 | else { | |
1409 | raw_inode->i_size_high = cpu_to_le32(inode->i_size >> 32); | |
1410 | if (inode->i_size > 0x7fffffffULL) { | |
1411 | if (!EXT2_HAS_RO_COMPAT_FEATURE(sb, | |
1412 | EXT2_FEATURE_RO_COMPAT_LARGE_FILE) || | |
1413 | EXT2_SB(sb)->s_es->s_rev_level == | |
1414 | cpu_to_le32(EXT2_GOOD_OLD_REV)) { | |
1415 | /* If this is the first large file | |
1416 | * created, add a flag to the superblock. | |
1417 | */ | |
1418 | lock_kernel(); | |
1419 | ext2_update_dynamic_rev(sb); | |
1420 | EXT2_SET_RO_COMPAT_FEATURE(sb, | |
1421 | EXT2_FEATURE_RO_COMPAT_LARGE_FILE); | |
1422 | unlock_kernel(); | |
1423 | ext2_write_super(sb); | |
1424 | } | |
1425 | } | |
1426 | } | |
1427 | ||
1428 | raw_inode->i_generation = cpu_to_le32(inode->i_generation); | |
1429 | if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) { | |
1430 | if (old_valid_dev(inode->i_rdev)) { | |
1431 | raw_inode->i_block[0] = | |
1432 | cpu_to_le32(old_encode_dev(inode->i_rdev)); | |
1433 | raw_inode->i_block[1] = 0; | |
1434 | } else { | |
1435 | raw_inode->i_block[0] = 0; | |
1436 | raw_inode->i_block[1] = | |
1437 | cpu_to_le32(new_encode_dev(inode->i_rdev)); | |
1438 | raw_inode->i_block[2] = 0; | |
1439 | } | |
1440 | } else for (n = 0; n < EXT2_N_BLOCKS; n++) | |
1441 | raw_inode->i_block[n] = ei->i_data[n]; | |
1442 | mark_buffer_dirty(bh); | |
1443 | if (do_sync) { | |
1444 | sync_dirty_buffer(bh); | |
1445 | if (buffer_req(bh) && !buffer_uptodate(bh)) { | |
1446 | printk ("IO error syncing ext2 inode [%s:%08lx]\n", | |
1447 | sb->s_id, (unsigned long) ino); | |
1448 | err = -EIO; | |
1449 | } | |
1450 | } | |
1451 | ei->i_state &= ~EXT2_STATE_NEW; | |
1452 | brelse (bh); | |
1453 | return err; | |
1454 | } | |
1455 | ||
1da177e4 LT |
1456 | int ext2_sync_inode(struct inode *inode) |
1457 | { | |
1458 | struct writeback_control wbc = { | |
1459 | .sync_mode = WB_SYNC_ALL, | |
1460 | .nr_to_write = 0, /* sys_fsync did this */ | |
1461 | }; | |
1462 | return sync_inode(inode, &wbc); | |
1463 | } | |
1464 | ||
1465 | int ext2_setattr(struct dentry *dentry, struct iattr *iattr) | |
1466 | { | |
1467 | struct inode *inode = dentry->d_inode; | |
1468 | int error; | |
1469 | ||
1470 | error = inode_change_ok(inode, iattr); | |
1471 | if (error) | |
1472 | return error; | |
1473 | if ((iattr->ia_valid & ATTR_UID && iattr->ia_uid != inode->i_uid) || | |
1474 | (iattr->ia_valid & ATTR_GID && iattr->ia_gid != inode->i_gid)) { | |
6f90bee5 | 1475 | error = vfs_dq_transfer(inode, iattr) ? -EDQUOT : 0; |
1da177e4 LT |
1476 | if (error) |
1477 | return error; | |
1478 | } | |
1479 | error = inode_setattr(inode, iattr); | |
1480 | if (!error && (iattr->ia_valid & ATTR_MODE)) | |
1481 | error = ext2_acl_chmod(inode); | |
1482 | return error; | |
1483 | } |