]>
Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
1da177e4 LT |
2 | /* |
3 | * linux/fs/ufs/inode.c | |
4 | * | |
5 | * Copyright (C) 1998 | |
6 | * Daniel Pirkl <[email protected]> | |
7 | * Charles University, Faculty of Mathematics and Physics | |
8 | * | |
9 | * from | |
10 | * | |
11 | * linux/fs/ext2/inode.c | |
12 | * | |
13 | * Copyright (C) 1992, 1993, 1994, 1995 | |
14 | * Remy Card ([email protected]) | |
15 | * Laboratoire MASI - Institut Blaise Pascal | |
16 | * Universite Pierre et Marie Curie (Paris VI) | |
17 | * | |
18 | * from | |
19 | * | |
20 | * linux/fs/minix/inode.c | |
21 | * | |
22 | * Copyright (C) 1991, 1992 Linus Torvalds | |
23 | * | |
24 | * Goal-directed block allocation by Stephen Tweedie ([email protected]), 1993 | |
25 | * Big-endian to little-endian byte-swapping/bitmaps by | |
26 | * David S. Miller ([email protected]), 1995 | |
27 | */ | |
28 | ||
7c0f6ba6 | 29 | #include <linux/uaccess.h> |
1da177e4 LT |
30 | |
31 | #include <linux/errno.h> | |
32 | #include <linux/fs.h> | |
1da177e4 LT |
33 | #include <linux/time.h> |
34 | #include <linux/stat.h> | |
35 | #include <linux/string.h> | |
36 | #include <linux/mm.h> | |
1da177e4 | 37 | #include <linux/buffer_head.h> |
af34acc2 | 38 | #include <linux/mpage.h> |
a9185b41 | 39 | #include <linux/writeback.h> |
bb8c2d66 | 40 | #include <linux/iversion.h> |
1da177e4 | 41 | |
e5420598 | 42 | #include "ufs_fs.h" |
bcd6d4ec | 43 | #include "ufs.h" |
1da177e4 LT |
44 | #include "swab.h" |
45 | #include "util.h" | |
46 | ||
4e3911f3 | 47 | static int ufs_block_to_path(struct inode *inode, sector_t i_block, unsigned offsets[4]) |
1da177e4 LT |
48 | { |
49 | struct ufs_sb_private_info *uspi = UFS_SB(inode->i_sb)->s_uspi; | |
50 | int ptrs = uspi->s_apb; | |
51 | int ptrs_bits = uspi->s_apbshift; | |
52 | const long direct_blocks = UFS_NDADDR, | |
53 | indirect_blocks = ptrs, | |
54 | double_blocks = (1 << (ptrs_bits * 2)); | |
55 | int n = 0; | |
56 | ||
57 | ||
abf5d15f | 58 | UFSD("ptrs=uspi->s_apb = %d,double_blocks=%ld \n",ptrs,double_blocks); |
37044c86 | 59 | if (i_block < direct_blocks) { |
1da177e4 LT |
60 | offsets[n++] = i_block; |
61 | } else if ((i_block -= direct_blocks) < indirect_blocks) { | |
62 | offsets[n++] = UFS_IND_BLOCK; | |
63 | offsets[n++] = i_block; | |
64 | } else if ((i_block -= indirect_blocks) < double_blocks) { | |
65 | offsets[n++] = UFS_DIND_BLOCK; | |
66 | offsets[n++] = i_block >> ptrs_bits; | |
67 | offsets[n++] = i_block & (ptrs - 1); | |
68 | } else if (((i_block -= double_blocks) >> (ptrs_bits * 2)) < ptrs) { | |
69 | offsets[n++] = UFS_TIND_BLOCK; | |
70 | offsets[n++] = i_block >> (ptrs_bits * 2); | |
71 | offsets[n++] = (i_block >> ptrs_bits) & (ptrs - 1); | |
72 | offsets[n++] = i_block & (ptrs - 1); | |
73 | } else { | |
74 | ufs_warning(inode->i_sb, "ufs_block_to_path", "block > big"); | |
75 | } | |
76 | return n; | |
77 | } | |
78 | ||
724bb09f AV |
79 | typedef struct { |
80 | void *p; | |
81 | union { | |
82 | __fs32 key32; | |
83 | __fs64 key64; | |
84 | }; | |
85 | struct buffer_head *bh; | |
86 | } Indirect; | |
87 | ||
88 | static inline int grow_chain32(struct ufs_inode_info *ufsi, | |
89 | struct buffer_head *bh, __fs32 *v, | |
90 | Indirect *from, Indirect *to) | |
91 | { | |
92 | Indirect *p; | |
93 | unsigned seq; | |
94 | to->bh = bh; | |
95 | do { | |
96 | seq = read_seqbegin(&ufsi->meta_lock); | |
97 | to->key32 = *(__fs32 *)(to->p = v); | |
98 | for (p = from; p <= to && p->key32 == *(__fs32 *)p->p; p++) | |
99 | ; | |
100 | } while (read_seqretry(&ufsi->meta_lock, seq)); | |
101 | return (p > to); | |
102 | } | |
103 | ||
104 | static inline int grow_chain64(struct ufs_inode_info *ufsi, | |
105 | struct buffer_head *bh, __fs64 *v, | |
106 | Indirect *from, Indirect *to) | |
107 | { | |
108 | Indirect *p; | |
109 | unsigned seq; | |
110 | to->bh = bh; | |
111 | do { | |
112 | seq = read_seqbegin(&ufsi->meta_lock); | |
113 | to->key64 = *(__fs64 *)(to->p = v); | |
114 | for (p = from; p <= to && p->key64 == *(__fs64 *)p->p; p++) | |
115 | ; | |
116 | } while (read_seqretry(&ufsi->meta_lock, seq)); | |
117 | return (p > to); | |
118 | } | |
119 | ||
1da177e4 LT |
120 | /* |
121 | * Returns the location of the fragment from | |
25985edc | 122 | * the beginning of the filesystem. |
1da177e4 LT |
123 | */ |
124 | ||
4b7068c8 | 125 | static u64 ufs_frag_map(struct inode *inode, unsigned offsets[4], int depth) |
1da177e4 LT |
126 | { |
127 | struct ufs_inode_info *ufsi = UFS_I(inode); | |
128 | struct super_block *sb = inode->i_sb; | |
129 | struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi; | |
130 | u64 mask = (u64) uspi->s_apbmask>>uspi->s_fpbshift; | |
131 | int shift = uspi->s_apbshift-uspi->s_fpbshift; | |
724bb09f | 132 | Indirect chain[4], *q = chain; |
4b7068c8 | 133 | unsigned *p; |
1da177e4 | 134 | unsigned flags = UFS_SB(sb)->s_flags; |
724bb09f | 135 | u64 res = 0; |
1da177e4 | 136 | |
7256d819 AM |
137 | UFSD(": uspi->s_fpbshift = %d ,uspi->s_apbmask = %x, mask=%llx\n", |
138 | uspi->s_fpbshift, uspi->s_apbmask, | |
139 | (unsigned long long)mask); | |
1da177e4 LT |
140 | |
141 | if (depth == 0) | |
724bb09f | 142 | goto no_block; |
1da177e4 | 143 | |
724bb09f | 144 | again: |
1da177e4 LT |
145 | p = offsets; |
146 | ||
1da177e4 LT |
147 | if ((flags & UFS_TYPE_MASK) == UFS_TYPE_UFS2) |
148 | goto ufs2; | |
149 | ||
724bb09f AV |
150 | if (!grow_chain32(ufsi, NULL, &ufsi->i_u1.i_data[*p++], chain, q)) |
151 | goto changed; | |
152 | if (!q->key32) | |
153 | goto no_block; | |
1da177e4 | 154 | while (--depth) { |
724bb09f | 155 | __fs32 *ptr; |
1da177e4 | 156 | struct buffer_head *bh; |
4e3911f3 | 157 | unsigned n = *p++; |
1da177e4 | 158 | |
724bb09f AV |
159 | bh = sb_bread(sb, uspi->s_sbbase + |
160 | fs32_to_cpu(sb, q->key32) + (n>>shift)); | |
1da177e4 | 161 | if (!bh) |
724bb09f AV |
162 | goto no_block; |
163 | ptr = (__fs32 *)bh->b_data + (n & mask); | |
164 | if (!grow_chain32(ufsi, bh, ptr, chain, ++q)) | |
165 | goto changed; | |
166 | if (!q->key32) | |
167 | goto no_block; | |
1da177e4 | 168 | } |
724bb09f AV |
169 | res = fs32_to_cpu(sb, q->key32); |
170 | goto found; | |
1da177e4 | 171 | |
724bb09f AV |
172 | ufs2: |
173 | if (!grow_chain64(ufsi, NULL, &ufsi->i_u1.u2_i_data[*p++], chain, q)) | |
174 | goto changed; | |
175 | if (!q->key64) | |
176 | goto no_block; | |
1da177e4 LT |
177 | |
178 | while (--depth) { | |
724bb09f | 179 | __fs64 *ptr; |
1da177e4 | 180 | struct buffer_head *bh; |
4e3911f3 | 181 | unsigned n = *p++; |
1da177e4 | 182 | |
724bb09f AV |
183 | bh = sb_bread(sb, uspi->s_sbbase + |
184 | fs64_to_cpu(sb, q->key64) + (n>>shift)); | |
1da177e4 | 185 | if (!bh) |
724bb09f AV |
186 | goto no_block; |
187 | ptr = (__fs64 *)bh->b_data + (n & mask); | |
188 | if (!grow_chain64(ufsi, bh, ptr, chain, ++q)) | |
189 | goto changed; | |
190 | if (!q->key64) | |
191 | goto no_block; | |
192 | } | |
193 | res = fs64_to_cpu(sb, q->key64); | |
194 | found: | |
4b7068c8 | 195 | res += uspi->s_sbbase; |
724bb09f AV |
196 | no_block: |
197 | while (q > chain) { | |
198 | brelse(q->bh); | |
199 | q--; | |
1da177e4 | 200 | } |
724bb09f | 201 | return res; |
1da177e4 | 202 | |
724bb09f AV |
203 | changed: |
204 | while (q > chain) { | |
205 | brelse(q->bh); | |
206 | q--; | |
207 | } | |
208 | goto again; | |
1da177e4 LT |
209 | } |
210 | ||
0f3c1294 AV |
211 | /* |
212 | * Unpacking tails: we have a file with partial final block and | |
213 | * we had been asked to extend it. If the fragment being written | |
214 | * is within the same block, we need to extend the tail just to cover | |
215 | * that fragment. Otherwise the tail is extended to full block. | |
216 | * | |
217 | * Note that we might need to create a _new_ tail, but that will | |
218 | * be handled elsewhere; this is strictly for resizing old | |
219 | * ones. | |
220 | */ | |
221 | static bool | |
222 | ufs_extend_tail(struct inode *inode, u64 writes_to, | |
223 | int *err, struct page *locked_page) | |
224 | { | |
225 | struct ufs_inode_info *ufsi = UFS_I(inode); | |
226 | struct super_block *sb = inode->i_sb; | |
227 | struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi; | |
228 | unsigned lastfrag = ufsi->i_lastfrag; /* it's a short file, so unsigned is enough */ | |
229 | unsigned block = ufs_fragstoblks(lastfrag); | |
230 | unsigned new_size; | |
231 | void *p; | |
232 | u64 tmp; | |
233 | ||
234 | if (writes_to < (lastfrag | uspi->s_fpbmask)) | |
235 | new_size = (writes_to & uspi->s_fpbmask) + 1; | |
236 | else | |
237 | new_size = uspi->s_fpb; | |
238 | ||
239 | p = ufs_get_direct_data_ptr(uspi, ufsi, block); | |
240 | tmp = ufs_new_fragments(inode, p, lastfrag, ufs_data_ptr_to_cpu(sb, p), | |
940ef1a0 AV |
241 | new_size - (lastfrag & uspi->s_fpbmask), err, |
242 | locked_page); | |
0f3c1294 AV |
243 | return tmp != 0; |
244 | } | |
245 | ||
022a6dc5 ED |
246 | /** |
247 | * ufs_inode_getfrag() - allocate new fragment(s) | |
edc023ca | 248 | * @inode: pointer to inode |
5336970b | 249 | * @index: number of block pointer within the inode's array. |
edc023ca | 250 | * @new_fragment: number of new allocated fragment(s) |
edc023ca | 251 | * @err: we set it if something wrong |
edc023ca FF |
252 | * @new: we set it if we allocate new block |
253 | * @locked_page: for ufs_new_fragments() | |
022a6dc5 | 254 | */ |
177848a0 | 255 | static u64 |
5336970b AV |
256 | ufs_inode_getfrag(struct inode *inode, unsigned index, |
257 | sector_t new_fragment, int *err, | |
4e317ce7 | 258 | int *new, struct page *locked_page) |
1da177e4 LT |
259 | { |
260 | struct ufs_inode_info *ufsi = UFS_I(inode); | |
022a6dc5 ED |
261 | struct super_block *sb = inode->i_sb; |
262 | struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi; | |
5336970b | 263 | u64 tmp, goal, lastfrag; |
0f3c1294 AV |
264 | unsigned nfrags = uspi->s_fpb; |
265 | void *p; | |
1da177e4 | 266 | |
1da177e4 LT |
267 | /* TODO : to be done for write support |
268 | if ( (flags & UFS_TYPE_MASK) == UFS_TYPE_UFS2) | |
269 | goto ufs2; | |
270 | */ | |
271 | ||
5336970b | 272 | p = ufs_get_direct_data_ptr(uspi, ufsi, index); |
54fb996a | 273 | tmp = ufs_data_ptr_to_cpu(sb, p); |
0f3c1294 AV |
274 | if (tmp) |
275 | goto out; | |
54fb996a | 276 | |
1da177e4 | 277 | lastfrag = ufsi->i_lastfrag; |
1da177e4 | 278 | |
0f3c1294 AV |
279 | /* will that be a new tail? */ |
280 | if (new_fragment < UFS_NDIR_FRAGMENT && new_fragment >= lastfrag) | |
281 | nfrags = (new_fragment & uspi->s_fpbmask) + 1; | |
282 | ||
283 | goal = 0; | |
5336970b | 284 | if (index) { |
0f3c1294 | 285 | goal = ufs_data_ptr_to_cpu(sb, |
5336970b | 286 | ufs_get_direct_data_ptr(uspi, ufsi, index - 1)); |
0f3c1294 AV |
287 | if (goal) |
288 | goal += uspi->s_fpb; | |
1da177e4 | 289 | } |
5336970b | 290 | tmp = ufs_new_fragments(inode, p, ufs_blknum(new_fragment), |
8785d84d | 291 | goal, nfrags, err, locked_page); |
0f3c1294 | 292 | |
1da177e4 | 293 | if (!tmp) { |
1da177e4 | 294 | *err = -ENOSPC; |
177848a0 | 295 | return 0; |
1da177e4 LT |
296 | } |
297 | ||
4e317ce7 | 298 | if (new) |
1da177e4 | 299 | *new = 1; |
6eeb017e | 300 | inode_set_ctime_current(inode); |
1da177e4 LT |
301 | if (IS_SYNC(inode)) |
302 | ufs_sync_inode (inode); | |
303 | mark_inode_dirty(inode); | |
bbb3eb9d | 304 | out: |
177848a0 | 305 | return tmp + uspi->s_sbbase; |
1da177e4 LT |
306 | |
307 | /* This part : To be implemented .... | |
308 | Required only for writing, not required for READ-ONLY. | |
309 | ufs2: | |
310 | ||
311 | u2_block = ufs_fragstoblks(fragment); | |
312 | u2_blockoff = ufs_fragnum(fragment); | |
313 | p = ufsi->i_u1.u2_i_data + block; | |
314 | goal = 0; | |
315 | ||
316 | repeat2: | |
317 | tmp = fs32_to_cpu(sb, *p); | |
318 | lastfrag = ufsi->i_lastfrag; | |
319 | ||
320 | */ | |
321 | } | |
322 | ||
022a6dc5 ED |
323 | /** |
324 | * ufs_inode_getblock() - allocate new block | |
edc023ca | 325 | * @inode: pointer to inode |
619cfac0 AV |
326 | * @ind_block: block number of the indirect block |
327 | * @index: number of pointer within the indirect block | |
edc023ca | 328 | * @new_fragment: number of new allocated fragment |
022a6dc5 | 329 | * (block will hold this fragment and also uspi->s_fpb-1) |
edc023ca | 330 | * @err: see ufs_inode_getfrag() |
edc023ca FF |
331 | * @new: see ufs_inode_getfrag() |
332 | * @locked_page: see ufs_inode_getfrag() | |
022a6dc5 | 333 | */ |
177848a0 | 334 | static u64 |
619cfac0 | 335 | ufs_inode_getblock(struct inode *inode, u64 ind_block, |
721435a7 | 336 | unsigned index, sector_t new_fragment, int *err, |
4e317ce7 | 337 | int *new, struct page *locked_page) |
1da177e4 | 338 | { |
022a6dc5 ED |
339 | struct super_block *sb = inode->i_sb; |
340 | struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi; | |
619cfac0 | 341 | int shift = uspi->s_apbshift - uspi->s_fpbshift; |
721435a7 | 342 | u64 tmp = 0, goal; |
619cfac0 | 343 | struct buffer_head *bh; |
54fb996a | 344 | void *p; |
1da177e4 | 345 | |
619cfac0 AV |
346 | if (!ind_block) |
347 | return 0; | |
348 | ||
349 | bh = sb_bread(sb, ind_block + (index >> shift)); | |
5fbfb238 AV |
350 | if (unlikely(!bh)) { |
351 | *err = -EIO; | |
619cfac0 | 352 | return 0; |
5fbfb238 | 353 | } |
619cfac0 AV |
354 | |
355 | index &= uspi->s_apbmask >> uspi->s_fpbshift; | |
54fb996a | 356 | if (uspi->fs_magic == UFS2_MAGIC) |
721435a7 | 357 | p = (__fs64 *)bh->b_data + index; |
54fb996a | 358 | else |
721435a7 | 359 | p = (__fs32 *)bh->b_data + index; |
5a39c255 | 360 | |
54fb996a | 361 | tmp = ufs_data_ptr_to_cpu(sb, p); |
bbb3eb9d | 362 | if (tmp) |
5a39c255 | 363 | goto out; |
1da177e4 | 364 | |
721435a7 AV |
365 | if (index && (uspi->fs_magic == UFS2_MAGIC ? |
366 | (tmp = fs64_to_cpu(sb, ((__fs64 *)bh->b_data)[index-1])) : | |
367 | (tmp = fs32_to_cpu(sb, ((__fs32 *)bh->b_data)[index-1])))) | |
1da177e4 LT |
368 | goal = tmp + uspi->s_fpb; |
369 | else | |
370 | goal = bh->b_blocknr + uspi->s_fpb; | |
6ef4d6bf ED |
371 | tmp = ufs_new_fragments(inode, p, ufs_blknum(new_fragment), goal, |
372 | uspi->s_fpb, err, locked_page); | |
5a39c255 | 373 | if (!tmp) |
1da177e4 | 374 | goto out; |
c9a27b5d | 375 | |
bbb3eb9d | 376 | if (new) |
1da177e4 | 377 | *new = 1; |
1da177e4 LT |
378 | |
379 | mark_buffer_dirty(bh); | |
380 | if (IS_SYNC(inode)) | |
381 | sync_dirty_buffer(bh); | |
6eeb017e | 382 | inode_set_ctime_current(inode); |
1da177e4 LT |
383 | mark_inode_dirty(inode); |
384 | out: | |
385 | brelse (bh); | |
abf5d15f | 386 | UFSD("EXIT\n"); |
177848a0 AV |
387 | if (tmp) |
388 | tmp += uspi->s_sbbase; | |
389 | return tmp; | |
1da177e4 LT |
390 | } |
391 | ||
022a6dc5 | 392 | /** |
7422caa5 | 393 | * ufs_getfrag_block() - `get_block_t' function, interface between UFS and |
af34acc2 | 394 | * read_folio, writepages and so on |
1da177e4 LT |
395 | */ |
396 | ||
010d331f | 397 | static int ufs_getfrag_block(struct inode *inode, sector_t fragment, struct buffer_head *bh_result, int create) |
1da177e4 | 398 | { |
0385f1f9 AV |
399 | struct super_block *sb = inode->i_sb; |
400 | struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi; | |
401 | int err = 0, new = 0; | |
4b7068c8 AV |
402 | unsigned offsets[4]; |
403 | int depth = ufs_block_to_path(inode, fragment >> uspi->s_fpbshift, offsets); | |
1da177e4 | 404 | u64 phys64 = 0; |
177848a0 | 405 | unsigned frag = fragment & uspi->s_fpbmask; |
010d331f | 406 | |
09bf4f5b AV |
407 | phys64 = ufs_frag_map(inode, offsets, depth); |
408 | if (!create) | |
409 | goto done; | |
1da177e4 | 410 | |
09bf4f5b AV |
411 | if (phys64) { |
412 | if (fragment >= UFS_NDIR_FRAGMENT) | |
413 | goto done; | |
414 | read_seqlock_excl(&UFS_I(inode)->meta_lock); | |
415 | if (fragment < UFS_I(inode)->i_lastfrag) { | |
416 | read_sequnlock_excl(&UFS_I(inode)->meta_lock); | |
417 | goto done; | |
418 | } | |
419 | read_sequnlock_excl(&UFS_I(inode)->meta_lock); | |
420 | } | |
1da177e4 LT |
421 | /* This code entered only while writing ....? */ |
422 | ||
724bb09f | 423 | mutex_lock(&UFS_I(inode)->truncate_mutex); |
1da177e4 | 424 | |
abf5d15f | 425 | UFSD("ENTER, ino %lu, fragment %llu\n", inode->i_ino, (unsigned long long)fragment); |
0385f1f9 AV |
426 | if (unlikely(!depth)) { |
427 | ufs_warning(sb, "ufs_get_block", "block > big"); | |
428 | err = -EIO; | |
429 | goto out; | |
430 | } | |
0f3c1294 AV |
431 | |
432 | if (UFS_I(inode)->i_lastfrag < UFS_NDIR_FRAGMENT) { | |
433 | unsigned lastfrag = UFS_I(inode)->i_lastfrag; | |
434 | unsigned tailfrags = lastfrag & uspi->s_fpbmask; | |
435 | if (tailfrags && fragment >= lastfrag) { | |
436 | if (!ufs_extend_tail(inode, fragment, | |
437 | &err, bh_result->b_page)) | |
0385f1f9 | 438 | goto out; |
0f3c1294 AV |
439 | } |
440 | } | |
441 | ||
71dd4284 | 442 | if (depth == 1) { |
5336970b | 443 | phys64 = ufs_inode_getfrag(inode, offsets[0], fragment, |
4e317ce7 | 444 | &err, &new, bh_result->b_page); |
4eeff4c9 AV |
445 | } else { |
446 | int i; | |
5336970b | 447 | phys64 = ufs_inode_getfrag(inode, offsets[0], fragment, |
4e317ce7 | 448 | &err, NULL, NULL); |
4eeff4c9 AV |
449 | for (i = 1; i < depth - 1; i++) |
450 | phys64 = ufs_inode_getblock(inode, phys64, offsets[i], | |
4e317ce7 | 451 | fragment, &err, NULL, NULL); |
4eeff4c9 | 452 | phys64 = ufs_inode_getblock(inode, phys64, offsets[depth - 1], |
4e317ce7 | 453 | fragment, &err, &new, bh_result->b_page); |
1da177e4 | 454 | } |
0385f1f9 | 455 | out: |
177848a0 AV |
456 | if (phys64) { |
457 | phys64 += frag; | |
0385f1f9 AV |
458 | map_bh(bh_result, sb, phys64); |
459 | if (new) | |
460 | set_buffer_new(bh_result); | |
177848a0 | 461 | } |
724bb09f | 462 | mutex_unlock(&UFS_I(inode)->truncate_mutex); |
1da177e4 | 463 | return err; |
09bf4f5b AV |
464 | |
465 | done: | |
466 | if (phys64) | |
467 | map_bh(bh_result, sb, phys64 + frag); | |
468 | return 0; | |
1da177e4 LT |
469 | } |
470 | ||
af34acc2 MWO |
471 | static int ufs_writepages(struct address_space *mapping, |
472 | struct writeback_control *wbc) | |
1da177e4 | 473 | { |
af34acc2 | 474 | return mpage_writepages(mapping, wbc, ufs_getfrag_block); |
1da177e4 | 475 | } |
82b9d1d0 | 476 | |
2c69e205 | 477 | static int ufs_read_folio(struct file *file, struct folio *folio) |
1da177e4 | 478 | { |
2c69e205 | 479 | return block_read_full_folio(folio, ufs_getfrag_block); |
1da177e4 | 480 | } |
82b9d1d0 | 481 | |
f4e420dc | 482 | int ufs_prepare_chunk(struct page *page, loff_t pos, unsigned len) |
1da177e4 | 483 | { |
6e1db88d | 484 | return __block_write_begin(page, pos, len, ufs_getfrag_block); |
1da177e4 | 485 | } |
82b9d1d0 | 486 | |
010d331f AV |
487 | static void ufs_truncate_blocks(struct inode *); |
488 | ||
83f6e371 MS |
489 | static void ufs_write_failed(struct address_space *mapping, loff_t to) |
490 | { | |
491 | struct inode *inode = mapping->host; | |
492 | ||
3b7a3a05 | 493 | if (to > inode->i_size) { |
7caef267 | 494 | truncate_pagecache(inode, inode->i_size); |
3b7a3a05 AV |
495 | ufs_truncate_blocks(inode); |
496 | } | |
83f6e371 MS |
497 | } |
498 | ||
82b9d1d0 | 499 | static int ufs_write_begin(struct file *file, struct address_space *mapping, |
9d6b0cd7 | 500 | loff_t pos, unsigned len, |
82b9d1d0 NP |
501 | struct page **pagep, void **fsdata) |
502 | { | |
155130a4 CH |
503 | int ret; |
504 | ||
b3992d1e | 505 | ret = block_write_begin(mapping, pos, len, pagep, ufs_getfrag_block); |
83f6e371 MS |
506 | if (unlikely(ret)) |
507 | ufs_write_failed(mapping, pos + len); | |
155130a4 CH |
508 | |
509 | return ret; | |
82b9d1d0 NP |
510 | } |
511 | ||
3b7a3a05 AV |
512 | static int ufs_write_end(struct file *file, struct address_space *mapping, |
513 | loff_t pos, unsigned len, unsigned copied, | |
514 | struct page *page, void *fsdata) | |
515 | { | |
516 | int ret; | |
517 | ||
518 | ret = generic_write_end(file, mapping, pos, len, copied, page, fsdata); | |
519 | if (ret < len) | |
520 | ufs_write_failed(mapping, pos + len); | |
521 | return ret; | |
522 | } | |
523 | ||
1da177e4 LT |
524 | static sector_t ufs_bmap(struct address_space *mapping, sector_t block) |
525 | { | |
526 | return generic_block_bmap(mapping,block,ufs_getfrag_block); | |
527 | } | |
82b9d1d0 | 528 | |
f5e54d6e | 529 | const struct address_space_operations ufs_aops = { |
e621900a | 530 | .dirty_folio = block_dirty_folio, |
7ba13abb | 531 | .invalidate_folio = block_invalidate_folio, |
2c69e205 | 532 | .read_folio = ufs_read_folio, |
af34acc2 | 533 | .writepages = ufs_writepages, |
82b9d1d0 | 534 | .write_begin = ufs_write_begin, |
3b7a3a05 | 535 | .write_end = ufs_write_end, |
af34acc2 | 536 | .migrate_folio = buffer_migrate_folio, |
1da177e4 LT |
537 | .bmap = ufs_bmap |
538 | }; | |
539 | ||
826843a3 ED |
540 | static void ufs_set_inode_ops(struct inode *inode) |
541 | { | |
542 | if (S_ISREG(inode->i_mode)) { | |
543 | inode->i_op = &ufs_file_inode_operations; | |
544 | inode->i_fop = &ufs_file_operations; | |
545 | inode->i_mapping->a_ops = &ufs_aops; | |
546 | } else if (S_ISDIR(inode->i_mode)) { | |
547 | inode->i_op = &ufs_dir_inode_operations; | |
548 | inode->i_fop = &ufs_dir_operations; | |
549 | inode->i_mapping->a_ops = &ufs_aops; | |
550 | } else if (S_ISLNK(inode->i_mode)) { | |
4b8061a6 | 551 | if (!inode->i_blocks) { |
4b8061a6 | 552 | inode->i_link = (char *)UFS_I(inode)->i_u1.i_symlink; |
9cdce3c0 | 553 | inode->i_op = &simple_symlink_inode_operations; |
4b8061a6 | 554 | } else { |
826843a3 | 555 | inode->i_mapping->a_ops = &ufs_aops; |
9cdce3c0 | 556 | inode->i_op = &page_symlink_inode_operations; |
21fc61c7 | 557 | inode_nohighmem(inode); |
826843a3 ED |
558 | } |
559 | } else | |
560 | init_special_inode(inode, inode->i_mode, | |
561 | ufs_get_inode_dev(inode->i_sb, UFS_I(inode))); | |
562 | } | |
563 | ||
07a0cfec | 564 | static int ufs1_read_inode(struct inode *inode, struct ufs_inode *ufs_inode) |
1da177e4 LT |
565 | { |
566 | struct ufs_inode_info *ufsi = UFS_I(inode); | |
05f225dc | 567 | struct super_block *sb = inode->i_sb; |
6a9a06d9 | 568 | umode_t mode; |
1da177e4 LT |
569 | |
570 | /* | |
571 | * Copy data to the in-core inode. | |
572 | */ | |
573 | inode->i_mode = mode = fs16_to_cpu(sb, ufs_inode->ui_mode); | |
bfe86848 | 574 | set_nlink(inode, fs16_to_cpu(sb, ufs_inode->ui_nlink)); |
c0ef65d2 AV |
575 | if (inode->i_nlink == 0) |
576 | return -ESTALE; | |
010d331f | 577 | |
1da177e4 LT |
578 | /* |
579 | * Linux now has 32-bit uid and gid, so we can support EFT. | |
580 | */ | |
72235465 EB |
581 | i_uid_write(inode, ufs_get_inode_uid(sb, ufs_inode)); |
582 | i_gid_write(inode, ufs_get_inode_gid(sb, ufs_inode)); | |
1da177e4 LT |
583 | |
584 | inode->i_size = fs64_to_cpu(sb, ufs_inode->ui_size); | |
d936d382 JL |
585 | inode_set_atime(inode, |
586 | (signed)fs32_to_cpu(sb, ufs_inode->ui_atime.tv_sec), | |
587 | 0); | |
6eeb017e JL |
588 | inode_set_ctime(inode, |
589 | (signed)fs32_to_cpu(sb, ufs_inode->ui_ctime.tv_sec), | |
590 | 0); | |
d936d382 JL |
591 | inode_set_mtime(inode, |
592 | (signed)fs32_to_cpu(sb, ufs_inode->ui_mtime.tv_sec), | |
593 | 0); | |
1da177e4 | 594 | inode->i_blocks = fs32_to_cpu(sb, ufs_inode->ui_blocks); |
3313e292 | 595 | inode->i_generation = fs32_to_cpu(sb, ufs_inode->ui_gen); |
1da177e4 | 596 | ufsi->i_flags = fs32_to_cpu(sb, ufs_inode->ui_flags); |
1da177e4 LT |
597 | ufsi->i_shadow = fs32_to_cpu(sb, ufs_inode->ui_u3.ui_sun.ui_shadow); |
598 | ufsi->i_oeftflag = fs32_to_cpu(sb, ufs_inode->ui_u3.ui_sun.ui_oeftflag); | |
05f225dc | 599 | |
010d331f | 600 | |
1da177e4 | 601 | if (S_ISCHR(mode) || S_ISBLK(mode) || inode->i_blocks) { |
f33219b7 DG |
602 | memcpy(ufsi->i_u1.i_data, &ufs_inode->ui_u2.ui_addr, |
603 | sizeof(ufs_inode->ui_u2.ui_addr)); | |
dd187a26 | 604 | } else { |
f33219b7 | 605 | memcpy(ufsi->i_u1.i_symlink, ufs_inode->ui_u2.ui_symlink, |
b12903f1 DG |
606 | sizeof(ufs_inode->ui_u2.ui_symlink) - 1); |
607 | ufsi->i_u1.i_symlink[sizeof(ufs_inode->ui_u2.ui_symlink) - 1] = 0; | |
1da177e4 | 608 | } |
07a0cfec | 609 | return 0; |
05f225dc | 610 | } |
1da177e4 | 611 | |
07a0cfec | 612 | static int ufs2_read_inode(struct inode *inode, struct ufs2_inode *ufs2_inode) |
05f225dc ED |
613 | { |
614 | struct ufs_inode_info *ufsi = UFS_I(inode); | |
615 | struct super_block *sb = inode->i_sb; | |
6a9a06d9 | 616 | umode_t mode; |
1da177e4 | 617 | |
abf5d15f | 618 | UFSD("Reading ufs2 inode, ino %lu\n", inode->i_ino); |
1da177e4 LT |
619 | /* |
620 | * Copy data to the in-core inode. | |
621 | */ | |
622 | inode->i_mode = mode = fs16_to_cpu(sb, ufs2_inode->ui_mode); | |
bfe86848 | 623 | set_nlink(inode, fs16_to_cpu(sb, ufs2_inode->ui_nlink)); |
c0ef65d2 AV |
624 | if (inode->i_nlink == 0) |
625 | return -ESTALE; | |
1da177e4 LT |
626 | |
627 | /* | |
628 | * Linux now has 32-bit uid and gid, so we can support EFT. | |
629 | */ | |
72235465 EB |
630 | i_uid_write(inode, fs32_to_cpu(sb, ufs2_inode->ui_uid)); |
631 | i_gid_write(inode, fs32_to_cpu(sb, ufs2_inode->ui_gid)); | |
1da177e4 LT |
632 | |
633 | inode->i_size = fs64_to_cpu(sb, ufs2_inode->ui_size); | |
d936d382 JL |
634 | inode_set_atime(inode, fs64_to_cpu(sb, ufs2_inode->ui_atime), |
635 | fs32_to_cpu(sb, ufs2_inode->ui_atimensec)); | |
6eeb017e JL |
636 | inode_set_ctime(inode, fs64_to_cpu(sb, ufs2_inode->ui_ctime), |
637 | fs32_to_cpu(sb, ufs2_inode->ui_ctimensec)); | |
d936d382 JL |
638 | inode_set_mtime(inode, fs64_to_cpu(sb, ufs2_inode->ui_mtime), |
639 | fs32_to_cpu(sb, ufs2_inode->ui_mtimensec)); | |
1da177e4 | 640 | inode->i_blocks = fs64_to_cpu(sb, ufs2_inode->ui_blocks); |
3313e292 | 641 | inode->i_generation = fs32_to_cpu(sb, ufs2_inode->ui_gen); |
1da177e4 | 642 | ufsi->i_flags = fs32_to_cpu(sb, ufs2_inode->ui_flags); |
1da177e4 LT |
643 | /* |
644 | ufsi->i_shadow = fs32_to_cpu(sb, ufs_inode->ui_u3.ui_sun.ui_shadow); | |
645 | ufsi->i_oeftflag = fs32_to_cpu(sb, ufs_inode->ui_u3.ui_sun.ui_oeftflag); | |
646 | */ | |
1da177e4 LT |
647 | |
648 | if (S_ISCHR(mode) || S_ISBLK(mode) || inode->i_blocks) { | |
f33219b7 DG |
649 | memcpy(ufsi->i_u1.u2_i_data, &ufs2_inode->ui_u2.ui_addr, |
650 | sizeof(ufs2_inode->ui_u2.ui_addr)); | |
05f225dc | 651 | } else { |
f33219b7 | 652 | memcpy(ufsi->i_u1.i_symlink, ufs2_inode->ui_u2.ui_symlink, |
b12903f1 DG |
653 | sizeof(ufs2_inode->ui_u2.ui_symlink) - 1); |
654 | ufsi->i_u1.i_symlink[sizeof(ufs2_inode->ui_u2.ui_symlink) - 1] = 0; | |
1da177e4 | 655 | } |
07a0cfec | 656 | return 0; |
05f225dc ED |
657 | } |
658 | ||
b55c460d | 659 | struct inode *ufs_iget(struct super_block *sb, unsigned long ino) |
05f225dc | 660 | { |
b55c460d DH |
661 | struct ufs_inode_info *ufsi; |
662 | struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi; | |
05f225dc | 663 | struct buffer_head * bh; |
b55c460d | 664 | struct inode *inode; |
c0ef65d2 | 665 | int err = -EIO; |
05f225dc | 666 | |
b55c460d | 667 | UFSD("ENTER, ino %lu\n", ino); |
05f225dc | 668 | |
b55c460d | 669 | if (ino < UFS_ROOTINO || ino > (uspi->s_ncg * uspi->s_ipg)) { |
05f225dc | 670 | ufs_warning(sb, "ufs_read_inode", "bad inode number (%lu)\n", |
b55c460d DH |
671 | ino); |
672 | return ERR_PTR(-EIO); | |
05f225dc ED |
673 | } |
674 | ||
b55c460d DH |
675 | inode = iget_locked(sb, ino); |
676 | if (!inode) | |
677 | return ERR_PTR(-ENOMEM); | |
678 | if (!(inode->i_state & I_NEW)) | |
679 | return inode; | |
680 | ||
681 | ufsi = UFS_I(inode); | |
682 | ||
05f225dc ED |
683 | bh = sb_bread(sb, uspi->s_sbbase + ufs_inotofsba(inode->i_ino)); |
684 | if (!bh) { | |
685 | ufs_warning(sb, "ufs_read_inode", "unable to read inode %lu\n", | |
686 | inode->i_ino); | |
687 | goto bad_inode; | |
688 | } | |
689 | if ((UFS_SB(sb)->s_flags & UFS_TYPE_MASK) == UFS_TYPE_UFS2) { | |
690 | struct ufs2_inode *ufs2_inode = (struct ufs2_inode *)bh->b_data; | |
691 | ||
07a0cfec ED |
692 | err = ufs2_read_inode(inode, |
693 | ufs2_inode + ufs_inotofsbo(inode->i_ino)); | |
05f225dc ED |
694 | } else { |
695 | struct ufs_inode *ufs_inode = (struct ufs_inode *)bh->b_data; | |
696 | ||
07a0cfec ED |
697 | err = ufs1_read_inode(inode, |
698 | ufs_inode + ufs_inotofsbo(inode->i_ino)); | |
05f225dc | 699 | } |
c0ef65d2 | 700 | brelse(bh); |
07a0cfec ED |
701 | if (err) |
702 | goto bad_inode; | |
c0ef65d2 | 703 | |
bb8c2d66 | 704 | inode_inc_iversion(inode); |
05f225dc ED |
705 | ufsi->i_lastfrag = |
706 | (inode->i_size + uspi->s_fsize - 1) >> uspi->s_fshift; | |
707 | ufsi->i_dir_start_lookup = 0; | |
1da177e4 LT |
708 | ufsi->i_osync = 0; |
709 | ||
826843a3 | 710 | ufs_set_inode_ops(inode); |
1da177e4 | 711 | |
abf5d15f | 712 | UFSD("EXIT\n"); |
b55c460d DH |
713 | unlock_new_inode(inode); |
714 | return inode; | |
05f225dc ED |
715 | |
716 | bad_inode: | |
b55c460d | 717 | iget_failed(inode); |
c0ef65d2 | 718 | return ERR_PTR(err); |
1da177e4 LT |
719 | } |
720 | ||
3313e292 | 721 | static void ufs1_update_inode(struct inode *inode, struct ufs_inode *ufs_inode) |
1da177e4 | 722 | { |
3313e292 ED |
723 | struct super_block *sb = inode->i_sb; |
724 | struct ufs_inode_info *ufsi = UFS_I(inode); | |
1da177e4 LT |
725 | |
726 | ufs_inode->ui_mode = cpu_to_fs16(sb, inode->i_mode); | |
727 | ufs_inode->ui_nlink = cpu_to_fs16(sb, inode->i_nlink); | |
728 | ||
72235465 EB |
729 | ufs_set_inode_uid(sb, ufs_inode, i_uid_read(inode)); |
730 | ufs_set_inode_gid(sb, ufs_inode, i_gid_read(inode)); | |
010d331f | 731 | |
1da177e4 | 732 | ufs_inode->ui_size = cpu_to_fs64(sb, inode->i_size); |
d936d382 JL |
733 | ufs_inode->ui_atime.tv_sec = cpu_to_fs32(sb, |
734 | inode_get_atime_sec(inode)); | |
1da177e4 | 735 | ufs_inode->ui_atime.tv_usec = 0; |
6eeb017e | 736 | ufs_inode->ui_ctime.tv_sec = cpu_to_fs32(sb, |
d936d382 | 737 | inode_get_ctime_sec(inode)); |
1da177e4 | 738 | ufs_inode->ui_ctime.tv_usec = 0; |
d936d382 JL |
739 | ufs_inode->ui_mtime.tv_sec = cpu_to_fs32(sb, |
740 | inode_get_mtime_sec(inode)); | |
1da177e4 LT |
741 | ufs_inode->ui_mtime.tv_usec = 0; |
742 | ufs_inode->ui_blocks = cpu_to_fs32(sb, inode->i_blocks); | |
743 | ufs_inode->ui_flags = cpu_to_fs32(sb, ufsi->i_flags); | |
3313e292 | 744 | ufs_inode->ui_gen = cpu_to_fs32(sb, inode->i_generation); |
1da177e4 | 745 | |
3313e292 | 746 | if ((UFS_SB(sb)->s_flags & UFS_UID_MASK) == UFS_UID_EFT) { |
1da177e4 LT |
747 | ufs_inode->ui_u3.ui_sun.ui_shadow = cpu_to_fs32(sb, ufsi->i_shadow); |
748 | ufs_inode->ui_u3.ui_sun.ui_oeftflag = cpu_to_fs32(sb, ufsi->i_oeftflag); | |
749 | } | |
750 | ||
751 | if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) { | |
752 | /* ufs_inode->ui_u2.ui_addr.ui_db[0] = cpu_to_fs32(sb, inode->i_rdev); */ | |
753 | ufs_inode->ui_u2.ui_addr.ui_db[0] = ufsi->i_u1.i_data[0]; | |
754 | } else if (inode->i_blocks) { | |
f33219b7 DG |
755 | memcpy(&ufs_inode->ui_u2.ui_addr, ufsi->i_u1.i_data, |
756 | sizeof(ufs_inode->ui_u2.ui_addr)); | |
1da177e4 LT |
757 | } |
758 | else { | |
f33219b7 DG |
759 | memcpy(&ufs_inode->ui_u2.ui_symlink, ufsi->i_u1.i_symlink, |
760 | sizeof(ufs_inode->ui_u2.ui_symlink)); | |
1da177e4 LT |
761 | } |
762 | ||
763 | if (!inode->i_nlink) | |
764 | memset (ufs_inode, 0, sizeof(struct ufs_inode)); | |
3313e292 ED |
765 | } |
766 | ||
767 | static void ufs2_update_inode(struct inode *inode, struct ufs2_inode *ufs_inode) | |
768 | { | |
769 | struct super_block *sb = inode->i_sb; | |
770 | struct ufs_inode_info *ufsi = UFS_I(inode); | |
3313e292 ED |
771 | |
772 | UFSD("ENTER\n"); | |
773 | ufs_inode->ui_mode = cpu_to_fs16(sb, inode->i_mode); | |
774 | ufs_inode->ui_nlink = cpu_to_fs16(sb, inode->i_nlink); | |
775 | ||
72235465 EB |
776 | ufs_inode->ui_uid = cpu_to_fs32(sb, i_uid_read(inode)); |
777 | ufs_inode->ui_gid = cpu_to_fs32(sb, i_gid_read(inode)); | |
3313e292 ED |
778 | |
779 | ufs_inode->ui_size = cpu_to_fs64(sb, inode->i_size); | |
d936d382 JL |
780 | ufs_inode->ui_atime = cpu_to_fs64(sb, inode_get_atime_sec(inode)); |
781 | ufs_inode->ui_atimensec = cpu_to_fs32(sb, | |
782 | inode_get_atime_nsec(inode)); | |
783 | ufs_inode->ui_ctime = cpu_to_fs64(sb, inode_get_ctime_sec(inode)); | |
6eeb017e | 784 | ufs_inode->ui_ctimensec = cpu_to_fs32(sb, |
d936d382 JL |
785 | inode_get_ctime_nsec(inode)); |
786 | ufs_inode->ui_mtime = cpu_to_fs64(sb, inode_get_mtime_sec(inode)); | |
787 | ufs_inode->ui_mtimensec = cpu_to_fs32(sb, | |
788 | inode_get_mtime_nsec(inode)); | |
3313e292 ED |
789 | |
790 | ufs_inode->ui_blocks = cpu_to_fs64(sb, inode->i_blocks); | |
791 | ufs_inode->ui_flags = cpu_to_fs32(sb, ufsi->i_flags); | |
792 | ufs_inode->ui_gen = cpu_to_fs32(sb, inode->i_generation); | |
793 | ||
794 | if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) { | |
795 | /* ufs_inode->ui_u2.ui_addr.ui_db[0] = cpu_to_fs32(sb, inode->i_rdev); */ | |
796 | ufs_inode->ui_u2.ui_addr.ui_db[0] = ufsi->i_u1.u2_i_data[0]; | |
797 | } else if (inode->i_blocks) { | |
f33219b7 DG |
798 | memcpy(&ufs_inode->ui_u2.ui_addr, ufsi->i_u1.u2_i_data, |
799 | sizeof(ufs_inode->ui_u2.ui_addr)); | |
3313e292 | 800 | } else { |
f33219b7 DG |
801 | memcpy(&ufs_inode->ui_u2.ui_symlink, ufsi->i_u1.i_symlink, |
802 | sizeof(ufs_inode->ui_u2.ui_symlink)); | |
3313e292 ED |
803 | } |
804 | ||
805 | if (!inode->i_nlink) | |
806 | memset (ufs_inode, 0, sizeof(struct ufs2_inode)); | |
807 | UFSD("EXIT\n"); | |
808 | } | |
809 | ||
810 | static int ufs_update_inode(struct inode * inode, int do_sync) | |
811 | { | |
812 | struct super_block *sb = inode->i_sb; | |
813 | struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi; | |
814 | struct buffer_head * bh; | |
815 | ||
816 | UFSD("ENTER, ino %lu\n", inode->i_ino); | |
817 | ||
818 | if (inode->i_ino < UFS_ROOTINO || | |
819 | inode->i_ino > (uspi->s_ncg * uspi->s_ipg)) { | |
820 | ufs_warning (sb, "ufs_read_inode", "bad inode number (%lu)\n", inode->i_ino); | |
821 | return -1; | |
822 | } | |
823 | ||
824 | bh = sb_bread(sb, ufs_inotofsba(inode->i_ino)); | |
825 | if (!bh) { | |
826 | ufs_warning (sb, "ufs_read_inode", "unable to read inode %lu\n", inode->i_ino); | |
827 | return -1; | |
828 | } | |
829 | if (uspi->fs_magic == UFS2_MAGIC) { | |
830 | struct ufs2_inode *ufs2_inode = (struct ufs2_inode *)bh->b_data; | |
831 | ||
832 | ufs2_update_inode(inode, | |
833 | ufs2_inode + ufs_inotofsbo(inode->i_ino)); | |
834 | } else { | |
835 | struct ufs_inode *ufs_inode = (struct ufs_inode *) bh->b_data; | |
836 | ||
837 | ufs1_update_inode(inode, ufs_inode + ufs_inotofsbo(inode->i_ino)); | |
838 | } | |
010d331f | 839 | |
1da177e4 LT |
840 | mark_buffer_dirty(bh); |
841 | if (do_sync) | |
842 | sync_dirty_buffer(bh); | |
843 | brelse (bh); | |
010d331f | 844 | |
abf5d15f | 845 | UFSD("EXIT\n"); |
1da177e4 LT |
846 | return 0; |
847 | } | |
848 | ||
a9185b41 | 849 | int ufs_write_inode(struct inode *inode, struct writeback_control *wbc) |
1da177e4 | 850 | { |
f3e0f3da | 851 | return ufs_update_inode(inode, wbc->sync_mode == WB_SYNC_ALL); |
1da177e4 LT |
852 | } |
853 | ||
854 | int ufs_sync_inode (struct inode *inode) | |
855 | { | |
856 | return ufs_update_inode (inode, 1); | |
857 | } | |
858 | ||
58e8268c | 859 | void ufs_evict_inode(struct inode * inode) |
1da177e4 | 860 | { |
58e8268c AV |
861 | int want_delete = 0; |
862 | ||
863 | if (!inode->i_nlink && !is_bad_inode(inode)) | |
864 | want_delete = 1; | |
10e5dce0 | 865 | |
91b0abe3 | 866 | truncate_inode_pages_final(&inode->i_data); |
58e8268c | 867 | if (want_delete) { |
58e8268c | 868 | inode->i_size = 0; |
babef37d AV |
869 | if (inode->i_blocks && |
870 | (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || | |
871 | S_ISLNK(inode->i_mode))) | |
d622f167 | 872 | ufs_truncate_blocks(inode); |
67a70017 | 873 | ufs_update_inode(inode, inode_needs_sync(inode)); |
58e8268c AV |
874 | } |
875 | ||
876 | invalidate_inode_buffers(inode); | |
dbd5768f | 877 | clear_inode(inode); |
58e8268c | 878 | |
f3e0f3da | 879 | if (want_delete) |
9ef7db7f | 880 | ufs_free_inode(inode); |
1da177e4 | 881 | } |
010d331f | 882 | |
a138b4b6 AV |
883 | struct to_free { |
884 | struct inode *inode; | |
885 | u64 to; | |
886 | unsigned count; | |
887 | }; | |
888 | ||
889 | static inline void free_data(struct to_free *ctx, u64 from, unsigned count) | |
890 | { | |
891 | if (ctx->count && ctx->to != from) { | |
892 | ufs_free_blocks(ctx->inode, ctx->to - ctx->count, ctx->count); | |
893 | ctx->count = 0; | |
894 | } | |
895 | ctx->count += count; | |
896 | ctx->to = from + count; | |
897 | } | |
898 | ||
010d331f AV |
899 | #define DIRECT_FRAGMENT ((inode->i_size + uspi->s_fsize - 1) >> uspi->s_fshift) |
900 | ||
901 | static void ufs_trunc_direct(struct inode *inode) | |
902 | { | |
903 | struct ufs_inode_info *ufsi = UFS_I(inode); | |
904 | struct super_block * sb; | |
905 | struct ufs_sb_private_info * uspi; | |
906 | void *p; | |
907 | u64 frag1, frag2, frag3, frag4, block1, block2; | |
a138b4b6 | 908 | struct to_free ctx = {.inode = inode}; |
010d331f AV |
909 | unsigned i, tmp; |
910 | ||
911 | UFSD("ENTER: ino %lu\n", inode->i_ino); | |
912 | ||
913 | sb = inode->i_sb; | |
914 | uspi = UFS_SB(sb)->s_uspi; | |
915 | ||
010d331f AV |
916 | frag1 = DIRECT_FRAGMENT; |
917 | frag4 = min_t(u64, UFS_NDIR_FRAGMENT, ufsi->i_lastfrag); | |
918 | frag2 = ((frag1 & uspi->s_fpbmask) ? ((frag1 | uspi->s_fpbmask) + 1) : frag1); | |
919 | frag3 = frag4 & ~uspi->s_fpbmask; | |
920 | block1 = block2 = 0; | |
921 | if (frag2 > frag3) { | |
922 | frag2 = frag4; | |
923 | frag3 = frag4 = 0; | |
924 | } else if (frag2 < frag3) { | |
925 | block1 = ufs_fragstoblks (frag2); | |
926 | block2 = ufs_fragstoblks (frag3); | |
927 | } | |
928 | ||
929 | UFSD("ino %lu, frag1 %llu, frag2 %llu, block1 %llu, block2 %llu," | |
930 | " frag3 %llu, frag4 %llu\n", inode->i_ino, | |
931 | (unsigned long long)frag1, (unsigned long long)frag2, | |
932 | (unsigned long long)block1, (unsigned long long)block2, | |
933 | (unsigned long long)frag3, (unsigned long long)frag4); | |
934 | ||
935 | if (frag1 >= frag2) | |
936 | goto next1; | |
937 | ||
938 | /* | |
939 | * Free first free fragments | |
940 | */ | |
941 | p = ufs_get_direct_data_ptr(uspi, ufsi, ufs_fragstoblks(frag1)); | |
942 | tmp = ufs_data_ptr_to_cpu(sb, p); | |
943 | if (!tmp ) | |
944 | ufs_panic (sb, "ufs_trunc_direct", "internal error"); | |
945 | frag2 -= frag1; | |
946 | frag1 = ufs_fragnum (frag1); | |
947 | ||
948 | ufs_free_fragments(inode, tmp + frag1, frag2); | |
010d331f AV |
949 | |
950 | next1: | |
951 | /* | |
952 | * Free whole blocks | |
953 | */ | |
954 | for (i = block1 ; i < block2; i++) { | |
955 | p = ufs_get_direct_data_ptr(uspi, ufsi, i); | |
956 | tmp = ufs_data_ptr_to_cpu(sb, p); | |
957 | if (!tmp) | |
958 | continue; | |
959 | write_seqlock(&ufsi->meta_lock); | |
960 | ufs_data_ptr_clear(uspi, p); | |
961 | write_sequnlock(&ufsi->meta_lock); | |
962 | ||
a138b4b6 | 963 | free_data(&ctx, tmp, uspi->s_fpb); |
010d331f AV |
964 | } |
965 | ||
a138b4b6 | 966 | free_data(&ctx, 0, 0); |
010d331f AV |
967 | |
968 | if (frag3 >= frag4) | |
969 | goto next3; | |
970 | ||
971 | /* | |
972 | * Free last free fragments | |
973 | */ | |
974 | p = ufs_get_direct_data_ptr(uspi, ufsi, ufs_fragstoblks(frag3)); | |
975 | tmp = ufs_data_ptr_to_cpu(sb, p); | |
976 | if (!tmp ) | |
977 | ufs_panic(sb, "ufs_truncate_direct", "internal error"); | |
978 | frag4 = ufs_fragnum (frag4); | |
979 | write_seqlock(&ufsi->meta_lock); | |
980 | ufs_data_ptr_clear(uspi, p); | |
981 | write_sequnlock(&ufsi->meta_lock); | |
982 | ||
983 | ufs_free_fragments (inode, tmp, frag4); | |
010d331f AV |
984 | next3: |
985 | ||
986 | UFSD("EXIT: ino %lu\n", inode->i_ino); | |
987 | } | |
988 | ||
163073db | 989 | static void free_full_branch(struct inode *inode, u64 ind_block, int depth) |
6d1ebbca AV |
990 | { |
991 | struct super_block *sb = inode->i_sb; | |
992 | struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi; | |
163073db | 993 | struct ufs_buffer_head *ubh = ubh_bread(sb, ind_block, uspi->s_bsize); |
6d1ebbca AV |
994 | unsigned i; |
995 | ||
163073db | 996 | if (!ubh) |
6d1ebbca | 997 | return; |
6d1ebbca AV |
998 | |
999 | if (--depth) { | |
163073db AV |
1000 | for (i = 0; i < uspi->s_apb; i++) { |
1001 | void *p = ubh_get_data_ptr(uspi, ubh, i); | |
1002 | u64 block = ufs_data_ptr_to_cpu(sb, p); | |
cc7231e3 | 1003 | if (block) |
163073db | 1004 | free_full_branch(inode, block, depth); |
6d1ebbca AV |
1005 | } |
1006 | } else { | |
1007 | struct to_free ctx = {.inode = inode}; | |
1008 | ||
1009 | for (i = 0; i < uspi->s_apb; i++) { | |
163073db AV |
1010 | void *p = ubh_get_data_ptr(uspi, ubh, i); |
1011 | u64 block = ufs_data_ptr_to_cpu(sb, p); | |
cc7231e3 | 1012 | if (block) |
163073db | 1013 | free_data(&ctx, block, uspi->s_fpb); |
6d1ebbca AV |
1014 | } |
1015 | free_data(&ctx, 0, 0); | |
1016 | } | |
6d1ebbca AV |
1017 | |
1018 | ubh_bforget(ubh); | |
163073db | 1019 | ufs_free_blocks(inode, ind_block, uspi->s_fpb); |
6d1ebbca AV |
1020 | } |
1021 | ||
7b4e4f7f | 1022 | static void free_branch_tail(struct inode *inode, unsigned from, struct ufs_buffer_head *ubh, int depth) |
010d331f | 1023 | { |
7bad5939 AV |
1024 | struct super_block *sb = inode->i_sb; |
1025 | struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi; | |
7bad5939 | 1026 | unsigned i; |
010d331f | 1027 | |
9e0fbbde | 1028 | if (--depth) { |
7b4e4f7f | 1029 | for (i = from; i < uspi->s_apb ; i++) { |
163073db AV |
1030 | void *p = ubh_get_data_ptr(uspi, ubh, i); |
1031 | u64 block = ufs_data_ptr_to_cpu(sb, p); | |
1032 | if (block) { | |
1033 | write_seqlock(&UFS_I(inode)->meta_lock); | |
1034 | ufs_data_ptr_clear(uspi, p); | |
1035 | write_sequnlock(&UFS_I(inode)->meta_lock); | |
1036 | ubh_mark_buffer_dirty(ubh); | |
1037 | free_full_branch(inode, block, depth); | |
1038 | } | |
a9657423 | 1039 | } |
9e0fbbde | 1040 | } else { |
a138b4b6 | 1041 | struct to_free ctx = {.inode = inode}; |
9e0fbbde AV |
1042 | |
1043 | for (i = from; i < uspi->s_apb; i++) { | |
163073db AV |
1044 | void *p = ubh_get_data_ptr(uspi, ubh, i); |
1045 | u64 block = ufs_data_ptr_to_cpu(sb, p); | |
1046 | if (block) { | |
1047 | write_seqlock(&UFS_I(inode)->meta_lock); | |
1048 | ufs_data_ptr_clear(uspi, p); | |
1049 | write_sequnlock(&UFS_I(inode)->meta_lock); | |
1050 | ubh_mark_buffer_dirty(ubh); | |
1051 | free_data(&ctx, block, uspi->s_fpb); | |
163073db | 1052 | } |
9e0fbbde | 1053 | } |
a138b4b6 | 1054 | free_data(&ctx, 0, 0); |
010d331f | 1055 | } |
9e0fbbde AV |
1056 | if (IS_SYNC(inode) && ubh_buffer_dirty(ubh)) |
1057 | ubh_sync_block(ubh); | |
1058 | ubh_brelse(ubh); | |
010d331f AV |
1059 | } |
1060 | ||
1061 | static int ufs_alloc_lastblock(struct inode *inode, loff_t size) | |
1062 | { | |
1063 | int err = 0; | |
1064 | struct super_block *sb = inode->i_sb; | |
1065 | struct address_space *mapping = inode->i_mapping; | |
1066 | struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi; | |
1067 | unsigned i, end; | |
1068 | sector_t lastfrag; | |
e7ca7f17 | 1069 | struct folio *folio; |
010d331f AV |
1070 | struct buffer_head *bh; |
1071 | u64 phys64; | |
1072 | ||
1073 | lastfrag = (size + uspi->s_fsize - 1) >> uspi->s_fshift; | |
1074 | ||
1075 | if (!lastfrag) | |
1076 | goto out; | |
1077 | ||
1078 | lastfrag--; | |
1079 | ||
e7ca7f17 | 1080 | folio = ufs_get_locked_folio(mapping, lastfrag >> |
09cbfeaf | 1081 | (PAGE_SHIFT - inode->i_blkbits)); |
e7ca7f17 MWO |
1082 | if (IS_ERR(folio)) { |
1083 | err = -EIO; | |
1084 | goto out; | |
1085 | } | |
010d331f | 1086 | |
e7ca7f17 MWO |
1087 | end = lastfrag & ((1 << (PAGE_SHIFT - inode->i_blkbits)) - 1); |
1088 | bh = folio_buffers(folio); | |
1089 | for (i = 0; i < end; ++i) | |
1090 | bh = bh->b_this_page; | |
010d331f AV |
1091 | |
1092 | err = ufs_getfrag_block(inode, lastfrag, bh, 1); | |
1093 | ||
1094 | if (unlikely(err)) | |
1095 | goto out_unlock; | |
1096 | ||
1097 | if (buffer_new(bh)) { | |
1098 | clear_buffer_new(bh); | |
e64855c6 | 1099 | clean_bdev_bh_alias(bh); |
010d331f AV |
1100 | /* |
1101 | * we do not zeroize fragment, because of | |
1102 | * if it maped to hole, it already contains zeroes | |
1103 | */ | |
1104 | set_buffer_uptodate(bh); | |
1105 | mark_buffer_dirty(bh); | |
e7ca7f17 | 1106 | folio_mark_dirty(folio); |
010d331f AV |
1107 | } |
1108 | ||
1109 | if (lastfrag >= UFS_IND_FRAGMENT) { | |
1110 | end = uspi->s_fpb - ufs_fragnum(lastfrag) - 1; | |
1111 | phys64 = bh->b_blocknr + 1; | |
1112 | for (i = 0; i < end; ++i) { | |
1113 | bh = sb_getblk(sb, i + phys64); | |
1114 | lock_buffer(bh); | |
1115 | memset(bh->b_data, 0, sb->s_blocksize); | |
1116 | set_buffer_uptodate(bh); | |
1117 | mark_buffer_dirty(bh); | |
1118 | unlock_buffer(bh); | |
1119 | sync_dirty_buffer(bh); | |
1120 | brelse(bh); | |
1121 | } | |
1122 | } | |
1123 | out_unlock: | |
e7ca7f17 | 1124 | ufs_put_locked_folio(folio); |
010d331f AV |
1125 | out: |
1126 | return err; | |
1127 | } | |
1128 | ||
babef37d | 1129 | static void ufs_truncate_blocks(struct inode *inode) |
010d331f AV |
1130 | { |
1131 | struct ufs_inode_info *ufsi = UFS_I(inode); | |
1132 | struct super_block *sb = inode->i_sb; | |
1133 | struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi; | |
7bad5939 | 1134 | unsigned offsets[4]; |
a8fad984 | 1135 | int depth; |
6775e24d | 1136 | int depth2; |
42432739 | 1137 | unsigned i; |
7b4e4f7f AV |
1138 | struct ufs_buffer_head *ubh[3]; |
1139 | void *p; | |
1140 | u64 block; | |
6775e24d | 1141 | |
a8fad984 AV |
1142 | if (inode->i_size) { |
1143 | sector_t last = (inode->i_size - 1) >> uspi->s_bshift; | |
1144 | depth = ufs_block_to_path(inode, last, offsets); | |
1145 | if (!depth) | |
1146 | return; | |
1147 | } else { | |
1148 | depth = 1; | |
1149 | } | |
6775e24d | 1150 | |
6775e24d | 1151 | for (depth2 = depth - 1; depth2; depth2--) |
a8fad984 | 1152 | if (offsets[depth2] != uspi->s_apb - 1) |
6775e24d | 1153 | break; |
010d331f AV |
1154 | |
1155 | mutex_lock(&ufsi->truncate_mutex); | |
42432739 | 1156 | if (depth == 1) { |
31cd043e | 1157 | ufs_trunc_direct(inode); |
42432739 AV |
1158 | offsets[0] = UFS_IND_BLOCK; |
1159 | } else { | |
7b4e4f7f | 1160 | /* get the blocks that should be partially emptied */ |
a8fad984 | 1161 | p = ufs_get_direct_data_ptr(uspi, ufsi, offsets[0]++); |
7b4e4f7f | 1162 | for (i = 0; i < depth2; i++) { |
7b4e4f7f AV |
1163 | block = ufs_data_ptr_to_cpu(sb, p); |
1164 | if (!block) | |
1165 | break; | |
1166 | ubh[i] = ubh_bread(sb, block, uspi->s_bsize); | |
1167 | if (!ubh[i]) { | |
1168 | write_seqlock(&ufsi->meta_lock); | |
1169 | ufs_data_ptr_clear(uspi, p); | |
1170 | write_sequnlock(&ufsi->meta_lock); | |
1171 | break; | |
1172 | } | |
a8fad984 | 1173 | p = ubh_get_data_ptr(uspi, ubh[i], offsets[i + 1]++); |
7b4e4f7f | 1174 | } |
f53bd142 | 1175 | while (i--) |
7b4e4f7f | 1176 | free_branch_tail(inode, offsets[i + 1], ubh[i], depth - i - 1); |
42432739 AV |
1177 | } |
1178 | for (i = offsets[0]; i <= UFS_TIND_BLOCK; i++) { | |
163073db AV |
1179 | p = ufs_get_direct_data_ptr(uspi, ufsi, i); |
1180 | block = ufs_data_ptr_to_cpu(sb, p); | |
1181 | if (block) { | |
1182 | write_seqlock(&ufsi->meta_lock); | |
1183 | ufs_data_ptr_clear(uspi, p); | |
1184 | write_sequnlock(&ufsi->meta_lock); | |
1185 | free_full_branch(inode, block, i - UFS_IND_BLOCK + 1); | |
1186 | } | |
31cd043e | 1187 | } |
09bf4f5b | 1188 | read_seqlock_excl(&ufsi->meta_lock); |
010d331f | 1189 | ufsi->i_lastfrag = DIRECT_FRAGMENT; |
09bf4f5b | 1190 | read_sequnlock_excl(&ufsi->meta_lock); |
b6eede0e | 1191 | mark_inode_dirty(inode); |
010d331f AV |
1192 | mutex_unlock(&ufsi->truncate_mutex); |
1193 | } | |
1194 | ||
1195 | static int ufs_truncate(struct inode *inode, loff_t size) | |
1196 | { | |
1197 | int err = 0; | |
1198 | ||
1199 | UFSD("ENTER: ino %lu, i_size: %llu, old_i_size: %llu\n", | |
1200 | inode->i_ino, (unsigned long long)size, | |
1201 | (unsigned long long)i_size_read(inode)); | |
1202 | ||
1203 | if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || | |
1204 | S_ISLNK(inode->i_mode))) | |
1205 | return -EINVAL; | |
1206 | if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) | |
1207 | return -EPERM; | |
1208 | ||
1209 | err = ufs_alloc_lastblock(inode, size); | |
1210 | ||
1211 | if (err) | |
1212 | goto out; | |
1213 | ||
1214 | block_truncate_page(inode->i_mapping, size, ufs_getfrag_block); | |
1215 | ||
1216 | truncate_setsize(inode, size); | |
1217 | ||
babef37d | 1218 | ufs_truncate_blocks(inode); |
d936d382 | 1219 | inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode)); |
010d331f AV |
1220 | mark_inode_dirty(inode); |
1221 | out: | |
1222 | UFSD("EXIT: err %d\n", err); | |
1223 | return err; | |
1224 | } | |
1225 | ||
c1632a0f | 1226 | int ufs_setattr(struct mnt_idmap *idmap, struct dentry *dentry, |
549c7297 | 1227 | struct iattr *attr) |
010d331f AV |
1228 | { |
1229 | struct inode *inode = d_inode(dentry); | |
1230 | unsigned int ia_valid = attr->ia_valid; | |
1231 | int error; | |
1232 | ||
c1632a0f | 1233 | error = setattr_prepare(&nop_mnt_idmap, dentry, attr); |
010d331f AV |
1234 | if (error) |
1235 | return error; | |
1236 | ||
1237 | if (ia_valid & ATTR_SIZE && attr->ia_size != inode->i_size) { | |
1238 | error = ufs_truncate(inode, attr->ia_size); | |
1239 | if (error) | |
1240 | return error; | |
1241 | } | |
1242 | ||
c1632a0f | 1243 | setattr_copy(&nop_mnt_idmap, inode, attr); |
010d331f AV |
1244 | mark_inode_dirty(inode); |
1245 | return 0; | |
1246 | } | |
1247 | ||
1248 | const struct inode_operations ufs_file_inode_operations = { | |
1249 | .setattr = ufs_setattr, | |
1250 | }; |