Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
3e57ecf6 | 2 | * Copyright (c) 2000-2006 Silicon Graphics, Inc. |
7b718769 | 3 | * All Rights Reserved. |
1da177e4 | 4 | * |
7b718769 NS |
5 | * This program is free software; you can redistribute it and/or |
6 | * modify it under the terms of the GNU General Public License as | |
1da177e4 LT |
7 | * published by the Free Software Foundation. |
8 | * | |
7b718769 NS |
9 | * This program is distributed in the hope that it would be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | * GNU General Public License for more details. | |
1da177e4 | 13 | * |
7b718769 NS |
14 | * You should have received a copy of the GNU General Public License |
15 | * along with this program; if not, write the Free Software Foundation, | |
16 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
1da177e4 | 17 | */ |
40ebd81d RD |
18 | #include <linux/log2.h> |
19 | ||
1da177e4 | 20 | #include "xfs.h" |
a844f451 | 21 | #include "xfs_fs.h" |
1da177e4 | 22 | #include "xfs_types.h" |
1da177e4 | 23 | #include "xfs_log.h" |
a844f451 | 24 | #include "xfs_inum.h" |
1da177e4 LT |
25 | #include "xfs_trans.h" |
26 | #include "xfs_trans_priv.h" | |
27 | #include "xfs_sb.h" | |
28 | #include "xfs_ag.h" | |
1da177e4 | 29 | #include "xfs_mount.h" |
1da177e4 | 30 | #include "xfs_bmap_btree.h" |
a844f451 | 31 | #include "xfs_alloc_btree.h" |
1da177e4 | 32 | #include "xfs_ialloc_btree.h" |
a844f451 | 33 | #include "xfs_attr_sf.h" |
1da177e4 | 34 | #include "xfs_dinode.h" |
1da177e4 | 35 | #include "xfs_inode.h" |
1da177e4 | 36 | #include "xfs_buf_item.h" |
a844f451 NS |
37 | #include "xfs_inode_item.h" |
38 | #include "xfs_btree.h" | |
39 | #include "xfs_alloc.h" | |
40 | #include "xfs_ialloc.h" | |
41 | #include "xfs_bmap.h" | |
1da177e4 | 42 | #include "xfs_error.h" |
1da177e4 | 43 | #include "xfs_utils.h" |
1da177e4 | 44 | #include "xfs_quota.h" |
2a82b8be | 45 | #include "xfs_filestream.h" |
739bfb2a | 46 | #include "xfs_vnodeops.h" |
0b1b213f | 47 | #include "xfs_trace.h" |
1da177e4 | 48 | |
1da177e4 LT |
49 | kmem_zone_t *xfs_ifork_zone; |
50 | kmem_zone_t *xfs_inode_zone; | |
1da177e4 LT |
51 | |
52 | /* | |
8f04c47a | 53 | * Used in xfs_itruncate_extents(). This is the maximum number of extents |
1da177e4 LT |
54 | * freed from a file in a single transaction. |
55 | */ | |
56 | #define XFS_ITRUNC_MAX_EXTENTS 2 | |
57 | ||
58 | STATIC int xfs_iflush_int(xfs_inode_t *, xfs_buf_t *); | |
59 | STATIC int xfs_iformat_local(xfs_inode_t *, xfs_dinode_t *, int, int); | |
60 | STATIC int xfs_iformat_extents(xfs_inode_t *, xfs_dinode_t *, int); | |
61 | STATIC int xfs_iformat_btree(xfs_inode_t *, xfs_dinode_t *, int); | |
62 | ||
2a0ec1d9 DC |
63 | /* |
64 | * helper function to extract extent size hint from inode | |
65 | */ | |
66 | xfs_extlen_t | |
67 | xfs_get_extsz_hint( | |
68 | struct xfs_inode *ip) | |
69 | { | |
70 | if ((ip->i_d.di_flags & XFS_DIFLAG_EXTSIZE) && ip->i_d.di_extsize) | |
71 | return ip->i_d.di_extsize; | |
72 | if (XFS_IS_REALTIME_INODE(ip)) | |
73 | return ip->i_mount->m_sb.sb_rextsize; | |
74 | return 0; | |
75 | } | |
76 | ||
1da177e4 LT |
77 | #ifdef DEBUG |
78 | /* | |
79 | * Make sure that the extents in the given memory buffer | |
80 | * are valid. | |
81 | */ | |
82 | STATIC void | |
83 | xfs_validate_extents( | |
4eea22f0 | 84 | xfs_ifork_t *ifp, |
1da177e4 | 85 | int nrecs, |
1da177e4 LT |
86 | xfs_exntfmt_t fmt) |
87 | { | |
88 | xfs_bmbt_irec_t irec; | |
a6f64d4a | 89 | xfs_bmbt_rec_host_t rec; |
1da177e4 LT |
90 | int i; |
91 | ||
92 | for (i = 0; i < nrecs; i++) { | |
a6f64d4a CH |
93 | xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, i); |
94 | rec.l0 = get_unaligned(&ep->l0); | |
95 | rec.l1 = get_unaligned(&ep->l1); | |
96 | xfs_bmbt_get_all(&rec, &irec); | |
1da177e4 LT |
97 | if (fmt == XFS_EXTFMT_NOSTATE) |
98 | ASSERT(irec.br_state == XFS_EXT_NORM); | |
1da177e4 LT |
99 | } |
100 | } | |
101 | #else /* DEBUG */ | |
a6f64d4a | 102 | #define xfs_validate_extents(ifp, nrecs, fmt) |
1da177e4 LT |
103 | #endif /* DEBUG */ |
104 | ||
105 | /* | |
106 | * Check that none of the inode's in the buffer have a next | |
107 | * unlinked field of 0. | |
108 | */ | |
109 | #if defined(DEBUG) | |
110 | void | |
111 | xfs_inobp_check( | |
112 | xfs_mount_t *mp, | |
113 | xfs_buf_t *bp) | |
114 | { | |
115 | int i; | |
116 | int j; | |
117 | xfs_dinode_t *dip; | |
118 | ||
119 | j = mp->m_inode_cluster_size >> mp->m_sb.sb_inodelog; | |
120 | ||
121 | for (i = 0; i < j; i++) { | |
122 | dip = (xfs_dinode_t *)xfs_buf_offset(bp, | |
123 | i * mp->m_sb.sb_inodesize); | |
124 | if (!dip->di_next_unlinked) { | |
53487786 DC |
125 | xfs_alert(mp, |
126 | "Detected bogus zero next_unlinked field in incore inode buffer 0x%p.", | |
1da177e4 LT |
127 | bp); |
128 | ASSERT(dip->di_next_unlinked); | |
129 | } | |
130 | } | |
131 | } | |
132 | #endif | |
133 | ||
4ae29b43 DC |
134 | /* |
135 | * Find the buffer associated with the given inode map | |
136 | * We do basic validation checks on the buffer once it has been | |
137 | * retrieved from disk. | |
138 | */ | |
139 | STATIC int | |
140 | xfs_imap_to_bp( | |
141 | xfs_mount_t *mp, | |
142 | xfs_trans_t *tp, | |
92bfc6e7 | 143 | struct xfs_imap *imap, |
4ae29b43 DC |
144 | xfs_buf_t **bpp, |
145 | uint buf_flags, | |
b48d8d64 | 146 | uint iget_flags) |
4ae29b43 DC |
147 | { |
148 | int error; | |
149 | int i; | |
150 | int ni; | |
151 | xfs_buf_t *bp; | |
152 | ||
153 | error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, imap->im_blkno, | |
a3f74ffb | 154 | (int)imap->im_len, buf_flags, &bp); |
4ae29b43 | 155 | if (error) { |
a3f74ffb | 156 | if (error != EAGAIN) { |
0b932ccc DC |
157 | xfs_warn(mp, |
158 | "%s: xfs_trans_read_buf() returned error %d.", | |
159 | __func__, error); | |
a3f74ffb | 160 | } else { |
0cadda1c | 161 | ASSERT(buf_flags & XBF_TRYLOCK); |
a3f74ffb | 162 | } |
4ae29b43 DC |
163 | return error; |
164 | } | |
165 | ||
166 | /* | |
167 | * Validate the magic number and version of every inode in the buffer | |
168 | * (if DEBUG kernel) or the first inode in the buffer, otherwise. | |
169 | */ | |
170 | #ifdef DEBUG | |
171 | ni = BBTOB(imap->im_len) >> mp->m_sb.sb_inodelog; | |
172 | #else /* usual case */ | |
173 | ni = 1; | |
174 | #endif | |
175 | ||
176 | for (i = 0; i < ni; i++) { | |
177 | int di_ok; | |
178 | xfs_dinode_t *dip; | |
179 | ||
180 | dip = (xfs_dinode_t *)xfs_buf_offset(bp, | |
181 | (i << mp->m_sb.sb_inodelog)); | |
69ef921b | 182 | di_ok = dip->di_magic == cpu_to_be16(XFS_DINODE_MAGIC) && |
81591fe2 | 183 | XFS_DINODE_GOOD_VERSION(dip->di_version); |
4ae29b43 DC |
184 | if (unlikely(XFS_TEST_ERROR(!di_ok, mp, |
185 | XFS_ERRTAG_ITOBP_INOTOBP, | |
186 | XFS_RANDOM_ITOBP_INOTOBP))) { | |
1920779e | 187 | if (iget_flags & XFS_IGET_UNTRUSTED) { |
4ae29b43 DC |
188 | xfs_trans_brelse(tp, bp); |
189 | return XFS_ERROR(EINVAL); | |
190 | } | |
191 | XFS_CORRUPTION_ERROR("xfs_imap_to_bp", | |
192 | XFS_ERRLEVEL_HIGH, mp, dip); | |
193 | #ifdef DEBUG | |
0b932ccc DC |
194 | xfs_emerg(mp, |
195 | "bad inode magic/vsn daddr %lld #%d (magic=%x)", | |
4ae29b43 | 196 | (unsigned long long)imap->im_blkno, i, |
81591fe2 | 197 | be16_to_cpu(dip->di_magic)); |
0b932ccc | 198 | ASSERT(0); |
4ae29b43 DC |
199 | #endif |
200 | xfs_trans_brelse(tp, bp); | |
201 | return XFS_ERROR(EFSCORRUPTED); | |
202 | } | |
203 | } | |
204 | ||
205 | xfs_inobp_check(mp, bp); | |
4ae29b43 DC |
206 | *bpp = bp; |
207 | return 0; | |
208 | } | |
209 | ||
1da177e4 LT |
210 | /* |
211 | * This routine is called to map an inode number within a file | |
212 | * system to the buffer containing the on-disk version of the | |
213 | * inode. It returns a pointer to the buffer containing the | |
214 | * on-disk inode in the bpp parameter, and in the dip parameter | |
215 | * it returns a pointer to the on-disk inode within that buffer. | |
216 | * | |
217 | * If a non-zero error is returned, then the contents of bpp and | |
218 | * dipp are undefined. | |
219 | * | |
220 | * Use xfs_imap() to determine the size and location of the | |
221 | * buffer to read from disk. | |
222 | */ | |
c679eef0 | 223 | int |
1da177e4 LT |
224 | xfs_inotobp( |
225 | xfs_mount_t *mp, | |
226 | xfs_trans_t *tp, | |
227 | xfs_ino_t ino, | |
228 | xfs_dinode_t **dipp, | |
229 | xfs_buf_t **bpp, | |
c679eef0 CH |
230 | int *offset, |
231 | uint imap_flags) | |
1da177e4 | 232 | { |
92bfc6e7 | 233 | struct xfs_imap imap; |
1da177e4 LT |
234 | xfs_buf_t *bp; |
235 | int error; | |
1da177e4 | 236 | |
1da177e4 | 237 | imap.im_blkno = 0; |
a1941895 | 238 | error = xfs_imap(mp, tp, ino, &imap, imap_flags); |
4ae29b43 | 239 | if (error) |
1da177e4 | 240 | return error; |
1da177e4 | 241 | |
a8acad70 | 242 | error = xfs_imap_to_bp(mp, tp, &imap, &bp, 0, imap_flags); |
4ae29b43 | 243 | if (error) |
1da177e4 | 244 | return error; |
1da177e4 | 245 | |
1da177e4 LT |
246 | *dipp = (xfs_dinode_t *)xfs_buf_offset(bp, imap.im_boffset); |
247 | *bpp = bp; | |
248 | *offset = imap.im_boffset; | |
249 | return 0; | |
250 | } | |
251 | ||
252 | ||
253 | /* | |
254 | * This routine is called to map an inode to the buffer containing | |
255 | * the on-disk version of the inode. It returns a pointer to the | |
256 | * buffer containing the on-disk inode in the bpp parameter, and in | |
257 | * the dip parameter it returns a pointer to the on-disk inode within | |
258 | * that buffer. | |
259 | * | |
260 | * If a non-zero error is returned, then the contents of bpp and | |
261 | * dipp are undefined. | |
262 | * | |
76d8b277 CH |
263 | * The inode is expected to already been mapped to its buffer and read |
264 | * in once, thus we can use the mapping information stored in the inode | |
265 | * rather than calling xfs_imap(). This allows us to avoid the overhead | |
266 | * of looking at the inode btree for small block file systems | |
94e1b69d | 267 | * (see xfs_imap()). |
1da177e4 LT |
268 | */ |
269 | int | |
270 | xfs_itobp( | |
271 | xfs_mount_t *mp, | |
272 | xfs_trans_t *tp, | |
273 | xfs_inode_t *ip, | |
274 | xfs_dinode_t **dipp, | |
275 | xfs_buf_t **bpp, | |
a3f74ffb | 276 | uint buf_flags) |
1da177e4 LT |
277 | { |
278 | xfs_buf_t *bp; | |
279 | int error; | |
1da177e4 | 280 | |
92bfc6e7 | 281 | ASSERT(ip->i_imap.im_blkno != 0); |
1da177e4 | 282 | |
92bfc6e7 | 283 | error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &bp, buf_flags, 0); |
4ae29b43 | 284 | if (error) |
1da177e4 | 285 | return error; |
1da177e4 | 286 | |
a3f74ffb | 287 | if (!bp) { |
0cadda1c | 288 | ASSERT(buf_flags & XBF_TRYLOCK); |
a3f74ffb DC |
289 | ASSERT(tp == NULL); |
290 | *bpp = NULL; | |
291 | return EAGAIN; | |
292 | } | |
293 | ||
92bfc6e7 | 294 | *dipp = (xfs_dinode_t *)xfs_buf_offset(bp, ip->i_imap.im_boffset); |
1da177e4 LT |
295 | *bpp = bp; |
296 | return 0; | |
297 | } | |
298 | ||
299 | /* | |
300 | * Move inode type and inode format specific information from the | |
301 | * on-disk inode to the in-core inode. For fifos, devs, and sockets | |
302 | * this means set if_rdev to the proper value. For files, directories, | |
303 | * and symlinks this means to bring in the in-line data or extent | |
304 | * pointers. For a file in B-tree format, only the root is immediately | |
305 | * brought in-core. The rest will be in-lined in if_extents when it | |
306 | * is first referenced (see xfs_iread_extents()). | |
307 | */ | |
308 | STATIC int | |
309 | xfs_iformat( | |
310 | xfs_inode_t *ip, | |
311 | xfs_dinode_t *dip) | |
312 | { | |
313 | xfs_attr_shortform_t *atp; | |
314 | int size; | |
8096b1eb | 315 | int error = 0; |
1da177e4 | 316 | xfs_fsize_t di_size; |
1da177e4 | 317 | |
81591fe2 CH |
318 | if (unlikely(be32_to_cpu(dip->di_nextents) + |
319 | be16_to_cpu(dip->di_anextents) > | |
320 | be64_to_cpu(dip->di_nblocks))) { | |
65333b4c | 321 | xfs_warn(ip->i_mount, |
3762ec6b | 322 | "corrupt dinode %Lu, extent total = %d, nblocks = %Lu.", |
1da177e4 | 323 | (unsigned long long)ip->i_ino, |
81591fe2 CH |
324 | (int)(be32_to_cpu(dip->di_nextents) + |
325 | be16_to_cpu(dip->di_anextents)), | |
1da177e4 | 326 | (unsigned long long) |
81591fe2 | 327 | be64_to_cpu(dip->di_nblocks)); |
1da177e4 LT |
328 | XFS_CORRUPTION_ERROR("xfs_iformat(1)", XFS_ERRLEVEL_LOW, |
329 | ip->i_mount, dip); | |
330 | return XFS_ERROR(EFSCORRUPTED); | |
331 | } | |
332 | ||
81591fe2 | 333 | if (unlikely(dip->di_forkoff > ip->i_mount->m_sb.sb_inodesize)) { |
65333b4c | 334 | xfs_warn(ip->i_mount, "corrupt dinode %Lu, forkoff = 0x%x.", |
1da177e4 | 335 | (unsigned long long)ip->i_ino, |
81591fe2 | 336 | dip->di_forkoff); |
1da177e4 LT |
337 | XFS_CORRUPTION_ERROR("xfs_iformat(2)", XFS_ERRLEVEL_LOW, |
338 | ip->i_mount, dip); | |
339 | return XFS_ERROR(EFSCORRUPTED); | |
340 | } | |
341 | ||
b89d4208 CH |
342 | if (unlikely((ip->i_d.di_flags & XFS_DIFLAG_REALTIME) && |
343 | !ip->i_mount->m_rtdev_targp)) { | |
65333b4c | 344 | xfs_warn(ip->i_mount, |
b89d4208 CH |
345 | "corrupt dinode %Lu, has realtime flag set.", |
346 | ip->i_ino); | |
347 | XFS_CORRUPTION_ERROR("xfs_iformat(realtime)", | |
348 | XFS_ERRLEVEL_LOW, ip->i_mount, dip); | |
349 | return XFS_ERROR(EFSCORRUPTED); | |
350 | } | |
351 | ||
1da177e4 LT |
352 | switch (ip->i_d.di_mode & S_IFMT) { |
353 | case S_IFIFO: | |
354 | case S_IFCHR: | |
355 | case S_IFBLK: | |
356 | case S_IFSOCK: | |
81591fe2 | 357 | if (unlikely(dip->di_format != XFS_DINODE_FMT_DEV)) { |
1da177e4 LT |
358 | XFS_CORRUPTION_ERROR("xfs_iformat(3)", XFS_ERRLEVEL_LOW, |
359 | ip->i_mount, dip); | |
360 | return XFS_ERROR(EFSCORRUPTED); | |
361 | } | |
362 | ip->i_d.di_size = 0; | |
81591fe2 | 363 | ip->i_df.if_u2.if_rdev = xfs_dinode_get_rdev(dip); |
1da177e4 LT |
364 | break; |
365 | ||
366 | case S_IFREG: | |
367 | case S_IFLNK: | |
368 | case S_IFDIR: | |
81591fe2 | 369 | switch (dip->di_format) { |
1da177e4 LT |
370 | case XFS_DINODE_FMT_LOCAL: |
371 | /* | |
372 | * no local regular files yet | |
373 | */ | |
abbede1b | 374 | if (unlikely(S_ISREG(be16_to_cpu(dip->di_mode)))) { |
65333b4c DC |
375 | xfs_warn(ip->i_mount, |
376 | "corrupt inode %Lu (local format for regular file).", | |
1da177e4 LT |
377 | (unsigned long long) ip->i_ino); |
378 | XFS_CORRUPTION_ERROR("xfs_iformat(4)", | |
379 | XFS_ERRLEVEL_LOW, | |
380 | ip->i_mount, dip); | |
381 | return XFS_ERROR(EFSCORRUPTED); | |
382 | } | |
383 | ||
81591fe2 | 384 | di_size = be64_to_cpu(dip->di_size); |
1da177e4 | 385 | if (unlikely(di_size > XFS_DFORK_DSIZE(dip, ip->i_mount))) { |
65333b4c DC |
386 | xfs_warn(ip->i_mount, |
387 | "corrupt inode %Lu (bad size %Ld for local inode).", | |
1da177e4 LT |
388 | (unsigned long long) ip->i_ino, |
389 | (long long) di_size); | |
390 | XFS_CORRUPTION_ERROR("xfs_iformat(5)", | |
391 | XFS_ERRLEVEL_LOW, | |
392 | ip->i_mount, dip); | |
393 | return XFS_ERROR(EFSCORRUPTED); | |
394 | } | |
395 | ||
396 | size = (int)di_size; | |
397 | error = xfs_iformat_local(ip, dip, XFS_DATA_FORK, size); | |
398 | break; | |
399 | case XFS_DINODE_FMT_EXTENTS: | |
400 | error = xfs_iformat_extents(ip, dip, XFS_DATA_FORK); | |
401 | break; | |
402 | case XFS_DINODE_FMT_BTREE: | |
403 | error = xfs_iformat_btree(ip, dip, XFS_DATA_FORK); | |
404 | break; | |
405 | default: | |
406 | XFS_ERROR_REPORT("xfs_iformat(6)", XFS_ERRLEVEL_LOW, | |
407 | ip->i_mount); | |
408 | return XFS_ERROR(EFSCORRUPTED); | |
409 | } | |
410 | break; | |
411 | ||
412 | default: | |
413 | XFS_ERROR_REPORT("xfs_iformat(7)", XFS_ERRLEVEL_LOW, ip->i_mount); | |
414 | return XFS_ERROR(EFSCORRUPTED); | |
415 | } | |
416 | if (error) { | |
417 | return error; | |
418 | } | |
419 | if (!XFS_DFORK_Q(dip)) | |
420 | return 0; | |
8096b1eb | 421 | |
1da177e4 | 422 | ASSERT(ip->i_afp == NULL); |
4a7edddc | 423 | ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP | KM_NOFS); |
8096b1eb | 424 | |
81591fe2 | 425 | switch (dip->di_aformat) { |
1da177e4 LT |
426 | case XFS_DINODE_FMT_LOCAL: |
427 | atp = (xfs_attr_shortform_t *)XFS_DFORK_APTR(dip); | |
3b244aa8 | 428 | size = be16_to_cpu(atp->hdr.totsize); |
2809f76a CH |
429 | |
430 | if (unlikely(size < sizeof(struct xfs_attr_sf_hdr))) { | |
65333b4c DC |
431 | xfs_warn(ip->i_mount, |
432 | "corrupt inode %Lu (bad attr fork size %Ld).", | |
2809f76a CH |
433 | (unsigned long long) ip->i_ino, |
434 | (long long) size); | |
435 | XFS_CORRUPTION_ERROR("xfs_iformat(8)", | |
436 | XFS_ERRLEVEL_LOW, | |
437 | ip->i_mount, dip); | |
438 | return XFS_ERROR(EFSCORRUPTED); | |
439 | } | |
440 | ||
1da177e4 LT |
441 | error = xfs_iformat_local(ip, dip, XFS_ATTR_FORK, size); |
442 | break; | |
443 | case XFS_DINODE_FMT_EXTENTS: | |
444 | error = xfs_iformat_extents(ip, dip, XFS_ATTR_FORK); | |
445 | break; | |
446 | case XFS_DINODE_FMT_BTREE: | |
447 | error = xfs_iformat_btree(ip, dip, XFS_ATTR_FORK); | |
448 | break; | |
449 | default: | |
450 | error = XFS_ERROR(EFSCORRUPTED); | |
451 | break; | |
452 | } | |
453 | if (error) { | |
454 | kmem_zone_free(xfs_ifork_zone, ip->i_afp); | |
455 | ip->i_afp = NULL; | |
456 | xfs_idestroy_fork(ip, XFS_DATA_FORK); | |
457 | } | |
458 | return error; | |
459 | } | |
460 | ||
461 | /* | |
462 | * The file is in-lined in the on-disk inode. | |
463 | * If it fits into if_inline_data, then copy | |
464 | * it there, otherwise allocate a buffer for it | |
465 | * and copy the data there. Either way, set | |
466 | * if_data to point at the data. | |
467 | * If we allocate a buffer for the data, make | |
468 | * sure that its size is a multiple of 4 and | |
469 | * record the real size in i_real_bytes. | |
470 | */ | |
471 | STATIC int | |
472 | xfs_iformat_local( | |
473 | xfs_inode_t *ip, | |
474 | xfs_dinode_t *dip, | |
475 | int whichfork, | |
476 | int size) | |
477 | { | |
478 | xfs_ifork_t *ifp; | |
479 | int real_size; | |
480 | ||
481 | /* | |
482 | * If the size is unreasonable, then something | |
483 | * is wrong and we just bail out rather than crash in | |
484 | * kmem_alloc() or memcpy() below. | |
485 | */ | |
486 | if (unlikely(size > XFS_DFORK_SIZE(dip, ip->i_mount, whichfork))) { | |
65333b4c DC |
487 | xfs_warn(ip->i_mount, |
488 | "corrupt inode %Lu (bad size %d for local fork, size = %d).", | |
1da177e4 LT |
489 | (unsigned long long) ip->i_ino, size, |
490 | XFS_DFORK_SIZE(dip, ip->i_mount, whichfork)); | |
491 | XFS_CORRUPTION_ERROR("xfs_iformat_local", XFS_ERRLEVEL_LOW, | |
492 | ip->i_mount, dip); | |
493 | return XFS_ERROR(EFSCORRUPTED); | |
494 | } | |
495 | ifp = XFS_IFORK_PTR(ip, whichfork); | |
496 | real_size = 0; | |
497 | if (size == 0) | |
498 | ifp->if_u1.if_data = NULL; | |
499 | else if (size <= sizeof(ifp->if_u2.if_inline_data)) | |
500 | ifp->if_u1.if_data = ifp->if_u2.if_inline_data; | |
501 | else { | |
502 | real_size = roundup(size, 4); | |
4a7edddc | 503 | ifp->if_u1.if_data = kmem_alloc(real_size, KM_SLEEP | KM_NOFS); |
1da177e4 LT |
504 | } |
505 | ifp->if_bytes = size; | |
506 | ifp->if_real_bytes = real_size; | |
507 | if (size) | |
508 | memcpy(ifp->if_u1.if_data, XFS_DFORK_PTR(dip, whichfork), size); | |
509 | ifp->if_flags &= ~XFS_IFEXTENTS; | |
510 | ifp->if_flags |= XFS_IFINLINE; | |
511 | return 0; | |
512 | } | |
513 | ||
514 | /* | |
515 | * The file consists of a set of extents all | |
516 | * of which fit into the on-disk inode. | |
517 | * If there are few enough extents to fit into | |
518 | * the if_inline_ext, then copy them there. | |
519 | * Otherwise allocate a buffer for them and copy | |
520 | * them into it. Either way, set if_extents | |
521 | * to point at the extents. | |
522 | */ | |
523 | STATIC int | |
524 | xfs_iformat_extents( | |
525 | xfs_inode_t *ip, | |
526 | xfs_dinode_t *dip, | |
527 | int whichfork) | |
528 | { | |
a6f64d4a | 529 | xfs_bmbt_rec_t *dp; |
1da177e4 LT |
530 | xfs_ifork_t *ifp; |
531 | int nex; | |
1da177e4 LT |
532 | int size; |
533 | int i; | |
534 | ||
535 | ifp = XFS_IFORK_PTR(ip, whichfork); | |
536 | nex = XFS_DFORK_NEXTENTS(dip, whichfork); | |
537 | size = nex * (uint)sizeof(xfs_bmbt_rec_t); | |
538 | ||
539 | /* | |
540 | * If the number of extents is unreasonable, then something | |
541 | * is wrong and we just bail out rather than crash in | |
542 | * kmem_alloc() or memcpy() below. | |
543 | */ | |
544 | if (unlikely(size < 0 || size > XFS_DFORK_SIZE(dip, ip->i_mount, whichfork))) { | |
65333b4c | 545 | xfs_warn(ip->i_mount, "corrupt inode %Lu ((a)extents = %d).", |
1da177e4 LT |
546 | (unsigned long long) ip->i_ino, nex); |
547 | XFS_CORRUPTION_ERROR("xfs_iformat_extents(1)", XFS_ERRLEVEL_LOW, | |
548 | ip->i_mount, dip); | |
549 | return XFS_ERROR(EFSCORRUPTED); | |
550 | } | |
551 | ||
4eea22f0 | 552 | ifp->if_real_bytes = 0; |
1da177e4 LT |
553 | if (nex == 0) |
554 | ifp->if_u1.if_extents = NULL; | |
555 | else if (nex <= XFS_INLINE_EXTS) | |
556 | ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext; | |
4eea22f0 MK |
557 | else |
558 | xfs_iext_add(ifp, 0, nex); | |
559 | ||
1da177e4 | 560 | ifp->if_bytes = size; |
1da177e4 LT |
561 | if (size) { |
562 | dp = (xfs_bmbt_rec_t *) XFS_DFORK_PTR(dip, whichfork); | |
a6f64d4a | 563 | xfs_validate_extents(ifp, nex, XFS_EXTFMT_INODE(ip)); |
4eea22f0 | 564 | for (i = 0; i < nex; i++, dp++) { |
a6f64d4a | 565 | xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, i); |
597bca63 HH |
566 | ep->l0 = get_unaligned_be64(&dp->l0); |
567 | ep->l1 = get_unaligned_be64(&dp->l1); | |
1da177e4 | 568 | } |
3a59c94c | 569 | XFS_BMAP_TRACE_EXLIST(ip, nex, whichfork); |
1da177e4 LT |
570 | if (whichfork != XFS_DATA_FORK || |
571 | XFS_EXTFMT_INODE(ip) == XFS_EXTFMT_NOSTATE) | |
572 | if (unlikely(xfs_check_nostate_extents( | |
4eea22f0 | 573 | ifp, 0, nex))) { |
1da177e4 LT |
574 | XFS_ERROR_REPORT("xfs_iformat_extents(2)", |
575 | XFS_ERRLEVEL_LOW, | |
576 | ip->i_mount); | |
577 | return XFS_ERROR(EFSCORRUPTED); | |
578 | } | |
579 | } | |
580 | ifp->if_flags |= XFS_IFEXTENTS; | |
581 | return 0; | |
582 | } | |
583 | ||
584 | /* | |
585 | * The file has too many extents to fit into | |
586 | * the inode, so they are in B-tree format. | |
587 | * Allocate a buffer for the root of the B-tree | |
588 | * and copy the root into it. The i_extents | |
589 | * field will remain NULL until all of the | |
590 | * extents are read in (when they are needed). | |
591 | */ | |
592 | STATIC int | |
593 | xfs_iformat_btree( | |
594 | xfs_inode_t *ip, | |
595 | xfs_dinode_t *dip, | |
596 | int whichfork) | |
597 | { | |
598 | xfs_bmdr_block_t *dfp; | |
599 | xfs_ifork_t *ifp; | |
600 | /* REFERENCED */ | |
601 | int nrecs; | |
602 | int size; | |
603 | ||
604 | ifp = XFS_IFORK_PTR(ip, whichfork); | |
605 | dfp = (xfs_bmdr_block_t *)XFS_DFORK_PTR(dip, whichfork); | |
606 | size = XFS_BMAP_BROOT_SPACE(dfp); | |
60197e8d | 607 | nrecs = be16_to_cpu(dfp->bb_numrecs); |
1da177e4 LT |
608 | |
609 | /* | |
610 | * blow out if -- fork has less extents than can fit in | |
611 | * fork (fork shouldn't be a btree format), root btree | |
612 | * block has more records than can fit into the fork, | |
613 | * or the number of extents is greater than the number of | |
614 | * blocks. | |
615 | */ | |
8096b1eb CH |
616 | if (unlikely(XFS_IFORK_NEXTENTS(ip, whichfork) <= |
617 | XFS_IFORK_MAXEXT(ip, whichfork) || | |
618 | XFS_BMDR_SPACE_CALC(nrecs) > | |
619 | XFS_DFORK_SIZE(dip, ip->i_mount, whichfork) || | |
620 | XFS_IFORK_NEXTENTS(ip, whichfork) > ip->i_d.di_nblocks)) { | |
65333b4c | 621 | xfs_warn(ip->i_mount, "corrupt inode %Lu (btree).", |
1da177e4 | 622 | (unsigned long long) ip->i_ino); |
65333b4c DC |
623 | XFS_CORRUPTION_ERROR("xfs_iformat_btree", XFS_ERRLEVEL_LOW, |
624 | ip->i_mount, dip); | |
1da177e4 LT |
625 | return XFS_ERROR(EFSCORRUPTED); |
626 | } | |
627 | ||
628 | ifp->if_broot_bytes = size; | |
4a7edddc | 629 | ifp->if_broot = kmem_alloc(size, KM_SLEEP | KM_NOFS); |
1da177e4 LT |
630 | ASSERT(ifp->if_broot != NULL); |
631 | /* | |
632 | * Copy and convert from the on-disk structure | |
633 | * to the in-memory structure. | |
634 | */ | |
60197e8d CH |
635 | xfs_bmdr_to_bmbt(ip->i_mount, dfp, |
636 | XFS_DFORK_SIZE(dip, ip->i_mount, whichfork), | |
637 | ifp->if_broot, size); | |
1da177e4 LT |
638 | ifp->if_flags &= ~XFS_IFEXTENTS; |
639 | ifp->if_flags |= XFS_IFBROOT; | |
640 | ||
641 | return 0; | |
642 | } | |
643 | ||
d96f8f89 | 644 | STATIC void |
347d1c01 CH |
645 | xfs_dinode_from_disk( |
646 | xfs_icdinode_t *to, | |
81591fe2 | 647 | xfs_dinode_t *from) |
1da177e4 | 648 | { |
347d1c01 CH |
649 | to->di_magic = be16_to_cpu(from->di_magic); |
650 | to->di_mode = be16_to_cpu(from->di_mode); | |
651 | to->di_version = from ->di_version; | |
652 | to->di_format = from->di_format; | |
653 | to->di_onlink = be16_to_cpu(from->di_onlink); | |
654 | to->di_uid = be32_to_cpu(from->di_uid); | |
655 | to->di_gid = be32_to_cpu(from->di_gid); | |
656 | to->di_nlink = be32_to_cpu(from->di_nlink); | |
6743099c AM |
657 | to->di_projid_lo = be16_to_cpu(from->di_projid_lo); |
658 | to->di_projid_hi = be16_to_cpu(from->di_projid_hi); | |
347d1c01 CH |
659 | memcpy(to->di_pad, from->di_pad, sizeof(to->di_pad)); |
660 | to->di_flushiter = be16_to_cpu(from->di_flushiter); | |
661 | to->di_atime.t_sec = be32_to_cpu(from->di_atime.t_sec); | |
662 | to->di_atime.t_nsec = be32_to_cpu(from->di_atime.t_nsec); | |
663 | to->di_mtime.t_sec = be32_to_cpu(from->di_mtime.t_sec); | |
664 | to->di_mtime.t_nsec = be32_to_cpu(from->di_mtime.t_nsec); | |
665 | to->di_ctime.t_sec = be32_to_cpu(from->di_ctime.t_sec); | |
666 | to->di_ctime.t_nsec = be32_to_cpu(from->di_ctime.t_nsec); | |
667 | to->di_size = be64_to_cpu(from->di_size); | |
668 | to->di_nblocks = be64_to_cpu(from->di_nblocks); | |
669 | to->di_extsize = be32_to_cpu(from->di_extsize); | |
670 | to->di_nextents = be32_to_cpu(from->di_nextents); | |
671 | to->di_anextents = be16_to_cpu(from->di_anextents); | |
672 | to->di_forkoff = from->di_forkoff; | |
673 | to->di_aformat = from->di_aformat; | |
674 | to->di_dmevmask = be32_to_cpu(from->di_dmevmask); | |
675 | to->di_dmstate = be16_to_cpu(from->di_dmstate); | |
676 | to->di_flags = be16_to_cpu(from->di_flags); | |
677 | to->di_gen = be32_to_cpu(from->di_gen); | |
678 | } | |
679 | ||
680 | void | |
681 | xfs_dinode_to_disk( | |
81591fe2 | 682 | xfs_dinode_t *to, |
347d1c01 CH |
683 | xfs_icdinode_t *from) |
684 | { | |
685 | to->di_magic = cpu_to_be16(from->di_magic); | |
686 | to->di_mode = cpu_to_be16(from->di_mode); | |
687 | to->di_version = from ->di_version; | |
688 | to->di_format = from->di_format; | |
689 | to->di_onlink = cpu_to_be16(from->di_onlink); | |
690 | to->di_uid = cpu_to_be32(from->di_uid); | |
691 | to->di_gid = cpu_to_be32(from->di_gid); | |
692 | to->di_nlink = cpu_to_be32(from->di_nlink); | |
6743099c AM |
693 | to->di_projid_lo = cpu_to_be16(from->di_projid_lo); |
694 | to->di_projid_hi = cpu_to_be16(from->di_projid_hi); | |
347d1c01 CH |
695 | memcpy(to->di_pad, from->di_pad, sizeof(to->di_pad)); |
696 | to->di_flushiter = cpu_to_be16(from->di_flushiter); | |
697 | to->di_atime.t_sec = cpu_to_be32(from->di_atime.t_sec); | |
698 | to->di_atime.t_nsec = cpu_to_be32(from->di_atime.t_nsec); | |
699 | to->di_mtime.t_sec = cpu_to_be32(from->di_mtime.t_sec); | |
700 | to->di_mtime.t_nsec = cpu_to_be32(from->di_mtime.t_nsec); | |
701 | to->di_ctime.t_sec = cpu_to_be32(from->di_ctime.t_sec); | |
702 | to->di_ctime.t_nsec = cpu_to_be32(from->di_ctime.t_nsec); | |
703 | to->di_size = cpu_to_be64(from->di_size); | |
704 | to->di_nblocks = cpu_to_be64(from->di_nblocks); | |
705 | to->di_extsize = cpu_to_be32(from->di_extsize); | |
706 | to->di_nextents = cpu_to_be32(from->di_nextents); | |
707 | to->di_anextents = cpu_to_be16(from->di_anextents); | |
708 | to->di_forkoff = from->di_forkoff; | |
709 | to->di_aformat = from->di_aformat; | |
710 | to->di_dmevmask = cpu_to_be32(from->di_dmevmask); | |
711 | to->di_dmstate = cpu_to_be16(from->di_dmstate); | |
712 | to->di_flags = cpu_to_be16(from->di_flags); | |
713 | to->di_gen = cpu_to_be32(from->di_gen); | |
1da177e4 LT |
714 | } |
715 | ||
716 | STATIC uint | |
717 | _xfs_dic2xflags( | |
1da177e4 LT |
718 | __uint16_t di_flags) |
719 | { | |
720 | uint flags = 0; | |
721 | ||
722 | if (di_flags & XFS_DIFLAG_ANY) { | |
723 | if (di_flags & XFS_DIFLAG_REALTIME) | |
724 | flags |= XFS_XFLAG_REALTIME; | |
725 | if (di_flags & XFS_DIFLAG_PREALLOC) | |
726 | flags |= XFS_XFLAG_PREALLOC; | |
727 | if (di_flags & XFS_DIFLAG_IMMUTABLE) | |
728 | flags |= XFS_XFLAG_IMMUTABLE; | |
729 | if (di_flags & XFS_DIFLAG_APPEND) | |
730 | flags |= XFS_XFLAG_APPEND; | |
731 | if (di_flags & XFS_DIFLAG_SYNC) | |
732 | flags |= XFS_XFLAG_SYNC; | |
733 | if (di_flags & XFS_DIFLAG_NOATIME) | |
734 | flags |= XFS_XFLAG_NOATIME; | |
735 | if (di_flags & XFS_DIFLAG_NODUMP) | |
736 | flags |= XFS_XFLAG_NODUMP; | |
737 | if (di_flags & XFS_DIFLAG_RTINHERIT) | |
738 | flags |= XFS_XFLAG_RTINHERIT; | |
739 | if (di_flags & XFS_DIFLAG_PROJINHERIT) | |
740 | flags |= XFS_XFLAG_PROJINHERIT; | |
741 | if (di_flags & XFS_DIFLAG_NOSYMLINKS) | |
742 | flags |= XFS_XFLAG_NOSYMLINKS; | |
dd9f438e NS |
743 | if (di_flags & XFS_DIFLAG_EXTSIZE) |
744 | flags |= XFS_XFLAG_EXTSIZE; | |
745 | if (di_flags & XFS_DIFLAG_EXTSZINHERIT) | |
746 | flags |= XFS_XFLAG_EXTSZINHERIT; | |
d3446eac BN |
747 | if (di_flags & XFS_DIFLAG_NODEFRAG) |
748 | flags |= XFS_XFLAG_NODEFRAG; | |
2a82b8be DC |
749 | if (di_flags & XFS_DIFLAG_FILESTREAM) |
750 | flags |= XFS_XFLAG_FILESTREAM; | |
1da177e4 LT |
751 | } |
752 | ||
753 | return flags; | |
754 | } | |
755 | ||
756 | uint | |
757 | xfs_ip2xflags( | |
758 | xfs_inode_t *ip) | |
759 | { | |
347d1c01 | 760 | xfs_icdinode_t *dic = &ip->i_d; |
1da177e4 | 761 | |
a916e2bd | 762 | return _xfs_dic2xflags(dic->di_flags) | |
45ba598e | 763 | (XFS_IFORK_Q(ip) ? XFS_XFLAG_HASATTR : 0); |
1da177e4 LT |
764 | } |
765 | ||
766 | uint | |
767 | xfs_dic2xflags( | |
45ba598e | 768 | xfs_dinode_t *dip) |
1da177e4 | 769 | { |
81591fe2 | 770 | return _xfs_dic2xflags(be16_to_cpu(dip->di_flags)) | |
45ba598e | 771 | (XFS_DFORK_Q(dip) ? XFS_XFLAG_HASATTR : 0); |
1da177e4 LT |
772 | } |
773 | ||
07c8f675 | 774 | /* |
24f211ba | 775 | * Read the disk inode attributes into the in-core inode structure. |
1da177e4 LT |
776 | */ |
777 | int | |
778 | xfs_iread( | |
779 | xfs_mount_t *mp, | |
780 | xfs_trans_t *tp, | |
24f211ba | 781 | xfs_inode_t *ip, |
24f211ba | 782 | uint iget_flags) |
1da177e4 LT |
783 | { |
784 | xfs_buf_t *bp; | |
785 | xfs_dinode_t *dip; | |
1da177e4 LT |
786 | int error; |
787 | ||
1da177e4 | 788 | /* |
92bfc6e7 | 789 | * Fill in the location information in the in-core inode. |
1da177e4 | 790 | */ |
24f211ba | 791 | error = xfs_imap(mp, tp, ip->i_ino, &ip->i_imap, iget_flags); |
76d8b277 | 792 | if (error) |
24f211ba | 793 | return error; |
76d8b277 CH |
794 | |
795 | /* | |
92bfc6e7 | 796 | * Get pointers to the on-disk inode and the buffer containing it. |
76d8b277 | 797 | */ |
a8acad70 | 798 | error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &bp, 0, iget_flags); |
9ed0451e | 799 | if (error) |
24f211ba | 800 | return error; |
92bfc6e7 | 801 | dip = (xfs_dinode_t *)xfs_buf_offset(bp, ip->i_imap.im_boffset); |
1da177e4 | 802 | |
1da177e4 LT |
803 | /* |
804 | * If we got something that isn't an inode it means someone | |
805 | * (nfs or dmi) has a stale handle. | |
806 | */ | |
69ef921b | 807 | if (dip->di_magic != cpu_to_be16(XFS_DINODE_MAGIC)) { |
1da177e4 | 808 | #ifdef DEBUG |
53487786 DC |
809 | xfs_alert(mp, |
810 | "%s: dip->di_magic (0x%x) != XFS_DINODE_MAGIC (0x%x)", | |
811 | __func__, be16_to_cpu(dip->di_magic), XFS_DINODE_MAGIC); | |
1da177e4 | 812 | #endif /* DEBUG */ |
9ed0451e CH |
813 | error = XFS_ERROR(EINVAL); |
814 | goto out_brelse; | |
1da177e4 LT |
815 | } |
816 | ||
817 | /* | |
818 | * If the on-disk inode is already linked to a directory | |
819 | * entry, copy all of the inode into the in-core inode. | |
820 | * xfs_iformat() handles copying in the inode format | |
821 | * specific information. | |
822 | * Otherwise, just get the truly permanent information. | |
823 | */ | |
81591fe2 CH |
824 | if (dip->di_mode) { |
825 | xfs_dinode_from_disk(&ip->i_d, dip); | |
1da177e4 LT |
826 | error = xfs_iformat(ip, dip); |
827 | if (error) { | |
1da177e4 | 828 | #ifdef DEBUG |
53487786 DC |
829 | xfs_alert(mp, "%s: xfs_iformat() returned error %d", |
830 | __func__, error); | |
1da177e4 | 831 | #endif /* DEBUG */ |
9ed0451e | 832 | goto out_brelse; |
1da177e4 LT |
833 | } |
834 | } else { | |
81591fe2 CH |
835 | ip->i_d.di_magic = be16_to_cpu(dip->di_magic); |
836 | ip->i_d.di_version = dip->di_version; | |
837 | ip->i_d.di_gen = be32_to_cpu(dip->di_gen); | |
838 | ip->i_d.di_flushiter = be16_to_cpu(dip->di_flushiter); | |
1da177e4 LT |
839 | /* |
840 | * Make sure to pull in the mode here as well in | |
841 | * case the inode is released without being used. | |
842 | * This ensures that xfs_inactive() will see that | |
843 | * the inode is already free and not try to mess | |
844 | * with the uninitialized part of it. | |
845 | */ | |
846 | ip->i_d.di_mode = 0; | |
1da177e4 LT |
847 | } |
848 | ||
1da177e4 LT |
849 | /* |
850 | * The inode format changed when we moved the link count and | |
851 | * made it 32 bits long. If this is an old format inode, | |
852 | * convert it in memory to look like a new one. If it gets | |
853 | * flushed to disk we will convert back before flushing or | |
854 | * logging it. We zero out the new projid field and the old link | |
855 | * count field. We'll handle clearing the pad field (the remains | |
856 | * of the old uuid field) when we actually convert the inode to | |
857 | * the new format. We don't change the version number so that we | |
858 | * can distinguish this from a real new format inode. | |
859 | */ | |
51ce16d5 | 860 | if (ip->i_d.di_version == 1) { |
1da177e4 LT |
861 | ip->i_d.di_nlink = ip->i_d.di_onlink; |
862 | ip->i_d.di_onlink = 0; | |
6743099c | 863 | xfs_set_projid(ip, 0); |
1da177e4 LT |
864 | } |
865 | ||
866 | ip->i_delayed_blks = 0; | |
867 | ||
868 | /* | |
869 | * Mark the buffer containing the inode as something to keep | |
870 | * around for a while. This helps to keep recently accessed | |
871 | * meta-data in-core longer. | |
872 | */ | |
821eb21d | 873 | xfs_buf_set_ref(bp, XFS_INO_REF); |
1da177e4 LT |
874 | |
875 | /* | |
876 | * Use xfs_trans_brelse() to release the buffer containing the | |
877 | * on-disk inode, because it was acquired with xfs_trans_read_buf() | |
878 | * in xfs_itobp() above. If tp is NULL, this is just a normal | |
879 | * brelse(). If we're within a transaction, then xfs_trans_brelse() | |
880 | * will only release the buffer if it is not dirty within the | |
881 | * transaction. It will be OK to release the buffer in this case, | |
882 | * because inodes on disk are never destroyed and we will be | |
883 | * locking the new in-core inode before putting it in the hash | |
884 | * table where other processes can find it. Thus we don't have | |
885 | * to worry about the inode being changed just because we released | |
886 | * the buffer. | |
887 | */ | |
9ed0451e CH |
888 | out_brelse: |
889 | xfs_trans_brelse(tp, bp); | |
9ed0451e | 890 | return error; |
1da177e4 LT |
891 | } |
892 | ||
893 | /* | |
894 | * Read in extents from a btree-format inode. | |
895 | * Allocate and fill in if_extents. Real work is done in xfs_bmap.c. | |
896 | */ | |
897 | int | |
898 | xfs_iread_extents( | |
899 | xfs_trans_t *tp, | |
900 | xfs_inode_t *ip, | |
901 | int whichfork) | |
902 | { | |
903 | int error; | |
904 | xfs_ifork_t *ifp; | |
4eea22f0 | 905 | xfs_extnum_t nextents; |
1da177e4 LT |
906 | |
907 | if (unlikely(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) { | |
908 | XFS_ERROR_REPORT("xfs_iread_extents", XFS_ERRLEVEL_LOW, | |
909 | ip->i_mount); | |
910 | return XFS_ERROR(EFSCORRUPTED); | |
911 | } | |
4eea22f0 | 912 | nextents = XFS_IFORK_NEXTENTS(ip, whichfork); |
1da177e4 | 913 | ifp = XFS_IFORK_PTR(ip, whichfork); |
4eea22f0 | 914 | |
1da177e4 LT |
915 | /* |
916 | * We know that the size is valid (it's checked in iformat_btree) | |
917 | */ | |
4eea22f0 | 918 | ifp->if_bytes = ifp->if_real_bytes = 0; |
1da177e4 | 919 | ifp->if_flags |= XFS_IFEXTENTS; |
4eea22f0 | 920 | xfs_iext_add(ifp, 0, nextents); |
1da177e4 LT |
921 | error = xfs_bmap_read_extents(tp, ip, whichfork); |
922 | if (error) { | |
4eea22f0 | 923 | xfs_iext_destroy(ifp); |
1da177e4 LT |
924 | ifp->if_flags &= ~XFS_IFEXTENTS; |
925 | return error; | |
926 | } | |
a6f64d4a | 927 | xfs_validate_extents(ifp, nextents, XFS_EXTFMT_INODE(ip)); |
1da177e4 LT |
928 | return 0; |
929 | } | |
930 | ||
931 | /* | |
932 | * Allocate an inode on disk and return a copy of its in-core version. | |
933 | * The in-core inode is locked exclusively. Set mode, nlink, and rdev | |
934 | * appropriately within the inode. The uid and gid for the inode are | |
935 | * set according to the contents of the given cred structure. | |
936 | * | |
937 | * Use xfs_dialloc() to allocate the on-disk inode. If xfs_dialloc() | |
938 | * has a free inode available, call xfs_iget() | |
939 | * to obtain the in-core version of the allocated inode. Finally, | |
940 | * fill in the inode and log its initial contents. In this case, | |
941 | * ialloc_context would be set to NULL and call_again set to false. | |
942 | * | |
943 | * If xfs_dialloc() does not have an available inode, | |
944 | * it will replenish its supply by doing an allocation. Since we can | |
945 | * only do one allocation within a transaction without deadlocks, we | |
946 | * must commit the current transaction before returning the inode itself. | |
947 | * In this case, therefore, we will set call_again to true and return. | |
948 | * The caller should then commit the current transaction, start a new | |
949 | * transaction, and call xfs_ialloc() again to actually get the inode. | |
950 | * | |
951 | * To ensure that some other process does not grab the inode that | |
952 | * was allocated during the first call to xfs_ialloc(), this routine | |
953 | * also returns the [locked] bp pointing to the head of the freelist | |
954 | * as ialloc_context. The caller should hold this buffer across | |
955 | * the commit and pass it back into this routine on the second call. | |
b11f94d5 DC |
956 | * |
957 | * If we are allocating quota inodes, we do not have a parent inode | |
958 | * to attach to or associate with (i.e. pip == NULL) because they | |
959 | * are not linked into the directory structure - they are attached | |
960 | * directly to the superblock - and so have no parent. | |
1da177e4 LT |
961 | */ |
962 | int | |
963 | xfs_ialloc( | |
964 | xfs_trans_t *tp, | |
965 | xfs_inode_t *pip, | |
576b1d67 | 966 | umode_t mode, |
31b084ae | 967 | xfs_nlink_t nlink, |
1da177e4 | 968 | xfs_dev_t rdev, |
6743099c | 969 | prid_t prid, |
1da177e4 LT |
970 | int okalloc, |
971 | xfs_buf_t **ialloc_context, | |
972 | boolean_t *call_again, | |
973 | xfs_inode_t **ipp) | |
974 | { | |
975 | xfs_ino_t ino; | |
976 | xfs_inode_t *ip; | |
1da177e4 LT |
977 | uint flags; |
978 | int error; | |
dff35fd4 | 979 | timespec_t tv; |
bf904248 | 980 | int filestreams = 0; |
1da177e4 LT |
981 | |
982 | /* | |
983 | * Call the space management code to pick | |
984 | * the on-disk inode to be allocated. | |
985 | */ | |
b11f94d5 | 986 | error = xfs_dialloc(tp, pip ? pip->i_ino : 0, mode, okalloc, |
1da177e4 | 987 | ialloc_context, call_again, &ino); |
bf904248 | 988 | if (error) |
1da177e4 | 989 | return error; |
1da177e4 LT |
990 | if (*call_again || ino == NULLFSINO) { |
991 | *ipp = NULL; | |
992 | return 0; | |
993 | } | |
994 | ASSERT(*ialloc_context == NULL); | |
995 | ||
996 | /* | |
997 | * Get the in-core inode with the lock held exclusively. | |
998 | * This is because we're setting fields here we need | |
999 | * to prevent others from looking at until we're done. | |
1000 | */ | |
ec3ba85f CH |
1001 | error = xfs_iget(tp->t_mountp, tp, ino, XFS_IGET_CREATE, |
1002 | XFS_ILOCK_EXCL, &ip); | |
bf904248 | 1003 | if (error) |
1da177e4 | 1004 | return error; |
1da177e4 LT |
1005 | ASSERT(ip != NULL); |
1006 | ||
576b1d67 | 1007 | ip->i_d.di_mode = mode; |
1da177e4 LT |
1008 | ip->i_d.di_onlink = 0; |
1009 | ip->i_d.di_nlink = nlink; | |
1010 | ASSERT(ip->i_d.di_nlink == nlink); | |
9e2b2dc4 DH |
1011 | ip->i_d.di_uid = current_fsuid(); |
1012 | ip->i_d.di_gid = current_fsgid(); | |
6743099c | 1013 | xfs_set_projid(ip, prid); |
1da177e4 LT |
1014 | memset(&(ip->i_d.di_pad[0]), 0, sizeof(ip->i_d.di_pad)); |
1015 | ||
1016 | /* | |
1017 | * If the superblock version is up to where we support new format | |
1018 | * inodes and this is currently an old format inode, then change | |
1019 | * the inode version number now. This way we only do the conversion | |
1020 | * here rather than here and in the flush/logging code. | |
1021 | */ | |
62118709 | 1022 | if (xfs_sb_version_hasnlink(&tp->t_mountp->m_sb) && |
51ce16d5 CH |
1023 | ip->i_d.di_version == 1) { |
1024 | ip->i_d.di_version = 2; | |
1da177e4 LT |
1025 | /* |
1026 | * We've already zeroed the old link count, the projid field, | |
1027 | * and the pad field. | |
1028 | */ | |
1029 | } | |
1030 | ||
1031 | /* | |
1032 | * Project ids won't be stored on disk if we are using a version 1 inode. | |
1033 | */ | |
51ce16d5 | 1034 | if ((prid != 0) && (ip->i_d.di_version == 1)) |
1da177e4 LT |
1035 | xfs_bump_ino_vers2(tp, ip); |
1036 | ||
bd186aa9 | 1037 | if (pip && XFS_INHERIT_GID(pip)) { |
1da177e4 | 1038 | ip->i_d.di_gid = pip->i_d.di_gid; |
abbede1b | 1039 | if ((pip->i_d.di_mode & S_ISGID) && S_ISDIR(mode)) { |
1da177e4 LT |
1040 | ip->i_d.di_mode |= S_ISGID; |
1041 | } | |
1042 | } | |
1043 | ||
1044 | /* | |
1045 | * If the group ID of the new file does not match the effective group | |
1046 | * ID or one of the supplementary group IDs, the S_ISGID bit is cleared | |
1047 | * (and only if the irix_sgid_inherit compatibility variable is set). | |
1048 | */ | |
1049 | if ((irix_sgid_inherit) && | |
1050 | (ip->i_d.di_mode & S_ISGID) && | |
1051 | (!in_group_p((gid_t)ip->i_d.di_gid))) { | |
1052 | ip->i_d.di_mode &= ~S_ISGID; | |
1053 | } | |
1054 | ||
1055 | ip->i_d.di_size = 0; | |
1056 | ip->i_d.di_nextents = 0; | |
1057 | ASSERT(ip->i_d.di_nblocks == 0); | |
dff35fd4 CH |
1058 | |
1059 | nanotime(&tv); | |
1060 | ip->i_d.di_mtime.t_sec = (__int32_t)tv.tv_sec; | |
1061 | ip->i_d.di_mtime.t_nsec = (__int32_t)tv.tv_nsec; | |
1062 | ip->i_d.di_atime = ip->i_d.di_mtime; | |
1063 | ip->i_d.di_ctime = ip->i_d.di_mtime; | |
1064 | ||
1da177e4 LT |
1065 | /* |
1066 | * di_gen will have been taken care of in xfs_iread. | |
1067 | */ | |
1068 | ip->i_d.di_extsize = 0; | |
1069 | ip->i_d.di_dmevmask = 0; | |
1070 | ip->i_d.di_dmstate = 0; | |
1071 | ip->i_d.di_flags = 0; | |
1072 | flags = XFS_ILOG_CORE; | |
1073 | switch (mode & S_IFMT) { | |
1074 | case S_IFIFO: | |
1075 | case S_IFCHR: | |
1076 | case S_IFBLK: | |
1077 | case S_IFSOCK: | |
1078 | ip->i_d.di_format = XFS_DINODE_FMT_DEV; | |
1079 | ip->i_df.if_u2.if_rdev = rdev; | |
1080 | ip->i_df.if_flags = 0; | |
1081 | flags |= XFS_ILOG_DEV; | |
1082 | break; | |
1083 | case S_IFREG: | |
bf904248 DC |
1084 | /* |
1085 | * we can't set up filestreams until after the VFS inode | |
1086 | * is set up properly. | |
1087 | */ | |
1088 | if (pip && xfs_inode_is_filestream(pip)) | |
1089 | filestreams = 1; | |
2a82b8be | 1090 | /* fall through */ |
1da177e4 | 1091 | case S_IFDIR: |
b11f94d5 | 1092 | if (pip && (pip->i_d.di_flags & XFS_DIFLAG_ANY)) { |
365ca83d NS |
1093 | uint di_flags = 0; |
1094 | ||
abbede1b | 1095 | if (S_ISDIR(mode)) { |
365ca83d NS |
1096 | if (pip->i_d.di_flags & XFS_DIFLAG_RTINHERIT) |
1097 | di_flags |= XFS_DIFLAG_RTINHERIT; | |
dd9f438e NS |
1098 | if (pip->i_d.di_flags & XFS_DIFLAG_EXTSZINHERIT) { |
1099 | di_flags |= XFS_DIFLAG_EXTSZINHERIT; | |
1100 | ip->i_d.di_extsize = pip->i_d.di_extsize; | |
1101 | } | |
abbede1b | 1102 | } else if (S_ISREG(mode)) { |
613d7043 | 1103 | if (pip->i_d.di_flags & XFS_DIFLAG_RTINHERIT) |
365ca83d | 1104 | di_flags |= XFS_DIFLAG_REALTIME; |
dd9f438e NS |
1105 | if (pip->i_d.di_flags & XFS_DIFLAG_EXTSZINHERIT) { |
1106 | di_flags |= XFS_DIFLAG_EXTSIZE; | |
1107 | ip->i_d.di_extsize = pip->i_d.di_extsize; | |
1108 | } | |
1da177e4 LT |
1109 | } |
1110 | if ((pip->i_d.di_flags & XFS_DIFLAG_NOATIME) && | |
1111 | xfs_inherit_noatime) | |
365ca83d | 1112 | di_flags |= XFS_DIFLAG_NOATIME; |
1da177e4 LT |
1113 | if ((pip->i_d.di_flags & XFS_DIFLAG_NODUMP) && |
1114 | xfs_inherit_nodump) | |
365ca83d | 1115 | di_flags |= XFS_DIFLAG_NODUMP; |
1da177e4 LT |
1116 | if ((pip->i_d.di_flags & XFS_DIFLAG_SYNC) && |
1117 | xfs_inherit_sync) | |
365ca83d | 1118 | di_flags |= XFS_DIFLAG_SYNC; |
1da177e4 LT |
1119 | if ((pip->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) && |
1120 | xfs_inherit_nosymlinks) | |
365ca83d NS |
1121 | di_flags |= XFS_DIFLAG_NOSYMLINKS; |
1122 | if (pip->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) | |
1123 | di_flags |= XFS_DIFLAG_PROJINHERIT; | |
d3446eac BN |
1124 | if ((pip->i_d.di_flags & XFS_DIFLAG_NODEFRAG) && |
1125 | xfs_inherit_nodefrag) | |
1126 | di_flags |= XFS_DIFLAG_NODEFRAG; | |
2a82b8be DC |
1127 | if (pip->i_d.di_flags & XFS_DIFLAG_FILESTREAM) |
1128 | di_flags |= XFS_DIFLAG_FILESTREAM; | |
365ca83d | 1129 | ip->i_d.di_flags |= di_flags; |
1da177e4 LT |
1130 | } |
1131 | /* FALLTHROUGH */ | |
1132 | case S_IFLNK: | |
1133 | ip->i_d.di_format = XFS_DINODE_FMT_EXTENTS; | |
1134 | ip->i_df.if_flags = XFS_IFEXTENTS; | |
1135 | ip->i_df.if_bytes = ip->i_df.if_real_bytes = 0; | |
1136 | ip->i_df.if_u1.if_extents = NULL; | |
1137 | break; | |
1138 | default: | |
1139 | ASSERT(0); | |
1140 | } | |
1141 | /* | |
1142 | * Attribute fork settings for new inode. | |
1143 | */ | |
1144 | ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS; | |
1145 | ip->i_d.di_anextents = 0; | |
1146 | ||
1147 | /* | |
1148 | * Log the new values stuffed into the inode. | |
1149 | */ | |
ddc3415a | 1150 | xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL); |
1da177e4 LT |
1151 | xfs_trans_log_inode(tp, ip, flags); |
1152 | ||
b83bd138 | 1153 | /* now that we have an i_mode we can setup inode ops and unlock */ |
41be8bed | 1154 | xfs_setup_inode(ip); |
1da177e4 | 1155 | |
bf904248 DC |
1156 | /* now we have set up the vfs inode we can associate the filestream */ |
1157 | if (filestreams) { | |
1158 | error = xfs_filestream_associate(pip, ip); | |
1159 | if (error < 0) | |
1160 | return -error; | |
1161 | if (!error) | |
1162 | xfs_iflags_set(ip, XFS_IFILESTREAM); | |
1163 | } | |
1164 | ||
1da177e4 LT |
1165 | *ipp = ip; |
1166 | return 0; | |
1167 | } | |
1168 | ||
1da177e4 | 1169 | /* |
8f04c47a CH |
1170 | * Free up the underlying blocks past new_size. The new size must be smaller |
1171 | * than the current size. This routine can be used both for the attribute and | |
1172 | * data fork, and does not modify the inode size, which is left to the caller. | |
1da177e4 | 1173 | * |
f6485057 DC |
1174 | * The transaction passed to this routine must have made a permanent log |
1175 | * reservation of at least XFS_ITRUNCATE_LOG_RES. This routine may commit the | |
1176 | * given transaction and start new ones, so make sure everything involved in | |
1177 | * the transaction is tidy before calling here. Some transaction will be | |
1178 | * returned to the caller to be committed. The incoming transaction must | |
1179 | * already include the inode, and both inode locks must be held exclusively. | |
1180 | * The inode must also be "held" within the transaction. On return the inode | |
1181 | * will be "held" within the returned transaction. This routine does NOT | |
1182 | * require any disk space to be reserved for it within the transaction. | |
1da177e4 | 1183 | * |
f6485057 DC |
1184 | * If we get an error, we must return with the inode locked and linked into the |
1185 | * current transaction. This keeps things simple for the higher level code, | |
1186 | * because it always knows that the inode is locked and held in the transaction | |
1187 | * that returns to it whether errors occur or not. We don't mark the inode | |
1188 | * dirty on error so that transactions can be easily aborted if possible. | |
1da177e4 LT |
1189 | */ |
1190 | int | |
8f04c47a CH |
1191 | xfs_itruncate_extents( |
1192 | struct xfs_trans **tpp, | |
1193 | struct xfs_inode *ip, | |
1194 | int whichfork, | |
1195 | xfs_fsize_t new_size) | |
1da177e4 | 1196 | { |
8f04c47a CH |
1197 | struct xfs_mount *mp = ip->i_mount; |
1198 | struct xfs_trans *tp = *tpp; | |
1199 | struct xfs_trans *ntp; | |
1200 | xfs_bmap_free_t free_list; | |
1201 | xfs_fsblock_t first_block; | |
1202 | xfs_fileoff_t first_unmap_block; | |
1203 | xfs_fileoff_t last_block; | |
1204 | xfs_filblks_t unmap_len; | |
1205 | int committed; | |
1206 | int error = 0; | |
1207 | int done = 0; | |
1da177e4 | 1208 | |
579aa9ca | 1209 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_IOLOCK_EXCL)); |
ce7ae151 | 1210 | ASSERT(new_size <= XFS_ISIZE(ip)); |
8f04c47a | 1211 | ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES); |
1da177e4 | 1212 | ASSERT(ip->i_itemp != NULL); |
898621d5 | 1213 | ASSERT(ip->i_itemp->ili_lock_flags == 0); |
8f04c47a | 1214 | ASSERT(!XFS_NOT_DQATTACHED(mp, ip)); |
1da177e4 | 1215 | |
673e8e59 CH |
1216 | trace_xfs_itruncate_extents_start(ip, new_size); |
1217 | ||
1da177e4 LT |
1218 | /* |
1219 | * Since it is possible for space to become allocated beyond | |
1220 | * the end of the file (in a crash where the space is allocated | |
1221 | * but the inode size is not yet updated), simply remove any | |
1222 | * blocks which show up between the new EOF and the maximum | |
1223 | * possible file size. If the first block to be removed is | |
1224 | * beyond the maximum file size (ie it is the same as last_block), | |
1225 | * then there is nothing to do. | |
1226 | */ | |
8f04c47a | 1227 | first_unmap_block = XFS_B_TO_FSB(mp, (xfs_ufsize_t)new_size); |
1da177e4 | 1228 | last_block = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp)); |
8f04c47a CH |
1229 | if (first_unmap_block == last_block) |
1230 | return 0; | |
1231 | ||
1232 | ASSERT(first_unmap_block < last_block); | |
1233 | unmap_len = last_block - first_unmap_block + 1; | |
1da177e4 | 1234 | while (!done) { |
9d87c319 | 1235 | xfs_bmap_init(&free_list, &first_block); |
8f04c47a | 1236 | error = xfs_bunmapi(tp, ip, |
3e57ecf6 | 1237 | first_unmap_block, unmap_len, |
8f04c47a | 1238 | xfs_bmapi_aflag(whichfork), |
1da177e4 | 1239 | XFS_ITRUNC_MAX_EXTENTS, |
3e57ecf6 | 1240 | &first_block, &free_list, |
b4e9181e | 1241 | &done); |
8f04c47a CH |
1242 | if (error) |
1243 | goto out_bmap_cancel; | |
1da177e4 LT |
1244 | |
1245 | /* | |
1246 | * Duplicate the transaction that has the permanent | |
1247 | * reservation and commit the old transaction. | |
1248 | */ | |
8f04c47a | 1249 | error = xfs_bmap_finish(&tp, &free_list, &committed); |
898621d5 | 1250 | if (committed) |
ddc3415a | 1251 | xfs_trans_ijoin(tp, ip, 0); |
8f04c47a CH |
1252 | if (error) |
1253 | goto out_bmap_cancel; | |
1da177e4 LT |
1254 | |
1255 | if (committed) { | |
1256 | /* | |
f6485057 | 1257 | * Mark the inode dirty so it will be logged and |
e5720eec | 1258 | * moved forward in the log as part of every commit. |
1da177e4 | 1259 | */ |
8f04c47a | 1260 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
1da177e4 | 1261 | } |
f6485057 | 1262 | |
8f04c47a CH |
1263 | ntp = xfs_trans_dup(tp); |
1264 | error = xfs_trans_commit(tp, 0); | |
1265 | tp = ntp; | |
e5720eec | 1266 | |
ddc3415a | 1267 | xfs_trans_ijoin(tp, ip, 0); |
f6485057 | 1268 | |
cc09c0dc | 1269 | if (error) |
8f04c47a CH |
1270 | goto out; |
1271 | ||
cc09c0dc | 1272 | /* |
8f04c47a | 1273 | * Transaction commit worked ok so we can drop the extra ticket |
cc09c0dc DC |
1274 | * reference that we gained in xfs_trans_dup() |
1275 | */ | |
8f04c47a CH |
1276 | xfs_log_ticket_put(tp->t_ticket); |
1277 | error = xfs_trans_reserve(tp, 0, | |
f6485057 DC |
1278 | XFS_ITRUNCATE_LOG_RES(mp), 0, |
1279 | XFS_TRANS_PERM_LOG_RES, | |
1280 | XFS_ITRUNCATE_LOG_COUNT); | |
1281 | if (error) | |
8f04c47a | 1282 | goto out; |
1da177e4 | 1283 | } |
8f04c47a | 1284 | |
673e8e59 CH |
1285 | /* |
1286 | * Always re-log the inode so that our permanent transaction can keep | |
1287 | * on rolling it forward in the log. | |
1288 | */ | |
1289 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); | |
1290 | ||
1291 | trace_xfs_itruncate_extents_end(ip, new_size); | |
1292 | ||
8f04c47a CH |
1293 | out: |
1294 | *tpp = tp; | |
1295 | return error; | |
1296 | out_bmap_cancel: | |
1da177e4 | 1297 | /* |
8f04c47a CH |
1298 | * If the bunmapi call encounters an error, return to the caller where |
1299 | * the transaction can be properly aborted. We just need to make sure | |
1300 | * we're not holding any resources that we were not when we came in. | |
1da177e4 | 1301 | */ |
8f04c47a CH |
1302 | xfs_bmap_cancel(&free_list); |
1303 | goto out; | |
1304 | } | |
1305 | ||
1da177e4 LT |
1306 | /* |
1307 | * This is called when the inode's link count goes to 0. | |
1308 | * We place the on-disk inode on a list in the AGI. It | |
1309 | * will be pulled from this list when the inode is freed. | |
1310 | */ | |
1311 | int | |
1312 | xfs_iunlink( | |
1313 | xfs_trans_t *tp, | |
1314 | xfs_inode_t *ip) | |
1315 | { | |
1316 | xfs_mount_t *mp; | |
1317 | xfs_agi_t *agi; | |
1318 | xfs_dinode_t *dip; | |
1319 | xfs_buf_t *agibp; | |
1320 | xfs_buf_t *ibp; | |
1da177e4 LT |
1321 | xfs_agino_t agino; |
1322 | short bucket_index; | |
1323 | int offset; | |
1324 | int error; | |
1da177e4 LT |
1325 | |
1326 | ASSERT(ip->i_d.di_nlink == 0); | |
1327 | ASSERT(ip->i_d.di_mode != 0); | |
1da177e4 LT |
1328 | |
1329 | mp = tp->t_mountp; | |
1330 | ||
1da177e4 LT |
1331 | /* |
1332 | * Get the agi buffer first. It ensures lock ordering | |
1333 | * on the list. | |
1334 | */ | |
5e1be0fb | 1335 | error = xfs_read_agi(mp, tp, XFS_INO_TO_AGNO(mp, ip->i_ino), &agibp); |
859d7182 | 1336 | if (error) |
1da177e4 | 1337 | return error; |
1da177e4 | 1338 | agi = XFS_BUF_TO_AGI(agibp); |
5e1be0fb | 1339 | |
1da177e4 LT |
1340 | /* |
1341 | * Get the index into the agi hash table for the | |
1342 | * list this inode will go on. | |
1343 | */ | |
1344 | agino = XFS_INO_TO_AGINO(mp, ip->i_ino); | |
1345 | ASSERT(agino != 0); | |
1346 | bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS; | |
1347 | ASSERT(agi->agi_unlinked[bucket_index]); | |
16259e7d | 1348 | ASSERT(be32_to_cpu(agi->agi_unlinked[bucket_index]) != agino); |
1da177e4 | 1349 | |
69ef921b | 1350 | if (agi->agi_unlinked[bucket_index] != cpu_to_be32(NULLAGINO)) { |
1da177e4 LT |
1351 | /* |
1352 | * There is already another inode in the bucket we need | |
1353 | * to add ourselves to. Add us at the front of the list. | |
1354 | * Here we put the head pointer into our next pointer, | |
1355 | * and then we fall through to point the head at us. | |
1356 | */ | |
a8acad70 | 1357 | error = xfs_itobp(mp, tp, ip, &dip, &ibp, 0); |
c319b58b VA |
1358 | if (error) |
1359 | return error; | |
1360 | ||
69ef921b | 1361 | ASSERT(dip->di_next_unlinked == cpu_to_be32(NULLAGINO)); |
1da177e4 | 1362 | dip->di_next_unlinked = agi->agi_unlinked[bucket_index]; |
92bfc6e7 | 1363 | offset = ip->i_imap.im_boffset + |
1da177e4 LT |
1364 | offsetof(xfs_dinode_t, di_next_unlinked); |
1365 | xfs_trans_inode_buf(tp, ibp); | |
1366 | xfs_trans_log_buf(tp, ibp, offset, | |
1367 | (offset + sizeof(xfs_agino_t) - 1)); | |
1368 | xfs_inobp_check(mp, ibp); | |
1369 | } | |
1370 | ||
1371 | /* | |
1372 | * Point the bucket head pointer at the inode being inserted. | |
1373 | */ | |
1374 | ASSERT(agino != 0); | |
16259e7d | 1375 | agi->agi_unlinked[bucket_index] = cpu_to_be32(agino); |
1da177e4 LT |
1376 | offset = offsetof(xfs_agi_t, agi_unlinked) + |
1377 | (sizeof(xfs_agino_t) * bucket_index); | |
1378 | xfs_trans_log_buf(tp, agibp, offset, | |
1379 | (offset + sizeof(xfs_agino_t) - 1)); | |
1380 | return 0; | |
1381 | } | |
1382 | ||
1383 | /* | |
1384 | * Pull the on-disk inode from the AGI unlinked list. | |
1385 | */ | |
1386 | STATIC int | |
1387 | xfs_iunlink_remove( | |
1388 | xfs_trans_t *tp, | |
1389 | xfs_inode_t *ip) | |
1390 | { | |
1391 | xfs_ino_t next_ino; | |
1392 | xfs_mount_t *mp; | |
1393 | xfs_agi_t *agi; | |
1394 | xfs_dinode_t *dip; | |
1395 | xfs_buf_t *agibp; | |
1396 | xfs_buf_t *ibp; | |
1397 | xfs_agnumber_t agno; | |
1da177e4 LT |
1398 | xfs_agino_t agino; |
1399 | xfs_agino_t next_agino; | |
1400 | xfs_buf_t *last_ibp; | |
6fdf8ccc | 1401 | xfs_dinode_t *last_dip = NULL; |
1da177e4 | 1402 | short bucket_index; |
6fdf8ccc | 1403 | int offset, last_offset = 0; |
1da177e4 | 1404 | int error; |
1da177e4 | 1405 | |
1da177e4 | 1406 | mp = tp->t_mountp; |
1da177e4 | 1407 | agno = XFS_INO_TO_AGNO(mp, ip->i_ino); |
1da177e4 LT |
1408 | |
1409 | /* | |
1410 | * Get the agi buffer first. It ensures lock ordering | |
1411 | * on the list. | |
1412 | */ | |
5e1be0fb CH |
1413 | error = xfs_read_agi(mp, tp, agno, &agibp); |
1414 | if (error) | |
1da177e4 | 1415 | return error; |
5e1be0fb | 1416 | |
1da177e4 | 1417 | agi = XFS_BUF_TO_AGI(agibp); |
5e1be0fb | 1418 | |
1da177e4 LT |
1419 | /* |
1420 | * Get the index into the agi hash table for the | |
1421 | * list this inode will go on. | |
1422 | */ | |
1423 | agino = XFS_INO_TO_AGINO(mp, ip->i_ino); | |
1424 | ASSERT(agino != 0); | |
1425 | bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS; | |
69ef921b | 1426 | ASSERT(agi->agi_unlinked[bucket_index] != cpu_to_be32(NULLAGINO)); |
1da177e4 LT |
1427 | ASSERT(agi->agi_unlinked[bucket_index]); |
1428 | ||
16259e7d | 1429 | if (be32_to_cpu(agi->agi_unlinked[bucket_index]) == agino) { |
1da177e4 LT |
1430 | /* |
1431 | * We're at the head of the list. Get the inode's | |
1432 | * on-disk buffer to see if there is anyone after us | |
1433 | * on the list. Only modify our next pointer if it | |
1434 | * is not already NULLAGINO. This saves us the overhead | |
1435 | * of dealing with the buffer when there is no need to | |
1436 | * change it. | |
1437 | */ | |
a8acad70 | 1438 | error = xfs_itobp(mp, tp, ip, &dip, &ibp, 0); |
1da177e4 | 1439 | if (error) { |
0b932ccc DC |
1440 | xfs_warn(mp, "%s: xfs_itobp() returned error %d.", |
1441 | __func__, error); | |
1da177e4 LT |
1442 | return error; |
1443 | } | |
347d1c01 | 1444 | next_agino = be32_to_cpu(dip->di_next_unlinked); |
1da177e4 LT |
1445 | ASSERT(next_agino != 0); |
1446 | if (next_agino != NULLAGINO) { | |
347d1c01 | 1447 | dip->di_next_unlinked = cpu_to_be32(NULLAGINO); |
92bfc6e7 | 1448 | offset = ip->i_imap.im_boffset + |
1da177e4 LT |
1449 | offsetof(xfs_dinode_t, di_next_unlinked); |
1450 | xfs_trans_inode_buf(tp, ibp); | |
1451 | xfs_trans_log_buf(tp, ibp, offset, | |
1452 | (offset + sizeof(xfs_agino_t) - 1)); | |
1453 | xfs_inobp_check(mp, ibp); | |
1454 | } else { | |
1455 | xfs_trans_brelse(tp, ibp); | |
1456 | } | |
1457 | /* | |
1458 | * Point the bucket head pointer at the next inode. | |
1459 | */ | |
1460 | ASSERT(next_agino != 0); | |
1461 | ASSERT(next_agino != agino); | |
16259e7d | 1462 | agi->agi_unlinked[bucket_index] = cpu_to_be32(next_agino); |
1da177e4 LT |
1463 | offset = offsetof(xfs_agi_t, agi_unlinked) + |
1464 | (sizeof(xfs_agino_t) * bucket_index); | |
1465 | xfs_trans_log_buf(tp, agibp, offset, | |
1466 | (offset + sizeof(xfs_agino_t) - 1)); | |
1467 | } else { | |
1468 | /* | |
1469 | * We need to search the list for the inode being freed. | |
1470 | */ | |
16259e7d | 1471 | next_agino = be32_to_cpu(agi->agi_unlinked[bucket_index]); |
1da177e4 LT |
1472 | last_ibp = NULL; |
1473 | while (next_agino != agino) { | |
1474 | /* | |
1475 | * If the last inode wasn't the one pointing to | |
1476 | * us, then release its buffer since we're not | |
1477 | * going to do anything with it. | |
1478 | */ | |
1479 | if (last_ibp != NULL) { | |
1480 | xfs_trans_brelse(tp, last_ibp); | |
1481 | } | |
1482 | next_ino = XFS_AGINO_TO_INO(mp, agno, next_agino); | |
1483 | error = xfs_inotobp(mp, tp, next_ino, &last_dip, | |
c679eef0 | 1484 | &last_ibp, &last_offset, 0); |
1da177e4 | 1485 | if (error) { |
0b932ccc DC |
1486 | xfs_warn(mp, |
1487 | "%s: xfs_inotobp() returned error %d.", | |
1488 | __func__, error); | |
1da177e4 LT |
1489 | return error; |
1490 | } | |
347d1c01 | 1491 | next_agino = be32_to_cpu(last_dip->di_next_unlinked); |
1da177e4 LT |
1492 | ASSERT(next_agino != NULLAGINO); |
1493 | ASSERT(next_agino != 0); | |
1494 | } | |
1495 | /* | |
1496 | * Now last_ibp points to the buffer previous to us on | |
1497 | * the unlinked list. Pull us from the list. | |
1498 | */ | |
a8acad70 | 1499 | error = xfs_itobp(mp, tp, ip, &dip, &ibp, 0); |
1da177e4 | 1500 | if (error) { |
0b932ccc DC |
1501 | xfs_warn(mp, "%s: xfs_itobp(2) returned error %d.", |
1502 | __func__, error); | |
1da177e4 LT |
1503 | return error; |
1504 | } | |
347d1c01 | 1505 | next_agino = be32_to_cpu(dip->di_next_unlinked); |
1da177e4 LT |
1506 | ASSERT(next_agino != 0); |
1507 | ASSERT(next_agino != agino); | |
1508 | if (next_agino != NULLAGINO) { | |
347d1c01 | 1509 | dip->di_next_unlinked = cpu_to_be32(NULLAGINO); |
92bfc6e7 | 1510 | offset = ip->i_imap.im_boffset + |
1da177e4 LT |
1511 | offsetof(xfs_dinode_t, di_next_unlinked); |
1512 | xfs_trans_inode_buf(tp, ibp); | |
1513 | xfs_trans_log_buf(tp, ibp, offset, | |
1514 | (offset + sizeof(xfs_agino_t) - 1)); | |
1515 | xfs_inobp_check(mp, ibp); | |
1516 | } else { | |
1517 | xfs_trans_brelse(tp, ibp); | |
1518 | } | |
1519 | /* | |
1520 | * Point the previous inode on the list to the next inode. | |
1521 | */ | |
347d1c01 | 1522 | last_dip->di_next_unlinked = cpu_to_be32(next_agino); |
1da177e4 LT |
1523 | ASSERT(next_agino != 0); |
1524 | offset = last_offset + offsetof(xfs_dinode_t, di_next_unlinked); | |
1525 | xfs_trans_inode_buf(tp, last_ibp); | |
1526 | xfs_trans_log_buf(tp, last_ibp, offset, | |
1527 | (offset + sizeof(xfs_agino_t) - 1)); | |
1528 | xfs_inobp_check(mp, last_ibp); | |
1529 | } | |
1530 | return 0; | |
1531 | } | |
1532 | ||
5b3eed75 DC |
1533 | /* |
1534 | * A big issue when freeing the inode cluster is is that we _cannot_ skip any | |
1535 | * inodes that are in memory - they all must be marked stale and attached to | |
1536 | * the cluster buffer. | |
1537 | */ | |
2a30f36d | 1538 | STATIC int |
1da177e4 LT |
1539 | xfs_ifree_cluster( |
1540 | xfs_inode_t *free_ip, | |
1541 | xfs_trans_t *tp, | |
1542 | xfs_ino_t inum) | |
1543 | { | |
1544 | xfs_mount_t *mp = free_ip->i_mount; | |
1545 | int blks_per_cluster; | |
1546 | int nbufs; | |
1547 | int ninodes; | |
5b257b4a | 1548 | int i, j; |
1da177e4 LT |
1549 | xfs_daddr_t blkno; |
1550 | xfs_buf_t *bp; | |
5b257b4a | 1551 | xfs_inode_t *ip; |
1da177e4 LT |
1552 | xfs_inode_log_item_t *iip; |
1553 | xfs_log_item_t *lip; | |
5017e97d | 1554 | struct xfs_perag *pag; |
1da177e4 | 1555 | |
5017e97d | 1556 | pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, inum)); |
1da177e4 LT |
1557 | if (mp->m_sb.sb_blocksize >= XFS_INODE_CLUSTER_SIZE(mp)) { |
1558 | blks_per_cluster = 1; | |
1559 | ninodes = mp->m_sb.sb_inopblock; | |
1560 | nbufs = XFS_IALLOC_BLOCKS(mp); | |
1561 | } else { | |
1562 | blks_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) / | |
1563 | mp->m_sb.sb_blocksize; | |
1564 | ninodes = blks_per_cluster * mp->m_sb.sb_inopblock; | |
1565 | nbufs = XFS_IALLOC_BLOCKS(mp) / blks_per_cluster; | |
1566 | } | |
1567 | ||
1da177e4 LT |
1568 | for (j = 0; j < nbufs; j++, inum += ninodes) { |
1569 | blkno = XFS_AGB_TO_DADDR(mp, XFS_INO_TO_AGNO(mp, inum), | |
1570 | XFS_INO_TO_AGBNO(mp, inum)); | |
1571 | ||
5b257b4a DC |
1572 | /* |
1573 | * We obtain and lock the backing buffer first in the process | |
1574 | * here, as we have to ensure that any dirty inode that we | |
1575 | * can't get the flush lock on is attached to the buffer. | |
1576 | * If we scan the in-memory inodes first, then buffer IO can | |
1577 | * complete before we get a lock on it, and hence we may fail | |
1578 | * to mark all the active inodes on the buffer stale. | |
1579 | */ | |
1580 | bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, blkno, | |
a8acad70 | 1581 | mp->m_bsize * blks_per_cluster, 0); |
5b257b4a | 1582 | |
2a30f36d CS |
1583 | if (!bp) |
1584 | return ENOMEM; | |
5b257b4a DC |
1585 | /* |
1586 | * Walk the inodes already attached to the buffer and mark them | |
1587 | * stale. These will all have the flush locks held, so an | |
5b3eed75 DC |
1588 | * in-memory inode walk can't lock them. By marking them all |
1589 | * stale first, we will not attempt to lock them in the loop | |
1590 | * below as the XFS_ISTALE flag will be set. | |
5b257b4a | 1591 | */ |
adadbeef | 1592 | lip = bp->b_fspriv; |
5b257b4a DC |
1593 | while (lip) { |
1594 | if (lip->li_type == XFS_LI_INODE) { | |
1595 | iip = (xfs_inode_log_item_t *)lip; | |
1596 | ASSERT(iip->ili_logged == 1); | |
ca30b2a7 | 1597 | lip->li_cb = xfs_istale_done; |
5b257b4a DC |
1598 | xfs_trans_ail_copy_lsn(mp->m_ail, |
1599 | &iip->ili_flush_lsn, | |
1600 | &iip->ili_item.li_lsn); | |
1601 | xfs_iflags_set(iip->ili_inode, XFS_ISTALE); | |
5b257b4a DC |
1602 | } |
1603 | lip = lip->li_bio_list; | |
1604 | } | |
1da177e4 | 1605 | |
5b3eed75 | 1606 | |
1da177e4 | 1607 | /* |
5b257b4a DC |
1608 | * For each inode in memory attempt to add it to the inode |
1609 | * buffer and set it up for being staled on buffer IO | |
1610 | * completion. This is safe as we've locked out tail pushing | |
1611 | * and flushing by locking the buffer. | |
1da177e4 | 1612 | * |
5b257b4a DC |
1613 | * We have already marked every inode that was part of a |
1614 | * transaction stale above, which means there is no point in | |
1615 | * even trying to lock them. | |
1da177e4 | 1616 | */ |
1da177e4 | 1617 | for (i = 0; i < ninodes; i++) { |
5b3eed75 | 1618 | retry: |
1a3e8f3d | 1619 | rcu_read_lock(); |
da353b0d DC |
1620 | ip = radix_tree_lookup(&pag->pag_ici_root, |
1621 | XFS_INO_TO_AGINO(mp, (inum + i))); | |
1da177e4 | 1622 | |
1a3e8f3d DC |
1623 | /* Inode not in memory, nothing to do */ |
1624 | if (!ip) { | |
1625 | rcu_read_unlock(); | |
1da177e4 LT |
1626 | continue; |
1627 | } | |
1628 | ||
1a3e8f3d DC |
1629 | /* |
1630 | * because this is an RCU protected lookup, we could | |
1631 | * find a recently freed or even reallocated inode | |
1632 | * during the lookup. We need to check under the | |
1633 | * i_flags_lock for a valid inode here. Skip it if it | |
1634 | * is not valid, the wrong inode or stale. | |
1635 | */ | |
1636 | spin_lock(&ip->i_flags_lock); | |
1637 | if (ip->i_ino != inum + i || | |
1638 | __xfs_iflags_test(ip, XFS_ISTALE)) { | |
1639 | spin_unlock(&ip->i_flags_lock); | |
1640 | rcu_read_unlock(); | |
1641 | continue; | |
1642 | } | |
1643 | spin_unlock(&ip->i_flags_lock); | |
1644 | ||
5b3eed75 DC |
1645 | /* |
1646 | * Don't try to lock/unlock the current inode, but we | |
1647 | * _cannot_ skip the other inodes that we did not find | |
1648 | * in the list attached to the buffer and are not | |
1649 | * already marked stale. If we can't lock it, back off | |
1650 | * and retry. | |
1651 | */ | |
5b257b4a DC |
1652 | if (ip != free_ip && |
1653 | !xfs_ilock_nowait(ip, XFS_ILOCK_EXCL)) { | |
1a3e8f3d | 1654 | rcu_read_unlock(); |
5b3eed75 DC |
1655 | delay(1); |
1656 | goto retry; | |
1da177e4 | 1657 | } |
1a3e8f3d | 1658 | rcu_read_unlock(); |
1da177e4 | 1659 | |
5b3eed75 | 1660 | xfs_iflock(ip); |
5b257b4a | 1661 | xfs_iflags_set(ip, XFS_ISTALE); |
1da177e4 | 1662 | |
5b3eed75 DC |
1663 | /* |
1664 | * we don't need to attach clean inodes or those only | |
1665 | * with unlogged changes (which we throw away, anyway). | |
1666 | */ | |
1da177e4 | 1667 | iip = ip->i_itemp; |
5b3eed75 | 1668 | if (!iip || xfs_inode_clean(ip)) { |
5b257b4a | 1669 | ASSERT(ip != free_ip); |
1da177e4 LT |
1670 | xfs_ifunlock(ip); |
1671 | xfs_iunlock(ip, XFS_ILOCK_EXCL); | |
1672 | continue; | |
1673 | } | |
1674 | ||
f5d8d5c4 CH |
1675 | iip->ili_last_fields = iip->ili_fields; |
1676 | iip->ili_fields = 0; | |
1da177e4 | 1677 | iip->ili_logged = 1; |
7b2e2a31 DC |
1678 | xfs_trans_ail_copy_lsn(mp->m_ail, &iip->ili_flush_lsn, |
1679 | &iip->ili_item.li_lsn); | |
1da177e4 | 1680 | |
ca30b2a7 CH |
1681 | xfs_buf_attach_iodone(bp, xfs_istale_done, |
1682 | &iip->ili_item); | |
5b257b4a DC |
1683 | |
1684 | if (ip != free_ip) | |
1da177e4 | 1685 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
1da177e4 LT |
1686 | } |
1687 | ||
5b3eed75 | 1688 | xfs_trans_stale_inode_buf(tp, bp); |
1da177e4 LT |
1689 | xfs_trans_binval(tp, bp); |
1690 | } | |
1691 | ||
5017e97d | 1692 | xfs_perag_put(pag); |
2a30f36d | 1693 | return 0; |
1da177e4 LT |
1694 | } |
1695 | ||
1696 | /* | |
1697 | * This is called to return an inode to the inode free list. | |
1698 | * The inode should already be truncated to 0 length and have | |
1699 | * no pages associated with it. This routine also assumes that | |
1700 | * the inode is already a part of the transaction. | |
1701 | * | |
1702 | * The on-disk copy of the inode will have been added to the list | |
1703 | * of unlinked inodes in the AGI. We need to remove the inode from | |
1704 | * that list atomically with respect to freeing it here. | |
1705 | */ | |
1706 | int | |
1707 | xfs_ifree( | |
1708 | xfs_trans_t *tp, | |
1709 | xfs_inode_t *ip, | |
1710 | xfs_bmap_free_t *flist) | |
1711 | { | |
1712 | int error; | |
1713 | int delete; | |
1714 | xfs_ino_t first_ino; | |
c319b58b VA |
1715 | xfs_dinode_t *dip; |
1716 | xfs_buf_t *ibp; | |
1da177e4 | 1717 | |
579aa9ca | 1718 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
1da177e4 LT |
1719 | ASSERT(ip->i_d.di_nlink == 0); |
1720 | ASSERT(ip->i_d.di_nextents == 0); | |
1721 | ASSERT(ip->i_d.di_anextents == 0); | |
ce7ae151 | 1722 | ASSERT(ip->i_d.di_size == 0 || !S_ISREG(ip->i_d.di_mode)); |
1da177e4 LT |
1723 | ASSERT(ip->i_d.di_nblocks == 0); |
1724 | ||
1725 | /* | |
1726 | * Pull the on-disk inode from the AGI unlinked list. | |
1727 | */ | |
1728 | error = xfs_iunlink_remove(tp, ip); | |
1729 | if (error != 0) { | |
1730 | return error; | |
1731 | } | |
1732 | ||
1733 | error = xfs_difree(tp, ip->i_ino, flist, &delete, &first_ino); | |
1734 | if (error != 0) { | |
1735 | return error; | |
1736 | } | |
1737 | ip->i_d.di_mode = 0; /* mark incore inode as free */ | |
1738 | ip->i_d.di_flags = 0; | |
1739 | ip->i_d.di_dmevmask = 0; | |
1740 | ip->i_d.di_forkoff = 0; /* mark the attr fork not in use */ | |
1da177e4 LT |
1741 | ip->i_d.di_format = XFS_DINODE_FMT_EXTENTS; |
1742 | ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS; | |
1743 | /* | |
1744 | * Bump the generation count so no one will be confused | |
1745 | * by reincarnations of this inode. | |
1746 | */ | |
1747 | ip->i_d.di_gen++; | |
c319b58b | 1748 | |
1da177e4 LT |
1749 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
1750 | ||
a8acad70 | 1751 | error = xfs_itobp(ip->i_mount, tp, ip, &dip, &ibp, 0); |
c319b58b VA |
1752 | if (error) |
1753 | return error; | |
1754 | ||
1755 | /* | |
1756 | * Clear the on-disk di_mode. This is to prevent xfs_bulkstat | |
1757 | * from picking up this inode when it is reclaimed (its incore state | |
1758 | * initialzed but not flushed to disk yet). The in-core di_mode is | |
1759 | * already cleared and a corresponding transaction logged. | |
1760 | * The hack here just synchronizes the in-core to on-disk | |
1761 | * di_mode value in advance before the actual inode sync to disk. | |
1762 | * This is OK because the inode is already unlinked and would never | |
1763 | * change its di_mode again for this inode generation. | |
1764 | * This is a temporary hack that would require a proper fix | |
1765 | * in the future. | |
1766 | */ | |
81591fe2 | 1767 | dip->di_mode = 0; |
c319b58b | 1768 | |
1da177e4 | 1769 | if (delete) { |
2a30f36d | 1770 | error = xfs_ifree_cluster(ip, tp, first_ino); |
1da177e4 LT |
1771 | } |
1772 | ||
2a30f36d | 1773 | return error; |
1da177e4 LT |
1774 | } |
1775 | ||
1776 | /* | |
1777 | * Reallocate the space for if_broot based on the number of records | |
1778 | * being added or deleted as indicated in rec_diff. Move the records | |
1779 | * and pointers in if_broot to fit the new size. When shrinking this | |
1780 | * will eliminate holes between the records and pointers created by | |
1781 | * the caller. When growing this will create holes to be filled in | |
1782 | * by the caller. | |
1783 | * | |
1784 | * The caller must not request to add more records than would fit in | |
1785 | * the on-disk inode root. If the if_broot is currently NULL, then | |
1786 | * if we adding records one will be allocated. The caller must also | |
1787 | * not request that the number of records go below zero, although | |
1788 | * it can go to zero. | |
1789 | * | |
1790 | * ip -- the inode whose if_broot area is changing | |
1791 | * ext_diff -- the change in the number of records, positive or negative, | |
1792 | * requested for the if_broot array. | |
1793 | */ | |
1794 | void | |
1795 | xfs_iroot_realloc( | |
1796 | xfs_inode_t *ip, | |
1797 | int rec_diff, | |
1798 | int whichfork) | |
1799 | { | |
60197e8d | 1800 | struct xfs_mount *mp = ip->i_mount; |
1da177e4 LT |
1801 | int cur_max; |
1802 | xfs_ifork_t *ifp; | |
7cc95a82 | 1803 | struct xfs_btree_block *new_broot; |
1da177e4 LT |
1804 | int new_max; |
1805 | size_t new_size; | |
1806 | char *np; | |
1807 | char *op; | |
1808 | ||
1809 | /* | |
1810 | * Handle the degenerate case quietly. | |
1811 | */ | |
1812 | if (rec_diff == 0) { | |
1813 | return; | |
1814 | } | |
1815 | ||
1816 | ifp = XFS_IFORK_PTR(ip, whichfork); | |
1817 | if (rec_diff > 0) { | |
1818 | /* | |
1819 | * If there wasn't any memory allocated before, just | |
1820 | * allocate it now and get out. | |
1821 | */ | |
1822 | if (ifp->if_broot_bytes == 0) { | |
1823 | new_size = (size_t)XFS_BMAP_BROOT_SPACE_CALC(rec_diff); | |
4a7edddc | 1824 | ifp->if_broot = kmem_alloc(new_size, KM_SLEEP | KM_NOFS); |
1da177e4 LT |
1825 | ifp->if_broot_bytes = (int)new_size; |
1826 | return; | |
1827 | } | |
1828 | ||
1829 | /* | |
1830 | * If there is already an existing if_broot, then we need | |
1831 | * to realloc() it and shift the pointers to their new | |
1832 | * location. The records don't change location because | |
1833 | * they are kept butted up against the btree block header. | |
1834 | */ | |
60197e8d | 1835 | cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0); |
1da177e4 LT |
1836 | new_max = cur_max + rec_diff; |
1837 | new_size = (size_t)XFS_BMAP_BROOT_SPACE_CALC(new_max); | |
7cc95a82 | 1838 | ifp->if_broot = kmem_realloc(ifp->if_broot, new_size, |
1da177e4 | 1839 | (size_t)XFS_BMAP_BROOT_SPACE_CALC(cur_max), /* old size */ |
4a7edddc | 1840 | KM_SLEEP | KM_NOFS); |
60197e8d CH |
1841 | op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1, |
1842 | ifp->if_broot_bytes); | |
1843 | np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1, | |
1844 | (int)new_size); | |
1da177e4 LT |
1845 | ifp->if_broot_bytes = (int)new_size; |
1846 | ASSERT(ifp->if_broot_bytes <= | |
1847 | XFS_IFORK_SIZE(ip, whichfork) + XFS_BROOT_SIZE_ADJ); | |
1848 | memmove(np, op, cur_max * (uint)sizeof(xfs_dfsbno_t)); | |
1849 | return; | |
1850 | } | |
1851 | ||
1852 | /* | |
1853 | * rec_diff is less than 0. In this case, we are shrinking the | |
1854 | * if_broot buffer. It must already exist. If we go to zero | |
1855 | * records, just get rid of the root and clear the status bit. | |
1856 | */ | |
1857 | ASSERT((ifp->if_broot != NULL) && (ifp->if_broot_bytes > 0)); | |
60197e8d | 1858 | cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0); |
1da177e4 LT |
1859 | new_max = cur_max + rec_diff; |
1860 | ASSERT(new_max >= 0); | |
1861 | if (new_max > 0) | |
1862 | new_size = (size_t)XFS_BMAP_BROOT_SPACE_CALC(new_max); | |
1863 | else | |
1864 | new_size = 0; | |
1865 | if (new_size > 0) { | |
4a7edddc | 1866 | new_broot = kmem_alloc(new_size, KM_SLEEP | KM_NOFS); |
1da177e4 LT |
1867 | /* |
1868 | * First copy over the btree block header. | |
1869 | */ | |
7cc95a82 | 1870 | memcpy(new_broot, ifp->if_broot, XFS_BTREE_LBLOCK_LEN); |
1da177e4 LT |
1871 | } else { |
1872 | new_broot = NULL; | |
1873 | ifp->if_flags &= ~XFS_IFBROOT; | |
1874 | } | |
1875 | ||
1876 | /* | |
1877 | * Only copy the records and pointers if there are any. | |
1878 | */ | |
1879 | if (new_max > 0) { | |
1880 | /* | |
1881 | * First copy the records. | |
1882 | */ | |
136341b4 CH |
1883 | op = (char *)XFS_BMBT_REC_ADDR(mp, ifp->if_broot, 1); |
1884 | np = (char *)XFS_BMBT_REC_ADDR(mp, new_broot, 1); | |
1da177e4 LT |
1885 | memcpy(np, op, new_max * (uint)sizeof(xfs_bmbt_rec_t)); |
1886 | ||
1887 | /* | |
1888 | * Then copy the pointers. | |
1889 | */ | |
60197e8d | 1890 | op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1, |
1da177e4 | 1891 | ifp->if_broot_bytes); |
60197e8d | 1892 | np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, new_broot, 1, |
1da177e4 LT |
1893 | (int)new_size); |
1894 | memcpy(np, op, new_max * (uint)sizeof(xfs_dfsbno_t)); | |
1895 | } | |
f0e2d93c | 1896 | kmem_free(ifp->if_broot); |
1da177e4 LT |
1897 | ifp->if_broot = new_broot; |
1898 | ifp->if_broot_bytes = (int)new_size; | |
1899 | ASSERT(ifp->if_broot_bytes <= | |
1900 | XFS_IFORK_SIZE(ip, whichfork) + XFS_BROOT_SIZE_ADJ); | |
1901 | return; | |
1902 | } | |
1903 | ||
1904 | ||
1da177e4 LT |
1905 | /* |
1906 | * This is called when the amount of space needed for if_data | |
1907 | * is increased or decreased. The change in size is indicated by | |
1908 | * the number of bytes that need to be added or deleted in the | |
1909 | * byte_diff parameter. | |
1910 | * | |
1911 | * If the amount of space needed has decreased below the size of the | |
1912 | * inline buffer, then switch to using the inline buffer. Otherwise, | |
1913 | * use kmem_realloc() or kmem_alloc() to adjust the size of the buffer | |
1914 | * to what is needed. | |
1915 | * | |
1916 | * ip -- the inode whose if_data area is changing | |
1917 | * byte_diff -- the change in the number of bytes, positive or negative, | |
1918 | * requested for the if_data array. | |
1919 | */ | |
1920 | void | |
1921 | xfs_idata_realloc( | |
1922 | xfs_inode_t *ip, | |
1923 | int byte_diff, | |
1924 | int whichfork) | |
1925 | { | |
1926 | xfs_ifork_t *ifp; | |
1927 | int new_size; | |
1928 | int real_size; | |
1929 | ||
1930 | if (byte_diff == 0) { | |
1931 | return; | |
1932 | } | |
1933 | ||
1934 | ifp = XFS_IFORK_PTR(ip, whichfork); | |
1935 | new_size = (int)ifp->if_bytes + byte_diff; | |
1936 | ASSERT(new_size >= 0); | |
1937 | ||
1938 | if (new_size == 0) { | |
1939 | if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) { | |
f0e2d93c | 1940 | kmem_free(ifp->if_u1.if_data); |
1da177e4 LT |
1941 | } |
1942 | ifp->if_u1.if_data = NULL; | |
1943 | real_size = 0; | |
1944 | } else if (new_size <= sizeof(ifp->if_u2.if_inline_data)) { | |
1945 | /* | |
1946 | * If the valid extents/data can fit in if_inline_ext/data, | |
1947 | * copy them from the malloc'd vector and free it. | |
1948 | */ | |
1949 | if (ifp->if_u1.if_data == NULL) { | |
1950 | ifp->if_u1.if_data = ifp->if_u2.if_inline_data; | |
1951 | } else if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) { | |
1952 | ASSERT(ifp->if_real_bytes != 0); | |
1953 | memcpy(ifp->if_u2.if_inline_data, ifp->if_u1.if_data, | |
1954 | new_size); | |
f0e2d93c | 1955 | kmem_free(ifp->if_u1.if_data); |
1da177e4 LT |
1956 | ifp->if_u1.if_data = ifp->if_u2.if_inline_data; |
1957 | } | |
1958 | real_size = 0; | |
1959 | } else { | |
1960 | /* | |
1961 | * Stuck with malloc/realloc. | |
1962 | * For inline data, the underlying buffer must be | |
1963 | * a multiple of 4 bytes in size so that it can be | |
1964 | * logged and stay on word boundaries. We enforce | |
1965 | * that here. | |
1966 | */ | |
1967 | real_size = roundup(new_size, 4); | |
1968 | if (ifp->if_u1.if_data == NULL) { | |
1969 | ASSERT(ifp->if_real_bytes == 0); | |
4a7edddc DC |
1970 | ifp->if_u1.if_data = kmem_alloc(real_size, |
1971 | KM_SLEEP | KM_NOFS); | |
1da177e4 LT |
1972 | } else if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) { |
1973 | /* | |
1974 | * Only do the realloc if the underlying size | |
1975 | * is really changing. | |
1976 | */ | |
1977 | if (ifp->if_real_bytes != real_size) { | |
1978 | ifp->if_u1.if_data = | |
1979 | kmem_realloc(ifp->if_u1.if_data, | |
1980 | real_size, | |
1981 | ifp->if_real_bytes, | |
4a7edddc | 1982 | KM_SLEEP | KM_NOFS); |
1da177e4 LT |
1983 | } |
1984 | } else { | |
1985 | ASSERT(ifp->if_real_bytes == 0); | |
4a7edddc DC |
1986 | ifp->if_u1.if_data = kmem_alloc(real_size, |
1987 | KM_SLEEP | KM_NOFS); | |
1da177e4 LT |
1988 | memcpy(ifp->if_u1.if_data, ifp->if_u2.if_inline_data, |
1989 | ifp->if_bytes); | |
1990 | } | |
1991 | } | |
1992 | ifp->if_real_bytes = real_size; | |
1993 | ifp->if_bytes = new_size; | |
1994 | ASSERT(ifp->if_bytes <= XFS_IFORK_SIZE(ip, whichfork)); | |
1995 | } | |
1996 | ||
1da177e4 LT |
1997 | void |
1998 | xfs_idestroy_fork( | |
1999 | xfs_inode_t *ip, | |
2000 | int whichfork) | |
2001 | { | |
2002 | xfs_ifork_t *ifp; | |
2003 | ||
2004 | ifp = XFS_IFORK_PTR(ip, whichfork); | |
2005 | if (ifp->if_broot != NULL) { | |
f0e2d93c | 2006 | kmem_free(ifp->if_broot); |
1da177e4 LT |
2007 | ifp->if_broot = NULL; |
2008 | } | |
2009 | ||
2010 | /* | |
2011 | * If the format is local, then we can't have an extents | |
2012 | * array so just look for an inline data array. If we're | |
2013 | * not local then we may or may not have an extents list, | |
2014 | * so check and free it up if we do. | |
2015 | */ | |
2016 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) { | |
2017 | if ((ifp->if_u1.if_data != ifp->if_u2.if_inline_data) && | |
2018 | (ifp->if_u1.if_data != NULL)) { | |
2019 | ASSERT(ifp->if_real_bytes != 0); | |
f0e2d93c | 2020 | kmem_free(ifp->if_u1.if_data); |
1da177e4 LT |
2021 | ifp->if_u1.if_data = NULL; |
2022 | ifp->if_real_bytes = 0; | |
2023 | } | |
2024 | } else if ((ifp->if_flags & XFS_IFEXTENTS) && | |
0293ce3a MK |
2025 | ((ifp->if_flags & XFS_IFEXTIREC) || |
2026 | ((ifp->if_u1.if_extents != NULL) && | |
2027 | (ifp->if_u1.if_extents != ifp->if_u2.if_inline_ext)))) { | |
1da177e4 | 2028 | ASSERT(ifp->if_real_bytes != 0); |
4eea22f0 | 2029 | xfs_iext_destroy(ifp); |
1da177e4 LT |
2030 | } |
2031 | ASSERT(ifp->if_u1.if_extents == NULL || | |
2032 | ifp->if_u1.if_extents == ifp->if_u2.if_inline_ext); | |
2033 | ASSERT(ifp->if_real_bytes == 0); | |
2034 | if (whichfork == XFS_ATTR_FORK) { | |
2035 | kmem_zone_free(xfs_ifork_zone, ip->i_afp); | |
2036 | ip->i_afp = NULL; | |
2037 | } | |
2038 | } | |
2039 | ||
1da177e4 | 2040 | /* |
60ec6783 CH |
2041 | * This is called to unpin an inode. The caller must have the inode locked |
2042 | * in at least shared mode so that the buffer cannot be subsequently pinned | |
2043 | * once someone is waiting for it to be unpinned. | |
1da177e4 | 2044 | */ |
60ec6783 | 2045 | static void |
f392e631 | 2046 | xfs_iunpin( |
60ec6783 | 2047 | struct xfs_inode *ip) |
1da177e4 | 2048 | { |
579aa9ca | 2049 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED)); |
1da177e4 | 2050 | |
4aaf15d1 DC |
2051 | trace_xfs_inode_unpin_nowait(ip, _RET_IP_); |
2052 | ||
a3f74ffb | 2053 | /* Give the log a push to start the unpinning I/O */ |
60ec6783 | 2054 | xfs_log_force_lsn(ip->i_mount, ip->i_itemp->ili_last_lsn, 0); |
a14a348b | 2055 | |
a3f74ffb | 2056 | } |
1da177e4 | 2057 | |
f392e631 CH |
2058 | static void |
2059 | __xfs_iunpin_wait( | |
2060 | struct xfs_inode *ip) | |
2061 | { | |
2062 | wait_queue_head_t *wq = bit_waitqueue(&ip->i_flags, __XFS_IPINNED_BIT); | |
2063 | DEFINE_WAIT_BIT(wait, &ip->i_flags, __XFS_IPINNED_BIT); | |
2064 | ||
2065 | xfs_iunpin(ip); | |
2066 | ||
2067 | do { | |
2068 | prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE); | |
2069 | if (xfs_ipincount(ip)) | |
2070 | io_schedule(); | |
2071 | } while (xfs_ipincount(ip)); | |
2072 | finish_wait(wq, &wait.wait); | |
2073 | } | |
2074 | ||
777df5af | 2075 | void |
a3f74ffb | 2076 | xfs_iunpin_wait( |
60ec6783 | 2077 | struct xfs_inode *ip) |
a3f74ffb | 2078 | { |
f392e631 CH |
2079 | if (xfs_ipincount(ip)) |
2080 | __xfs_iunpin_wait(ip); | |
1da177e4 LT |
2081 | } |
2082 | ||
1da177e4 LT |
2083 | /* |
2084 | * xfs_iextents_copy() | |
2085 | * | |
2086 | * This is called to copy the REAL extents (as opposed to the delayed | |
2087 | * allocation extents) from the inode into the given buffer. It | |
2088 | * returns the number of bytes copied into the buffer. | |
2089 | * | |
2090 | * If there are no delayed allocation extents, then we can just | |
2091 | * memcpy() the extents into the buffer. Otherwise, we need to | |
2092 | * examine each extent in turn and skip those which are delayed. | |
2093 | */ | |
2094 | int | |
2095 | xfs_iextents_copy( | |
2096 | xfs_inode_t *ip, | |
a6f64d4a | 2097 | xfs_bmbt_rec_t *dp, |
1da177e4 LT |
2098 | int whichfork) |
2099 | { | |
2100 | int copied; | |
1da177e4 LT |
2101 | int i; |
2102 | xfs_ifork_t *ifp; | |
2103 | int nrecs; | |
2104 | xfs_fsblock_t start_block; | |
2105 | ||
2106 | ifp = XFS_IFORK_PTR(ip, whichfork); | |
579aa9ca | 2107 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED)); |
1da177e4 LT |
2108 | ASSERT(ifp->if_bytes > 0); |
2109 | ||
2110 | nrecs = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); | |
3a59c94c | 2111 | XFS_BMAP_TRACE_EXLIST(ip, nrecs, whichfork); |
1da177e4 LT |
2112 | ASSERT(nrecs > 0); |
2113 | ||
2114 | /* | |
2115 | * There are some delayed allocation extents in the | |
2116 | * inode, so copy the extents one at a time and skip | |
2117 | * the delayed ones. There must be at least one | |
2118 | * non-delayed extent. | |
2119 | */ | |
1da177e4 LT |
2120 | copied = 0; |
2121 | for (i = 0; i < nrecs; i++) { | |
a6f64d4a | 2122 | xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, i); |
1da177e4 | 2123 | start_block = xfs_bmbt_get_startblock(ep); |
9d87c319 | 2124 | if (isnullstartblock(start_block)) { |
1da177e4 LT |
2125 | /* |
2126 | * It's a delayed allocation extent, so skip it. | |
2127 | */ | |
1da177e4 LT |
2128 | continue; |
2129 | } | |
2130 | ||
2131 | /* Translate to on disk format */ | |
cd8b0a97 CH |
2132 | put_unaligned(cpu_to_be64(ep->l0), &dp->l0); |
2133 | put_unaligned(cpu_to_be64(ep->l1), &dp->l1); | |
a6f64d4a | 2134 | dp++; |
1da177e4 LT |
2135 | copied++; |
2136 | } | |
2137 | ASSERT(copied != 0); | |
a6f64d4a | 2138 | xfs_validate_extents(ifp, copied, XFS_EXTFMT_INODE(ip)); |
1da177e4 LT |
2139 | |
2140 | return (copied * (uint)sizeof(xfs_bmbt_rec_t)); | |
2141 | } | |
2142 | ||
2143 | /* | |
2144 | * Each of the following cases stores data into the same region | |
2145 | * of the on-disk inode, so only one of them can be valid at | |
2146 | * any given time. While it is possible to have conflicting formats | |
2147 | * and log flags, e.g. having XFS_ILOG_?DATA set when the fork is | |
2148 | * in EXTENTS format, this can only happen when the fork has | |
2149 | * changed formats after being modified but before being flushed. | |
2150 | * In these cases, the format always takes precedence, because the | |
2151 | * format indicates the current state of the fork. | |
2152 | */ | |
2153 | /*ARGSUSED*/ | |
e4ac967b | 2154 | STATIC void |
1da177e4 LT |
2155 | xfs_iflush_fork( |
2156 | xfs_inode_t *ip, | |
2157 | xfs_dinode_t *dip, | |
2158 | xfs_inode_log_item_t *iip, | |
2159 | int whichfork, | |
2160 | xfs_buf_t *bp) | |
2161 | { | |
2162 | char *cp; | |
2163 | xfs_ifork_t *ifp; | |
2164 | xfs_mount_t *mp; | |
2165 | #ifdef XFS_TRANS_DEBUG | |
2166 | int first; | |
2167 | #endif | |
2168 | static const short brootflag[2] = | |
2169 | { XFS_ILOG_DBROOT, XFS_ILOG_ABROOT }; | |
2170 | static const short dataflag[2] = | |
2171 | { XFS_ILOG_DDATA, XFS_ILOG_ADATA }; | |
2172 | static const short extflag[2] = | |
2173 | { XFS_ILOG_DEXT, XFS_ILOG_AEXT }; | |
2174 | ||
e4ac967b DC |
2175 | if (!iip) |
2176 | return; | |
1da177e4 LT |
2177 | ifp = XFS_IFORK_PTR(ip, whichfork); |
2178 | /* | |
2179 | * This can happen if we gave up in iformat in an error path, | |
2180 | * for the attribute fork. | |
2181 | */ | |
e4ac967b | 2182 | if (!ifp) { |
1da177e4 | 2183 | ASSERT(whichfork == XFS_ATTR_FORK); |
e4ac967b | 2184 | return; |
1da177e4 LT |
2185 | } |
2186 | cp = XFS_DFORK_PTR(dip, whichfork); | |
2187 | mp = ip->i_mount; | |
2188 | switch (XFS_IFORK_FORMAT(ip, whichfork)) { | |
2189 | case XFS_DINODE_FMT_LOCAL: | |
f5d8d5c4 | 2190 | if ((iip->ili_fields & dataflag[whichfork]) && |
1da177e4 LT |
2191 | (ifp->if_bytes > 0)) { |
2192 | ASSERT(ifp->if_u1.if_data != NULL); | |
2193 | ASSERT(ifp->if_bytes <= XFS_IFORK_SIZE(ip, whichfork)); | |
2194 | memcpy(cp, ifp->if_u1.if_data, ifp->if_bytes); | |
2195 | } | |
1da177e4 LT |
2196 | break; |
2197 | ||
2198 | case XFS_DINODE_FMT_EXTENTS: | |
2199 | ASSERT((ifp->if_flags & XFS_IFEXTENTS) || | |
f5d8d5c4 CH |
2200 | !(iip->ili_fields & extflag[whichfork])); |
2201 | if ((iip->ili_fields & extflag[whichfork]) && | |
1da177e4 | 2202 | (ifp->if_bytes > 0)) { |
ab1908a5 | 2203 | ASSERT(xfs_iext_get_ext(ifp, 0)); |
1da177e4 LT |
2204 | ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) > 0); |
2205 | (void)xfs_iextents_copy(ip, (xfs_bmbt_rec_t *)cp, | |
2206 | whichfork); | |
2207 | } | |
2208 | break; | |
2209 | ||
2210 | case XFS_DINODE_FMT_BTREE: | |
f5d8d5c4 | 2211 | if ((iip->ili_fields & brootflag[whichfork]) && |
1da177e4 LT |
2212 | (ifp->if_broot_bytes > 0)) { |
2213 | ASSERT(ifp->if_broot != NULL); | |
2214 | ASSERT(ifp->if_broot_bytes <= | |
2215 | (XFS_IFORK_SIZE(ip, whichfork) + | |
2216 | XFS_BROOT_SIZE_ADJ)); | |
60197e8d | 2217 | xfs_bmbt_to_bmdr(mp, ifp->if_broot, ifp->if_broot_bytes, |
1da177e4 LT |
2218 | (xfs_bmdr_block_t *)cp, |
2219 | XFS_DFORK_SIZE(dip, mp, whichfork)); | |
2220 | } | |
2221 | break; | |
2222 | ||
2223 | case XFS_DINODE_FMT_DEV: | |
f5d8d5c4 | 2224 | if (iip->ili_fields & XFS_ILOG_DEV) { |
1da177e4 | 2225 | ASSERT(whichfork == XFS_DATA_FORK); |
81591fe2 | 2226 | xfs_dinode_put_rdev(dip, ip->i_df.if_u2.if_rdev); |
1da177e4 LT |
2227 | } |
2228 | break; | |
2229 | ||
2230 | case XFS_DINODE_FMT_UUID: | |
f5d8d5c4 | 2231 | if (iip->ili_fields & XFS_ILOG_UUID) { |
1da177e4 | 2232 | ASSERT(whichfork == XFS_DATA_FORK); |
81591fe2 CH |
2233 | memcpy(XFS_DFORK_DPTR(dip), |
2234 | &ip->i_df.if_u2.if_uuid, | |
2235 | sizeof(uuid_t)); | |
1da177e4 LT |
2236 | } |
2237 | break; | |
2238 | ||
2239 | default: | |
2240 | ASSERT(0); | |
2241 | break; | |
2242 | } | |
1da177e4 LT |
2243 | } |
2244 | ||
bad55843 DC |
2245 | STATIC int |
2246 | xfs_iflush_cluster( | |
2247 | xfs_inode_t *ip, | |
2248 | xfs_buf_t *bp) | |
2249 | { | |
2250 | xfs_mount_t *mp = ip->i_mount; | |
5017e97d | 2251 | struct xfs_perag *pag; |
bad55843 | 2252 | unsigned long first_index, mask; |
c8f5f12e | 2253 | unsigned long inodes_per_cluster; |
bad55843 DC |
2254 | int ilist_size; |
2255 | xfs_inode_t **ilist; | |
2256 | xfs_inode_t *iq; | |
bad55843 DC |
2257 | int nr_found; |
2258 | int clcount = 0; | |
2259 | int bufwasdelwri; | |
2260 | int i; | |
2261 | ||
5017e97d | 2262 | pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino)); |
bad55843 | 2263 | |
c8f5f12e DC |
2264 | inodes_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_inodelog; |
2265 | ilist_size = inodes_per_cluster * sizeof(xfs_inode_t *); | |
49383b0e | 2266 | ilist = kmem_alloc(ilist_size, KM_MAYFAIL|KM_NOFS); |
bad55843 | 2267 | if (!ilist) |
44b56e0a | 2268 | goto out_put; |
bad55843 DC |
2269 | |
2270 | mask = ~(((XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_inodelog)) - 1); | |
2271 | first_index = XFS_INO_TO_AGINO(mp, ip->i_ino) & mask; | |
1a3e8f3d | 2272 | rcu_read_lock(); |
bad55843 DC |
2273 | /* really need a gang lookup range call here */ |
2274 | nr_found = radix_tree_gang_lookup(&pag->pag_ici_root, (void**)ilist, | |
c8f5f12e | 2275 | first_index, inodes_per_cluster); |
bad55843 DC |
2276 | if (nr_found == 0) |
2277 | goto out_free; | |
2278 | ||
2279 | for (i = 0; i < nr_found; i++) { | |
2280 | iq = ilist[i]; | |
2281 | if (iq == ip) | |
2282 | continue; | |
1a3e8f3d DC |
2283 | |
2284 | /* | |
2285 | * because this is an RCU protected lookup, we could find a | |
2286 | * recently freed or even reallocated inode during the lookup. | |
2287 | * We need to check under the i_flags_lock for a valid inode | |
2288 | * here. Skip it if it is not valid or the wrong inode. | |
2289 | */ | |
2290 | spin_lock(&ip->i_flags_lock); | |
2291 | if (!ip->i_ino || | |
2292 | (XFS_INO_TO_AGINO(mp, iq->i_ino) & mask) != first_index) { | |
2293 | spin_unlock(&ip->i_flags_lock); | |
2294 | continue; | |
2295 | } | |
2296 | spin_unlock(&ip->i_flags_lock); | |
2297 | ||
bad55843 DC |
2298 | /* |
2299 | * Do an un-protected check to see if the inode is dirty and | |
2300 | * is a candidate for flushing. These checks will be repeated | |
2301 | * later after the appropriate locks are acquired. | |
2302 | */ | |
33540408 | 2303 | if (xfs_inode_clean(iq) && xfs_ipincount(iq) == 0) |
bad55843 | 2304 | continue; |
bad55843 DC |
2305 | |
2306 | /* | |
2307 | * Try to get locks. If any are unavailable or it is pinned, | |
2308 | * then this inode cannot be flushed and is skipped. | |
2309 | */ | |
2310 | ||
2311 | if (!xfs_ilock_nowait(iq, XFS_ILOCK_SHARED)) | |
2312 | continue; | |
2313 | if (!xfs_iflock_nowait(iq)) { | |
2314 | xfs_iunlock(iq, XFS_ILOCK_SHARED); | |
2315 | continue; | |
2316 | } | |
2317 | if (xfs_ipincount(iq)) { | |
2318 | xfs_ifunlock(iq); | |
2319 | xfs_iunlock(iq, XFS_ILOCK_SHARED); | |
2320 | continue; | |
2321 | } | |
2322 | ||
2323 | /* | |
2324 | * arriving here means that this inode can be flushed. First | |
2325 | * re-check that it's dirty before flushing. | |
2326 | */ | |
33540408 DC |
2327 | if (!xfs_inode_clean(iq)) { |
2328 | int error; | |
bad55843 DC |
2329 | error = xfs_iflush_int(iq, bp); |
2330 | if (error) { | |
2331 | xfs_iunlock(iq, XFS_ILOCK_SHARED); | |
2332 | goto cluster_corrupt_out; | |
2333 | } | |
2334 | clcount++; | |
2335 | } else { | |
2336 | xfs_ifunlock(iq); | |
2337 | } | |
2338 | xfs_iunlock(iq, XFS_ILOCK_SHARED); | |
2339 | } | |
2340 | ||
2341 | if (clcount) { | |
2342 | XFS_STATS_INC(xs_icluster_flushcnt); | |
2343 | XFS_STATS_ADD(xs_icluster_flushinode, clcount); | |
2344 | } | |
2345 | ||
2346 | out_free: | |
1a3e8f3d | 2347 | rcu_read_unlock(); |
f0e2d93c | 2348 | kmem_free(ilist); |
44b56e0a DC |
2349 | out_put: |
2350 | xfs_perag_put(pag); | |
bad55843 DC |
2351 | return 0; |
2352 | ||
2353 | ||
2354 | cluster_corrupt_out: | |
2355 | /* | |
2356 | * Corruption detected in the clustering loop. Invalidate the | |
2357 | * inode buffer and shut down the filesystem. | |
2358 | */ | |
1a3e8f3d | 2359 | rcu_read_unlock(); |
bad55843 | 2360 | /* |
43ff2122 | 2361 | * Clean up the buffer. If it was delwri, just release it -- |
bad55843 DC |
2362 | * brelse can handle it with no problems. If not, shut down the |
2363 | * filesystem before releasing the buffer. | |
2364 | */ | |
43ff2122 | 2365 | bufwasdelwri = (bp->b_flags & _XBF_DELWRI_Q); |
bad55843 DC |
2366 | if (bufwasdelwri) |
2367 | xfs_buf_relse(bp); | |
2368 | ||
2369 | xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE); | |
2370 | ||
2371 | if (!bufwasdelwri) { | |
2372 | /* | |
2373 | * Just like incore_relse: if we have b_iodone functions, | |
2374 | * mark the buffer as an error and call them. Otherwise | |
2375 | * mark it as stale and brelse. | |
2376 | */ | |
cb669ca5 | 2377 | if (bp->b_iodone) { |
bad55843 | 2378 | XFS_BUF_UNDONE(bp); |
c867cb61 | 2379 | xfs_buf_stale(bp); |
5a52c2a5 | 2380 | xfs_buf_ioerror(bp, EIO); |
1a1a3e97 | 2381 | xfs_buf_ioend(bp, 0); |
bad55843 | 2382 | } else { |
c867cb61 | 2383 | xfs_buf_stale(bp); |
bad55843 DC |
2384 | xfs_buf_relse(bp); |
2385 | } | |
2386 | } | |
2387 | ||
2388 | /* | |
2389 | * Unlocks the flush lock | |
2390 | */ | |
04913fdd | 2391 | xfs_iflush_abort(iq, false); |
f0e2d93c | 2392 | kmem_free(ilist); |
44b56e0a | 2393 | xfs_perag_put(pag); |
bad55843 DC |
2394 | return XFS_ERROR(EFSCORRUPTED); |
2395 | } | |
2396 | ||
1da177e4 | 2397 | /* |
4c46819a CH |
2398 | * Flush dirty inode metadata into the backing buffer. |
2399 | * | |
2400 | * The caller must have the inode lock and the inode flush lock held. The | |
2401 | * inode lock will still be held upon return to the caller, and the inode | |
2402 | * flush lock will be released after the inode has reached the disk. | |
2403 | * | |
2404 | * The caller must write out the buffer returned in *bpp and release it. | |
1da177e4 LT |
2405 | */ |
2406 | int | |
2407 | xfs_iflush( | |
4c46819a CH |
2408 | struct xfs_inode *ip, |
2409 | struct xfs_buf **bpp) | |
1da177e4 | 2410 | { |
4c46819a CH |
2411 | struct xfs_mount *mp = ip->i_mount; |
2412 | struct xfs_buf *bp; | |
2413 | struct xfs_dinode *dip; | |
1da177e4 | 2414 | int error; |
1da177e4 LT |
2415 | |
2416 | XFS_STATS_INC(xs_iflush_count); | |
2417 | ||
579aa9ca | 2418 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED)); |
474fce06 | 2419 | ASSERT(xfs_isiflocked(ip)); |
1da177e4 | 2420 | ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE || |
8096b1eb | 2421 | ip->i_d.di_nextents > XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK)); |
1da177e4 | 2422 | |
4c46819a | 2423 | *bpp = NULL; |
1da177e4 | 2424 | |
1da177e4 LT |
2425 | xfs_iunpin_wait(ip); |
2426 | ||
4b6a4688 DC |
2427 | /* |
2428 | * For stale inodes we cannot rely on the backing buffer remaining | |
2429 | * stale in cache for the remaining life of the stale inode and so | |
2430 | * xfs_itobp() below may give us a buffer that no longer contains | |
2431 | * inodes below. We have to check this after ensuring the inode is | |
2432 | * unpinned so that it is safe to reclaim the stale inode after the | |
2433 | * flush call. | |
2434 | */ | |
2435 | if (xfs_iflags_test(ip, XFS_ISTALE)) { | |
2436 | xfs_ifunlock(ip); | |
2437 | return 0; | |
2438 | } | |
2439 | ||
1da177e4 LT |
2440 | /* |
2441 | * This may have been unpinned because the filesystem is shutting | |
2442 | * down forcibly. If that's the case we must not write this inode | |
32ce90a4 CH |
2443 | * to disk, because the log record didn't make it to disk. |
2444 | * | |
2445 | * We also have to remove the log item from the AIL in this case, | |
2446 | * as we wait for an empty AIL as part of the unmount process. | |
1da177e4 LT |
2447 | */ |
2448 | if (XFS_FORCED_SHUTDOWN(mp)) { | |
32ce90a4 CH |
2449 | error = XFS_ERROR(EIO); |
2450 | goto abort_out; | |
1da177e4 LT |
2451 | } |
2452 | ||
a3f74ffb DC |
2453 | /* |
2454 | * Get the buffer containing the on-disk inode. | |
2455 | */ | |
4c46819a | 2456 | error = xfs_itobp(mp, NULL, ip, &dip, &bp, XBF_TRYLOCK); |
a3f74ffb DC |
2457 | if (error || !bp) { |
2458 | xfs_ifunlock(ip); | |
2459 | return error; | |
2460 | } | |
2461 | ||
1da177e4 LT |
2462 | /* |
2463 | * First flush out the inode that xfs_iflush was called with. | |
2464 | */ | |
2465 | error = xfs_iflush_int(ip, bp); | |
bad55843 | 2466 | if (error) |
1da177e4 | 2467 | goto corrupt_out; |
1da177e4 | 2468 | |
a3f74ffb DC |
2469 | /* |
2470 | * If the buffer is pinned then push on the log now so we won't | |
2471 | * get stuck waiting in the write for too long. | |
2472 | */ | |
811e64c7 | 2473 | if (xfs_buf_ispinned(bp)) |
a14a348b | 2474 | xfs_log_force(mp, 0); |
a3f74ffb | 2475 | |
1da177e4 LT |
2476 | /* |
2477 | * inode clustering: | |
2478 | * see if other inodes can be gathered into this write | |
2479 | */ | |
bad55843 DC |
2480 | error = xfs_iflush_cluster(ip, bp); |
2481 | if (error) | |
2482 | goto cluster_corrupt_out; | |
1da177e4 | 2483 | |
4c46819a CH |
2484 | *bpp = bp; |
2485 | return 0; | |
1da177e4 LT |
2486 | |
2487 | corrupt_out: | |
2488 | xfs_buf_relse(bp); | |
7d04a335 | 2489 | xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE); |
1da177e4 | 2490 | cluster_corrupt_out: |
32ce90a4 CH |
2491 | error = XFS_ERROR(EFSCORRUPTED); |
2492 | abort_out: | |
1da177e4 LT |
2493 | /* |
2494 | * Unlocks the flush lock | |
2495 | */ | |
04913fdd | 2496 | xfs_iflush_abort(ip, false); |
32ce90a4 | 2497 | return error; |
1da177e4 LT |
2498 | } |
2499 | ||
2500 | ||
2501 | STATIC int | |
2502 | xfs_iflush_int( | |
2503 | xfs_inode_t *ip, | |
2504 | xfs_buf_t *bp) | |
2505 | { | |
2506 | xfs_inode_log_item_t *iip; | |
2507 | xfs_dinode_t *dip; | |
2508 | xfs_mount_t *mp; | |
2509 | #ifdef XFS_TRANS_DEBUG | |
2510 | int first; | |
2511 | #endif | |
1da177e4 | 2512 | |
579aa9ca | 2513 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED)); |
474fce06 | 2514 | ASSERT(xfs_isiflocked(ip)); |
1da177e4 | 2515 | ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE || |
8096b1eb | 2516 | ip->i_d.di_nextents > XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK)); |
1da177e4 LT |
2517 | |
2518 | iip = ip->i_itemp; | |
2519 | mp = ip->i_mount; | |
2520 | ||
1da177e4 | 2521 | /* set *dip = inode's place in the buffer */ |
92bfc6e7 | 2522 | dip = (xfs_dinode_t *)xfs_buf_offset(bp, ip->i_imap.im_boffset); |
1da177e4 | 2523 | |
69ef921b | 2524 | if (XFS_TEST_ERROR(dip->di_magic != cpu_to_be16(XFS_DINODE_MAGIC), |
1da177e4 | 2525 | mp, XFS_ERRTAG_IFLUSH_1, XFS_RANDOM_IFLUSH_1)) { |
6a19d939 DC |
2526 | xfs_alert_tag(mp, XFS_PTAG_IFLUSH, |
2527 | "%s: Bad inode %Lu magic number 0x%x, ptr 0x%p", | |
2528 | __func__, ip->i_ino, be16_to_cpu(dip->di_magic), dip); | |
1da177e4 LT |
2529 | goto corrupt_out; |
2530 | } | |
2531 | if (XFS_TEST_ERROR(ip->i_d.di_magic != XFS_DINODE_MAGIC, | |
2532 | mp, XFS_ERRTAG_IFLUSH_2, XFS_RANDOM_IFLUSH_2)) { | |
6a19d939 DC |
2533 | xfs_alert_tag(mp, XFS_PTAG_IFLUSH, |
2534 | "%s: Bad inode %Lu, ptr 0x%p, magic number 0x%x", | |
2535 | __func__, ip->i_ino, ip, ip->i_d.di_magic); | |
1da177e4 LT |
2536 | goto corrupt_out; |
2537 | } | |
abbede1b | 2538 | if (S_ISREG(ip->i_d.di_mode)) { |
1da177e4 LT |
2539 | if (XFS_TEST_ERROR( |
2540 | (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS) && | |
2541 | (ip->i_d.di_format != XFS_DINODE_FMT_BTREE), | |
2542 | mp, XFS_ERRTAG_IFLUSH_3, XFS_RANDOM_IFLUSH_3)) { | |
6a19d939 DC |
2543 | xfs_alert_tag(mp, XFS_PTAG_IFLUSH, |
2544 | "%s: Bad regular inode %Lu, ptr 0x%p", | |
2545 | __func__, ip->i_ino, ip); | |
1da177e4 LT |
2546 | goto corrupt_out; |
2547 | } | |
abbede1b | 2548 | } else if (S_ISDIR(ip->i_d.di_mode)) { |
1da177e4 LT |
2549 | if (XFS_TEST_ERROR( |
2550 | (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS) && | |
2551 | (ip->i_d.di_format != XFS_DINODE_FMT_BTREE) && | |
2552 | (ip->i_d.di_format != XFS_DINODE_FMT_LOCAL), | |
2553 | mp, XFS_ERRTAG_IFLUSH_4, XFS_RANDOM_IFLUSH_4)) { | |
6a19d939 DC |
2554 | xfs_alert_tag(mp, XFS_PTAG_IFLUSH, |
2555 | "%s: Bad directory inode %Lu, ptr 0x%p", | |
2556 | __func__, ip->i_ino, ip); | |
1da177e4 LT |
2557 | goto corrupt_out; |
2558 | } | |
2559 | } | |
2560 | if (XFS_TEST_ERROR(ip->i_d.di_nextents + ip->i_d.di_anextents > | |
2561 | ip->i_d.di_nblocks, mp, XFS_ERRTAG_IFLUSH_5, | |
2562 | XFS_RANDOM_IFLUSH_5)) { | |
6a19d939 DC |
2563 | xfs_alert_tag(mp, XFS_PTAG_IFLUSH, |
2564 | "%s: detected corrupt incore inode %Lu, " | |
2565 | "total extents = %d, nblocks = %Ld, ptr 0x%p", | |
2566 | __func__, ip->i_ino, | |
1da177e4 | 2567 | ip->i_d.di_nextents + ip->i_d.di_anextents, |
6a19d939 | 2568 | ip->i_d.di_nblocks, ip); |
1da177e4 LT |
2569 | goto corrupt_out; |
2570 | } | |
2571 | if (XFS_TEST_ERROR(ip->i_d.di_forkoff > mp->m_sb.sb_inodesize, | |
2572 | mp, XFS_ERRTAG_IFLUSH_6, XFS_RANDOM_IFLUSH_6)) { | |
6a19d939 DC |
2573 | xfs_alert_tag(mp, XFS_PTAG_IFLUSH, |
2574 | "%s: bad inode %Lu, forkoff 0x%x, ptr 0x%p", | |
2575 | __func__, ip->i_ino, ip->i_d.di_forkoff, ip); | |
1da177e4 LT |
2576 | goto corrupt_out; |
2577 | } | |
2578 | /* | |
2579 | * bump the flush iteration count, used to detect flushes which | |
2580 | * postdate a log record during recovery. | |
2581 | */ | |
2582 | ||
2583 | ip->i_d.di_flushiter++; | |
2584 | ||
2585 | /* | |
2586 | * Copy the dirty parts of the inode into the on-disk | |
2587 | * inode. We always copy out the core of the inode, | |
2588 | * because if the inode is dirty at all the core must | |
2589 | * be. | |
2590 | */ | |
81591fe2 | 2591 | xfs_dinode_to_disk(dip, &ip->i_d); |
1da177e4 LT |
2592 | |
2593 | /* Wrap, we never let the log put out DI_MAX_FLUSH */ | |
2594 | if (ip->i_d.di_flushiter == DI_MAX_FLUSH) | |
2595 | ip->i_d.di_flushiter = 0; | |
2596 | ||
2597 | /* | |
2598 | * If this is really an old format inode and the superblock version | |
2599 | * has not been updated to support only new format inodes, then | |
2600 | * convert back to the old inode format. If the superblock version | |
2601 | * has been updated, then make the conversion permanent. | |
2602 | */ | |
51ce16d5 CH |
2603 | ASSERT(ip->i_d.di_version == 1 || xfs_sb_version_hasnlink(&mp->m_sb)); |
2604 | if (ip->i_d.di_version == 1) { | |
62118709 | 2605 | if (!xfs_sb_version_hasnlink(&mp->m_sb)) { |
1da177e4 LT |
2606 | /* |
2607 | * Convert it back. | |
2608 | */ | |
2609 | ASSERT(ip->i_d.di_nlink <= XFS_MAXLINK_1); | |
81591fe2 | 2610 | dip->di_onlink = cpu_to_be16(ip->i_d.di_nlink); |
1da177e4 LT |
2611 | } else { |
2612 | /* | |
2613 | * The superblock version has already been bumped, | |
2614 | * so just make the conversion to the new inode | |
2615 | * format permanent. | |
2616 | */ | |
51ce16d5 CH |
2617 | ip->i_d.di_version = 2; |
2618 | dip->di_version = 2; | |
1da177e4 | 2619 | ip->i_d.di_onlink = 0; |
81591fe2 | 2620 | dip->di_onlink = 0; |
1da177e4 | 2621 | memset(&(ip->i_d.di_pad[0]), 0, sizeof(ip->i_d.di_pad)); |
81591fe2 CH |
2622 | memset(&(dip->di_pad[0]), 0, |
2623 | sizeof(dip->di_pad)); | |
6743099c | 2624 | ASSERT(xfs_get_projid(ip) == 0); |
1da177e4 LT |
2625 | } |
2626 | } | |
2627 | ||
e4ac967b DC |
2628 | xfs_iflush_fork(ip, dip, iip, XFS_DATA_FORK, bp); |
2629 | if (XFS_IFORK_Q(ip)) | |
2630 | xfs_iflush_fork(ip, dip, iip, XFS_ATTR_FORK, bp); | |
1da177e4 LT |
2631 | xfs_inobp_check(mp, bp); |
2632 | ||
2633 | /* | |
f5d8d5c4 CH |
2634 | * We've recorded everything logged in the inode, so we'd like to clear |
2635 | * the ili_fields bits so we don't log and flush things unnecessarily. | |
2636 | * However, we can't stop logging all this information until the data | |
2637 | * we've copied into the disk buffer is written to disk. If we did we | |
2638 | * might overwrite the copy of the inode in the log with all the data | |
2639 | * after re-logging only part of it, and in the face of a crash we | |
2640 | * wouldn't have all the data we need to recover. | |
1da177e4 | 2641 | * |
f5d8d5c4 CH |
2642 | * What we do is move the bits to the ili_last_fields field. When |
2643 | * logging the inode, these bits are moved back to the ili_fields field. | |
2644 | * In the xfs_iflush_done() routine we clear ili_last_fields, since we | |
2645 | * know that the information those bits represent is permanently on | |
2646 | * disk. As long as the flush completes before the inode is logged | |
2647 | * again, then both ili_fields and ili_last_fields will be cleared. | |
1da177e4 | 2648 | * |
f5d8d5c4 CH |
2649 | * We can play with the ili_fields bits here, because the inode lock |
2650 | * must be held exclusively in order to set bits there and the flush | |
2651 | * lock protects the ili_last_fields bits. Set ili_logged so the flush | |
2652 | * done routine can tell whether or not to look in the AIL. Also, store | |
2653 | * the current LSN of the inode so that we can tell whether the item has | |
2654 | * moved in the AIL from xfs_iflush_done(). In order to read the lsn we | |
2655 | * need the AIL lock, because it is a 64 bit value that cannot be read | |
2656 | * atomically. | |
1da177e4 | 2657 | */ |
f5d8d5c4 CH |
2658 | if (iip != NULL && iip->ili_fields != 0) { |
2659 | iip->ili_last_fields = iip->ili_fields; | |
2660 | iip->ili_fields = 0; | |
1da177e4 LT |
2661 | iip->ili_logged = 1; |
2662 | ||
7b2e2a31 DC |
2663 | xfs_trans_ail_copy_lsn(mp->m_ail, &iip->ili_flush_lsn, |
2664 | &iip->ili_item.li_lsn); | |
1da177e4 LT |
2665 | |
2666 | /* | |
2667 | * Attach the function xfs_iflush_done to the inode's | |
2668 | * buffer. This will remove the inode from the AIL | |
2669 | * and unlock the inode's flush lock when the inode is | |
2670 | * completely written to disk. | |
2671 | */ | |
ca30b2a7 | 2672 | xfs_buf_attach_iodone(bp, xfs_iflush_done, &iip->ili_item); |
1da177e4 | 2673 | |
adadbeef | 2674 | ASSERT(bp->b_fspriv != NULL); |
cb669ca5 | 2675 | ASSERT(bp->b_iodone != NULL); |
1da177e4 LT |
2676 | } else { |
2677 | /* | |
2678 | * We're flushing an inode which is not in the AIL and has | |
8a9c9980 | 2679 | * not been logged. For this case we can immediately drop |
1da177e4 LT |
2680 | * the inode flush lock because we can avoid the whole |
2681 | * AIL state thing. It's OK to drop the flush lock now, | |
2682 | * because we've already locked the buffer and to do anything | |
2683 | * you really need both. | |
2684 | */ | |
2685 | if (iip != NULL) { | |
2686 | ASSERT(iip->ili_logged == 0); | |
2687 | ASSERT(iip->ili_last_fields == 0); | |
2688 | ASSERT((iip->ili_item.li_flags & XFS_LI_IN_AIL) == 0); | |
2689 | } | |
2690 | xfs_ifunlock(ip); | |
2691 | } | |
2692 | ||
2693 | return 0; | |
2694 | ||
2695 | corrupt_out: | |
2696 | return XFS_ERROR(EFSCORRUPTED); | |
2697 | } | |
2698 | ||
4eea22f0 MK |
2699 | /* |
2700 | * Return a pointer to the extent record at file index idx. | |
2701 | */ | |
a6f64d4a | 2702 | xfs_bmbt_rec_host_t * |
4eea22f0 MK |
2703 | xfs_iext_get_ext( |
2704 | xfs_ifork_t *ifp, /* inode fork pointer */ | |
2705 | xfs_extnum_t idx) /* index of target extent */ | |
2706 | { | |
2707 | ASSERT(idx >= 0); | |
87bef181 CH |
2708 | ASSERT(idx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t)); |
2709 | ||
0293ce3a MK |
2710 | if ((ifp->if_flags & XFS_IFEXTIREC) && (idx == 0)) { |
2711 | return ifp->if_u1.if_ext_irec->er_extbuf; | |
2712 | } else if (ifp->if_flags & XFS_IFEXTIREC) { | |
2713 | xfs_ext_irec_t *erp; /* irec pointer */ | |
2714 | int erp_idx = 0; /* irec index */ | |
2715 | xfs_extnum_t page_idx = idx; /* ext index in target list */ | |
2716 | ||
2717 | erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 0); | |
2718 | return &erp->er_extbuf[page_idx]; | |
2719 | } else if (ifp->if_bytes) { | |
4eea22f0 MK |
2720 | return &ifp->if_u1.if_extents[idx]; |
2721 | } else { | |
2722 | return NULL; | |
2723 | } | |
2724 | } | |
2725 | ||
2726 | /* | |
2727 | * Insert new item(s) into the extent records for incore inode | |
2728 | * fork 'ifp'. 'count' new items are inserted at index 'idx'. | |
2729 | */ | |
2730 | void | |
2731 | xfs_iext_insert( | |
6ef35544 | 2732 | xfs_inode_t *ip, /* incore inode pointer */ |
4eea22f0 MK |
2733 | xfs_extnum_t idx, /* starting index of new items */ |
2734 | xfs_extnum_t count, /* number of inserted items */ | |
6ef35544 CH |
2735 | xfs_bmbt_irec_t *new, /* items to insert */ |
2736 | int state) /* type of extent conversion */ | |
4eea22f0 | 2737 | { |
6ef35544 | 2738 | xfs_ifork_t *ifp = (state & BMAP_ATTRFORK) ? ip->i_afp : &ip->i_df; |
4eea22f0 MK |
2739 | xfs_extnum_t i; /* extent record index */ |
2740 | ||
0b1b213f CH |
2741 | trace_xfs_iext_insert(ip, idx, new, state, _RET_IP_); |
2742 | ||
4eea22f0 MK |
2743 | ASSERT(ifp->if_flags & XFS_IFEXTENTS); |
2744 | xfs_iext_add(ifp, idx, count); | |
a6f64d4a CH |
2745 | for (i = idx; i < idx + count; i++, new++) |
2746 | xfs_bmbt_set_all(xfs_iext_get_ext(ifp, i), new); | |
4eea22f0 MK |
2747 | } |
2748 | ||
2749 | /* | |
2750 | * This is called when the amount of space required for incore file | |
2751 | * extents needs to be increased. The ext_diff parameter stores the | |
2752 | * number of new extents being added and the idx parameter contains | |
2753 | * the extent index where the new extents will be added. If the new | |
2754 | * extents are being appended, then we just need to (re)allocate and | |
2755 | * initialize the space. Otherwise, if the new extents are being | |
2756 | * inserted into the middle of the existing entries, a bit more work | |
2757 | * is required to make room for the new extents to be inserted. The | |
2758 | * caller is responsible for filling in the new extent entries upon | |
2759 | * return. | |
2760 | */ | |
2761 | void | |
2762 | xfs_iext_add( | |
2763 | xfs_ifork_t *ifp, /* inode fork pointer */ | |
2764 | xfs_extnum_t idx, /* index to begin adding exts */ | |
c41564b5 | 2765 | int ext_diff) /* number of extents to add */ |
4eea22f0 MK |
2766 | { |
2767 | int byte_diff; /* new bytes being added */ | |
2768 | int new_size; /* size of extents after adding */ | |
2769 | xfs_extnum_t nextents; /* number of extents in file */ | |
2770 | ||
2771 | nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); | |
2772 | ASSERT((idx >= 0) && (idx <= nextents)); | |
2773 | byte_diff = ext_diff * sizeof(xfs_bmbt_rec_t); | |
2774 | new_size = ifp->if_bytes + byte_diff; | |
2775 | /* | |
2776 | * If the new number of extents (nextents + ext_diff) | |
2777 | * fits inside the inode, then continue to use the inline | |
2778 | * extent buffer. | |
2779 | */ | |
2780 | if (nextents + ext_diff <= XFS_INLINE_EXTS) { | |
2781 | if (idx < nextents) { | |
2782 | memmove(&ifp->if_u2.if_inline_ext[idx + ext_diff], | |
2783 | &ifp->if_u2.if_inline_ext[idx], | |
2784 | (nextents - idx) * sizeof(xfs_bmbt_rec_t)); | |
2785 | memset(&ifp->if_u2.if_inline_ext[idx], 0, byte_diff); | |
2786 | } | |
2787 | ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext; | |
2788 | ifp->if_real_bytes = 0; | |
2789 | } | |
2790 | /* | |
2791 | * Otherwise use a linear (direct) extent list. | |
2792 | * If the extents are currently inside the inode, | |
2793 | * xfs_iext_realloc_direct will switch us from | |
2794 | * inline to direct extent allocation mode. | |
2795 | */ | |
0293ce3a | 2796 | else if (nextents + ext_diff <= XFS_LINEAR_EXTS) { |
4eea22f0 MK |
2797 | xfs_iext_realloc_direct(ifp, new_size); |
2798 | if (idx < nextents) { | |
2799 | memmove(&ifp->if_u1.if_extents[idx + ext_diff], | |
2800 | &ifp->if_u1.if_extents[idx], | |
2801 | (nextents - idx) * sizeof(xfs_bmbt_rec_t)); | |
2802 | memset(&ifp->if_u1.if_extents[idx], 0, byte_diff); | |
2803 | } | |
2804 | } | |
0293ce3a MK |
2805 | /* Indirection array */ |
2806 | else { | |
2807 | xfs_ext_irec_t *erp; | |
2808 | int erp_idx = 0; | |
2809 | int page_idx = idx; | |
2810 | ||
2811 | ASSERT(nextents + ext_diff > XFS_LINEAR_EXTS); | |
2812 | if (ifp->if_flags & XFS_IFEXTIREC) { | |
2813 | erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 1); | |
2814 | } else { | |
2815 | xfs_iext_irec_init(ifp); | |
2816 | ASSERT(ifp->if_flags & XFS_IFEXTIREC); | |
2817 | erp = ifp->if_u1.if_ext_irec; | |
2818 | } | |
2819 | /* Extents fit in target extent page */ | |
2820 | if (erp && erp->er_extcount + ext_diff <= XFS_LINEAR_EXTS) { | |
2821 | if (page_idx < erp->er_extcount) { | |
2822 | memmove(&erp->er_extbuf[page_idx + ext_diff], | |
2823 | &erp->er_extbuf[page_idx], | |
2824 | (erp->er_extcount - page_idx) * | |
2825 | sizeof(xfs_bmbt_rec_t)); | |
2826 | memset(&erp->er_extbuf[page_idx], 0, byte_diff); | |
2827 | } | |
2828 | erp->er_extcount += ext_diff; | |
2829 | xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff); | |
2830 | } | |
2831 | /* Insert a new extent page */ | |
2832 | else if (erp) { | |
2833 | xfs_iext_add_indirect_multi(ifp, | |
2834 | erp_idx, page_idx, ext_diff); | |
2835 | } | |
2836 | /* | |
2837 | * If extent(s) are being appended to the last page in | |
2838 | * the indirection array and the new extent(s) don't fit | |
2839 | * in the page, then erp is NULL and erp_idx is set to | |
2840 | * the next index needed in the indirection array. | |
2841 | */ | |
2842 | else { | |
2843 | int count = ext_diff; | |
2844 | ||
2845 | while (count) { | |
2846 | erp = xfs_iext_irec_new(ifp, erp_idx); | |
2847 | erp->er_extcount = count; | |
2848 | count -= MIN(count, (int)XFS_LINEAR_EXTS); | |
2849 | if (count) { | |
2850 | erp_idx++; | |
2851 | } | |
2852 | } | |
2853 | } | |
2854 | } | |
4eea22f0 MK |
2855 | ifp->if_bytes = new_size; |
2856 | } | |
2857 | ||
0293ce3a MK |
2858 | /* |
2859 | * This is called when incore extents are being added to the indirection | |
2860 | * array and the new extents do not fit in the target extent list. The | |
2861 | * erp_idx parameter contains the irec index for the target extent list | |
2862 | * in the indirection array, and the idx parameter contains the extent | |
2863 | * index within the list. The number of extents being added is stored | |
2864 | * in the count parameter. | |
2865 | * | |
2866 | * |-------| |-------| | |
2867 | * | | | | idx - number of extents before idx | |
2868 | * | idx | | count | | |
2869 | * | | | | count - number of extents being inserted at idx | |
2870 | * |-------| |-------| | |
2871 | * | count | | nex2 | nex2 - number of extents after idx + count | |
2872 | * |-------| |-------| | |
2873 | */ | |
2874 | void | |
2875 | xfs_iext_add_indirect_multi( | |
2876 | xfs_ifork_t *ifp, /* inode fork pointer */ | |
2877 | int erp_idx, /* target extent irec index */ | |
2878 | xfs_extnum_t idx, /* index within target list */ | |
2879 | int count) /* new extents being added */ | |
2880 | { | |
2881 | int byte_diff; /* new bytes being added */ | |
2882 | xfs_ext_irec_t *erp; /* pointer to irec entry */ | |
2883 | xfs_extnum_t ext_diff; /* number of extents to add */ | |
2884 | xfs_extnum_t ext_cnt; /* new extents still needed */ | |
2885 | xfs_extnum_t nex2; /* extents after idx + count */ | |
2886 | xfs_bmbt_rec_t *nex2_ep = NULL; /* temp list for nex2 extents */ | |
2887 | int nlists; /* number of irec's (lists) */ | |
2888 | ||
2889 | ASSERT(ifp->if_flags & XFS_IFEXTIREC); | |
2890 | erp = &ifp->if_u1.if_ext_irec[erp_idx]; | |
2891 | nex2 = erp->er_extcount - idx; | |
2892 | nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ; | |
2893 | ||
2894 | /* | |
2895 | * Save second part of target extent list | |
2896 | * (all extents past */ | |
2897 | if (nex2) { | |
2898 | byte_diff = nex2 * sizeof(xfs_bmbt_rec_t); | |
6785073b | 2899 | nex2_ep = (xfs_bmbt_rec_t *) kmem_alloc(byte_diff, KM_NOFS); |
0293ce3a MK |
2900 | memmove(nex2_ep, &erp->er_extbuf[idx], byte_diff); |
2901 | erp->er_extcount -= nex2; | |
2902 | xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, -nex2); | |
2903 | memset(&erp->er_extbuf[idx], 0, byte_diff); | |
2904 | } | |
2905 | ||
2906 | /* | |
2907 | * Add the new extents to the end of the target | |
2908 | * list, then allocate new irec record(s) and | |
2909 | * extent buffer(s) as needed to store the rest | |
2910 | * of the new extents. | |
2911 | */ | |
2912 | ext_cnt = count; | |
2913 | ext_diff = MIN(ext_cnt, (int)XFS_LINEAR_EXTS - erp->er_extcount); | |
2914 | if (ext_diff) { | |
2915 | erp->er_extcount += ext_diff; | |
2916 | xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff); | |
2917 | ext_cnt -= ext_diff; | |
2918 | } | |
2919 | while (ext_cnt) { | |
2920 | erp_idx++; | |
2921 | erp = xfs_iext_irec_new(ifp, erp_idx); | |
2922 | ext_diff = MIN(ext_cnt, (int)XFS_LINEAR_EXTS); | |
2923 | erp->er_extcount = ext_diff; | |
2924 | xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff); | |
2925 | ext_cnt -= ext_diff; | |
2926 | } | |
2927 | ||
2928 | /* Add nex2 extents back to indirection array */ | |
2929 | if (nex2) { | |
2930 | xfs_extnum_t ext_avail; | |
2931 | int i; | |
2932 | ||
2933 | byte_diff = nex2 * sizeof(xfs_bmbt_rec_t); | |
2934 | ext_avail = XFS_LINEAR_EXTS - erp->er_extcount; | |
2935 | i = 0; | |
2936 | /* | |
2937 | * If nex2 extents fit in the current page, append | |
2938 | * nex2_ep after the new extents. | |
2939 | */ | |
2940 | if (nex2 <= ext_avail) { | |
2941 | i = erp->er_extcount; | |
2942 | } | |
2943 | /* | |
2944 | * Otherwise, check if space is available in the | |
2945 | * next page. | |
2946 | */ | |
2947 | else if ((erp_idx < nlists - 1) && | |
2948 | (nex2 <= (ext_avail = XFS_LINEAR_EXTS - | |
2949 | ifp->if_u1.if_ext_irec[erp_idx+1].er_extcount))) { | |
2950 | erp_idx++; | |
2951 | erp++; | |
2952 | /* Create a hole for nex2 extents */ | |
2953 | memmove(&erp->er_extbuf[nex2], erp->er_extbuf, | |
2954 | erp->er_extcount * sizeof(xfs_bmbt_rec_t)); | |
2955 | } | |
2956 | /* | |
2957 | * Final choice, create a new extent page for | |
2958 | * nex2 extents. | |
2959 | */ | |
2960 | else { | |
2961 | erp_idx++; | |
2962 | erp = xfs_iext_irec_new(ifp, erp_idx); | |
2963 | } | |
2964 | memmove(&erp->er_extbuf[i], nex2_ep, byte_diff); | |
f0e2d93c | 2965 | kmem_free(nex2_ep); |
0293ce3a MK |
2966 | erp->er_extcount += nex2; |
2967 | xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, nex2); | |
2968 | } | |
2969 | } | |
2970 | ||
4eea22f0 MK |
2971 | /* |
2972 | * This is called when the amount of space required for incore file | |
2973 | * extents needs to be decreased. The ext_diff parameter stores the | |
2974 | * number of extents to be removed and the idx parameter contains | |
2975 | * the extent index where the extents will be removed from. | |
0293ce3a MK |
2976 | * |
2977 | * If the amount of space needed has decreased below the linear | |
2978 | * limit, XFS_IEXT_BUFSZ, then switch to using the contiguous | |
2979 | * extent array. Otherwise, use kmem_realloc() to adjust the | |
2980 | * size to what is needed. | |
4eea22f0 MK |
2981 | */ |
2982 | void | |
2983 | xfs_iext_remove( | |
6ef35544 | 2984 | xfs_inode_t *ip, /* incore inode pointer */ |
4eea22f0 | 2985 | xfs_extnum_t idx, /* index to begin removing exts */ |
6ef35544 CH |
2986 | int ext_diff, /* number of extents to remove */ |
2987 | int state) /* type of extent conversion */ | |
4eea22f0 | 2988 | { |
6ef35544 | 2989 | xfs_ifork_t *ifp = (state & BMAP_ATTRFORK) ? ip->i_afp : &ip->i_df; |
4eea22f0 MK |
2990 | xfs_extnum_t nextents; /* number of extents in file */ |
2991 | int new_size; /* size of extents after removal */ | |
2992 | ||
0b1b213f CH |
2993 | trace_xfs_iext_remove(ip, idx, state, _RET_IP_); |
2994 | ||
4eea22f0 MK |
2995 | ASSERT(ext_diff > 0); |
2996 | nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); | |
2997 | new_size = (nextents - ext_diff) * sizeof(xfs_bmbt_rec_t); | |
2998 | ||
2999 | if (new_size == 0) { | |
3000 | xfs_iext_destroy(ifp); | |
0293ce3a MK |
3001 | } else if (ifp->if_flags & XFS_IFEXTIREC) { |
3002 | xfs_iext_remove_indirect(ifp, idx, ext_diff); | |
4eea22f0 MK |
3003 | } else if (ifp->if_real_bytes) { |
3004 | xfs_iext_remove_direct(ifp, idx, ext_diff); | |
3005 | } else { | |
3006 | xfs_iext_remove_inline(ifp, idx, ext_diff); | |
3007 | } | |
3008 | ifp->if_bytes = new_size; | |
3009 | } | |
3010 | ||
3011 | /* | |
3012 | * This removes ext_diff extents from the inline buffer, beginning | |
3013 | * at extent index idx. | |
3014 | */ | |
3015 | void | |
3016 | xfs_iext_remove_inline( | |
3017 | xfs_ifork_t *ifp, /* inode fork pointer */ | |
3018 | xfs_extnum_t idx, /* index to begin removing exts */ | |
3019 | int ext_diff) /* number of extents to remove */ | |
3020 | { | |
3021 | int nextents; /* number of extents in file */ | |
3022 | ||
0293ce3a | 3023 | ASSERT(!(ifp->if_flags & XFS_IFEXTIREC)); |
4eea22f0 MK |
3024 | ASSERT(idx < XFS_INLINE_EXTS); |
3025 | nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); | |
3026 | ASSERT(((nextents - ext_diff) > 0) && | |
3027 | (nextents - ext_diff) < XFS_INLINE_EXTS); | |
3028 | ||
3029 | if (idx + ext_diff < nextents) { | |
3030 | memmove(&ifp->if_u2.if_inline_ext[idx], | |
3031 | &ifp->if_u2.if_inline_ext[idx + ext_diff], | |
3032 | (nextents - (idx + ext_diff)) * | |
3033 | sizeof(xfs_bmbt_rec_t)); | |
3034 | memset(&ifp->if_u2.if_inline_ext[nextents - ext_diff], | |
3035 | 0, ext_diff * sizeof(xfs_bmbt_rec_t)); | |
3036 | } else { | |
3037 | memset(&ifp->if_u2.if_inline_ext[idx], 0, | |
3038 | ext_diff * sizeof(xfs_bmbt_rec_t)); | |
3039 | } | |
3040 | } | |
3041 | ||
3042 | /* | |
3043 | * This removes ext_diff extents from a linear (direct) extent list, | |
3044 | * beginning at extent index idx. If the extents are being removed | |
3045 | * from the end of the list (ie. truncate) then we just need to re- | |
3046 | * allocate the list to remove the extra space. Otherwise, if the | |
3047 | * extents are being removed from the middle of the existing extent | |
3048 | * entries, then we first need to move the extent records beginning | |
3049 | * at idx + ext_diff up in the list to overwrite the records being | |
3050 | * removed, then remove the extra space via kmem_realloc. | |
3051 | */ | |
3052 | void | |
3053 | xfs_iext_remove_direct( | |
3054 | xfs_ifork_t *ifp, /* inode fork pointer */ | |
3055 | xfs_extnum_t idx, /* index to begin removing exts */ | |
3056 | int ext_diff) /* number of extents to remove */ | |
3057 | { | |
3058 | xfs_extnum_t nextents; /* number of extents in file */ | |
3059 | int new_size; /* size of extents after removal */ | |
3060 | ||
0293ce3a | 3061 | ASSERT(!(ifp->if_flags & XFS_IFEXTIREC)); |
4eea22f0 MK |
3062 | new_size = ifp->if_bytes - |
3063 | (ext_diff * sizeof(xfs_bmbt_rec_t)); | |
3064 | nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); | |
3065 | ||
3066 | if (new_size == 0) { | |
3067 | xfs_iext_destroy(ifp); | |
3068 | return; | |
3069 | } | |
3070 | /* Move extents up in the list (if needed) */ | |
3071 | if (idx + ext_diff < nextents) { | |
3072 | memmove(&ifp->if_u1.if_extents[idx], | |
3073 | &ifp->if_u1.if_extents[idx + ext_diff], | |
3074 | (nextents - (idx + ext_diff)) * | |
3075 | sizeof(xfs_bmbt_rec_t)); | |
3076 | } | |
3077 | memset(&ifp->if_u1.if_extents[nextents - ext_diff], | |
3078 | 0, ext_diff * sizeof(xfs_bmbt_rec_t)); | |
3079 | /* | |
3080 | * Reallocate the direct extent list. If the extents | |
3081 | * will fit inside the inode then xfs_iext_realloc_direct | |
3082 | * will switch from direct to inline extent allocation | |
3083 | * mode for us. | |
3084 | */ | |
3085 | xfs_iext_realloc_direct(ifp, new_size); | |
3086 | ifp->if_bytes = new_size; | |
3087 | } | |
3088 | ||
0293ce3a MK |
3089 | /* |
3090 | * This is called when incore extents are being removed from the | |
3091 | * indirection array and the extents being removed span multiple extent | |
3092 | * buffers. The idx parameter contains the file extent index where we | |
3093 | * want to begin removing extents, and the count parameter contains | |
3094 | * how many extents need to be removed. | |
3095 | * | |
3096 | * |-------| |-------| | |
3097 | * | nex1 | | | nex1 - number of extents before idx | |
3098 | * |-------| | count | | |
3099 | * | | | | count - number of extents being removed at idx | |
3100 | * | count | |-------| | |
3101 | * | | | nex2 | nex2 - number of extents after idx + count | |
3102 | * |-------| |-------| | |
3103 | */ | |
3104 | void | |
3105 | xfs_iext_remove_indirect( | |
3106 | xfs_ifork_t *ifp, /* inode fork pointer */ | |
3107 | xfs_extnum_t idx, /* index to begin removing extents */ | |
3108 | int count) /* number of extents to remove */ | |
3109 | { | |
3110 | xfs_ext_irec_t *erp; /* indirection array pointer */ | |
3111 | int erp_idx = 0; /* indirection array index */ | |
3112 | xfs_extnum_t ext_cnt; /* extents left to remove */ | |
3113 | xfs_extnum_t ext_diff; /* extents to remove in current list */ | |
3114 | xfs_extnum_t nex1; /* number of extents before idx */ | |
3115 | xfs_extnum_t nex2; /* extents after idx + count */ | |
0293ce3a MK |
3116 | int page_idx = idx; /* index in target extent list */ |
3117 | ||
3118 | ASSERT(ifp->if_flags & XFS_IFEXTIREC); | |
3119 | erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 0); | |
3120 | ASSERT(erp != NULL); | |
0293ce3a MK |
3121 | nex1 = page_idx; |
3122 | ext_cnt = count; | |
3123 | while (ext_cnt) { | |
3124 | nex2 = MAX((erp->er_extcount - (nex1 + ext_cnt)), 0); | |
3125 | ext_diff = MIN(ext_cnt, (erp->er_extcount - nex1)); | |
3126 | /* | |
3127 | * Check for deletion of entire list; | |
3128 | * xfs_iext_irec_remove() updates extent offsets. | |
3129 | */ | |
3130 | if (ext_diff == erp->er_extcount) { | |
3131 | xfs_iext_irec_remove(ifp, erp_idx); | |
3132 | ext_cnt -= ext_diff; | |
3133 | nex1 = 0; | |
3134 | if (ext_cnt) { | |
3135 | ASSERT(erp_idx < ifp->if_real_bytes / | |
3136 | XFS_IEXT_BUFSZ); | |
3137 | erp = &ifp->if_u1.if_ext_irec[erp_idx]; | |
3138 | nex1 = 0; | |
3139 | continue; | |
3140 | } else { | |
3141 | break; | |
3142 | } | |
3143 | } | |
3144 | /* Move extents up (if needed) */ | |
3145 | if (nex2) { | |
3146 | memmove(&erp->er_extbuf[nex1], | |
3147 | &erp->er_extbuf[nex1 + ext_diff], | |
3148 | nex2 * sizeof(xfs_bmbt_rec_t)); | |
3149 | } | |
3150 | /* Zero out rest of page */ | |
3151 | memset(&erp->er_extbuf[nex1 + nex2], 0, (XFS_IEXT_BUFSZ - | |
3152 | ((nex1 + nex2) * sizeof(xfs_bmbt_rec_t)))); | |
3153 | /* Update remaining counters */ | |
3154 | erp->er_extcount -= ext_diff; | |
3155 | xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, -ext_diff); | |
3156 | ext_cnt -= ext_diff; | |
3157 | nex1 = 0; | |
3158 | erp_idx++; | |
3159 | erp++; | |
3160 | } | |
3161 | ifp->if_bytes -= count * sizeof(xfs_bmbt_rec_t); | |
3162 | xfs_iext_irec_compact(ifp); | |
3163 | } | |
3164 | ||
4eea22f0 MK |
3165 | /* |
3166 | * Create, destroy, or resize a linear (direct) block of extents. | |
3167 | */ | |
3168 | void | |
3169 | xfs_iext_realloc_direct( | |
3170 | xfs_ifork_t *ifp, /* inode fork pointer */ | |
3171 | int new_size) /* new size of extents */ | |
3172 | { | |
3173 | int rnew_size; /* real new size of extents */ | |
3174 | ||
3175 | rnew_size = new_size; | |
3176 | ||
0293ce3a MK |
3177 | ASSERT(!(ifp->if_flags & XFS_IFEXTIREC) || |
3178 | ((new_size >= 0) && (new_size <= XFS_IEXT_BUFSZ) && | |
3179 | (new_size != ifp->if_real_bytes))); | |
3180 | ||
4eea22f0 MK |
3181 | /* Free extent records */ |
3182 | if (new_size == 0) { | |
3183 | xfs_iext_destroy(ifp); | |
3184 | } | |
3185 | /* Resize direct extent list and zero any new bytes */ | |
3186 | else if (ifp->if_real_bytes) { | |
3187 | /* Check if extents will fit inside the inode */ | |
3188 | if (new_size <= XFS_INLINE_EXTS * sizeof(xfs_bmbt_rec_t)) { | |
3189 | xfs_iext_direct_to_inline(ifp, new_size / | |
3190 | (uint)sizeof(xfs_bmbt_rec_t)); | |
3191 | ifp->if_bytes = new_size; | |
3192 | return; | |
3193 | } | |
16a087d8 | 3194 | if (!is_power_of_2(new_size)){ |
40ebd81d | 3195 | rnew_size = roundup_pow_of_two(new_size); |
4eea22f0 MK |
3196 | } |
3197 | if (rnew_size != ifp->if_real_bytes) { | |
a6f64d4a | 3198 | ifp->if_u1.if_extents = |
4eea22f0 MK |
3199 | kmem_realloc(ifp->if_u1.if_extents, |
3200 | rnew_size, | |
6785073b | 3201 | ifp->if_real_bytes, KM_NOFS); |
4eea22f0 MK |
3202 | } |
3203 | if (rnew_size > ifp->if_real_bytes) { | |
3204 | memset(&ifp->if_u1.if_extents[ifp->if_bytes / | |
3205 | (uint)sizeof(xfs_bmbt_rec_t)], 0, | |
3206 | rnew_size - ifp->if_real_bytes); | |
3207 | } | |
3208 | } | |
3209 | /* | |
3210 | * Switch from the inline extent buffer to a direct | |
3211 | * extent list. Be sure to include the inline extent | |
3212 | * bytes in new_size. | |
3213 | */ | |
3214 | else { | |
3215 | new_size += ifp->if_bytes; | |
16a087d8 | 3216 | if (!is_power_of_2(new_size)) { |
40ebd81d | 3217 | rnew_size = roundup_pow_of_two(new_size); |
4eea22f0 MK |
3218 | } |
3219 | xfs_iext_inline_to_direct(ifp, rnew_size); | |
3220 | } | |
3221 | ifp->if_real_bytes = rnew_size; | |
3222 | ifp->if_bytes = new_size; | |
3223 | } | |
3224 | ||
3225 | /* | |
3226 | * Switch from linear (direct) extent records to inline buffer. | |
3227 | */ | |
3228 | void | |
3229 | xfs_iext_direct_to_inline( | |
3230 | xfs_ifork_t *ifp, /* inode fork pointer */ | |
3231 | xfs_extnum_t nextents) /* number of extents in file */ | |
3232 | { | |
3233 | ASSERT(ifp->if_flags & XFS_IFEXTENTS); | |
3234 | ASSERT(nextents <= XFS_INLINE_EXTS); | |
3235 | /* | |
3236 | * The inline buffer was zeroed when we switched | |
3237 | * from inline to direct extent allocation mode, | |
3238 | * so we don't need to clear it here. | |
3239 | */ | |
3240 | memcpy(ifp->if_u2.if_inline_ext, ifp->if_u1.if_extents, | |
3241 | nextents * sizeof(xfs_bmbt_rec_t)); | |
f0e2d93c | 3242 | kmem_free(ifp->if_u1.if_extents); |
4eea22f0 MK |
3243 | ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext; |
3244 | ifp->if_real_bytes = 0; | |
3245 | } | |
3246 | ||
3247 | /* | |
3248 | * Switch from inline buffer to linear (direct) extent records. | |
3249 | * new_size should already be rounded up to the next power of 2 | |
3250 | * by the caller (when appropriate), so use new_size as it is. | |
3251 | * However, since new_size may be rounded up, we can't update | |
3252 | * if_bytes here. It is the caller's responsibility to update | |
3253 | * if_bytes upon return. | |
3254 | */ | |
3255 | void | |
3256 | xfs_iext_inline_to_direct( | |
3257 | xfs_ifork_t *ifp, /* inode fork pointer */ | |
3258 | int new_size) /* number of extents in file */ | |
3259 | { | |
6785073b | 3260 | ifp->if_u1.if_extents = kmem_alloc(new_size, KM_NOFS); |
4eea22f0 MK |
3261 | memset(ifp->if_u1.if_extents, 0, new_size); |
3262 | if (ifp->if_bytes) { | |
3263 | memcpy(ifp->if_u1.if_extents, ifp->if_u2.if_inline_ext, | |
3264 | ifp->if_bytes); | |
3265 | memset(ifp->if_u2.if_inline_ext, 0, XFS_INLINE_EXTS * | |
3266 | sizeof(xfs_bmbt_rec_t)); | |
3267 | } | |
3268 | ifp->if_real_bytes = new_size; | |
3269 | } | |
3270 | ||
0293ce3a MK |
3271 | /* |
3272 | * Resize an extent indirection array to new_size bytes. | |
3273 | */ | |
d96f8f89 | 3274 | STATIC void |
0293ce3a MK |
3275 | xfs_iext_realloc_indirect( |
3276 | xfs_ifork_t *ifp, /* inode fork pointer */ | |
3277 | int new_size) /* new indirection array size */ | |
3278 | { | |
3279 | int nlists; /* number of irec's (ex lists) */ | |
3280 | int size; /* current indirection array size */ | |
3281 | ||
3282 | ASSERT(ifp->if_flags & XFS_IFEXTIREC); | |
3283 | nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ; | |
3284 | size = nlists * sizeof(xfs_ext_irec_t); | |
3285 | ASSERT(ifp->if_real_bytes); | |
3286 | ASSERT((new_size >= 0) && (new_size != size)); | |
3287 | if (new_size == 0) { | |
3288 | xfs_iext_destroy(ifp); | |
3289 | } else { | |
3290 | ifp->if_u1.if_ext_irec = (xfs_ext_irec_t *) | |
3291 | kmem_realloc(ifp->if_u1.if_ext_irec, | |
6785073b | 3292 | new_size, size, KM_NOFS); |
0293ce3a MK |
3293 | } |
3294 | } | |
3295 | ||
3296 | /* | |
3297 | * Switch from indirection array to linear (direct) extent allocations. | |
3298 | */ | |
d96f8f89 | 3299 | STATIC void |
0293ce3a MK |
3300 | xfs_iext_indirect_to_direct( |
3301 | xfs_ifork_t *ifp) /* inode fork pointer */ | |
3302 | { | |
a6f64d4a | 3303 | xfs_bmbt_rec_host_t *ep; /* extent record pointer */ |
0293ce3a MK |
3304 | xfs_extnum_t nextents; /* number of extents in file */ |
3305 | int size; /* size of file extents */ | |
3306 | ||
3307 | ASSERT(ifp->if_flags & XFS_IFEXTIREC); | |
3308 | nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); | |
3309 | ASSERT(nextents <= XFS_LINEAR_EXTS); | |
3310 | size = nextents * sizeof(xfs_bmbt_rec_t); | |
3311 | ||
71a8c87f | 3312 | xfs_iext_irec_compact_pages(ifp); |
0293ce3a MK |
3313 | ASSERT(ifp->if_real_bytes == XFS_IEXT_BUFSZ); |
3314 | ||
3315 | ep = ifp->if_u1.if_ext_irec->er_extbuf; | |
f0e2d93c | 3316 | kmem_free(ifp->if_u1.if_ext_irec); |
0293ce3a MK |
3317 | ifp->if_flags &= ~XFS_IFEXTIREC; |
3318 | ifp->if_u1.if_extents = ep; | |
3319 | ifp->if_bytes = size; | |
3320 | if (nextents < XFS_LINEAR_EXTS) { | |
3321 | xfs_iext_realloc_direct(ifp, size); | |
3322 | } | |
3323 | } | |
3324 | ||
4eea22f0 MK |
3325 | /* |
3326 | * Free incore file extents. | |
3327 | */ | |
3328 | void | |
3329 | xfs_iext_destroy( | |
3330 | xfs_ifork_t *ifp) /* inode fork pointer */ | |
3331 | { | |
0293ce3a MK |
3332 | if (ifp->if_flags & XFS_IFEXTIREC) { |
3333 | int erp_idx; | |
3334 | int nlists; | |
3335 | ||
3336 | nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ; | |
3337 | for (erp_idx = nlists - 1; erp_idx >= 0 ; erp_idx--) { | |
3338 | xfs_iext_irec_remove(ifp, erp_idx); | |
3339 | } | |
3340 | ifp->if_flags &= ~XFS_IFEXTIREC; | |
3341 | } else if (ifp->if_real_bytes) { | |
f0e2d93c | 3342 | kmem_free(ifp->if_u1.if_extents); |
4eea22f0 MK |
3343 | } else if (ifp->if_bytes) { |
3344 | memset(ifp->if_u2.if_inline_ext, 0, XFS_INLINE_EXTS * | |
3345 | sizeof(xfs_bmbt_rec_t)); | |
3346 | } | |
3347 | ifp->if_u1.if_extents = NULL; | |
3348 | ifp->if_real_bytes = 0; | |
3349 | ifp->if_bytes = 0; | |
3350 | } | |
0293ce3a | 3351 | |
8867bc9b MK |
3352 | /* |
3353 | * Return a pointer to the extent record for file system block bno. | |
3354 | */ | |
a6f64d4a | 3355 | xfs_bmbt_rec_host_t * /* pointer to found extent record */ |
8867bc9b MK |
3356 | xfs_iext_bno_to_ext( |
3357 | xfs_ifork_t *ifp, /* inode fork pointer */ | |
3358 | xfs_fileoff_t bno, /* block number to search for */ | |
3359 | xfs_extnum_t *idxp) /* index of target extent */ | |
3360 | { | |
a6f64d4a | 3361 | xfs_bmbt_rec_host_t *base; /* pointer to first extent */ |
8867bc9b | 3362 | xfs_filblks_t blockcount = 0; /* number of blocks in extent */ |
a6f64d4a | 3363 | xfs_bmbt_rec_host_t *ep = NULL; /* pointer to target extent */ |
8867bc9b | 3364 | xfs_ext_irec_t *erp = NULL; /* indirection array pointer */ |
c41564b5 | 3365 | int high; /* upper boundary in search */ |
8867bc9b | 3366 | xfs_extnum_t idx = 0; /* index of target extent */ |
c41564b5 | 3367 | int low; /* lower boundary in search */ |
8867bc9b MK |
3368 | xfs_extnum_t nextents; /* number of file extents */ |
3369 | xfs_fileoff_t startoff = 0; /* start offset of extent */ | |
3370 | ||
3371 | nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); | |
3372 | if (nextents == 0) { | |
3373 | *idxp = 0; | |
3374 | return NULL; | |
3375 | } | |
3376 | low = 0; | |
3377 | if (ifp->if_flags & XFS_IFEXTIREC) { | |
3378 | /* Find target extent list */ | |
3379 | int erp_idx = 0; | |
3380 | erp = xfs_iext_bno_to_irec(ifp, bno, &erp_idx); | |
3381 | base = erp->er_extbuf; | |
3382 | high = erp->er_extcount - 1; | |
3383 | } else { | |
3384 | base = ifp->if_u1.if_extents; | |
3385 | high = nextents - 1; | |
3386 | } | |
3387 | /* Binary search extent records */ | |
3388 | while (low <= high) { | |
3389 | idx = (low + high) >> 1; | |
3390 | ep = base + idx; | |
3391 | startoff = xfs_bmbt_get_startoff(ep); | |
3392 | blockcount = xfs_bmbt_get_blockcount(ep); | |
3393 | if (bno < startoff) { | |
3394 | high = idx - 1; | |
3395 | } else if (bno >= startoff + blockcount) { | |
3396 | low = idx + 1; | |
3397 | } else { | |
3398 | /* Convert back to file-based extent index */ | |
3399 | if (ifp->if_flags & XFS_IFEXTIREC) { | |
3400 | idx += erp->er_extoff; | |
3401 | } | |
3402 | *idxp = idx; | |
3403 | return ep; | |
3404 | } | |
3405 | } | |
3406 | /* Convert back to file-based extent index */ | |
3407 | if (ifp->if_flags & XFS_IFEXTIREC) { | |
3408 | idx += erp->er_extoff; | |
3409 | } | |
3410 | if (bno >= startoff + blockcount) { | |
3411 | if (++idx == nextents) { | |
3412 | ep = NULL; | |
3413 | } else { | |
3414 | ep = xfs_iext_get_ext(ifp, idx); | |
3415 | } | |
3416 | } | |
3417 | *idxp = idx; | |
3418 | return ep; | |
3419 | } | |
3420 | ||
0293ce3a MK |
3421 | /* |
3422 | * Return a pointer to the indirection array entry containing the | |
3423 | * extent record for filesystem block bno. Store the index of the | |
3424 | * target irec in *erp_idxp. | |
3425 | */ | |
8867bc9b | 3426 | xfs_ext_irec_t * /* pointer to found extent record */ |
0293ce3a MK |
3427 | xfs_iext_bno_to_irec( |
3428 | xfs_ifork_t *ifp, /* inode fork pointer */ | |
3429 | xfs_fileoff_t bno, /* block number to search for */ | |
3430 | int *erp_idxp) /* irec index of target ext list */ | |
3431 | { | |
3432 | xfs_ext_irec_t *erp = NULL; /* indirection array pointer */ | |
3433 | xfs_ext_irec_t *erp_next; /* next indirection array entry */ | |
8867bc9b | 3434 | int erp_idx; /* indirection array index */ |
0293ce3a MK |
3435 | int nlists; /* number of extent irec's (lists) */ |
3436 | int high; /* binary search upper limit */ | |
3437 | int low; /* binary search lower limit */ | |
3438 | ||
3439 | ASSERT(ifp->if_flags & XFS_IFEXTIREC); | |
3440 | nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ; | |
3441 | erp_idx = 0; | |
3442 | low = 0; | |
3443 | high = nlists - 1; | |
3444 | while (low <= high) { | |
3445 | erp_idx = (low + high) >> 1; | |
3446 | erp = &ifp->if_u1.if_ext_irec[erp_idx]; | |
3447 | erp_next = erp_idx < nlists - 1 ? erp + 1 : NULL; | |
3448 | if (bno < xfs_bmbt_get_startoff(erp->er_extbuf)) { | |
3449 | high = erp_idx - 1; | |
3450 | } else if (erp_next && bno >= | |
3451 | xfs_bmbt_get_startoff(erp_next->er_extbuf)) { | |
3452 | low = erp_idx + 1; | |
3453 | } else { | |
3454 | break; | |
3455 | } | |
3456 | } | |
3457 | *erp_idxp = erp_idx; | |
3458 | return erp; | |
3459 | } | |
3460 | ||
3461 | /* | |
3462 | * Return a pointer to the indirection array entry containing the | |
3463 | * extent record at file extent index *idxp. Store the index of the | |
3464 | * target irec in *erp_idxp and store the page index of the target | |
3465 | * extent record in *idxp. | |
3466 | */ | |
3467 | xfs_ext_irec_t * | |
3468 | xfs_iext_idx_to_irec( | |
3469 | xfs_ifork_t *ifp, /* inode fork pointer */ | |
3470 | xfs_extnum_t *idxp, /* extent index (file -> page) */ | |
3471 | int *erp_idxp, /* pointer to target irec */ | |
3472 | int realloc) /* new bytes were just added */ | |
3473 | { | |
3474 | xfs_ext_irec_t *prev; /* pointer to previous irec */ | |
3475 | xfs_ext_irec_t *erp = NULL; /* pointer to current irec */ | |
3476 | int erp_idx; /* indirection array index */ | |
3477 | int nlists; /* number of irec's (ex lists) */ | |
3478 | int high; /* binary search upper limit */ | |
3479 | int low; /* binary search lower limit */ | |
3480 | xfs_extnum_t page_idx = *idxp; /* extent index in target list */ | |
3481 | ||
3482 | ASSERT(ifp->if_flags & XFS_IFEXTIREC); | |
87bef181 CH |
3483 | ASSERT(page_idx >= 0); |
3484 | ASSERT(page_idx <= ifp->if_bytes / sizeof(xfs_bmbt_rec_t)); | |
3485 | ASSERT(page_idx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t) || realloc); | |
3486 | ||
0293ce3a MK |
3487 | nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ; |
3488 | erp_idx = 0; | |
3489 | low = 0; | |
3490 | high = nlists - 1; | |
3491 | ||
3492 | /* Binary search extent irec's */ | |
3493 | while (low <= high) { | |
3494 | erp_idx = (low + high) >> 1; | |
3495 | erp = &ifp->if_u1.if_ext_irec[erp_idx]; | |
3496 | prev = erp_idx > 0 ? erp - 1 : NULL; | |
3497 | if (page_idx < erp->er_extoff || (page_idx == erp->er_extoff && | |
3498 | realloc && prev && prev->er_extcount < XFS_LINEAR_EXTS)) { | |
3499 | high = erp_idx - 1; | |
3500 | } else if (page_idx > erp->er_extoff + erp->er_extcount || | |
3501 | (page_idx == erp->er_extoff + erp->er_extcount && | |
3502 | !realloc)) { | |
3503 | low = erp_idx + 1; | |
3504 | } else if (page_idx == erp->er_extoff + erp->er_extcount && | |
3505 | erp->er_extcount == XFS_LINEAR_EXTS) { | |
3506 | ASSERT(realloc); | |
3507 | page_idx = 0; | |
3508 | erp_idx++; | |
3509 | erp = erp_idx < nlists ? erp + 1 : NULL; | |
3510 | break; | |
3511 | } else { | |
3512 | page_idx -= erp->er_extoff; | |
3513 | break; | |
3514 | } | |
3515 | } | |
3516 | *idxp = page_idx; | |
3517 | *erp_idxp = erp_idx; | |
3518 | return(erp); | |
3519 | } | |
3520 | ||
3521 | /* | |
3522 | * Allocate and initialize an indirection array once the space needed | |
3523 | * for incore extents increases above XFS_IEXT_BUFSZ. | |
3524 | */ | |
3525 | void | |
3526 | xfs_iext_irec_init( | |
3527 | xfs_ifork_t *ifp) /* inode fork pointer */ | |
3528 | { | |
3529 | xfs_ext_irec_t *erp; /* indirection array pointer */ | |
3530 | xfs_extnum_t nextents; /* number of extents in file */ | |
3531 | ||
3532 | ASSERT(!(ifp->if_flags & XFS_IFEXTIREC)); | |
3533 | nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); | |
3534 | ASSERT(nextents <= XFS_LINEAR_EXTS); | |
3535 | ||
6785073b | 3536 | erp = kmem_alloc(sizeof(xfs_ext_irec_t), KM_NOFS); |
0293ce3a MK |
3537 | |
3538 | if (nextents == 0) { | |
6785073b | 3539 | ifp->if_u1.if_extents = kmem_alloc(XFS_IEXT_BUFSZ, KM_NOFS); |
0293ce3a MK |
3540 | } else if (!ifp->if_real_bytes) { |
3541 | xfs_iext_inline_to_direct(ifp, XFS_IEXT_BUFSZ); | |
3542 | } else if (ifp->if_real_bytes < XFS_IEXT_BUFSZ) { | |
3543 | xfs_iext_realloc_direct(ifp, XFS_IEXT_BUFSZ); | |
3544 | } | |
3545 | erp->er_extbuf = ifp->if_u1.if_extents; | |
3546 | erp->er_extcount = nextents; | |
3547 | erp->er_extoff = 0; | |
3548 | ||
3549 | ifp->if_flags |= XFS_IFEXTIREC; | |
3550 | ifp->if_real_bytes = XFS_IEXT_BUFSZ; | |
3551 | ifp->if_bytes = nextents * sizeof(xfs_bmbt_rec_t); | |
3552 | ifp->if_u1.if_ext_irec = erp; | |
3553 | ||
3554 | return; | |
3555 | } | |
3556 | ||
3557 | /* | |
3558 | * Allocate and initialize a new entry in the indirection array. | |
3559 | */ | |
3560 | xfs_ext_irec_t * | |
3561 | xfs_iext_irec_new( | |
3562 | xfs_ifork_t *ifp, /* inode fork pointer */ | |
3563 | int erp_idx) /* index for new irec */ | |
3564 | { | |
3565 | xfs_ext_irec_t *erp; /* indirection array pointer */ | |
3566 | int i; /* loop counter */ | |
3567 | int nlists; /* number of irec's (ex lists) */ | |
3568 | ||
3569 | ASSERT(ifp->if_flags & XFS_IFEXTIREC); | |
3570 | nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ; | |
3571 | ||
3572 | /* Resize indirection array */ | |
3573 | xfs_iext_realloc_indirect(ifp, ++nlists * | |
3574 | sizeof(xfs_ext_irec_t)); | |
3575 | /* | |
3576 | * Move records down in the array so the | |
3577 | * new page can use erp_idx. | |
3578 | */ | |
3579 | erp = ifp->if_u1.if_ext_irec; | |
3580 | for (i = nlists - 1; i > erp_idx; i--) { | |
3581 | memmove(&erp[i], &erp[i-1], sizeof(xfs_ext_irec_t)); | |
3582 | } | |
3583 | ASSERT(i == erp_idx); | |
3584 | ||
3585 | /* Initialize new extent record */ | |
3586 | erp = ifp->if_u1.if_ext_irec; | |
6785073b | 3587 | erp[erp_idx].er_extbuf = kmem_alloc(XFS_IEXT_BUFSZ, KM_NOFS); |
0293ce3a MK |
3588 | ifp->if_real_bytes = nlists * XFS_IEXT_BUFSZ; |
3589 | memset(erp[erp_idx].er_extbuf, 0, XFS_IEXT_BUFSZ); | |
3590 | erp[erp_idx].er_extcount = 0; | |
3591 | erp[erp_idx].er_extoff = erp_idx > 0 ? | |
3592 | erp[erp_idx-1].er_extoff + erp[erp_idx-1].er_extcount : 0; | |
3593 | return (&erp[erp_idx]); | |
3594 | } | |
3595 | ||
3596 | /* | |
3597 | * Remove a record from the indirection array. | |
3598 | */ | |
3599 | void | |
3600 | xfs_iext_irec_remove( | |
3601 | xfs_ifork_t *ifp, /* inode fork pointer */ | |
3602 | int erp_idx) /* irec index to remove */ | |
3603 | { | |
3604 | xfs_ext_irec_t *erp; /* indirection array pointer */ | |
3605 | int i; /* loop counter */ | |
3606 | int nlists; /* number of irec's (ex lists) */ | |
3607 | ||
3608 | ASSERT(ifp->if_flags & XFS_IFEXTIREC); | |
3609 | nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ; | |
3610 | erp = &ifp->if_u1.if_ext_irec[erp_idx]; | |
3611 | if (erp->er_extbuf) { | |
3612 | xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, | |
3613 | -erp->er_extcount); | |
f0e2d93c | 3614 | kmem_free(erp->er_extbuf); |
0293ce3a MK |
3615 | } |
3616 | /* Compact extent records */ | |
3617 | erp = ifp->if_u1.if_ext_irec; | |
3618 | for (i = erp_idx; i < nlists - 1; i++) { | |
3619 | memmove(&erp[i], &erp[i+1], sizeof(xfs_ext_irec_t)); | |
3620 | } | |
3621 | /* | |
3622 | * Manually free the last extent record from the indirection | |
3623 | * array. A call to xfs_iext_realloc_indirect() with a size | |
3624 | * of zero would result in a call to xfs_iext_destroy() which | |
3625 | * would in turn call this function again, creating a nasty | |
3626 | * infinite loop. | |
3627 | */ | |
3628 | if (--nlists) { | |
3629 | xfs_iext_realloc_indirect(ifp, | |
3630 | nlists * sizeof(xfs_ext_irec_t)); | |
3631 | } else { | |
f0e2d93c | 3632 | kmem_free(ifp->if_u1.if_ext_irec); |
0293ce3a MK |
3633 | } |
3634 | ifp->if_real_bytes = nlists * XFS_IEXT_BUFSZ; | |
3635 | } | |
3636 | ||
3637 | /* | |
3638 | * This is called to clean up large amounts of unused memory allocated | |
3639 | * by the indirection array. Before compacting anything though, verify | |
3640 | * that the indirection array is still needed and switch back to the | |
3641 | * linear extent list (or even the inline buffer) if possible. The | |
3642 | * compaction policy is as follows: | |
3643 | * | |
3644 | * Full Compaction: Extents fit into a single page (or inline buffer) | |
71a8c87f | 3645 | * Partial Compaction: Extents occupy less than 50% of allocated space |
0293ce3a MK |
3646 | * No Compaction: Extents occupy at least 50% of allocated space |
3647 | */ | |
3648 | void | |
3649 | xfs_iext_irec_compact( | |
3650 | xfs_ifork_t *ifp) /* inode fork pointer */ | |
3651 | { | |
3652 | xfs_extnum_t nextents; /* number of extents in file */ | |
3653 | int nlists; /* number of irec's (ex lists) */ | |
3654 | ||
3655 | ASSERT(ifp->if_flags & XFS_IFEXTIREC); | |
3656 | nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ; | |
3657 | nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); | |
3658 | ||
3659 | if (nextents == 0) { | |
3660 | xfs_iext_destroy(ifp); | |
3661 | } else if (nextents <= XFS_INLINE_EXTS) { | |
3662 | xfs_iext_indirect_to_direct(ifp); | |
3663 | xfs_iext_direct_to_inline(ifp, nextents); | |
3664 | } else if (nextents <= XFS_LINEAR_EXTS) { | |
3665 | xfs_iext_indirect_to_direct(ifp); | |
0293ce3a MK |
3666 | } else if (nextents < (nlists * XFS_LINEAR_EXTS) >> 1) { |
3667 | xfs_iext_irec_compact_pages(ifp); | |
3668 | } | |
3669 | } | |
3670 | ||
3671 | /* | |
3672 | * Combine extents from neighboring extent pages. | |
3673 | */ | |
3674 | void | |
3675 | xfs_iext_irec_compact_pages( | |
3676 | xfs_ifork_t *ifp) /* inode fork pointer */ | |
3677 | { | |
3678 | xfs_ext_irec_t *erp, *erp_next;/* pointers to irec entries */ | |
3679 | int erp_idx = 0; /* indirection array index */ | |
3680 | int nlists; /* number of irec's (ex lists) */ | |
3681 | ||
3682 | ASSERT(ifp->if_flags & XFS_IFEXTIREC); | |
3683 | nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ; | |
3684 | while (erp_idx < nlists - 1) { | |
3685 | erp = &ifp->if_u1.if_ext_irec[erp_idx]; | |
3686 | erp_next = erp + 1; | |
3687 | if (erp_next->er_extcount <= | |
3688 | (XFS_LINEAR_EXTS - erp->er_extcount)) { | |
71a8c87f | 3689 | memcpy(&erp->er_extbuf[erp->er_extcount], |
0293ce3a MK |
3690 | erp_next->er_extbuf, erp_next->er_extcount * |
3691 | sizeof(xfs_bmbt_rec_t)); | |
3692 | erp->er_extcount += erp_next->er_extcount; | |
3693 | /* | |
3694 | * Free page before removing extent record | |
3695 | * so er_extoffs don't get modified in | |
3696 | * xfs_iext_irec_remove. | |
3697 | */ | |
f0e2d93c | 3698 | kmem_free(erp_next->er_extbuf); |
0293ce3a MK |
3699 | erp_next->er_extbuf = NULL; |
3700 | xfs_iext_irec_remove(ifp, erp_idx + 1); | |
3701 | nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ; | |
3702 | } else { | |
3703 | erp_idx++; | |
3704 | } | |
3705 | } | |
3706 | } | |
3707 | ||
0293ce3a MK |
3708 | /* |
3709 | * This is called to update the er_extoff field in the indirection | |
3710 | * array when extents have been added or removed from one of the | |
3711 | * extent lists. erp_idx contains the irec index to begin updating | |
3712 | * at and ext_diff contains the number of extents that were added | |
3713 | * or removed. | |
3714 | */ | |
3715 | void | |
3716 | xfs_iext_irec_update_extoffs( | |
3717 | xfs_ifork_t *ifp, /* inode fork pointer */ | |
3718 | int erp_idx, /* irec index to update */ | |
3719 | int ext_diff) /* number of new extents */ | |
3720 | { | |
3721 | int i; /* loop counter */ | |
3722 | int nlists; /* number of irec's (ex lists */ | |
3723 | ||
3724 | ASSERT(ifp->if_flags & XFS_IFEXTIREC); | |
3725 | nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ; | |
3726 | for (i = erp_idx; i < nlists; i++) { | |
3727 | ifp->if_u1.if_ext_irec[i].er_extoff += ext_diff; | |
3728 | } | |
3729 | } |