diff options
author | Linus Torvalds <torvalds@ppc970.osdl.org> | 2005-04-16 18:20:36 -0400 |
---|---|---|
committer | Linus Torvalds <torvalds@ppc970.osdl.org> | 2005-04-16 18:20:36 -0400 |
commit | 1da177e4c3f41524e886b7f1b8a0c1fc7321cac2 (patch) | |
tree | 0bba044c4ce775e45a88a51686b5d9f90697ea9d /fs/ext2/inode.c |
Linux-2.6.12-rc2
Initial git repository build. I'm not bothering with the full history,
even though we have it. We can create a separate "historical" git
archive of that later if we want to, and in the meantime it's about
3.2GB when imported into git - space that would just make the early
git days unnecessarily complicated, when we don't have a lot of good
infrastructure for it.
Let it rip!
Diffstat (limited to 'fs/ext2/inode.c')
-rw-r--r-- | fs/ext2/inode.c | 1276 |
1 files changed, 1276 insertions, 0 deletions
diff --git a/fs/ext2/inode.c b/fs/ext2/inode.c new file mode 100644 index 00000000000..b890be02249 --- /dev/null +++ b/fs/ext2/inode.c | |||
@@ -0,0 +1,1276 @@ | |||
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> | ||
34 | #include "ext2.h" | ||
35 | #include "acl.h" | ||
36 | |||
37 | MODULE_AUTHOR("Remy Card and others"); | ||
38 | MODULE_DESCRIPTION("Second Extended Filesystem"); | ||
39 | MODULE_LICENSE("GPL"); | ||
40 | |||
41 | static int ext2_update_inode(struct inode * inode, int do_sync); | ||
42 | |||
43 | /* | ||
44 | * Test whether an inode is a fast symlink. | ||
45 | */ | ||
46 | static inline int ext2_inode_is_fast_symlink(struct inode *inode) | ||
47 | { | ||
48 | int ea_blocks = EXT2_I(inode)->i_file_acl ? | ||
49 | (inode->i_sb->s_blocksize >> 9) : 0; | ||
50 | |||
51 | return (S_ISLNK(inode->i_mode) && | ||
52 | inode->i_blocks - ea_blocks == 0); | ||
53 | } | ||
54 | |||
55 | /* | ||
56 | * Called at the last iput() if i_nlink is zero. | ||
57 | */ | ||
58 | void ext2_delete_inode (struct inode * inode) | ||
59 | { | ||
60 | if (is_bad_inode(inode)) | ||
61 | goto no_delete; | ||
62 | EXT2_I(inode)->i_dtime = get_seconds(); | ||
63 | mark_inode_dirty(inode); | ||
64 | ext2_update_inode(inode, inode_needs_sync(inode)); | ||
65 | |||
66 | inode->i_size = 0; | ||
67 | if (inode->i_blocks) | ||
68 | ext2_truncate (inode); | ||
69 | ext2_free_inode (inode); | ||
70 | |||
71 | return; | ||
72 | no_delete: | ||
73 | clear_inode(inode); /* We must guarantee clearing of inode... */ | ||
74 | } | ||
75 | |||
76 | void ext2_discard_prealloc (struct inode * inode) | ||
77 | { | ||
78 | #ifdef EXT2_PREALLOCATE | ||
79 | struct ext2_inode_info *ei = EXT2_I(inode); | ||
80 | write_lock(&ei->i_meta_lock); | ||
81 | if (ei->i_prealloc_count) { | ||
82 | unsigned short total = ei->i_prealloc_count; | ||
83 | unsigned long block = ei->i_prealloc_block; | ||
84 | ei->i_prealloc_count = 0; | ||
85 | ei->i_prealloc_block = 0; | ||
86 | write_unlock(&ei->i_meta_lock); | ||
87 | ext2_free_blocks (inode, block, total); | ||
88 | return; | ||
89 | } else | ||
90 | write_unlock(&ei->i_meta_lock); | ||
91 | #endif | ||
92 | } | ||
93 | |||
94 | static int ext2_alloc_block (struct inode * inode, unsigned long goal, int *err) | ||
95 | { | ||
96 | #ifdef EXT2FS_DEBUG | ||
97 | static unsigned long alloc_hits, alloc_attempts; | ||
98 | #endif | ||
99 | unsigned long result; | ||
100 | |||
101 | |||
102 | #ifdef EXT2_PREALLOCATE | ||
103 | struct ext2_inode_info *ei = EXT2_I(inode); | ||
104 | write_lock(&ei->i_meta_lock); | ||
105 | if (ei->i_prealloc_count && | ||
106 | (goal == ei->i_prealloc_block || goal + 1 == ei->i_prealloc_block)) | ||
107 | { | ||
108 | result = ei->i_prealloc_block++; | ||
109 | ei->i_prealloc_count--; | ||
110 | write_unlock(&ei->i_meta_lock); | ||
111 | ext2_debug ("preallocation hit (%lu/%lu).\n", | ||
112 | ++alloc_hits, ++alloc_attempts); | ||
113 | } else { | ||
114 | write_unlock(&ei->i_meta_lock); | ||
115 | ext2_discard_prealloc (inode); | ||
116 | ext2_debug ("preallocation miss (%lu/%lu).\n", | ||
117 | alloc_hits, ++alloc_attempts); | ||
118 | if (S_ISREG(inode->i_mode)) | ||
119 | result = ext2_new_block (inode, goal, | ||
120 | &ei->i_prealloc_count, | ||
121 | &ei->i_prealloc_block, err); | ||
122 | else | ||
123 | result = ext2_new_block(inode, goal, NULL, NULL, err); | ||
124 | } | ||
125 | #else | ||
126 | result = ext2_new_block (inode, goal, 0, 0, err); | ||
127 | #endif | ||
128 | return result; | ||
129 | } | ||
130 | |||
131 | typedef struct { | ||
132 | __le32 *p; | ||
133 | __le32 key; | ||
134 | struct buffer_head *bh; | ||
135 | } Indirect; | ||
136 | |||
137 | static inline void add_chain(Indirect *p, struct buffer_head *bh, __le32 *v) | ||
138 | { | ||
139 | p->key = *(p->p = v); | ||
140 | p->bh = bh; | ||
141 | } | ||
142 | |||
143 | static inline int verify_chain(Indirect *from, Indirect *to) | ||
144 | { | ||
145 | while (from <= to && from->key == *from->p) | ||
146 | from++; | ||
147 | return (from > to); | ||
148 | } | ||
149 | |||
150 | /** | ||
151 | * ext2_block_to_path - parse the block number into array of offsets | ||
152 | * @inode: inode in question (we are only interested in its superblock) | ||
153 | * @i_block: block number to be parsed | ||
154 | * @offsets: array to store the offsets in | ||
155 | * @boundary: set this non-zero if the referred-to block is likely to be | ||
156 | * followed (on disk) by an indirect block. | ||
157 | * To store the locations of file's data ext2 uses a data structure common | ||
158 | * for UNIX filesystems - tree of pointers anchored in the inode, with | ||
159 | * data blocks at leaves and indirect blocks in intermediate nodes. | ||
160 | * This function translates the block number into path in that tree - | ||
161 | * return value is the path length and @offsets[n] is the offset of | ||
162 | * pointer to (n+1)th node in the nth one. If @block is out of range | ||
163 | * (negative or too large) warning is printed and zero returned. | ||
164 | * | ||
165 | * Note: function doesn't find node addresses, so no IO is needed. All | ||
166 | * we need to know is the capacity of indirect blocks (taken from the | ||
167 | * inode->i_sb). | ||
168 | */ | ||
169 | |||
170 | /* | ||
171 | * Portability note: the last comparison (check that we fit into triple | ||
172 | * indirect block) is spelled differently, because otherwise on an | ||
173 | * architecture with 32-bit longs and 8Kb pages we might get into trouble | ||
174 | * if our filesystem had 8Kb blocks. We might use long long, but that would | ||
175 | * kill us on x86. Oh, well, at least the sign propagation does not matter - | ||
176 | * i_block would have to be negative in the very beginning, so we would not | ||
177 | * get there at all. | ||
178 | */ | ||
179 | |||
180 | static int ext2_block_to_path(struct inode *inode, | ||
181 | long i_block, int offsets[4], int *boundary) | ||
182 | { | ||
183 | int ptrs = EXT2_ADDR_PER_BLOCK(inode->i_sb); | ||
184 | int ptrs_bits = EXT2_ADDR_PER_BLOCK_BITS(inode->i_sb); | ||
185 | const long direct_blocks = EXT2_NDIR_BLOCKS, | ||
186 | indirect_blocks = ptrs, | ||
187 | double_blocks = (1 << (ptrs_bits * 2)); | ||
188 | int n = 0; | ||
189 | int final = 0; | ||
190 | |||
191 | if (i_block < 0) { | ||
192 | ext2_warning (inode->i_sb, "ext2_block_to_path", "block < 0"); | ||
193 | } else if (i_block < direct_blocks) { | ||
194 | offsets[n++] = i_block; | ||
195 | final = direct_blocks; | ||
196 | } else if ( (i_block -= direct_blocks) < indirect_blocks) { | ||
197 | offsets[n++] = EXT2_IND_BLOCK; | ||
198 | offsets[n++] = i_block; | ||
199 | final = ptrs; | ||
200 | } else if ((i_block -= indirect_blocks) < double_blocks) { | ||
201 | offsets[n++] = EXT2_DIND_BLOCK; | ||
202 | offsets[n++] = i_block >> ptrs_bits; | ||
203 | offsets[n++] = i_block & (ptrs - 1); | ||
204 | final = ptrs; | ||
205 | } else if (((i_block -= double_blocks) >> (ptrs_bits * 2)) < ptrs) { | ||
206 | offsets[n++] = EXT2_TIND_BLOCK; | ||
207 | offsets[n++] = i_block >> (ptrs_bits * 2); | ||
208 | offsets[n++] = (i_block >> ptrs_bits) & (ptrs - 1); | ||
209 | offsets[n++] = i_block & (ptrs - 1); | ||
210 | final = ptrs; | ||
211 | } else { | ||
212 | ext2_warning (inode->i_sb, "ext2_block_to_path", "block > big"); | ||
213 | } | ||
214 | if (boundary) | ||
215 | *boundary = (i_block & (ptrs - 1)) == (final - 1); | ||
216 | return n; | ||
217 | } | ||
218 | |||
219 | /** | ||
220 | * ext2_get_branch - read the chain of indirect blocks leading to data | ||
221 | * @inode: inode in question | ||
222 | * @depth: depth of the chain (1 - direct pointer, etc.) | ||
223 | * @offsets: offsets of pointers in inode/indirect blocks | ||
224 | * @chain: place to store the result | ||
225 | * @err: here we store the error value | ||
226 | * | ||
227 | * Function fills the array of triples <key, p, bh> and returns %NULL | ||
228 | * if everything went OK or the pointer to the last filled triple | ||
229 | * (incomplete one) otherwise. Upon the return chain[i].key contains | ||
230 | * the number of (i+1)-th block in the chain (as it is stored in memory, | ||
231 | * i.e. little-endian 32-bit), chain[i].p contains the address of that | ||
232 | * number (it points into struct inode for i==0 and into the bh->b_data | ||
233 | * for i>0) and chain[i].bh points to the buffer_head of i-th indirect | ||
234 | * block for i>0 and NULL for i==0. In other words, it holds the block | ||
235 | * numbers of the chain, addresses they were taken from (and where we can | ||
236 | * verify that chain did not change) and buffer_heads hosting these | ||
237 | * numbers. | ||
238 | * | ||
239 | * Function stops when it stumbles upon zero pointer (absent block) | ||
240 | * (pointer to last triple returned, *@err == 0) | ||
241 | * or when it gets an IO error reading an indirect block | ||
242 | * (ditto, *@err == -EIO) | ||
243 | * or when it notices that chain had been changed while it was reading | ||
244 | * (ditto, *@err == -EAGAIN) | ||
245 | * or when it reads all @depth-1 indirect blocks successfully and finds | ||
246 | * the whole chain, all way to the data (returns %NULL, *err == 0). | ||
247 | */ | ||
248 | static Indirect *ext2_get_branch(struct inode *inode, | ||
249 | int depth, | ||
250 | int *offsets, | ||
251 | Indirect chain[4], | ||
252 | int *err) | ||
253 | { | ||
254 | struct super_block *sb = inode->i_sb; | ||
255 | Indirect *p = chain; | ||
256 | struct buffer_head *bh; | ||
257 | |||
258 | *err = 0; | ||
259 | /* i_data is not going away, no lock needed */ | ||
260 | add_chain (chain, NULL, EXT2_I(inode)->i_data + *offsets); | ||
261 | if (!p->key) | ||
262 | goto no_block; | ||
263 | while (--depth) { | ||
264 | bh = sb_bread(sb, le32_to_cpu(p->key)); | ||
265 | if (!bh) | ||
266 | goto failure; | ||
267 | read_lock(&EXT2_I(inode)->i_meta_lock); | ||
268 | if (!verify_chain(chain, p)) | ||
269 | goto changed; | ||
270 | add_chain(++p, bh, (__le32*)bh->b_data + *++offsets); | ||
271 | read_unlock(&EXT2_I(inode)->i_meta_lock); | ||
272 | if (!p->key) | ||
273 | goto no_block; | ||
274 | } | ||
275 | return NULL; | ||
276 | |||
277 | changed: | ||
278 | read_unlock(&EXT2_I(inode)->i_meta_lock); | ||
279 | brelse(bh); | ||
280 | *err = -EAGAIN; | ||
281 | goto no_block; | ||
282 | failure: | ||
283 | *err = -EIO; | ||
284 | no_block: | ||
285 | return p; | ||
286 | } | ||
287 | |||
288 | /** | ||
289 | * ext2_find_near - find a place for allocation with sufficient locality | ||
290 | * @inode: owner | ||
291 | * @ind: descriptor of indirect block. | ||
292 | * | ||
293 | * This function returns the prefered place for block allocation. | ||
294 | * It is used when heuristic for sequential allocation fails. | ||
295 | * Rules are: | ||
296 | * + if there is a block to the left of our position - allocate near it. | ||
297 | * + if pointer will live in indirect block - allocate near that block. | ||
298 | * + if pointer will live in inode - allocate in the same cylinder group. | ||
299 | * | ||
300 | * In the latter case we colour the starting block by the callers PID to | ||
301 | * prevent it from clashing with concurrent allocations for a different inode | ||
302 | * in the same block group. The PID is used here so that functionally related | ||
303 | * files will be close-by on-disk. | ||
304 | * | ||
305 | * Caller must make sure that @ind is valid and will stay that way. | ||
306 | */ | ||
307 | |||
308 | static unsigned long ext2_find_near(struct inode *inode, Indirect *ind) | ||
309 | { | ||
310 | struct ext2_inode_info *ei = EXT2_I(inode); | ||
311 | __le32 *start = ind->bh ? (__le32 *) ind->bh->b_data : ei->i_data; | ||
312 | __le32 *p; | ||
313 | unsigned long bg_start; | ||
314 | unsigned long colour; | ||
315 | |||
316 | /* Try to find previous block */ | ||
317 | for (p = ind->p - 1; p >= start; p--) | ||
318 | if (*p) | ||
319 | return le32_to_cpu(*p); | ||
320 | |||
321 | /* No such thing, so let's try location of indirect block */ | ||
322 | if (ind->bh) | ||
323 | return ind->bh->b_blocknr; | ||
324 | |||
325 | /* | ||
326 | * It is going to be refered from inode itself? OK, just put it into | ||
327 | * the same cylinder group then. | ||
328 | */ | ||
329 | bg_start = (ei->i_block_group * EXT2_BLOCKS_PER_GROUP(inode->i_sb)) + | ||
330 | le32_to_cpu(EXT2_SB(inode->i_sb)->s_es->s_first_data_block); | ||
331 | colour = (current->pid % 16) * | ||
332 | (EXT2_BLOCKS_PER_GROUP(inode->i_sb) / 16); | ||
333 | return bg_start + colour; | ||
334 | } | ||
335 | |||
336 | /** | ||
337 | * ext2_find_goal - find a prefered place for allocation. | ||
338 | * @inode: owner | ||
339 | * @block: block we want | ||
340 | * @chain: chain of indirect blocks | ||
341 | * @partial: pointer to the last triple within a chain | ||
342 | * @goal: place to store the result. | ||
343 | * | ||
344 | * Normally this function find the prefered place for block allocation, | ||
345 | * stores it in *@goal and returns zero. If the branch had been changed | ||
346 | * under us we return -EAGAIN. | ||
347 | */ | ||
348 | |||
349 | static inline int ext2_find_goal(struct inode *inode, | ||
350 | long block, | ||
351 | Indirect chain[4], | ||
352 | Indirect *partial, | ||
353 | unsigned long *goal) | ||
354 | { | ||
355 | struct ext2_inode_info *ei = EXT2_I(inode); | ||
356 | write_lock(&ei->i_meta_lock); | ||
357 | if ((block == ei->i_next_alloc_block + 1) && ei->i_next_alloc_goal) { | ||
358 | ei->i_next_alloc_block++; | ||
359 | ei->i_next_alloc_goal++; | ||
360 | } | ||
361 | if (verify_chain(chain, partial)) { | ||
362 | /* | ||
363 | * try the heuristic for sequential allocation, | ||
364 | * failing that at least try to get decent locality. | ||
365 | */ | ||
366 | if (block == ei->i_next_alloc_block) | ||
367 | *goal = ei->i_next_alloc_goal; | ||
368 | if (!*goal) | ||
369 | *goal = ext2_find_near(inode, partial); | ||
370 | write_unlock(&ei->i_meta_lock); | ||
371 | return 0; | ||
372 | } | ||
373 | write_unlock(&ei->i_meta_lock); | ||
374 | return -EAGAIN; | ||
375 | } | ||
376 | |||
377 | /** | ||
378 | * ext2_alloc_branch - allocate and set up a chain of blocks. | ||
379 | * @inode: owner | ||
380 | * @num: depth of the chain (number of blocks to allocate) | ||
381 | * @offsets: offsets (in the blocks) to store the pointers to next. | ||
382 | * @branch: place to store the chain in. | ||
383 | * | ||
384 | * This function allocates @num blocks, zeroes out all but the last one, | ||
385 | * links them into chain and (if we are synchronous) writes them to disk. | ||
386 | * In other words, it prepares a branch that can be spliced onto the | ||
387 | * inode. It stores the information about that chain in the branch[], in | ||
388 | * the same format as ext2_get_branch() would do. We are calling it after | ||
389 | * we had read the existing part of chain and partial points to the last | ||
390 | * triple of that (one with zero ->key). Upon the exit we have the same | ||
391 | * picture as after the successful ext2_get_block(), excpet that in one | ||
392 | * place chain is disconnected - *branch->p is still zero (we did not | ||
393 | * set the last link), but branch->key contains the number that should | ||
394 | * be placed into *branch->p to fill that gap. | ||
395 | * | ||
396 | * If allocation fails we free all blocks we've allocated (and forget | ||
397 | * their buffer_heads) and return the error value the from failed | ||
398 | * ext2_alloc_block() (normally -ENOSPC). Otherwise we set the chain | ||
399 | * as described above and return 0. | ||
400 | */ | ||
401 | |||
402 | static int ext2_alloc_branch(struct inode *inode, | ||
403 | int num, | ||
404 | unsigned long goal, | ||
405 | int *offsets, | ||
406 | Indirect *branch) | ||
407 | { | ||
408 | int blocksize = inode->i_sb->s_blocksize; | ||
409 | int n = 0; | ||
410 | int err; | ||
411 | int i; | ||
412 | int parent = ext2_alloc_block(inode, goal, &err); | ||
413 | |||
414 | branch[0].key = cpu_to_le32(parent); | ||
415 | if (parent) for (n = 1; n < num; n++) { | ||
416 | struct buffer_head *bh; | ||
417 | /* Allocate the next block */ | ||
418 | int nr = ext2_alloc_block(inode, parent, &err); | ||
419 | if (!nr) | ||
420 | break; | ||
421 | branch[n].key = cpu_to_le32(nr); | ||
422 | /* | ||
423 | * Get buffer_head for parent block, zero it out and set | ||
424 | * the pointer to new one, then send parent to disk. | ||
425 | */ | ||
426 | bh = sb_getblk(inode->i_sb, parent); | ||
427 | lock_buffer(bh); | ||
428 | memset(bh->b_data, 0, blocksize); | ||
429 | branch[n].bh = bh; | ||
430 | branch[n].p = (__le32 *) bh->b_data + offsets[n]; | ||
431 | *branch[n].p = branch[n].key; | ||
432 | set_buffer_uptodate(bh); | ||
433 | unlock_buffer(bh); | ||
434 | mark_buffer_dirty_inode(bh, inode); | ||
435 | /* We used to sync bh here if IS_SYNC(inode). | ||
436 | * But we now rely upon generic_osync_inode() | ||
437 | * and b_inode_buffers. But not for directories. | ||
438 | */ | ||
439 | if (S_ISDIR(inode->i_mode) && IS_DIRSYNC(inode)) | ||
440 | sync_dirty_buffer(bh); | ||
441 | parent = nr; | ||
442 | } | ||
443 | if (n == num) | ||
444 | return 0; | ||
445 | |||
446 | /* Allocation failed, free what we already allocated */ | ||
447 | for (i = 1; i < n; i++) | ||
448 | bforget(branch[i].bh); | ||
449 | for (i = 0; i < n; i++) | ||
450 | ext2_free_blocks(inode, le32_to_cpu(branch[i].key), 1); | ||
451 | return err; | ||
452 | } | ||
453 | |||
454 | /** | ||
455 | * ext2_splice_branch - splice the allocated branch onto inode. | ||
456 | * @inode: owner | ||
457 | * @block: (logical) number of block we are adding | ||
458 | * @chain: chain of indirect blocks (with a missing link - see | ||
459 | * ext2_alloc_branch) | ||
460 | * @where: location of missing link | ||
461 | * @num: number of blocks we are adding | ||
462 | * | ||
463 | * This function verifies that chain (up to the missing link) had not | ||
464 | * changed, fills the missing link and does all housekeeping needed in | ||
465 | * inode (->i_blocks, etc.). In case of success we end up with the full | ||
466 | * chain to new block and return 0. Otherwise (== chain had been changed) | ||
467 | * we free the new blocks (forgetting their buffer_heads, indeed) and | ||
468 | * return -EAGAIN. | ||
469 | */ | ||
470 | |||
471 | static inline int ext2_splice_branch(struct inode *inode, | ||
472 | long block, | ||
473 | Indirect chain[4], | ||
474 | Indirect *where, | ||
475 | int num) | ||
476 | { | ||
477 | struct ext2_inode_info *ei = EXT2_I(inode); | ||
478 | int i; | ||
479 | |||
480 | /* Verify that place we are splicing to is still there and vacant */ | ||
481 | |||
482 | write_lock(&ei->i_meta_lock); | ||
483 | if (!verify_chain(chain, where-1) || *where->p) | ||
484 | goto changed; | ||
485 | |||
486 | /* That's it */ | ||
487 | |||
488 | *where->p = where->key; | ||
489 | ei->i_next_alloc_block = block; | ||
490 | ei->i_next_alloc_goal = le32_to_cpu(where[num-1].key); | ||
491 | |||
492 | write_unlock(&ei->i_meta_lock); | ||
493 | |||
494 | /* We are done with atomic stuff, now do the rest of housekeeping */ | ||
495 | |||
496 | inode->i_ctime = CURRENT_TIME_SEC; | ||
497 | |||
498 | /* had we spliced it onto indirect block? */ | ||
499 | if (where->bh) | ||
500 | mark_buffer_dirty_inode(where->bh, inode); | ||
501 | |||
502 | mark_inode_dirty(inode); | ||
503 | return 0; | ||
504 | |||
505 | changed: | ||
506 | write_unlock(&ei->i_meta_lock); | ||
507 | for (i = 1; i < num; i++) | ||
508 | bforget(where[i].bh); | ||
509 | for (i = 0; i < num; i++) | ||
510 | ext2_free_blocks(inode, le32_to_cpu(where[i].key), 1); | ||
511 | return -EAGAIN; | ||
512 | } | ||
513 | |||
514 | /* | ||
515 | * Allocation strategy is simple: if we have to allocate something, we will | ||
516 | * have to go the whole way to leaf. So let's do it before attaching anything | ||
517 | * to tree, set linkage between the newborn blocks, write them if sync is | ||
518 | * required, recheck the path, free and repeat if check fails, otherwise | ||
519 | * set the last missing link (that will protect us from any truncate-generated | ||
520 | * removals - all blocks on the path are immune now) and possibly force the | ||
521 | * write on the parent block. | ||
522 | * That has a nice additional property: no special recovery from the failed | ||
523 | * allocations is needed - we simply release blocks and do not touch anything | ||
524 | * reachable from inode. | ||
525 | */ | ||
526 | |||
527 | int ext2_get_block(struct inode *inode, sector_t iblock, struct buffer_head *bh_result, int create) | ||
528 | { | ||
529 | int err = -EIO; | ||
530 | int offsets[4]; | ||
531 | Indirect chain[4]; | ||
532 | Indirect *partial; | ||
533 | unsigned long goal; | ||
534 | int left; | ||
535 | int boundary = 0; | ||
536 | int depth = ext2_block_to_path(inode, iblock, offsets, &boundary); | ||
537 | |||
538 | if (depth == 0) | ||
539 | goto out; | ||
540 | |||
541 | reread: | ||
542 | partial = ext2_get_branch(inode, depth, offsets, chain, &err); | ||
543 | |||
544 | /* Simplest case - block found, no allocation needed */ | ||
545 | if (!partial) { | ||
546 | got_it: | ||
547 | map_bh(bh_result, inode->i_sb, le32_to_cpu(chain[depth-1].key)); | ||
548 | if (boundary) | ||
549 | set_buffer_boundary(bh_result); | ||
550 | /* Clean up and exit */ | ||
551 | partial = chain+depth-1; /* the whole chain */ | ||
552 | goto cleanup; | ||
553 | } | ||
554 | |||
555 | /* Next simple case - plain lookup or failed read of indirect block */ | ||
556 | if (!create || err == -EIO) { | ||
557 | cleanup: | ||
558 | while (partial > chain) { | ||
559 | brelse(partial->bh); | ||
560 | partial--; | ||
561 | } | ||
562 | out: | ||
563 | return err; | ||
564 | } | ||
565 | |||
566 | /* | ||
567 | * Indirect block might be removed by truncate while we were | ||
568 | * reading it. Handling of that case (forget what we've got and | ||
569 | * reread) is taken out of the main path. | ||
570 | */ | ||
571 | if (err == -EAGAIN) | ||
572 | goto changed; | ||
573 | |||
574 | goal = 0; | ||
575 | if (ext2_find_goal(inode, iblock, chain, partial, &goal) < 0) | ||
576 | goto changed; | ||
577 | |||
578 | left = (chain + depth) - partial; | ||
579 | err = ext2_alloc_branch(inode, left, goal, | ||
580 | offsets+(partial-chain), partial); | ||
581 | if (err) | ||
582 | goto cleanup; | ||
583 | |||
584 | if (ext2_splice_branch(inode, iblock, chain, partial, left) < 0) | ||
585 | goto changed; | ||
586 | |||
587 | set_buffer_new(bh_result); | ||
588 | goto got_it; | ||
589 | |||
590 | changed: | ||
591 | while (partial > chain) { | ||
592 | brelse(partial->bh); | ||
593 | partial--; | ||
594 | } | ||
595 | goto reread; | ||
596 | } | ||
597 | |||
598 | static int ext2_writepage(struct page *page, struct writeback_control *wbc) | ||
599 | { | ||
600 | return block_write_full_page(page, ext2_get_block, wbc); | ||
601 | } | ||
602 | |||
603 | static int ext2_readpage(struct file *file, struct page *page) | ||
604 | { | ||
605 | return mpage_readpage(page, ext2_get_block); | ||
606 | } | ||
607 | |||
608 | static int | ||
609 | ext2_readpages(struct file *file, struct address_space *mapping, | ||
610 | struct list_head *pages, unsigned nr_pages) | ||
611 | { | ||
612 | return mpage_readpages(mapping, pages, nr_pages, ext2_get_block); | ||
613 | } | ||
614 | |||
615 | static int | ||
616 | ext2_prepare_write(struct file *file, struct page *page, | ||
617 | unsigned from, unsigned to) | ||
618 | { | ||
619 | return block_prepare_write(page,from,to,ext2_get_block); | ||
620 | } | ||
621 | |||
622 | static int | ||
623 | ext2_nobh_prepare_write(struct file *file, struct page *page, | ||
624 | unsigned from, unsigned to) | ||
625 | { | ||
626 | return nobh_prepare_write(page,from,to,ext2_get_block); | ||
627 | } | ||
628 | |||
629 | static int ext2_nobh_writepage(struct page *page, | ||
630 | struct writeback_control *wbc) | ||
631 | { | ||
632 | return nobh_writepage(page, ext2_get_block, wbc); | ||
633 | } | ||
634 | |||
635 | static sector_t ext2_bmap(struct address_space *mapping, sector_t block) | ||
636 | { | ||
637 | return generic_block_bmap(mapping,block,ext2_get_block); | ||
638 | } | ||
639 | |||
640 | static int | ||
641 | ext2_get_blocks(struct inode *inode, sector_t iblock, unsigned long max_blocks, | ||
642 | struct buffer_head *bh_result, int create) | ||
643 | { | ||
644 | int ret; | ||
645 | |||
646 | ret = ext2_get_block(inode, iblock, bh_result, create); | ||
647 | if (ret == 0) | ||
648 | bh_result->b_size = (1 << inode->i_blkbits); | ||
649 | return ret; | ||
650 | } | ||
651 | |||
652 | static ssize_t | ||
653 | ext2_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov, | ||
654 | loff_t offset, unsigned long nr_segs) | ||
655 | { | ||
656 | struct file *file = iocb->ki_filp; | ||
657 | struct inode *inode = file->f_mapping->host; | ||
658 | |||
659 | return blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov, | ||
660 | offset, nr_segs, ext2_get_blocks, NULL); | ||
661 | } | ||
662 | |||
663 | static int | ||
664 | ext2_writepages(struct address_space *mapping, struct writeback_control *wbc) | ||
665 | { | ||
666 | return mpage_writepages(mapping, wbc, ext2_get_block); | ||
667 | } | ||
668 | |||
669 | struct address_space_operations ext2_aops = { | ||
670 | .readpage = ext2_readpage, | ||
671 | .readpages = ext2_readpages, | ||
672 | .writepage = ext2_writepage, | ||
673 | .sync_page = block_sync_page, | ||
674 | .prepare_write = ext2_prepare_write, | ||
675 | .commit_write = generic_commit_write, | ||
676 | .bmap = ext2_bmap, | ||
677 | .direct_IO = ext2_direct_IO, | ||
678 | .writepages = ext2_writepages, | ||
679 | }; | ||
680 | |||
681 | struct address_space_operations ext2_nobh_aops = { | ||
682 | .readpage = ext2_readpage, | ||
683 | .readpages = ext2_readpages, | ||
684 | .writepage = ext2_nobh_writepage, | ||
685 | .sync_page = block_sync_page, | ||
686 | .prepare_write = ext2_nobh_prepare_write, | ||
687 | .commit_write = nobh_commit_write, | ||
688 | .bmap = ext2_bmap, | ||
689 | .direct_IO = ext2_direct_IO, | ||
690 | .writepages = ext2_writepages, | ||
691 | }; | ||
692 | |||
693 | /* | ||
694 | * Probably it should be a library function... search for first non-zero word | ||
695 | * or memcmp with zero_page, whatever is better for particular architecture. | ||
696 | * Linus? | ||
697 | */ | ||
698 | static inline int all_zeroes(__le32 *p, __le32 *q) | ||
699 | { | ||
700 | while (p < q) | ||
701 | if (*p++) | ||
702 | return 0; | ||
703 | return 1; | ||
704 | } | ||
705 | |||
706 | /** | ||
707 | * ext2_find_shared - find the indirect blocks for partial truncation. | ||
708 | * @inode: inode in question | ||
709 | * @depth: depth of the affected branch | ||
710 | * @offsets: offsets of pointers in that branch (see ext2_block_to_path) | ||
711 | * @chain: place to store the pointers to partial indirect blocks | ||
712 | * @top: place to the (detached) top of branch | ||
713 | * | ||
714 | * This is a helper function used by ext2_truncate(). | ||
715 | * | ||
716 | * When we do truncate() we may have to clean the ends of several indirect | ||
717 | * blocks but leave the blocks themselves alive. Block is partially | ||
718 | * truncated if some data below the new i_size is refered from it (and | ||
719 | * it is on the path to the first completely truncated data block, indeed). | ||
720 | * We have to free the top of that path along with everything to the right | ||
721 | * of the path. Since no allocation past the truncation point is possible | ||
722 | * until ext2_truncate() finishes, we may safely do the latter, but top | ||
723 | * of branch may require special attention - pageout below the truncation | ||
724 | * point might try to populate it. | ||
725 | * | ||
726 | * We atomically detach the top of branch from the tree, store the block | ||
727 | * number of its root in *@top, pointers to buffer_heads of partially | ||
728 | * truncated blocks - in @chain[].bh and pointers to their last elements | ||
729 | * that should not be removed - in @chain[].p. Return value is the pointer | ||
730 | * to last filled element of @chain. | ||
731 | * | ||
732 | * The work left to caller to do the actual freeing of subtrees: | ||
733 | * a) free the subtree starting from *@top | ||
734 | * b) free the subtrees whose roots are stored in | ||
735 | * (@chain[i].p+1 .. end of @chain[i].bh->b_data) | ||
736 | * c) free the subtrees growing from the inode past the @chain[0].p | ||
737 | * (no partially truncated stuff there). | ||
738 | */ | ||
739 | |||
740 | static Indirect *ext2_find_shared(struct inode *inode, | ||
741 | int depth, | ||
742 | int offsets[4], | ||
743 | Indirect chain[4], | ||
744 | __le32 *top) | ||
745 | { | ||
746 | Indirect *partial, *p; | ||
747 | int k, err; | ||
748 | |||
749 | *top = 0; | ||
750 | for (k = depth; k > 1 && !offsets[k-1]; k--) | ||
751 | ; | ||
752 | partial = ext2_get_branch(inode, k, offsets, chain, &err); | ||
753 | if (!partial) | ||
754 | partial = chain + k-1; | ||
755 | /* | ||
756 | * If the branch acquired continuation since we've looked at it - | ||
757 | * fine, it should all survive and (new) top doesn't belong to us. | ||
758 | */ | ||
759 | write_lock(&EXT2_I(inode)->i_meta_lock); | ||
760 | if (!partial->key && *partial->p) { | ||
761 | write_unlock(&EXT2_I(inode)->i_meta_lock); | ||
762 | goto no_top; | ||
763 | } | ||
764 | for (p=partial; p>chain && all_zeroes((__le32*)p->bh->b_data,p->p); p--) | ||
765 | ; | ||
766 | /* | ||
767 | * OK, we've found the last block that must survive. The rest of our | ||
768 | * branch should be detached before unlocking. However, if that rest | ||
769 | * of branch is all ours and does not grow immediately from the inode | ||
770 | * it's easier to cheat and just decrement partial->p. | ||
771 | */ | ||
772 | if (p == chain + k - 1 && p > chain) { | ||
773 | p->p--; | ||
774 | } else { | ||
775 | *top = *p->p; | ||
776 | *p->p = 0; | ||
777 | } | ||
778 | write_unlock(&EXT2_I(inode)->i_meta_lock); | ||
779 | |||
780 | while(partial > p) | ||
781 | { | ||
782 | brelse(partial->bh); | ||
783 | partial--; | ||
784 | } | ||
785 | no_top: | ||
786 | return partial; | ||
787 | } | ||
788 | |||
789 | /** | ||
790 | * ext2_free_data - free a list of data blocks | ||
791 | * @inode: inode we are dealing with | ||
792 | * @p: array of block numbers | ||
793 | * @q: points immediately past the end of array | ||
794 | * | ||
795 | * We are freeing all blocks refered from that array (numbers are | ||
796 | * stored as little-endian 32-bit) and updating @inode->i_blocks | ||
797 | * appropriately. | ||
798 | */ | ||
799 | static inline void ext2_free_data(struct inode *inode, __le32 *p, __le32 *q) | ||
800 | { | ||
801 | unsigned long block_to_free = 0, count = 0; | ||
802 | unsigned long nr; | ||
803 | |||
804 | for ( ; p < q ; p++) { | ||
805 | nr = le32_to_cpu(*p); | ||
806 | if (nr) { | ||
807 | *p = 0; | ||
808 | /* accumulate blocks to free if they're contiguous */ | ||
809 | if (count == 0) | ||
810 | goto free_this; | ||
811 | else if (block_to_free == nr - count) | ||
812 | count++; | ||
813 | else { | ||
814 | mark_inode_dirty(inode); | ||
815 | ext2_free_blocks (inode, block_to_free, count); | ||
816 | free_this: | ||
817 | block_to_free = nr; | ||
818 | count = 1; | ||
819 | } | ||
820 | } | ||
821 | } | ||
822 | if (count > 0) { | ||
823 | mark_inode_dirty(inode); | ||
824 | ext2_free_blocks (inode, block_to_free, count); | ||
825 | } | ||
826 | } | ||
827 | |||
828 | /** | ||
829 | * ext2_free_branches - free an array of branches | ||
830 | * @inode: inode we are dealing with | ||
831 | * @p: array of block numbers | ||
832 | * @q: pointer immediately past the end of array | ||
833 | * @depth: depth of the branches to free | ||
834 | * | ||
835 | * We are freeing all blocks refered from these branches (numbers are | ||
836 | * stored as little-endian 32-bit) and updating @inode->i_blocks | ||
837 | * appropriately. | ||
838 | */ | ||
839 | static void ext2_free_branches(struct inode *inode, __le32 *p, __le32 *q, int depth) | ||
840 | { | ||
841 | struct buffer_head * bh; | ||
842 | unsigned long nr; | ||
843 | |||
844 | if (depth--) { | ||
845 | int addr_per_block = EXT2_ADDR_PER_BLOCK(inode->i_sb); | ||
846 | for ( ; p < q ; p++) { | ||
847 | nr = le32_to_cpu(*p); | ||
848 | if (!nr) | ||
849 | continue; | ||
850 | *p = 0; | ||
851 | bh = sb_bread(inode->i_sb, nr); | ||
852 | /* | ||
853 | * A read failure? Report error and clear slot | ||
854 | * (should be rare). | ||
855 | */ | ||
856 | if (!bh) { | ||
857 | ext2_error(inode->i_sb, "ext2_free_branches", | ||
858 | "Read failure, inode=%ld, block=%ld", | ||
859 | inode->i_ino, nr); | ||
860 | continue; | ||
861 | } | ||
862 | ext2_free_branches(inode, | ||
863 | (__le32*)bh->b_data, | ||
864 | (__le32*)bh->b_data + addr_per_block, | ||
865 | depth); | ||
866 | bforget(bh); | ||
867 | ext2_free_blocks(inode, nr, 1); | ||
868 | mark_inode_dirty(inode); | ||
869 | } | ||
870 | } else | ||
871 | ext2_free_data(inode, p, q); | ||
872 | } | ||
873 | |||
874 | void ext2_truncate (struct inode * inode) | ||
875 | { | ||
876 | __le32 *i_data = EXT2_I(inode)->i_data; | ||
877 | int addr_per_block = EXT2_ADDR_PER_BLOCK(inode->i_sb); | ||
878 | int offsets[4]; | ||
879 | Indirect chain[4]; | ||
880 | Indirect *partial; | ||
881 | __le32 nr = 0; | ||
882 | int n; | ||
883 | long iblock; | ||
884 | unsigned blocksize; | ||
885 | |||
886 | if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || | ||
887 | S_ISLNK(inode->i_mode))) | ||
888 | return; | ||
889 | if (ext2_inode_is_fast_symlink(inode)) | ||
890 | return; | ||
891 | if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) | ||
892 | return; | ||
893 | |||
894 | ext2_discard_prealloc(inode); | ||
895 | |||
896 | blocksize = inode->i_sb->s_blocksize; | ||
897 | iblock = (inode->i_size + blocksize-1) | ||
898 | >> EXT2_BLOCK_SIZE_BITS(inode->i_sb); | ||
899 | |||
900 | if (test_opt(inode->i_sb, NOBH)) | ||
901 | nobh_truncate_page(inode->i_mapping, inode->i_size); | ||
902 | else | ||
903 | block_truncate_page(inode->i_mapping, | ||
904 | inode->i_size, ext2_get_block); | ||
905 | |||
906 | n = ext2_block_to_path(inode, iblock, offsets, NULL); | ||
907 | if (n == 0) | ||
908 | return; | ||
909 | |||
910 | if (n == 1) { | ||
911 | ext2_free_data(inode, i_data+offsets[0], | ||
912 | i_data + EXT2_NDIR_BLOCKS); | ||
913 | goto do_indirects; | ||
914 | } | ||
915 | |||
916 | partial = ext2_find_shared(inode, n, offsets, chain, &nr); | ||
917 | /* Kill the top of shared branch (already detached) */ | ||
918 | if (nr) { | ||
919 | if (partial == chain) | ||
920 | mark_inode_dirty(inode); | ||
921 | else | ||
922 | mark_buffer_dirty_inode(partial->bh, inode); | ||
923 | ext2_free_branches(inode, &nr, &nr+1, (chain+n-1) - partial); | ||
924 | } | ||
925 | /* Clear the ends of indirect blocks on the shared branch */ | ||
926 | while (partial > chain) { | ||
927 | ext2_free_branches(inode, | ||
928 | partial->p + 1, | ||
929 | (__le32*)partial->bh->b_data+addr_per_block, | ||
930 | (chain+n-1) - partial); | ||
931 | mark_buffer_dirty_inode(partial->bh, inode); | ||
932 | brelse (partial->bh); | ||
933 | partial--; | ||
934 | } | ||
935 | do_indirects: | ||
936 | /* Kill the remaining (whole) subtrees */ | ||
937 | switch (offsets[0]) { | ||
938 | default: | ||
939 | nr = i_data[EXT2_IND_BLOCK]; | ||
940 | if (nr) { | ||
941 | i_data[EXT2_IND_BLOCK] = 0; | ||
942 | mark_inode_dirty(inode); | ||
943 | ext2_free_branches(inode, &nr, &nr+1, 1); | ||
944 | } | ||
945 | case EXT2_IND_BLOCK: | ||
946 | nr = i_data[EXT2_DIND_BLOCK]; | ||
947 | if (nr) { | ||
948 | i_data[EXT2_DIND_BLOCK] = 0; | ||
949 | mark_inode_dirty(inode); | ||
950 | ext2_free_branches(inode, &nr, &nr+1, 2); | ||
951 | } | ||
952 | case EXT2_DIND_BLOCK: | ||
953 | nr = i_data[EXT2_TIND_BLOCK]; | ||
954 | if (nr) { | ||
955 | i_data[EXT2_TIND_BLOCK] = 0; | ||
956 | mark_inode_dirty(inode); | ||
957 | ext2_free_branches(inode, &nr, &nr+1, 3); | ||
958 | } | ||
959 | case EXT2_TIND_BLOCK: | ||
960 | ; | ||
961 | } | ||
962 | inode->i_mtime = inode->i_ctime = CURRENT_TIME_SEC; | ||
963 | if (inode_needs_sync(inode)) { | ||
964 | sync_mapping_buffers(inode->i_mapping); | ||
965 | ext2_sync_inode (inode); | ||
966 | } else { | ||
967 | mark_inode_dirty(inode); | ||
968 | } | ||
969 | } | ||
970 | |||
971 | static struct ext2_inode *ext2_get_inode(struct super_block *sb, ino_t ino, | ||
972 | struct buffer_head **p) | ||
973 | { | ||
974 | struct buffer_head * bh; | ||
975 | unsigned long block_group; | ||
976 | unsigned long block; | ||
977 | unsigned long offset; | ||
978 | struct ext2_group_desc * gdp; | ||
979 | |||
980 | *p = NULL; | ||
981 | if ((ino != EXT2_ROOT_INO && ino < EXT2_FIRST_INO(sb)) || | ||
982 | ino > le32_to_cpu(EXT2_SB(sb)->s_es->s_inodes_count)) | ||
983 | goto Einval; | ||
984 | |||
985 | block_group = (ino - 1) / EXT2_INODES_PER_GROUP(sb); | ||
986 | gdp = ext2_get_group_desc(sb, block_group, &bh); | ||
987 | if (!gdp) | ||
988 | goto Egdp; | ||
989 | /* | ||
990 | * Figure out the offset within the block group inode table | ||
991 | */ | ||
992 | offset = ((ino - 1) % EXT2_INODES_PER_GROUP(sb)) * EXT2_INODE_SIZE(sb); | ||
993 | block = le32_to_cpu(gdp->bg_inode_table) + | ||
994 | (offset >> EXT2_BLOCK_SIZE_BITS(sb)); | ||
995 | if (!(bh = sb_bread(sb, block))) | ||
996 | goto Eio; | ||
997 | |||
998 | *p = bh; | ||
999 | offset &= (EXT2_BLOCK_SIZE(sb) - 1); | ||
1000 | return (struct ext2_inode *) (bh->b_data + offset); | ||
1001 | |||
1002 | Einval: | ||
1003 | ext2_error(sb, "ext2_get_inode", "bad inode number: %lu", | ||
1004 | (unsigned long) ino); | ||
1005 | return ERR_PTR(-EINVAL); | ||
1006 | Eio: | ||
1007 | ext2_error(sb, "ext2_get_inode", | ||
1008 | "unable to read inode block - inode=%lu, block=%lu", | ||
1009 | (unsigned long) ino, block); | ||
1010 | Egdp: | ||
1011 | return ERR_PTR(-EIO); | ||
1012 | } | ||
1013 | |||
1014 | void ext2_set_inode_flags(struct inode *inode) | ||
1015 | { | ||
1016 | unsigned int flags = EXT2_I(inode)->i_flags; | ||
1017 | |||
1018 | inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC); | ||
1019 | if (flags & EXT2_SYNC_FL) | ||
1020 | inode->i_flags |= S_SYNC; | ||
1021 | if (flags & EXT2_APPEND_FL) | ||
1022 | inode->i_flags |= S_APPEND; | ||
1023 | if (flags & EXT2_IMMUTABLE_FL) | ||
1024 | inode->i_flags |= S_IMMUTABLE; | ||
1025 | if (flags & EXT2_NOATIME_FL) | ||
1026 | inode->i_flags |= S_NOATIME; | ||
1027 | if (flags & EXT2_DIRSYNC_FL) | ||
1028 | inode->i_flags |= S_DIRSYNC; | ||
1029 | } | ||
1030 | |||
1031 | void ext2_read_inode (struct inode * inode) | ||
1032 | { | ||
1033 | struct ext2_inode_info *ei = EXT2_I(inode); | ||
1034 | ino_t ino = inode->i_ino; | ||
1035 | struct buffer_head * bh; | ||
1036 | struct ext2_inode * raw_inode = ext2_get_inode(inode->i_sb, ino, &bh); | ||
1037 | int n; | ||
1038 | |||
1039 | #ifdef CONFIG_EXT2_FS_POSIX_ACL | ||
1040 | ei->i_acl = EXT2_ACL_NOT_CACHED; | ||
1041 | ei->i_default_acl = EXT2_ACL_NOT_CACHED; | ||
1042 | #endif | ||
1043 | if (IS_ERR(raw_inode)) | ||
1044 | goto bad_inode; | ||
1045 | |||
1046 | inode->i_mode = le16_to_cpu(raw_inode->i_mode); | ||
1047 | inode->i_uid = (uid_t)le16_to_cpu(raw_inode->i_uid_low); | ||
1048 | inode->i_gid = (gid_t)le16_to_cpu(raw_inode->i_gid_low); | ||
1049 | if (!(test_opt (inode->i_sb, NO_UID32))) { | ||
1050 | inode->i_uid |= le16_to_cpu(raw_inode->i_uid_high) << 16; | ||
1051 | inode->i_gid |= le16_to_cpu(raw_inode->i_gid_high) << 16; | ||
1052 | } | ||
1053 | inode->i_nlink = le16_to_cpu(raw_inode->i_links_count); | ||
1054 | inode->i_size = le32_to_cpu(raw_inode->i_size); | ||
1055 | inode->i_atime.tv_sec = le32_to_cpu(raw_inode->i_atime); | ||
1056 | inode->i_ctime.tv_sec = le32_to_cpu(raw_inode->i_ctime); | ||
1057 | inode->i_mtime.tv_sec = le32_to_cpu(raw_inode->i_mtime); | ||
1058 | inode->i_atime.tv_nsec = inode->i_mtime.tv_nsec = inode->i_ctime.tv_nsec = 0; | ||
1059 | ei->i_dtime = le32_to_cpu(raw_inode->i_dtime); | ||
1060 | /* We now have enough fields to check if the inode was active or not. | ||
1061 | * This is needed because nfsd might try to access dead inodes | ||
1062 | * the test is that same one that e2fsck uses | ||
1063 | * NeilBrown 1999oct15 | ||
1064 | */ | ||
1065 | if (inode->i_nlink == 0 && (inode->i_mode == 0 || ei->i_dtime)) { | ||
1066 | /* this inode is deleted */ | ||
1067 | brelse (bh); | ||
1068 | goto bad_inode; | ||
1069 | } | ||
1070 | inode->i_blksize = PAGE_SIZE; /* This is the optimal IO size (for stat), not the fs block size */ | ||
1071 | inode->i_blocks = le32_to_cpu(raw_inode->i_blocks); | ||
1072 | ei->i_flags = le32_to_cpu(raw_inode->i_flags); | ||
1073 | ei->i_faddr = le32_to_cpu(raw_inode->i_faddr); | ||
1074 | ei->i_frag_no = raw_inode->i_frag; | ||
1075 | ei->i_frag_size = raw_inode->i_fsize; | ||
1076 | ei->i_file_acl = le32_to_cpu(raw_inode->i_file_acl); | ||
1077 | ei->i_dir_acl = 0; | ||
1078 | if (S_ISREG(inode->i_mode)) | ||
1079 | inode->i_size |= ((__u64)le32_to_cpu(raw_inode->i_size_high)) << 32; | ||
1080 | else | ||
1081 | ei->i_dir_acl = le32_to_cpu(raw_inode->i_dir_acl); | ||
1082 | ei->i_dtime = 0; | ||
1083 | inode->i_generation = le32_to_cpu(raw_inode->i_generation); | ||
1084 | ei->i_state = 0; | ||
1085 | ei->i_next_alloc_block = 0; | ||
1086 | ei->i_next_alloc_goal = 0; | ||
1087 | ei->i_prealloc_count = 0; | ||
1088 | ei->i_block_group = (ino - 1) / EXT2_INODES_PER_GROUP(inode->i_sb); | ||
1089 | ei->i_dir_start_lookup = 0; | ||
1090 | |||
1091 | /* | ||
1092 | * NOTE! The in-memory inode i_data array is in little-endian order | ||
1093 | * even on big-endian machines: we do NOT byteswap the block numbers! | ||
1094 | */ | ||
1095 | for (n = 0; n < EXT2_N_BLOCKS; n++) | ||
1096 | ei->i_data[n] = raw_inode->i_block[n]; | ||
1097 | |||
1098 | if (S_ISREG(inode->i_mode)) { | ||
1099 | inode->i_op = &ext2_file_inode_operations; | ||
1100 | inode->i_fop = &ext2_file_operations; | ||
1101 | if (test_opt(inode->i_sb, NOBH)) | ||
1102 | inode->i_mapping->a_ops = &ext2_nobh_aops; | ||
1103 | else | ||
1104 | inode->i_mapping->a_ops = &ext2_aops; | ||
1105 | } else if (S_ISDIR(inode->i_mode)) { | ||
1106 | inode->i_op = &ext2_dir_inode_operations; | ||
1107 | inode->i_fop = &ext2_dir_operations; | ||
1108 | if (test_opt(inode->i_sb, NOBH)) | ||
1109 | inode->i_mapping->a_ops = &ext2_nobh_aops; | ||
1110 | else | ||
1111 | inode->i_mapping->a_ops = &ext2_aops; | ||
1112 | } else if (S_ISLNK(inode->i_mode)) { | ||
1113 | if (ext2_inode_is_fast_symlink(inode)) | ||
1114 | inode->i_op = &ext2_fast_symlink_inode_operations; | ||
1115 | else { | ||
1116 | inode->i_op = &ext2_symlink_inode_operations; | ||
1117 | if (test_opt(inode->i_sb, NOBH)) | ||
1118 | inode->i_mapping->a_ops = &ext2_nobh_aops; | ||
1119 | else | ||
1120 | inode->i_mapping->a_ops = &ext2_aops; | ||
1121 | } | ||
1122 | } else { | ||
1123 | inode->i_op = &ext2_special_inode_operations; | ||
1124 | if (raw_inode->i_block[0]) | ||
1125 | init_special_inode(inode, inode->i_mode, | ||
1126 | old_decode_dev(le32_to_cpu(raw_inode->i_block[0]))); | ||
1127 | else | ||
1128 | init_special_inode(inode, inode->i_mode, | ||
1129 | new_decode_dev(le32_to_cpu(raw_inode->i_block[1]))); | ||
1130 | } | ||
1131 | brelse (bh); | ||
1132 | ext2_set_inode_flags(inode); | ||
1133 | return; | ||
1134 | |||
1135 | bad_inode: | ||
1136 | make_bad_inode(inode); | ||
1137 | return; | ||
1138 | } | ||
1139 | |||
1140 | static int ext2_update_inode(struct inode * inode, int do_sync) | ||
1141 | { | ||
1142 | struct ext2_inode_info *ei = EXT2_I(inode); | ||
1143 | struct super_block *sb = inode->i_sb; | ||
1144 | ino_t ino = inode->i_ino; | ||
1145 | uid_t uid = inode->i_uid; | ||
1146 | gid_t gid = inode->i_gid; | ||
1147 | struct buffer_head * bh; | ||
1148 | struct ext2_inode * raw_inode = ext2_get_inode(sb, ino, &bh); | ||
1149 | int n; | ||
1150 | int err = 0; | ||
1151 | |||
1152 | if (IS_ERR(raw_inode)) | ||
1153 | return -EIO; | ||
1154 | |||
1155 | /* For fields not not tracking in the in-memory inode, | ||
1156 | * initialise them to zero for new inodes. */ | ||
1157 | if (ei->i_state & EXT2_STATE_NEW) | ||
1158 | memset(raw_inode, 0, EXT2_SB(sb)->s_inode_size); | ||
1159 | |||
1160 | raw_inode->i_mode = cpu_to_le16(inode->i_mode); | ||
1161 | if (!(test_opt(sb, NO_UID32))) { | ||
1162 | raw_inode->i_uid_low = cpu_to_le16(low_16_bits(uid)); | ||
1163 | raw_inode->i_gid_low = cpu_to_le16(low_16_bits(gid)); | ||
1164 | /* | ||
1165 | * Fix up interoperability with old kernels. Otherwise, old inodes get | ||
1166 | * re-used with the upper 16 bits of the uid/gid intact | ||
1167 | */ | ||
1168 | if (!ei->i_dtime) { | ||
1169 | raw_inode->i_uid_high = cpu_to_le16(high_16_bits(uid)); | ||
1170 | raw_inode->i_gid_high = cpu_to_le16(high_16_bits(gid)); | ||
1171 | } else { | ||
1172 | raw_inode->i_uid_high = 0; | ||
1173 | raw_inode->i_gid_high = 0; | ||
1174 | } | ||
1175 | } else { | ||
1176 | raw_inode->i_uid_low = cpu_to_le16(fs_high2lowuid(uid)); | ||
1177 | raw_inode->i_gid_low = cpu_to_le16(fs_high2lowgid(gid)); | ||
1178 | raw_inode->i_uid_high = 0; | ||
1179 | raw_inode->i_gid_high = 0; | ||
1180 | } | ||
1181 | raw_inode->i_links_count = cpu_to_le16(inode->i_nlink); | ||
1182 | raw_inode->i_size = cpu_to_le32(inode->i_size); | ||
1183 | raw_inode->i_atime = cpu_to_le32(inode->i_atime.tv_sec); | ||
1184 | raw_inode->i_ctime = cpu_to_le32(inode->i_ctime.tv_sec); | ||
1185 | raw_inode->i_mtime = cpu_to_le32(inode->i_mtime.tv_sec); | ||
1186 | |||
1187 | raw_inode->i_blocks = cpu_to_le32(inode->i_blocks); | ||
1188 | raw_inode->i_dtime = cpu_to_le32(ei->i_dtime); | ||
1189 | raw_inode->i_flags = cpu_to_le32(ei->i_flags); | ||
1190 | raw_inode->i_faddr = cpu_to_le32(ei->i_faddr); | ||
1191 | raw_inode->i_frag = ei->i_frag_no; | ||
1192 | raw_inode->i_fsize = ei->i_frag_size; | ||
1193 | raw_inode->i_file_acl = cpu_to_le32(ei->i_file_acl); | ||
1194 | if (!S_ISREG(inode->i_mode)) | ||
1195 | raw_inode->i_dir_acl = cpu_to_le32(ei->i_dir_acl); | ||
1196 | else { | ||
1197 | raw_inode->i_size_high = cpu_to_le32(inode->i_size >> 32); | ||
1198 | if (inode->i_size > 0x7fffffffULL) { | ||
1199 | if (!EXT2_HAS_RO_COMPAT_FEATURE(sb, | ||
1200 | EXT2_FEATURE_RO_COMPAT_LARGE_FILE) || | ||
1201 | EXT2_SB(sb)->s_es->s_rev_level == | ||
1202 | cpu_to_le32(EXT2_GOOD_OLD_REV)) { | ||
1203 | /* If this is the first large file | ||
1204 | * created, add a flag to the superblock. | ||
1205 | */ | ||
1206 | lock_kernel(); | ||
1207 | ext2_update_dynamic_rev(sb); | ||
1208 | EXT2_SET_RO_COMPAT_FEATURE(sb, | ||
1209 | EXT2_FEATURE_RO_COMPAT_LARGE_FILE); | ||
1210 | unlock_kernel(); | ||
1211 | ext2_write_super(sb); | ||
1212 | } | ||
1213 | } | ||
1214 | } | ||
1215 | |||
1216 | raw_inode->i_generation = cpu_to_le32(inode->i_generation); | ||
1217 | if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) { | ||
1218 | if (old_valid_dev(inode->i_rdev)) { | ||
1219 | raw_inode->i_block[0] = | ||
1220 | cpu_to_le32(old_encode_dev(inode->i_rdev)); | ||
1221 | raw_inode->i_block[1] = 0; | ||
1222 | } else { | ||
1223 | raw_inode->i_block[0] = 0; | ||
1224 | raw_inode->i_block[1] = | ||
1225 | cpu_to_le32(new_encode_dev(inode->i_rdev)); | ||
1226 | raw_inode->i_block[2] = 0; | ||
1227 | } | ||
1228 | } else for (n = 0; n < EXT2_N_BLOCKS; n++) | ||
1229 | raw_inode->i_block[n] = ei->i_data[n]; | ||
1230 | mark_buffer_dirty(bh); | ||
1231 | if (do_sync) { | ||
1232 | sync_dirty_buffer(bh); | ||
1233 | if (buffer_req(bh) && !buffer_uptodate(bh)) { | ||
1234 | printk ("IO error syncing ext2 inode [%s:%08lx]\n", | ||
1235 | sb->s_id, (unsigned long) ino); | ||
1236 | err = -EIO; | ||
1237 | } | ||
1238 | } | ||
1239 | ei->i_state &= ~EXT2_STATE_NEW; | ||
1240 | brelse (bh); | ||
1241 | return err; | ||
1242 | } | ||
1243 | |||
1244 | int ext2_write_inode(struct inode *inode, int wait) | ||
1245 | { | ||
1246 | return ext2_update_inode(inode, wait); | ||
1247 | } | ||
1248 | |||
1249 | int ext2_sync_inode(struct inode *inode) | ||
1250 | { | ||
1251 | struct writeback_control wbc = { | ||
1252 | .sync_mode = WB_SYNC_ALL, | ||
1253 | .nr_to_write = 0, /* sys_fsync did this */ | ||
1254 | }; | ||
1255 | return sync_inode(inode, &wbc); | ||
1256 | } | ||
1257 | |||
1258 | int ext2_setattr(struct dentry *dentry, struct iattr *iattr) | ||
1259 | { | ||
1260 | struct inode *inode = dentry->d_inode; | ||
1261 | int error; | ||
1262 | |||
1263 | error = inode_change_ok(inode, iattr); | ||
1264 | if (error) | ||
1265 | return error; | ||
1266 | if ((iattr->ia_valid & ATTR_UID && iattr->ia_uid != inode->i_uid) || | ||
1267 | (iattr->ia_valid & ATTR_GID && iattr->ia_gid != inode->i_gid)) { | ||
1268 | error = DQUOT_TRANSFER(inode, iattr) ? -EDQUOT : 0; | ||
1269 | if (error) | ||
1270 | return error; | ||
1271 | } | ||
1272 | error = inode_setattr(inode, iattr); | ||
1273 | if (!error && (iattr->ia_valid & ATTR_MODE)) | ||
1274 | error = ext2_acl_chmod(inode); | ||
1275 | return error; | ||
1276 | } | ||