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