blob: a550546a70832dc727dd117ef1c4bbc3f0ef5e90 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
Nathan Scott7b718762005-11-02 14:58:39 +11003 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Robert P. J. Day40ebd812007-11-23 16:30:51 +110018#include <linux/log2.h>
19
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110021#include "xfs_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "xfs_types.h"
Nathan Scotta844f452005-11-02 14:38:42 +110023#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "xfs_log.h"
Nathan Scotta844f452005-11-02 14:38:42 +110025#include "xfs_inum.h"
26#include "xfs_imap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "xfs_trans.h"
28#include "xfs_trans_priv.h"
29#include "xfs_sb.h"
30#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include "xfs_dir2.h"
32#include "xfs_dmapi.h"
33#include "xfs_mount.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "xfs_bmap_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110035#include "xfs_alloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include "xfs_ialloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include "xfs_dir2_sf.h"
Nathan Scotta844f452005-11-02 14:38:42 +110038#include "xfs_attr_sf.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include "xfs_dinode.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include "xfs_inode.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include "xfs_buf_item.h"
Nathan Scotta844f452005-11-02 14:38:42 +110042#include "xfs_inode_item.h"
43#include "xfs_btree.h"
44#include "xfs_alloc.h"
45#include "xfs_ialloc.h"
46#include "xfs_bmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include "xfs_rw.h"
48#include "xfs_error.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include "xfs_utils.h"
50#include "xfs_dir2_trace.h"
51#include "xfs_quota.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include "xfs_acl.h"
David Chinner2a82b8b2007-07-11 11:09:12 +100053#include "xfs_filestream.h"
Christoph Hellwig739bfb22007-08-29 10:58:01 +100054#include "xfs_vnodeops.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Linus Torvalds1da177e2005-04-16 15:20:36 -070056kmem_zone_t *xfs_ifork_zone;
57kmem_zone_t *xfs_inode_zone;
David Chinnerda353b02007-08-28 14:00:13 +100058kmem_zone_t *xfs_icluster_zone;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
60/*
61 * Used in xfs_itruncate(). This is the maximum number of extents
62 * freed from a file in a single transaction.
63 */
64#define XFS_ITRUNC_MAX_EXTENTS 2
65
66STATIC int xfs_iflush_int(xfs_inode_t *, xfs_buf_t *);
67STATIC int xfs_iformat_local(xfs_inode_t *, xfs_dinode_t *, int, int);
68STATIC int xfs_iformat_extents(xfs_inode_t *, xfs_dinode_t *, int);
69STATIC int xfs_iformat_btree(xfs_inode_t *, xfs_dinode_t *, int);
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#ifdef DEBUG
72/*
73 * Make sure that the extents in the given memory buffer
74 * are valid.
75 */
76STATIC void
77xfs_validate_extents(
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +110078 xfs_ifork_t *ifp,
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 int nrecs,
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 xfs_exntfmt_t fmt)
81{
82 xfs_bmbt_irec_t irec;
Christoph Hellwiga6f64d42007-08-16 16:23:40 +100083 xfs_bmbt_rec_host_t rec;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 int i;
85
86 for (i = 0; i < nrecs; i++) {
Christoph Hellwiga6f64d42007-08-16 16:23:40 +100087 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, i);
88 rec.l0 = get_unaligned(&ep->l0);
89 rec.l1 = get_unaligned(&ep->l1);
90 xfs_bmbt_get_all(&rec, &irec);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 if (fmt == XFS_EXTFMT_NOSTATE)
92 ASSERT(irec.br_state == XFS_EXT_NORM);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 }
94}
95#else /* DEBUG */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +100096#define xfs_validate_extents(ifp, nrecs, fmt)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097#endif /* DEBUG */
98
99/*
100 * Check that none of the inode's in the buffer have a next
101 * unlinked field of 0.
102 */
103#if defined(DEBUG)
104void
105xfs_inobp_check(
106 xfs_mount_t *mp,
107 xfs_buf_t *bp)
108{
109 int i;
110 int j;
111 xfs_dinode_t *dip;
112
113 j = mp->m_inode_cluster_size >> mp->m_sb.sb_inodelog;
114
115 for (i = 0; i < j; i++) {
116 dip = (xfs_dinode_t *)xfs_buf_offset(bp,
117 i * mp->m_sb.sb_inodesize);
118 if (!dip->di_next_unlinked) {
119 xfs_fs_cmn_err(CE_ALERT, mp,
120 "Detected a bogus zero next_unlinked field in incore inode buffer 0x%p. About to pop an ASSERT.",
121 bp);
122 ASSERT(dip->di_next_unlinked);
123 }
124 }
125}
126#endif
127
128/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 * This routine is called to map an inode number within a file
130 * system to the buffer containing the on-disk version of the
131 * inode. It returns a pointer to the buffer containing the
132 * on-disk inode in the bpp parameter, and in the dip parameter
133 * it returns a pointer to the on-disk inode within that buffer.
134 *
135 * If a non-zero error is returned, then the contents of bpp and
136 * dipp are undefined.
137 *
138 * Use xfs_imap() to determine the size and location of the
139 * buffer to read from disk.
140 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000141STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142xfs_inotobp(
143 xfs_mount_t *mp,
144 xfs_trans_t *tp,
145 xfs_ino_t ino,
146 xfs_dinode_t **dipp,
147 xfs_buf_t **bpp,
148 int *offset)
149{
150 int di_ok;
151 xfs_imap_t imap;
152 xfs_buf_t *bp;
153 int error;
154 xfs_dinode_t *dip;
155
156 /*
Nathan Scottc41564b2006-03-29 08:55:14 +1000157 * Call the space management code to find the location of the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 * inode on disk.
159 */
160 imap.im_blkno = 0;
161 error = xfs_imap(mp, tp, ino, &imap, XFS_IMAP_LOOKUP);
162 if (error != 0) {
163 cmn_err(CE_WARN,
164 "xfs_inotobp: xfs_imap() returned an "
165 "error %d on %s. Returning error.", error, mp->m_fsname);
166 return error;
167 }
168
169 /*
170 * If the inode number maps to a block outside the bounds of the
171 * file system then return NULL rather than calling read_buf
172 * and panicing when we get an error from the driver.
173 */
174 if ((imap.im_blkno + imap.im_len) >
175 XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks)) {
176 cmn_err(CE_WARN,
Christoph Hellwigda1650a2005-11-02 10:21:35 +1100177 "xfs_inotobp: inode number (%llu + %d) maps to a block outside the bounds "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 "of the file system %s. Returning EINVAL.",
Christoph Hellwigda1650a2005-11-02 10:21:35 +1100179 (unsigned long long)imap.im_blkno,
180 imap.im_len, mp->m_fsname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 return XFS_ERROR(EINVAL);
182 }
183
184 /*
185 * Read in the buffer. If tp is NULL, xfs_trans_read_buf() will
186 * default to just a read_buf() call.
187 */
188 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, imap.im_blkno,
189 (int)imap.im_len, XFS_BUF_LOCK, &bp);
190
191 if (error) {
192 cmn_err(CE_WARN,
193 "xfs_inotobp: xfs_trans_read_buf() returned an "
194 "error %d on %s. Returning error.", error, mp->m_fsname);
195 return error;
196 }
197 dip = (xfs_dinode_t *)xfs_buf_offset(bp, 0);
198 di_ok =
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000199 be16_to_cpu(dip->di_core.di_magic) == XFS_DINODE_MAGIC &&
200 XFS_DINODE_GOOD_VERSION(dip->di_core.di_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 if (unlikely(XFS_TEST_ERROR(!di_ok, mp, XFS_ERRTAG_ITOBP_INOTOBP,
202 XFS_RANDOM_ITOBP_INOTOBP))) {
203 XFS_CORRUPTION_ERROR("xfs_inotobp", XFS_ERRLEVEL_LOW, mp, dip);
204 xfs_trans_brelse(tp, bp);
205 cmn_err(CE_WARN,
206 "xfs_inotobp: XFS_TEST_ERROR() returned an "
207 "error on %s. Returning EFSCORRUPTED.", mp->m_fsname);
208 return XFS_ERROR(EFSCORRUPTED);
209 }
210
211 xfs_inobp_check(mp, bp);
212
213 /*
214 * Set *dipp to point to the on-disk inode in the buffer.
215 */
216 *dipp = (xfs_dinode_t *)xfs_buf_offset(bp, imap.im_boffset);
217 *bpp = bp;
218 *offset = imap.im_boffset;
219 return 0;
220}
221
222
223/*
224 * This routine is called to map an inode to the buffer containing
225 * the on-disk version of the inode. It returns a pointer to the
226 * buffer containing the on-disk inode in the bpp parameter, and in
227 * the dip parameter it returns a pointer to the on-disk inode within
228 * that buffer.
229 *
230 * If a non-zero error is returned, then the contents of bpp and
231 * dipp are undefined.
232 *
233 * If the inode is new and has not yet been initialized, use xfs_imap()
234 * to determine the size and location of the buffer to read from disk.
235 * If the inode has already been mapped to its buffer and read in once,
236 * then use the mapping information stored in the inode rather than
237 * calling xfs_imap(). This allows us to avoid the overhead of looking
238 * at the inode btree for small block file systems (see xfs_dilocate()).
239 * We can tell whether the inode has been mapped in before by comparing
240 * its disk block address to 0. Only uninitialized inodes will have
241 * 0 for the disk block address.
242 */
243int
244xfs_itobp(
245 xfs_mount_t *mp,
246 xfs_trans_t *tp,
247 xfs_inode_t *ip,
248 xfs_dinode_t **dipp,
249 xfs_buf_t **bpp,
Nathan Scottb12dd342006-03-17 17:26:04 +1100250 xfs_daddr_t bno,
251 uint imap_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252{
Nathan Scott4d1a2ed2006-06-09 17:12:28 +1000253 xfs_imap_t imap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 xfs_buf_t *bp;
255 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 int i;
257 int ni;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
259 if (ip->i_blkno == (xfs_daddr_t)0) {
260 /*
261 * Call the space management code to find the location of the
262 * inode on disk.
263 */
264 imap.im_blkno = bno;
Nathan Scottb12dd342006-03-17 17:26:04 +1100265 if ((error = xfs_imap(mp, tp, ip->i_ino, &imap,
266 XFS_IMAP_LOOKUP | imap_flags)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
269 /*
270 * If the inode number maps to a block outside the bounds
271 * of the file system then return NULL rather than calling
272 * read_buf and panicing when we get an error from the
273 * driver.
274 */
275 if ((imap.im_blkno + imap.im_len) >
276 XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks)) {
277#ifdef DEBUG
278 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_itobp: "
279 "(imap.im_blkno (0x%llx) "
280 "+ imap.im_len (0x%llx)) > "
281 " XFS_FSB_TO_BB(mp, "
282 "mp->m_sb.sb_dblocks) (0x%llx)",
283 (unsigned long long) imap.im_blkno,
284 (unsigned long long) imap.im_len,
285 XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks));
286#endif /* DEBUG */
287 return XFS_ERROR(EINVAL);
288 }
289
290 /*
291 * Fill in the fields in the inode that will be used to
292 * map the inode to its buffer from now on.
293 */
294 ip->i_blkno = imap.im_blkno;
295 ip->i_len = imap.im_len;
296 ip->i_boffset = imap.im_boffset;
297 } else {
298 /*
299 * We've already mapped the inode once, so just use the
300 * mapping that we saved the first time.
301 */
302 imap.im_blkno = ip->i_blkno;
303 imap.im_len = ip->i_len;
304 imap.im_boffset = ip->i_boffset;
305 }
306 ASSERT(bno == 0 || bno == imap.im_blkno);
307
308 /*
309 * Read in the buffer. If tp is NULL, xfs_trans_read_buf() will
310 * default to just a read_buf() call.
311 */
312 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, imap.im_blkno,
313 (int)imap.im_len, XFS_BUF_LOCK, &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 if (error) {
315#ifdef DEBUG
316 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_itobp: "
317 "xfs_trans_read_buf() returned error %d, "
318 "imap.im_blkno 0x%llx, imap.im_len 0x%llx",
319 error, (unsigned long long) imap.im_blkno,
320 (unsigned long long) imap.im_len);
321#endif /* DEBUG */
322 return error;
323 }
Nathan Scott4d1a2ed2006-06-09 17:12:28 +1000324
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 /*
326 * Validate the magic number and version of every inode in the buffer
327 * (if DEBUG kernel) or the first inode in the buffer, otherwise.
Nathan Scott4d1a2ed2006-06-09 17:12:28 +1000328 * No validation is done here in userspace (xfs_repair).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 */
Nathan Scott4d1a2ed2006-06-09 17:12:28 +1000330#if !defined(__KERNEL__)
331 ni = 0;
332#elif defined(DEBUG)
Nathan Scott41ff7152006-07-28 17:05:51 +1000333 ni = BBTOB(imap.im_len) >> mp->m_sb.sb_inodelog;
Nathan Scott4d1a2ed2006-06-09 17:12:28 +1000334#else /* usual case */
Nathan Scott41ff7152006-07-28 17:05:51 +1000335 ni = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336#endif
Nathan Scott4d1a2ed2006-06-09 17:12:28 +1000337
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 for (i = 0; i < ni; i++) {
339 int di_ok;
340 xfs_dinode_t *dip;
341
342 dip = (xfs_dinode_t *)xfs_buf_offset(bp,
343 (i << mp->m_sb.sb_inodelog));
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000344 di_ok = be16_to_cpu(dip->di_core.di_magic) == XFS_DINODE_MAGIC &&
345 XFS_DINODE_GOOD_VERSION(dip->di_core.di_version);
Nathan Scott41ff7152006-07-28 17:05:51 +1000346 if (unlikely(XFS_TEST_ERROR(!di_ok, mp,
347 XFS_ERRTAG_ITOBP_INOTOBP,
348 XFS_RANDOM_ITOBP_INOTOBP))) {
349 if (imap_flags & XFS_IMAP_BULKSTAT) {
350 xfs_trans_brelse(tp, bp);
351 return XFS_ERROR(EINVAL);
352 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353#ifdef DEBUG
Nathan Scott41ff7152006-07-28 17:05:51 +1000354 cmn_err(CE_ALERT,
Nathan Scott4d1a2ed2006-06-09 17:12:28 +1000355 "Device %s - bad inode magic/vsn "
356 "daddr %lld #%d (magic=%x)",
Nathan Scottb6574522006-06-09 15:29:40 +1000357 XFS_BUFTARG_NAME(mp->m_ddev_targp),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 (unsigned long long)imap.im_blkno, i,
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000359 be16_to_cpu(dip->di_core.di_magic));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360#endif
361 XFS_CORRUPTION_ERROR("xfs_itobp", XFS_ERRLEVEL_HIGH,
362 mp, dip);
363 xfs_trans_brelse(tp, bp);
364 return XFS_ERROR(EFSCORRUPTED);
365 }
366 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
368 xfs_inobp_check(mp, bp);
369
370 /*
371 * Mark the buffer as an inode buffer now that it looks good
372 */
373 XFS_BUF_SET_VTYPE(bp, B_FS_INO);
374
375 /*
376 * Set *dipp to point to the on-disk inode in the buffer.
377 */
378 *dipp = (xfs_dinode_t *)xfs_buf_offset(bp, imap.im_boffset);
379 *bpp = bp;
380 return 0;
381}
382
383/*
384 * Move inode type and inode format specific information from the
385 * on-disk inode to the in-core inode. For fifos, devs, and sockets
386 * this means set if_rdev to the proper value. For files, directories,
387 * and symlinks this means to bring in the in-line data or extent
388 * pointers. For a file in B-tree format, only the root is immediately
389 * brought in-core. The rest will be in-lined in if_extents when it
390 * is first referenced (see xfs_iread_extents()).
391 */
392STATIC int
393xfs_iformat(
394 xfs_inode_t *ip,
395 xfs_dinode_t *dip)
396{
397 xfs_attr_shortform_t *atp;
398 int size;
399 int error;
400 xfs_fsize_t di_size;
401 ip->i_df.if_ext_max =
402 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
403 error = 0;
404
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000405 if (unlikely(be32_to_cpu(dip->di_core.di_nextents) +
406 be16_to_cpu(dip->di_core.di_anextents) >
407 be64_to_cpu(dip->di_core.di_nblocks))) {
Nathan Scott3762ec62006-01-12 10:29:53 +1100408 xfs_fs_repair_cmn_err(CE_WARN, ip->i_mount,
409 "corrupt dinode %Lu, extent total = %d, nblocks = %Lu.",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 (unsigned long long)ip->i_ino,
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000411 (int)(be32_to_cpu(dip->di_core.di_nextents) +
412 be16_to_cpu(dip->di_core.di_anextents)),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 (unsigned long long)
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000414 be64_to_cpu(dip->di_core.di_nblocks));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 XFS_CORRUPTION_ERROR("xfs_iformat(1)", XFS_ERRLEVEL_LOW,
416 ip->i_mount, dip);
417 return XFS_ERROR(EFSCORRUPTED);
418 }
419
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000420 if (unlikely(dip->di_core.di_forkoff > ip->i_mount->m_sb.sb_inodesize)) {
Nathan Scott3762ec62006-01-12 10:29:53 +1100421 xfs_fs_repair_cmn_err(CE_WARN, ip->i_mount,
422 "corrupt dinode %Lu, forkoff = 0x%x.",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 (unsigned long long)ip->i_ino,
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000424 dip->di_core.di_forkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 XFS_CORRUPTION_ERROR("xfs_iformat(2)", XFS_ERRLEVEL_LOW,
426 ip->i_mount, dip);
427 return XFS_ERROR(EFSCORRUPTED);
428 }
429
430 switch (ip->i_d.di_mode & S_IFMT) {
431 case S_IFIFO:
432 case S_IFCHR:
433 case S_IFBLK:
434 case S_IFSOCK:
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000435 if (unlikely(dip->di_core.di_format != XFS_DINODE_FMT_DEV)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 XFS_CORRUPTION_ERROR("xfs_iformat(3)", XFS_ERRLEVEL_LOW,
437 ip->i_mount, dip);
438 return XFS_ERROR(EFSCORRUPTED);
439 }
440 ip->i_d.di_size = 0;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000441 ip->i_size = 0;
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000442 ip->i_df.if_u2.if_rdev = be32_to_cpu(dip->di_u.di_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 break;
444
445 case S_IFREG:
446 case S_IFLNK:
447 case S_IFDIR:
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000448 switch (dip->di_core.di_format) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 case XFS_DINODE_FMT_LOCAL:
450 /*
451 * no local regular files yet
452 */
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000453 if (unlikely((be16_to_cpu(dip->di_core.di_mode) & S_IFMT) == S_IFREG)) {
Nathan Scott3762ec62006-01-12 10:29:53 +1100454 xfs_fs_repair_cmn_err(CE_WARN, ip->i_mount,
455 "corrupt inode %Lu "
456 "(local format for regular file).",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 (unsigned long long) ip->i_ino);
458 XFS_CORRUPTION_ERROR("xfs_iformat(4)",
459 XFS_ERRLEVEL_LOW,
460 ip->i_mount, dip);
461 return XFS_ERROR(EFSCORRUPTED);
462 }
463
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000464 di_size = be64_to_cpu(dip->di_core.di_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 if (unlikely(di_size > XFS_DFORK_DSIZE(dip, ip->i_mount))) {
Nathan Scott3762ec62006-01-12 10:29:53 +1100466 xfs_fs_repair_cmn_err(CE_WARN, ip->i_mount,
467 "corrupt inode %Lu "
468 "(bad size %Ld for local inode).",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 (unsigned long long) ip->i_ino,
470 (long long) di_size);
471 XFS_CORRUPTION_ERROR("xfs_iformat(5)",
472 XFS_ERRLEVEL_LOW,
473 ip->i_mount, dip);
474 return XFS_ERROR(EFSCORRUPTED);
475 }
476
477 size = (int)di_size;
478 error = xfs_iformat_local(ip, dip, XFS_DATA_FORK, size);
479 break;
480 case XFS_DINODE_FMT_EXTENTS:
481 error = xfs_iformat_extents(ip, dip, XFS_DATA_FORK);
482 break;
483 case XFS_DINODE_FMT_BTREE:
484 error = xfs_iformat_btree(ip, dip, XFS_DATA_FORK);
485 break;
486 default:
487 XFS_ERROR_REPORT("xfs_iformat(6)", XFS_ERRLEVEL_LOW,
488 ip->i_mount);
489 return XFS_ERROR(EFSCORRUPTED);
490 }
491 break;
492
493 default:
494 XFS_ERROR_REPORT("xfs_iformat(7)", XFS_ERRLEVEL_LOW, ip->i_mount);
495 return XFS_ERROR(EFSCORRUPTED);
496 }
497 if (error) {
498 return error;
499 }
500 if (!XFS_DFORK_Q(dip))
501 return 0;
502 ASSERT(ip->i_afp == NULL);
503 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP);
504 ip->i_afp->if_ext_max =
505 XFS_IFORK_ASIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000506 switch (dip->di_core.di_aformat) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 case XFS_DINODE_FMT_LOCAL:
508 atp = (xfs_attr_shortform_t *)XFS_DFORK_APTR(dip);
Nathan Scott3b244aa2006-03-17 17:29:25 +1100509 size = be16_to_cpu(atp->hdr.totsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 error = xfs_iformat_local(ip, dip, XFS_ATTR_FORK, size);
511 break;
512 case XFS_DINODE_FMT_EXTENTS:
513 error = xfs_iformat_extents(ip, dip, XFS_ATTR_FORK);
514 break;
515 case XFS_DINODE_FMT_BTREE:
516 error = xfs_iformat_btree(ip, dip, XFS_ATTR_FORK);
517 break;
518 default:
519 error = XFS_ERROR(EFSCORRUPTED);
520 break;
521 }
522 if (error) {
523 kmem_zone_free(xfs_ifork_zone, ip->i_afp);
524 ip->i_afp = NULL;
525 xfs_idestroy_fork(ip, XFS_DATA_FORK);
526 }
527 return error;
528}
529
530/*
531 * The file is in-lined in the on-disk inode.
532 * If it fits into if_inline_data, then copy
533 * it there, otherwise allocate a buffer for it
534 * and copy the data there. Either way, set
535 * if_data to point at the data.
536 * If we allocate a buffer for the data, make
537 * sure that its size is a multiple of 4 and
538 * record the real size in i_real_bytes.
539 */
540STATIC int
541xfs_iformat_local(
542 xfs_inode_t *ip,
543 xfs_dinode_t *dip,
544 int whichfork,
545 int size)
546{
547 xfs_ifork_t *ifp;
548 int real_size;
549
550 /*
551 * If the size is unreasonable, then something
552 * is wrong and we just bail out rather than crash in
553 * kmem_alloc() or memcpy() below.
554 */
555 if (unlikely(size > XFS_DFORK_SIZE(dip, ip->i_mount, whichfork))) {
Nathan Scott3762ec62006-01-12 10:29:53 +1100556 xfs_fs_repair_cmn_err(CE_WARN, ip->i_mount,
557 "corrupt inode %Lu "
558 "(bad size %d for local fork, size = %d).",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 (unsigned long long) ip->i_ino, size,
560 XFS_DFORK_SIZE(dip, ip->i_mount, whichfork));
561 XFS_CORRUPTION_ERROR("xfs_iformat_local", XFS_ERRLEVEL_LOW,
562 ip->i_mount, dip);
563 return XFS_ERROR(EFSCORRUPTED);
564 }
565 ifp = XFS_IFORK_PTR(ip, whichfork);
566 real_size = 0;
567 if (size == 0)
568 ifp->if_u1.if_data = NULL;
569 else if (size <= sizeof(ifp->if_u2.if_inline_data))
570 ifp->if_u1.if_data = ifp->if_u2.if_inline_data;
571 else {
572 real_size = roundup(size, 4);
573 ifp->if_u1.if_data = kmem_alloc(real_size, KM_SLEEP);
574 }
575 ifp->if_bytes = size;
576 ifp->if_real_bytes = real_size;
577 if (size)
578 memcpy(ifp->if_u1.if_data, XFS_DFORK_PTR(dip, whichfork), size);
579 ifp->if_flags &= ~XFS_IFEXTENTS;
580 ifp->if_flags |= XFS_IFINLINE;
581 return 0;
582}
583
584/*
585 * The file consists of a set of extents all
586 * of which fit into the on-disk inode.
587 * If there are few enough extents to fit into
588 * the if_inline_ext, then copy them there.
589 * Otherwise allocate a buffer for them and copy
590 * them into it. Either way, set if_extents
591 * to point at the extents.
592 */
593STATIC int
594xfs_iformat_extents(
595 xfs_inode_t *ip,
596 xfs_dinode_t *dip,
597 int whichfork)
598{
Christoph Hellwiga6f64d42007-08-16 16:23:40 +1000599 xfs_bmbt_rec_t *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 xfs_ifork_t *ifp;
601 int nex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 int size;
603 int i;
604
605 ifp = XFS_IFORK_PTR(ip, whichfork);
606 nex = XFS_DFORK_NEXTENTS(dip, whichfork);
607 size = nex * (uint)sizeof(xfs_bmbt_rec_t);
608
609 /*
610 * If the number of extents is unreasonable, then something
611 * is wrong and we just bail out rather than crash in
612 * kmem_alloc() or memcpy() below.
613 */
614 if (unlikely(size < 0 || size > XFS_DFORK_SIZE(dip, ip->i_mount, whichfork))) {
Nathan Scott3762ec62006-01-12 10:29:53 +1100615 xfs_fs_repair_cmn_err(CE_WARN, ip->i_mount,
616 "corrupt inode %Lu ((a)extents = %d).",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 (unsigned long long) ip->i_ino, nex);
618 XFS_CORRUPTION_ERROR("xfs_iformat_extents(1)", XFS_ERRLEVEL_LOW,
619 ip->i_mount, dip);
620 return XFS_ERROR(EFSCORRUPTED);
621 }
622
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100623 ifp->if_real_bytes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 if (nex == 0)
625 ifp->if_u1.if_extents = NULL;
626 else if (nex <= XFS_INLINE_EXTS)
627 ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100628 else
629 xfs_iext_add(ifp, 0, nex);
630
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 ifp->if_bytes = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 if (size) {
633 dp = (xfs_bmbt_rec_t *) XFS_DFORK_PTR(dip, whichfork);
Christoph Hellwiga6f64d42007-08-16 16:23:40 +1000634 xfs_validate_extents(ifp, nex, XFS_EXTFMT_INODE(ip));
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100635 for (i = 0; i < nex; i++, dp++) {
Christoph Hellwiga6f64d42007-08-16 16:23:40 +1000636 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, i);
Christoph Hellwigcd8b0a92007-08-16 16:24:15 +1000637 ep->l0 = be64_to_cpu(get_unaligned(&dp->l0));
638 ep->l1 = be64_to_cpu(get_unaligned(&dp->l1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 }
Eric Sandeen3a59c942007-07-11 11:09:47 +1000640 XFS_BMAP_TRACE_EXLIST(ip, nex, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 if (whichfork != XFS_DATA_FORK ||
642 XFS_EXTFMT_INODE(ip) == XFS_EXTFMT_NOSTATE)
643 if (unlikely(xfs_check_nostate_extents(
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100644 ifp, 0, nex))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 XFS_ERROR_REPORT("xfs_iformat_extents(2)",
646 XFS_ERRLEVEL_LOW,
647 ip->i_mount);
648 return XFS_ERROR(EFSCORRUPTED);
649 }
650 }
651 ifp->if_flags |= XFS_IFEXTENTS;
652 return 0;
653}
654
655/*
656 * The file has too many extents to fit into
657 * the inode, so they are in B-tree format.
658 * Allocate a buffer for the root of the B-tree
659 * and copy the root into it. The i_extents
660 * field will remain NULL until all of the
661 * extents are read in (when they are needed).
662 */
663STATIC int
664xfs_iformat_btree(
665 xfs_inode_t *ip,
666 xfs_dinode_t *dip,
667 int whichfork)
668{
669 xfs_bmdr_block_t *dfp;
670 xfs_ifork_t *ifp;
671 /* REFERENCED */
672 int nrecs;
673 int size;
674
675 ifp = XFS_IFORK_PTR(ip, whichfork);
676 dfp = (xfs_bmdr_block_t *)XFS_DFORK_PTR(dip, whichfork);
677 size = XFS_BMAP_BROOT_SPACE(dfp);
678 nrecs = XFS_BMAP_BROOT_NUMRECS(dfp);
679
680 /*
681 * blow out if -- fork has less extents than can fit in
682 * fork (fork shouldn't be a btree format), root btree
683 * block has more records than can fit into the fork,
684 * or the number of extents is greater than the number of
685 * blocks.
686 */
687 if (unlikely(XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max
688 || XFS_BMDR_SPACE_CALC(nrecs) >
689 XFS_DFORK_SIZE(dip, ip->i_mount, whichfork)
690 || XFS_IFORK_NEXTENTS(ip, whichfork) > ip->i_d.di_nblocks)) {
Nathan Scott3762ec62006-01-12 10:29:53 +1100691 xfs_fs_repair_cmn_err(CE_WARN, ip->i_mount,
692 "corrupt inode %Lu (btree).",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 (unsigned long long) ip->i_ino);
694 XFS_ERROR_REPORT("xfs_iformat_btree", XFS_ERRLEVEL_LOW,
695 ip->i_mount);
696 return XFS_ERROR(EFSCORRUPTED);
697 }
698
699 ifp->if_broot_bytes = size;
700 ifp->if_broot = kmem_alloc(size, KM_SLEEP);
701 ASSERT(ifp->if_broot != NULL);
702 /*
703 * Copy and convert from the on-disk structure
704 * to the in-memory structure.
705 */
706 xfs_bmdr_to_bmbt(dfp, XFS_DFORK_SIZE(dip, ip->i_mount, whichfork),
707 ifp->if_broot, size);
708 ifp->if_flags &= ~XFS_IFEXTENTS;
709 ifp->if_flags |= XFS_IFBROOT;
710
711 return 0;
712}
713
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714void
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000715xfs_dinode_from_disk(
716 xfs_icdinode_t *to,
717 xfs_dinode_core_t *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718{
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000719 to->di_magic = be16_to_cpu(from->di_magic);
720 to->di_mode = be16_to_cpu(from->di_mode);
721 to->di_version = from ->di_version;
722 to->di_format = from->di_format;
723 to->di_onlink = be16_to_cpu(from->di_onlink);
724 to->di_uid = be32_to_cpu(from->di_uid);
725 to->di_gid = be32_to_cpu(from->di_gid);
726 to->di_nlink = be32_to_cpu(from->di_nlink);
727 to->di_projid = be16_to_cpu(from->di_projid);
728 memcpy(to->di_pad, from->di_pad, sizeof(to->di_pad));
729 to->di_flushiter = be16_to_cpu(from->di_flushiter);
730 to->di_atime.t_sec = be32_to_cpu(from->di_atime.t_sec);
731 to->di_atime.t_nsec = be32_to_cpu(from->di_atime.t_nsec);
732 to->di_mtime.t_sec = be32_to_cpu(from->di_mtime.t_sec);
733 to->di_mtime.t_nsec = be32_to_cpu(from->di_mtime.t_nsec);
734 to->di_ctime.t_sec = be32_to_cpu(from->di_ctime.t_sec);
735 to->di_ctime.t_nsec = be32_to_cpu(from->di_ctime.t_nsec);
736 to->di_size = be64_to_cpu(from->di_size);
737 to->di_nblocks = be64_to_cpu(from->di_nblocks);
738 to->di_extsize = be32_to_cpu(from->di_extsize);
739 to->di_nextents = be32_to_cpu(from->di_nextents);
740 to->di_anextents = be16_to_cpu(from->di_anextents);
741 to->di_forkoff = from->di_forkoff;
742 to->di_aformat = from->di_aformat;
743 to->di_dmevmask = be32_to_cpu(from->di_dmevmask);
744 to->di_dmstate = be16_to_cpu(from->di_dmstate);
745 to->di_flags = be16_to_cpu(from->di_flags);
746 to->di_gen = be32_to_cpu(from->di_gen);
747}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000749void
750xfs_dinode_to_disk(
751 xfs_dinode_core_t *to,
752 xfs_icdinode_t *from)
753{
754 to->di_magic = cpu_to_be16(from->di_magic);
755 to->di_mode = cpu_to_be16(from->di_mode);
756 to->di_version = from ->di_version;
757 to->di_format = from->di_format;
758 to->di_onlink = cpu_to_be16(from->di_onlink);
759 to->di_uid = cpu_to_be32(from->di_uid);
760 to->di_gid = cpu_to_be32(from->di_gid);
761 to->di_nlink = cpu_to_be32(from->di_nlink);
762 to->di_projid = cpu_to_be16(from->di_projid);
763 memcpy(to->di_pad, from->di_pad, sizeof(to->di_pad));
764 to->di_flushiter = cpu_to_be16(from->di_flushiter);
765 to->di_atime.t_sec = cpu_to_be32(from->di_atime.t_sec);
766 to->di_atime.t_nsec = cpu_to_be32(from->di_atime.t_nsec);
767 to->di_mtime.t_sec = cpu_to_be32(from->di_mtime.t_sec);
768 to->di_mtime.t_nsec = cpu_to_be32(from->di_mtime.t_nsec);
769 to->di_ctime.t_sec = cpu_to_be32(from->di_ctime.t_sec);
770 to->di_ctime.t_nsec = cpu_to_be32(from->di_ctime.t_nsec);
771 to->di_size = cpu_to_be64(from->di_size);
772 to->di_nblocks = cpu_to_be64(from->di_nblocks);
773 to->di_extsize = cpu_to_be32(from->di_extsize);
774 to->di_nextents = cpu_to_be32(from->di_nextents);
775 to->di_anextents = cpu_to_be16(from->di_anextents);
776 to->di_forkoff = from->di_forkoff;
777 to->di_aformat = from->di_aformat;
778 to->di_dmevmask = cpu_to_be32(from->di_dmevmask);
779 to->di_dmstate = cpu_to_be16(from->di_dmstate);
780 to->di_flags = cpu_to_be16(from->di_flags);
781 to->di_gen = cpu_to_be32(from->di_gen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782}
783
784STATIC uint
785_xfs_dic2xflags(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 __uint16_t di_flags)
787{
788 uint flags = 0;
789
790 if (di_flags & XFS_DIFLAG_ANY) {
791 if (di_flags & XFS_DIFLAG_REALTIME)
792 flags |= XFS_XFLAG_REALTIME;
793 if (di_flags & XFS_DIFLAG_PREALLOC)
794 flags |= XFS_XFLAG_PREALLOC;
795 if (di_flags & XFS_DIFLAG_IMMUTABLE)
796 flags |= XFS_XFLAG_IMMUTABLE;
797 if (di_flags & XFS_DIFLAG_APPEND)
798 flags |= XFS_XFLAG_APPEND;
799 if (di_flags & XFS_DIFLAG_SYNC)
800 flags |= XFS_XFLAG_SYNC;
801 if (di_flags & XFS_DIFLAG_NOATIME)
802 flags |= XFS_XFLAG_NOATIME;
803 if (di_flags & XFS_DIFLAG_NODUMP)
804 flags |= XFS_XFLAG_NODUMP;
805 if (di_flags & XFS_DIFLAG_RTINHERIT)
806 flags |= XFS_XFLAG_RTINHERIT;
807 if (di_flags & XFS_DIFLAG_PROJINHERIT)
808 flags |= XFS_XFLAG_PROJINHERIT;
809 if (di_flags & XFS_DIFLAG_NOSYMLINKS)
810 flags |= XFS_XFLAG_NOSYMLINKS;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100811 if (di_flags & XFS_DIFLAG_EXTSIZE)
812 flags |= XFS_XFLAG_EXTSIZE;
813 if (di_flags & XFS_DIFLAG_EXTSZINHERIT)
814 flags |= XFS_XFLAG_EXTSZINHERIT;
Barry Naujokd3446ea2006-06-09 14:54:19 +1000815 if (di_flags & XFS_DIFLAG_NODEFRAG)
816 flags |= XFS_XFLAG_NODEFRAG;
David Chinner2a82b8b2007-07-11 11:09:12 +1000817 if (di_flags & XFS_DIFLAG_FILESTREAM)
818 flags |= XFS_XFLAG_FILESTREAM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 }
820
821 return flags;
822}
823
824uint
825xfs_ip2xflags(
826 xfs_inode_t *ip)
827{
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000828 xfs_icdinode_t *dic = &ip->i_d;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
Nathan Scotta916e2b2006-06-09 17:12:17 +1000830 return _xfs_dic2xflags(dic->di_flags) |
Christoph Hellwig45ba5982007-12-07 14:07:20 +1100831 (XFS_IFORK_Q(ip) ? XFS_XFLAG_HASATTR : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832}
833
834uint
835xfs_dic2xflags(
Christoph Hellwig45ba5982007-12-07 14:07:20 +1100836 xfs_dinode_t *dip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837{
Christoph Hellwig45ba5982007-12-07 14:07:20 +1100838 xfs_dinode_core_t *dic = &dip->di_core;
839
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000840 return _xfs_dic2xflags(be16_to_cpu(dic->di_flags)) |
Christoph Hellwig45ba5982007-12-07 14:07:20 +1100841 (XFS_DFORK_Q(dip) ? XFS_XFLAG_HASATTR : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842}
843
844/*
845 * Given a mount structure and an inode number, return a pointer
Nathan Scottc41564b2006-03-29 08:55:14 +1000846 * to a newly allocated in-core inode corresponding to the given
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 * inode number.
848 *
849 * Initialize the inode's attributes and extent pointers if it
850 * already has them (it will not if the inode has no links).
851 */
852int
853xfs_iread(
854 xfs_mount_t *mp,
855 xfs_trans_t *tp,
856 xfs_ino_t ino,
857 xfs_inode_t **ipp,
Nathan Scott745b1f472006-09-28 11:02:23 +1000858 xfs_daddr_t bno,
859 uint imap_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860{
861 xfs_buf_t *bp;
862 xfs_dinode_t *dip;
863 xfs_inode_t *ip;
864 int error;
865
866 ASSERT(xfs_inode_zone != NULL);
867
868 ip = kmem_zone_zalloc(xfs_inode_zone, KM_SLEEP);
869 ip->i_ino = ino;
870 ip->i_mount = mp;
Christoph Hellwigb677c212007-08-29 11:46:28 +1000871 atomic_set(&ip->i_iocount, 0);
David Chinnerf273ab82006-09-28 11:06:03 +1000872 spin_lock_init(&ip->i_flags_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
874 /*
875 * Get pointer's to the on-disk inode and the buffer containing it.
876 * If the inode number refers to a block outside the file system
877 * then xfs_itobp() will return NULL. In this case we should
878 * return NULL as well. Set i_blkno to 0 so that xfs_itobp() will
879 * know that this is a new incore inode.
880 */
Nathan Scott745b1f472006-09-28 11:02:23 +1000881 error = xfs_itobp(mp, tp, ip, &dip, &bp, bno, imap_flags);
Nathan Scottb12dd342006-03-17 17:26:04 +1100882 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 kmem_zone_free(xfs_inode_zone, ip);
884 return error;
885 }
886
887 /*
888 * Initialize inode's trace buffers.
889 * Do this before xfs_iformat in case it adds entries.
890 */
Lachlan McIlroycf441ee2008-02-07 16:42:19 +1100891#ifdef XFS_INODE_TRACE
892 ip->i_trace = ktrace_alloc(INODE_TRACE_SIZE, KM_SLEEP);
Christoph Hellwig1543d792007-08-29 11:46:47 +1000893#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894#ifdef XFS_BMAP_TRACE
895 ip->i_xtrace = ktrace_alloc(XFS_BMAP_KTRACE_SIZE, KM_SLEEP);
896#endif
897#ifdef XFS_BMBT_TRACE
898 ip->i_btrace = ktrace_alloc(XFS_BMBT_KTRACE_SIZE, KM_SLEEP);
899#endif
900#ifdef XFS_RW_TRACE
901 ip->i_rwtrace = ktrace_alloc(XFS_RW_KTRACE_SIZE, KM_SLEEP);
902#endif
903#ifdef XFS_ILOCK_TRACE
904 ip->i_lock_trace = ktrace_alloc(XFS_ILOCK_KTRACE_SIZE, KM_SLEEP);
905#endif
906#ifdef XFS_DIR2_TRACE
907 ip->i_dir_trace = ktrace_alloc(XFS_DIR2_KTRACE_SIZE, KM_SLEEP);
908#endif
909
910 /*
911 * If we got something that isn't an inode it means someone
912 * (nfs or dmi) has a stale handle.
913 */
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000914 if (be16_to_cpu(dip->di_core.di_magic) != XFS_DINODE_MAGIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 kmem_zone_free(xfs_inode_zone, ip);
916 xfs_trans_brelse(tp, bp);
917#ifdef DEBUG
918 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_iread: "
919 "dip->di_core.di_magic (0x%x) != "
920 "XFS_DINODE_MAGIC (0x%x)",
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000921 be16_to_cpu(dip->di_core.di_magic),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 XFS_DINODE_MAGIC);
923#endif /* DEBUG */
924 return XFS_ERROR(EINVAL);
925 }
926
927 /*
928 * If the on-disk inode is already linked to a directory
929 * entry, copy all of the inode into the in-core inode.
930 * xfs_iformat() handles copying in the inode format
931 * specific information.
932 * Otherwise, just get the truly permanent information.
933 */
934 if (dip->di_core.di_mode) {
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000935 xfs_dinode_from_disk(&ip->i_d, &dip->di_core);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 error = xfs_iformat(ip, dip);
937 if (error) {
938 kmem_zone_free(xfs_inode_zone, ip);
939 xfs_trans_brelse(tp, bp);
940#ifdef DEBUG
941 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_iread: "
942 "xfs_iformat() returned error %d",
943 error);
944#endif /* DEBUG */
945 return error;
946 }
947 } else {
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000948 ip->i_d.di_magic = be16_to_cpu(dip->di_core.di_magic);
949 ip->i_d.di_version = dip->di_core.di_version;
950 ip->i_d.di_gen = be32_to_cpu(dip->di_core.di_gen);
951 ip->i_d.di_flushiter = be16_to_cpu(dip->di_core.di_flushiter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 /*
953 * Make sure to pull in the mode here as well in
954 * case the inode is released without being used.
955 * This ensures that xfs_inactive() will see that
956 * the inode is already free and not try to mess
957 * with the uninitialized part of it.
958 */
959 ip->i_d.di_mode = 0;
960 /*
961 * Initialize the per-fork minima and maxima for a new
962 * inode here. xfs_iformat will do it for old inodes.
963 */
964 ip->i_df.if_ext_max =
965 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
966 }
967
968 INIT_LIST_HEAD(&ip->i_reclaim);
969
970 /*
971 * The inode format changed when we moved the link count and
972 * made it 32 bits long. If this is an old format inode,
973 * convert it in memory to look like a new one. If it gets
974 * flushed to disk we will convert back before flushing or
975 * logging it. We zero out the new projid field and the old link
976 * count field. We'll handle clearing the pad field (the remains
977 * of the old uuid field) when we actually convert the inode to
978 * the new format. We don't change the version number so that we
979 * can distinguish this from a real new format inode.
980 */
981 if (ip->i_d.di_version == XFS_DINODE_VERSION_1) {
982 ip->i_d.di_nlink = ip->i_d.di_onlink;
983 ip->i_d.di_onlink = 0;
984 ip->i_d.di_projid = 0;
985 }
986
987 ip->i_delayed_blks = 0;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000988 ip->i_size = ip->i_d.di_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
990 /*
991 * Mark the buffer containing the inode as something to keep
992 * around for a while. This helps to keep recently accessed
993 * meta-data in-core longer.
994 */
995 XFS_BUF_SET_REF(bp, XFS_INO_REF);
996
997 /*
998 * Use xfs_trans_brelse() to release the buffer containing the
999 * on-disk inode, because it was acquired with xfs_trans_read_buf()
1000 * in xfs_itobp() above. If tp is NULL, this is just a normal
1001 * brelse(). If we're within a transaction, then xfs_trans_brelse()
1002 * will only release the buffer if it is not dirty within the
1003 * transaction. It will be OK to release the buffer in this case,
1004 * because inodes on disk are never destroyed and we will be
1005 * locking the new in-core inode before putting it in the hash
1006 * table where other processes can find it. Thus we don't have
1007 * to worry about the inode being changed just because we released
1008 * the buffer.
1009 */
1010 xfs_trans_brelse(tp, bp);
1011 *ipp = ip;
1012 return 0;
1013}
1014
1015/*
1016 * Read in extents from a btree-format inode.
1017 * Allocate and fill in if_extents. Real work is done in xfs_bmap.c.
1018 */
1019int
1020xfs_iread_extents(
1021 xfs_trans_t *tp,
1022 xfs_inode_t *ip,
1023 int whichfork)
1024{
1025 int error;
1026 xfs_ifork_t *ifp;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001027 xfs_extnum_t nextents;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 size_t size;
1029
1030 if (unlikely(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
1031 XFS_ERROR_REPORT("xfs_iread_extents", XFS_ERRLEVEL_LOW,
1032 ip->i_mount);
1033 return XFS_ERROR(EFSCORRUPTED);
1034 }
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001035 nextents = XFS_IFORK_NEXTENTS(ip, whichfork);
1036 size = nextents * sizeof(xfs_bmbt_rec_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 ifp = XFS_IFORK_PTR(ip, whichfork);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001038
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 /*
1040 * We know that the size is valid (it's checked in iformat_btree)
1041 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 ifp->if_lastex = NULLEXTNUM;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001043 ifp->if_bytes = ifp->if_real_bytes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 ifp->if_flags |= XFS_IFEXTENTS;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001045 xfs_iext_add(ifp, 0, nextents);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 error = xfs_bmap_read_extents(tp, ip, whichfork);
1047 if (error) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001048 xfs_iext_destroy(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 ifp->if_flags &= ~XFS_IFEXTENTS;
1050 return error;
1051 }
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10001052 xfs_validate_extents(ifp, nextents, XFS_EXTFMT_INODE(ip));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 return 0;
1054}
1055
1056/*
1057 * Allocate an inode on disk and return a copy of its in-core version.
1058 * The in-core inode is locked exclusively. Set mode, nlink, and rdev
1059 * appropriately within the inode. The uid and gid for the inode are
1060 * set according to the contents of the given cred structure.
1061 *
1062 * Use xfs_dialloc() to allocate the on-disk inode. If xfs_dialloc()
1063 * has a free inode available, call xfs_iget()
1064 * to obtain the in-core version of the allocated inode. Finally,
1065 * fill in the inode and log its initial contents. In this case,
1066 * ialloc_context would be set to NULL and call_again set to false.
1067 *
1068 * If xfs_dialloc() does not have an available inode,
1069 * it will replenish its supply by doing an allocation. Since we can
1070 * only do one allocation within a transaction without deadlocks, we
1071 * must commit the current transaction before returning the inode itself.
1072 * In this case, therefore, we will set call_again to true and return.
1073 * The caller should then commit the current transaction, start a new
1074 * transaction, and call xfs_ialloc() again to actually get the inode.
1075 *
1076 * To ensure that some other process does not grab the inode that
1077 * was allocated during the first call to xfs_ialloc(), this routine
1078 * also returns the [locked] bp pointing to the head of the freelist
1079 * as ialloc_context. The caller should hold this buffer across
1080 * the commit and pass it back into this routine on the second call.
David Chinnerb11f94d2007-07-11 11:09:33 +10001081 *
1082 * If we are allocating quota inodes, we do not have a parent inode
1083 * to attach to or associate with (i.e. pip == NULL) because they
1084 * are not linked into the directory structure - they are attached
1085 * directly to the superblock - and so have no parent.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 */
1087int
1088xfs_ialloc(
1089 xfs_trans_t *tp,
1090 xfs_inode_t *pip,
1091 mode_t mode,
Nathan Scott31b084a2005-05-05 13:25:00 -07001092 xfs_nlink_t nlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 xfs_dev_t rdev,
1094 cred_t *cr,
1095 xfs_prid_t prid,
1096 int okalloc,
1097 xfs_buf_t **ialloc_context,
1098 boolean_t *call_again,
1099 xfs_inode_t **ipp)
1100{
1101 xfs_ino_t ino;
1102 xfs_inode_t *ip;
Nathan Scott67fcaa72006-06-09 17:00:52 +10001103 bhv_vnode_t *vp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 uint flags;
1105 int error;
1106
1107 /*
1108 * Call the space management code to pick
1109 * the on-disk inode to be allocated.
1110 */
David Chinnerb11f94d2007-07-11 11:09:33 +10001111 error = xfs_dialloc(tp, pip ? pip->i_ino : 0, mode, okalloc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 ialloc_context, call_again, &ino);
1113 if (error != 0) {
1114 return error;
1115 }
1116 if (*call_again || ino == NULLFSINO) {
1117 *ipp = NULL;
1118 return 0;
1119 }
1120 ASSERT(*ialloc_context == NULL);
1121
1122 /*
1123 * Get the in-core inode with the lock held exclusively.
1124 * This is because we're setting fields here we need
1125 * to prevent others from looking at until we're done.
1126 */
1127 error = xfs_trans_iget(tp->t_mountp, tp, ino,
Nathan Scott745b1f472006-09-28 11:02:23 +10001128 XFS_IGET_CREATE, XFS_ILOCK_EXCL, &ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 if (error != 0) {
1130 return error;
1131 }
1132 ASSERT(ip != NULL);
1133
1134 vp = XFS_ITOV(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 ip->i_d.di_mode = (__uint16_t)mode;
1136 ip->i_d.di_onlink = 0;
1137 ip->i_d.di_nlink = nlink;
1138 ASSERT(ip->i_d.di_nlink == nlink);
1139 ip->i_d.di_uid = current_fsuid(cr);
1140 ip->i_d.di_gid = current_fsgid(cr);
1141 ip->i_d.di_projid = prid;
1142 memset(&(ip->i_d.di_pad[0]), 0, sizeof(ip->i_d.di_pad));
1143
1144 /*
1145 * If the superblock version is up to where we support new format
1146 * inodes and this is currently an old format inode, then change
1147 * the inode version number now. This way we only do the conversion
1148 * here rather than here and in the flush/logging code.
1149 */
1150 if (XFS_SB_VERSION_HASNLINK(&tp->t_mountp->m_sb) &&
1151 ip->i_d.di_version == XFS_DINODE_VERSION_1) {
1152 ip->i_d.di_version = XFS_DINODE_VERSION_2;
1153 /*
1154 * We've already zeroed the old link count, the projid field,
1155 * and the pad field.
1156 */
1157 }
1158
1159 /*
1160 * Project ids won't be stored on disk if we are using a version 1 inode.
1161 */
David Chinner2a82b8b2007-07-11 11:09:12 +10001162 if ((prid != 0) && (ip->i_d.di_version == XFS_DINODE_VERSION_1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 xfs_bump_ino_vers2(tp, ip);
1164
Christoph Hellwigbd186aa2007-08-30 17:21:12 +10001165 if (pip && XFS_INHERIT_GID(pip)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 ip->i_d.di_gid = pip->i_d.di_gid;
1167 if ((pip->i_d.di_mode & S_ISGID) && (mode & S_IFMT) == S_IFDIR) {
1168 ip->i_d.di_mode |= S_ISGID;
1169 }
1170 }
1171
1172 /*
1173 * If the group ID of the new file does not match the effective group
1174 * ID or one of the supplementary group IDs, the S_ISGID bit is cleared
1175 * (and only if the irix_sgid_inherit compatibility variable is set).
1176 */
1177 if ((irix_sgid_inherit) &&
1178 (ip->i_d.di_mode & S_ISGID) &&
1179 (!in_group_p((gid_t)ip->i_d.di_gid))) {
1180 ip->i_d.di_mode &= ~S_ISGID;
1181 }
1182
1183 ip->i_d.di_size = 0;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10001184 ip->i_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 ip->i_d.di_nextents = 0;
1186 ASSERT(ip->i_d.di_nblocks == 0);
1187 xfs_ichgtime(ip, XFS_ICHGTIME_CHG|XFS_ICHGTIME_ACC|XFS_ICHGTIME_MOD);
1188 /*
1189 * di_gen will have been taken care of in xfs_iread.
1190 */
1191 ip->i_d.di_extsize = 0;
1192 ip->i_d.di_dmevmask = 0;
1193 ip->i_d.di_dmstate = 0;
1194 ip->i_d.di_flags = 0;
1195 flags = XFS_ILOG_CORE;
1196 switch (mode & S_IFMT) {
1197 case S_IFIFO:
1198 case S_IFCHR:
1199 case S_IFBLK:
1200 case S_IFSOCK:
1201 ip->i_d.di_format = XFS_DINODE_FMT_DEV;
1202 ip->i_df.if_u2.if_rdev = rdev;
1203 ip->i_df.if_flags = 0;
1204 flags |= XFS_ILOG_DEV;
1205 break;
1206 case S_IFREG:
David Chinnerb11f94d2007-07-11 11:09:33 +10001207 if (pip && xfs_inode_is_filestream(pip)) {
David Chinner2a82b8b2007-07-11 11:09:12 +10001208 error = xfs_filestream_associate(pip, ip);
1209 if (error < 0)
1210 return -error;
1211 if (!error)
1212 xfs_iflags_set(ip, XFS_IFILESTREAM);
1213 }
1214 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 case S_IFDIR:
David Chinnerb11f94d2007-07-11 11:09:33 +10001216 if (pip && (pip->i_d.di_flags & XFS_DIFLAG_ANY)) {
Nathan Scott365ca832005-06-21 15:39:12 +10001217 uint di_flags = 0;
1218
1219 if ((mode & S_IFMT) == S_IFDIR) {
1220 if (pip->i_d.di_flags & XFS_DIFLAG_RTINHERIT)
1221 di_flags |= XFS_DIFLAG_RTINHERIT;
Nathan Scottdd9f4382006-01-11 15:28:28 +11001222 if (pip->i_d.di_flags & XFS_DIFLAG_EXTSZINHERIT) {
1223 di_flags |= XFS_DIFLAG_EXTSZINHERIT;
1224 ip->i_d.di_extsize = pip->i_d.di_extsize;
1225 }
1226 } else if ((mode & S_IFMT) == S_IFREG) {
Christoph Hellwig613d7042007-10-11 17:44:08 +10001227 if (pip->i_d.di_flags & XFS_DIFLAG_RTINHERIT)
Nathan Scott365ca832005-06-21 15:39:12 +10001228 di_flags |= XFS_DIFLAG_REALTIME;
Nathan Scottdd9f4382006-01-11 15:28:28 +11001229 if (pip->i_d.di_flags & XFS_DIFLAG_EXTSZINHERIT) {
1230 di_flags |= XFS_DIFLAG_EXTSIZE;
1231 ip->i_d.di_extsize = pip->i_d.di_extsize;
1232 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 }
1234 if ((pip->i_d.di_flags & XFS_DIFLAG_NOATIME) &&
1235 xfs_inherit_noatime)
Nathan Scott365ca832005-06-21 15:39:12 +10001236 di_flags |= XFS_DIFLAG_NOATIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 if ((pip->i_d.di_flags & XFS_DIFLAG_NODUMP) &&
1238 xfs_inherit_nodump)
Nathan Scott365ca832005-06-21 15:39:12 +10001239 di_flags |= XFS_DIFLAG_NODUMP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 if ((pip->i_d.di_flags & XFS_DIFLAG_SYNC) &&
1241 xfs_inherit_sync)
Nathan Scott365ca832005-06-21 15:39:12 +10001242 di_flags |= XFS_DIFLAG_SYNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 if ((pip->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) &&
1244 xfs_inherit_nosymlinks)
Nathan Scott365ca832005-06-21 15:39:12 +10001245 di_flags |= XFS_DIFLAG_NOSYMLINKS;
1246 if (pip->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
1247 di_flags |= XFS_DIFLAG_PROJINHERIT;
Barry Naujokd3446ea2006-06-09 14:54:19 +10001248 if ((pip->i_d.di_flags & XFS_DIFLAG_NODEFRAG) &&
1249 xfs_inherit_nodefrag)
1250 di_flags |= XFS_DIFLAG_NODEFRAG;
David Chinner2a82b8b2007-07-11 11:09:12 +10001251 if (pip->i_d.di_flags & XFS_DIFLAG_FILESTREAM)
1252 di_flags |= XFS_DIFLAG_FILESTREAM;
Nathan Scott365ca832005-06-21 15:39:12 +10001253 ip->i_d.di_flags |= di_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 }
1255 /* FALLTHROUGH */
1256 case S_IFLNK:
1257 ip->i_d.di_format = XFS_DINODE_FMT_EXTENTS;
1258 ip->i_df.if_flags = XFS_IFEXTENTS;
1259 ip->i_df.if_bytes = ip->i_df.if_real_bytes = 0;
1260 ip->i_df.if_u1.if_extents = NULL;
1261 break;
1262 default:
1263 ASSERT(0);
1264 }
1265 /*
1266 * Attribute fork settings for new inode.
1267 */
1268 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
1269 ip->i_d.di_anextents = 0;
1270
1271 /*
1272 * Log the new values stuffed into the inode.
1273 */
1274 xfs_trans_log_inode(tp, ip, flags);
1275
Nathan Scottb83bd132006-06-09 16:48:30 +10001276 /* now that we have an i_mode we can setup inode ops and unlock */
Christoph Hellwig745f6912007-08-30 17:20:39 +10001277 xfs_initialize_vnode(tp->t_mountp, vp, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278
1279 *ipp = ip;
1280 return 0;
1281}
1282
1283/*
1284 * Check to make sure that there are no blocks allocated to the
1285 * file beyond the size of the file. We don't check this for
1286 * files with fixed size extents or real time extents, but we
1287 * at least do it for regular files.
1288 */
1289#ifdef DEBUG
1290void
1291xfs_isize_check(
1292 xfs_mount_t *mp,
1293 xfs_inode_t *ip,
1294 xfs_fsize_t isize)
1295{
1296 xfs_fileoff_t map_first;
1297 int nimaps;
1298 xfs_bmbt_irec_t imaps[2];
1299
1300 if ((ip->i_d.di_mode & S_IFMT) != S_IFREG)
1301 return;
1302
Eric Sandeen71ddabb2007-11-23 16:29:42 +11001303 if (XFS_IS_REALTIME_INODE(ip))
1304 return;
1305
1306 if (ip->i_d.di_flags & XFS_DIFLAG_EXTSIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 return;
1308
1309 nimaps = 2;
1310 map_first = XFS_B_TO_FSB(mp, (xfs_ufsize_t)isize);
1311 /*
1312 * The filesystem could be shutting down, so bmapi may return
1313 * an error.
1314 */
1315 if (xfs_bmapi(NULL, ip, map_first,
1316 (XFS_B_TO_FSB(mp,
1317 (xfs_ufsize_t)XFS_MAXIOFFSET(mp)) -
1318 map_first),
1319 XFS_BMAPI_ENTIRE, NULL, 0, imaps, &nimaps,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001320 NULL, NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 return;
1322 ASSERT(nimaps == 1);
1323 ASSERT(imaps[0].br_startblock == HOLESTARTBLOCK);
1324}
1325#endif /* DEBUG */
1326
1327/*
1328 * Calculate the last possible buffered byte in a file. This must
1329 * include data that was buffered beyond the EOF by the write code.
1330 * This also needs to deal with overflowing the xfs_fsize_t type
1331 * which can happen for sizes near the limit.
1332 *
1333 * We also need to take into account any blocks beyond the EOF. It
1334 * may be the case that they were buffered by a write which failed.
1335 * In that case the pages will still be in memory, but the inode size
1336 * will never have been updated.
1337 */
1338xfs_fsize_t
1339xfs_file_last_byte(
1340 xfs_inode_t *ip)
1341{
1342 xfs_mount_t *mp;
1343 xfs_fsize_t last_byte;
1344 xfs_fileoff_t last_block;
1345 xfs_fileoff_t size_last_block;
1346 int error;
1347
1348 ASSERT(ismrlocked(&(ip->i_iolock), MR_UPDATE | MR_ACCESS));
1349
1350 mp = ip->i_mount;
1351 /*
1352 * Only check for blocks beyond the EOF if the extents have
1353 * been read in. This eliminates the need for the inode lock,
1354 * and it also saves us from looking when it really isn't
1355 * necessary.
1356 */
1357 if (ip->i_df.if_flags & XFS_IFEXTENTS) {
1358 error = xfs_bmap_last_offset(NULL, ip, &last_block,
1359 XFS_DATA_FORK);
1360 if (error) {
1361 last_block = 0;
1362 }
1363 } else {
1364 last_block = 0;
1365 }
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10001366 size_last_block = XFS_B_TO_FSB(mp, (xfs_ufsize_t)ip->i_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 last_block = XFS_FILEOFF_MAX(last_block, size_last_block);
1368
1369 last_byte = XFS_FSB_TO_B(mp, last_block);
1370 if (last_byte < 0) {
1371 return XFS_MAXIOFFSET(mp);
1372 }
1373 last_byte += (1 << mp->m_writeio_log);
1374 if (last_byte < 0) {
1375 return XFS_MAXIOFFSET(mp);
1376 }
1377 return last_byte;
1378}
1379
1380#if defined(XFS_RW_TRACE)
1381STATIC void
1382xfs_itrunc_trace(
1383 int tag,
1384 xfs_inode_t *ip,
1385 int flag,
1386 xfs_fsize_t new_size,
1387 xfs_off_t toss_start,
1388 xfs_off_t toss_finish)
1389{
1390 if (ip->i_rwtrace == NULL) {
1391 return;
1392 }
1393
1394 ktrace_enter(ip->i_rwtrace,
1395 (void*)((long)tag),
1396 (void*)ip,
1397 (void*)(unsigned long)((ip->i_d.di_size >> 32) & 0xffffffff),
1398 (void*)(unsigned long)(ip->i_d.di_size & 0xffffffff),
1399 (void*)((long)flag),
1400 (void*)(unsigned long)((new_size >> 32) & 0xffffffff),
1401 (void*)(unsigned long)(new_size & 0xffffffff),
1402 (void*)(unsigned long)((toss_start >> 32) & 0xffffffff),
1403 (void*)(unsigned long)(toss_start & 0xffffffff),
1404 (void*)(unsigned long)((toss_finish >> 32) & 0xffffffff),
1405 (void*)(unsigned long)(toss_finish & 0xffffffff),
1406 (void*)(unsigned long)current_cpu(),
Yingping Luf1fdc842006-03-22 12:44:15 +11001407 (void*)(unsigned long)current_pid(),
1408 (void*)NULL,
1409 (void*)NULL,
1410 (void*)NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411}
1412#else
1413#define xfs_itrunc_trace(tag, ip, flag, new_size, toss_start, toss_finish)
1414#endif
1415
1416/*
1417 * Start the truncation of the file to new_size. The new size
1418 * must be smaller than the current size. This routine will
1419 * clear the buffer and page caches of file data in the removed
1420 * range, and xfs_itruncate_finish() will remove the underlying
1421 * disk blocks.
1422 *
1423 * The inode must have its I/O lock locked EXCLUSIVELY, and it
1424 * must NOT have the inode lock held at all. This is because we're
1425 * calling into the buffer/page cache code and we can't hold the
1426 * inode lock when we do so.
1427 *
David Chinner38e22992006-03-22 12:47:15 +11001428 * We need to wait for any direct I/Os in flight to complete before we
1429 * proceed with the truncate. This is needed to prevent the extents
1430 * being read or written by the direct I/Os from being removed while the
1431 * I/O is in flight as there is no other method of synchronising
1432 * direct I/O with the truncate operation. Also, because we hold
1433 * the IOLOCK in exclusive mode, we prevent new direct I/Os from being
1434 * started until the truncate completes and drops the lock. Essentially,
1435 * the vn_iowait() call forms an I/O barrier that provides strict ordering
1436 * between direct I/Os and the truncate operation.
1437 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 * The flags parameter can have either the value XFS_ITRUNC_DEFINITE
1439 * or XFS_ITRUNC_MAYBE. The XFS_ITRUNC_MAYBE value should be used
1440 * in the case that the caller is locking things out of order and
1441 * may not be able to call xfs_itruncate_finish() with the inode lock
1442 * held without dropping the I/O lock. If the caller must drop the
1443 * I/O lock before calling xfs_itruncate_finish(), then xfs_itruncate_start()
1444 * must be called again with all the same restrictions as the initial
1445 * call.
1446 */
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +10001447int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448xfs_itruncate_start(
1449 xfs_inode_t *ip,
1450 uint flags,
1451 xfs_fsize_t new_size)
1452{
1453 xfs_fsize_t last_byte;
1454 xfs_off_t toss_start;
1455 xfs_mount_t *mp;
Nathan Scott67fcaa72006-06-09 17:00:52 +10001456 bhv_vnode_t *vp;
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +10001457 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
1459 ASSERT(ismrlocked(&ip->i_iolock, MR_UPDATE) != 0);
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10001460 ASSERT((new_size == 0) || (new_size <= ip->i_size));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 ASSERT((flags == XFS_ITRUNC_DEFINITE) ||
1462 (flags == XFS_ITRUNC_MAYBE));
1463
1464 mp = ip->i_mount;
1465 vp = XFS_ITOV(ip);
Yingping Lu9fa80462006-03-22 12:44:35 +11001466
Lachlan McIlroyc734c792007-12-18 16:17:41 +11001467 /* wait for the completion of any pending DIOs */
1468 if (new_size < ip->i_size)
1469 vn_iowait(ip);
1470
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 /*
Nathan Scott67fcaa72006-06-09 17:00:52 +10001472 * Call toss_pages or flushinval_pages to get rid of pages
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 * overlapping the region being removed. We have to use
Nathan Scott67fcaa72006-06-09 17:00:52 +10001474 * the less efficient flushinval_pages in the case that the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 * caller may not be able to finish the truncate without
1476 * dropping the inode's I/O lock. Make sure
1477 * to catch any pages brought in by buffers overlapping
1478 * the EOF by searching out beyond the isize by our
1479 * block size. We round new_size up to a block boundary
1480 * so that we don't toss things on the same block as
1481 * new_size but before it.
1482 *
Nathan Scott67fcaa72006-06-09 17:00:52 +10001483 * Before calling toss_page or flushinval_pages, make sure to
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 * call remapf() over the same region if the file is mapped.
1485 * This frees up mapped file references to the pages in the
Nathan Scott67fcaa72006-06-09 17:00:52 +10001486 * given range and for the flushinval_pages case it ensures
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 * that we get the latest mapped changes flushed out.
1488 */
1489 toss_start = XFS_B_TO_FSB(mp, (xfs_ufsize_t)new_size);
1490 toss_start = XFS_FSB_TO_B(mp, toss_start);
1491 if (toss_start < 0) {
1492 /*
1493 * The place to start tossing is beyond our maximum
1494 * file size, so there is no way that the data extended
1495 * out there.
1496 */
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +10001497 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 }
1499 last_byte = xfs_file_last_byte(ip);
1500 xfs_itrunc_trace(XFS_ITRUNC_START, ip, flags, new_size, toss_start,
1501 last_byte);
1502 if (last_byte > toss_start) {
1503 if (flags & XFS_ITRUNC_DEFINITE) {
Christoph Hellwig739bfb22007-08-29 10:58:01 +10001504 xfs_tosspages(ip, toss_start,
1505 -1, FI_REMAPF_LOCKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 } else {
Christoph Hellwig739bfb22007-08-29 10:58:01 +10001507 error = xfs_flushinval_pages(ip, toss_start,
1508 -1, FI_REMAPF_LOCKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 }
1510 }
1511
1512#ifdef DEBUG
1513 if (new_size == 0) {
1514 ASSERT(VN_CACHED(vp) == 0);
1515 }
1516#endif
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +10001517 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518}
1519
1520/*
1521 * Shrink the file to the given new_size. The new
1522 * size must be smaller than the current size.
1523 * This will free up the underlying blocks
1524 * in the removed range after a call to xfs_itruncate_start()
1525 * or xfs_atruncate_start().
1526 *
1527 * The transaction passed to this routine must have made
1528 * a permanent log reservation of at least XFS_ITRUNCATE_LOG_RES.
1529 * This routine may commit the given transaction and
1530 * start new ones, so make sure everything involved in
1531 * the transaction is tidy before calling here.
1532 * Some transaction will be returned to the caller to be
1533 * committed. The incoming transaction must already include
1534 * the inode, and both inode locks must be held exclusively.
1535 * The inode must also be "held" within the transaction. On
1536 * return the inode will be "held" within the returned transaction.
1537 * This routine does NOT require any disk space to be reserved
1538 * for it within the transaction.
1539 *
1540 * The fork parameter must be either xfs_attr_fork or xfs_data_fork,
1541 * and it indicates the fork which is to be truncated. For the
1542 * attribute fork we only support truncation to size 0.
1543 *
1544 * We use the sync parameter to indicate whether or not the first
1545 * transaction we perform might have to be synchronous. For the attr fork,
1546 * it needs to be so if the unlink of the inode is not yet known to be
1547 * permanent in the log. This keeps us from freeing and reusing the
1548 * blocks of the attribute fork before the unlink of the inode becomes
1549 * permanent.
1550 *
1551 * For the data fork, we normally have to run synchronously if we're
1552 * being called out of the inactive path or we're being called
1553 * out of the create path where we're truncating an existing file.
1554 * Either way, the truncate needs to be sync so blocks don't reappear
1555 * in the file with altered data in case of a crash. wsync filesystems
1556 * can run the first case async because anything that shrinks the inode
1557 * has to run sync so by the time we're called here from inactive, the
1558 * inode size is permanently set to 0.
1559 *
1560 * Calls from the truncate path always need to be sync unless we're
1561 * in a wsync filesystem and the file has already been unlinked.
1562 *
1563 * The caller is responsible for correctly setting the sync parameter.
1564 * It gets too hard for us to guess here which path we're being called
1565 * out of just based on inode state.
1566 */
1567int
1568xfs_itruncate_finish(
1569 xfs_trans_t **tp,
1570 xfs_inode_t *ip,
1571 xfs_fsize_t new_size,
1572 int fork,
1573 int sync)
1574{
1575 xfs_fsblock_t first_block;
1576 xfs_fileoff_t first_unmap_block;
1577 xfs_fileoff_t last_block;
1578 xfs_filblks_t unmap_len=0;
1579 xfs_mount_t *mp;
1580 xfs_trans_t *ntp;
1581 int done;
1582 int committed;
1583 xfs_bmap_free_t free_list;
1584 int error;
1585
1586 ASSERT(ismrlocked(&ip->i_iolock, MR_UPDATE) != 0);
1587 ASSERT(ismrlocked(&ip->i_lock, MR_UPDATE) != 0);
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10001588 ASSERT((new_size == 0) || (new_size <= ip->i_size));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 ASSERT(*tp != NULL);
1590 ASSERT((*tp)->t_flags & XFS_TRANS_PERM_LOG_RES);
1591 ASSERT(ip->i_transp == *tp);
1592 ASSERT(ip->i_itemp != NULL);
1593 ASSERT(ip->i_itemp->ili_flags & XFS_ILI_HOLD);
1594
1595
1596 ntp = *tp;
1597 mp = (ntp)->t_mountp;
1598 ASSERT(! XFS_NOT_DQATTACHED(mp, ip));
1599
1600 /*
1601 * We only support truncating the entire attribute fork.
1602 */
1603 if (fork == XFS_ATTR_FORK) {
1604 new_size = 0LL;
1605 }
1606 first_unmap_block = XFS_B_TO_FSB(mp, (xfs_ufsize_t)new_size);
1607 xfs_itrunc_trace(XFS_ITRUNC_FINISH1, ip, 0, new_size, 0, 0);
1608 /*
1609 * The first thing we do is set the size to new_size permanently
1610 * on disk. This way we don't have to worry about anyone ever
1611 * being able to look at the data being freed even in the face
1612 * of a crash. What we're getting around here is the case where
1613 * we free a block, it is allocated to another file, it is written
1614 * to, and then we crash. If the new data gets written to the
1615 * file but the log buffers containing the free and reallocation
1616 * don't, then we'd end up with garbage in the blocks being freed.
1617 * As long as we make the new_size permanent before actually
1618 * freeing any blocks it doesn't matter if they get writtten to.
1619 *
1620 * The callers must signal into us whether or not the size
1621 * setting here must be synchronous. There are a few cases
1622 * where it doesn't have to be synchronous. Those cases
1623 * occur if the file is unlinked and we know the unlink is
1624 * permanent or if the blocks being truncated are guaranteed
1625 * to be beyond the inode eof (regardless of the link count)
1626 * and the eof value is permanent. Both of these cases occur
1627 * only on wsync-mounted filesystems. In those cases, we're
1628 * guaranteed that no user will ever see the data in the blocks
1629 * that are being truncated so the truncate can run async.
1630 * In the free beyond eof case, the file may wind up with
1631 * more blocks allocated to it than it needs if we crash
1632 * and that won't get fixed until the next time the file
1633 * is re-opened and closed but that's ok as that shouldn't
1634 * be too many blocks.
1635 *
1636 * However, we can't just make all wsync xactions run async
1637 * because there's one call out of the create path that needs
1638 * to run sync where it's truncating an existing file to size
1639 * 0 whose size is > 0.
1640 *
1641 * It's probably possible to come up with a test in this
1642 * routine that would correctly distinguish all the above
1643 * cases from the values of the function parameters and the
1644 * inode state but for sanity's sake, I've decided to let the
1645 * layers above just tell us. It's simpler to correctly figure
1646 * out in the layer above exactly under what conditions we
1647 * can run async and I think it's easier for others read and
1648 * follow the logic in case something has to be changed.
1649 * cscope is your friend -- rcc.
1650 *
1651 * The attribute fork is much simpler.
1652 *
1653 * For the attribute fork we allow the caller to tell us whether
1654 * the unlink of the inode that led to this call is yet permanent
1655 * in the on disk log. If it is not and we will be freeing extents
1656 * in this inode then we make the first transaction synchronous
1657 * to make sure that the unlink is permanent by the time we free
1658 * the blocks.
1659 */
1660 if (fork == XFS_DATA_FORK) {
1661 if (ip->i_d.di_nextents > 0) {
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10001662 /*
1663 * If we are not changing the file size then do
1664 * not update the on-disk file size - we may be
1665 * called from xfs_inactive_free_eofblocks(). If we
1666 * update the on-disk file size and then the system
1667 * crashes before the contents of the file are
1668 * flushed to disk then the files may be full of
1669 * holes (ie NULL files bug).
1670 */
1671 if (ip->i_size != new_size) {
1672 ip->i_d.di_size = new_size;
1673 ip->i_size = new_size;
1674 xfs_trans_log_inode(ntp, ip, XFS_ILOG_CORE);
1675 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 }
1677 } else if (sync) {
1678 ASSERT(!(mp->m_flags & XFS_MOUNT_WSYNC));
1679 if (ip->i_d.di_anextents > 0)
1680 xfs_trans_set_sync(ntp);
1681 }
1682 ASSERT(fork == XFS_DATA_FORK ||
1683 (fork == XFS_ATTR_FORK &&
1684 ((sync && !(mp->m_flags & XFS_MOUNT_WSYNC)) ||
1685 (sync == 0 && (mp->m_flags & XFS_MOUNT_WSYNC)))));
1686
1687 /*
1688 * Since it is possible for space to become allocated beyond
1689 * the end of the file (in a crash where the space is allocated
1690 * but the inode size is not yet updated), simply remove any
1691 * blocks which show up between the new EOF and the maximum
1692 * possible file size. If the first block to be removed is
1693 * beyond the maximum file size (ie it is the same as last_block),
1694 * then there is nothing to do.
1695 */
1696 last_block = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp));
1697 ASSERT(first_unmap_block <= last_block);
1698 done = 0;
1699 if (last_block == first_unmap_block) {
1700 done = 1;
1701 } else {
1702 unmap_len = last_block - first_unmap_block + 1;
1703 }
1704 while (!done) {
1705 /*
1706 * Free up up to XFS_ITRUNC_MAX_EXTENTS. xfs_bunmapi()
1707 * will tell us whether it freed the entire range or
1708 * not. If this is a synchronous mount (wsync),
1709 * then we can tell bunmapi to keep all the
1710 * transactions asynchronous since the unlink
1711 * transaction that made this inode inactive has
1712 * already hit the disk. There's no danger of
1713 * the freed blocks being reused, there being a
1714 * crash, and the reused blocks suddenly reappearing
1715 * in this file with garbage in them once recovery
1716 * runs.
1717 */
1718 XFS_BMAP_INIT(&free_list, &first_block);
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10001719 error = xfs_bunmapi(ntp, ip,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001720 first_unmap_block, unmap_len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 XFS_BMAPI_AFLAG(fork) |
1722 (sync ? 0 : XFS_BMAPI_ASYNC),
1723 XFS_ITRUNC_MAX_EXTENTS,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001724 &first_block, &free_list,
1725 NULL, &done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 if (error) {
1727 /*
1728 * If the bunmapi call encounters an error,
1729 * return to the caller where the transaction
1730 * can be properly aborted. We just need to
1731 * make sure we're not holding any resources
1732 * that we were not when we came in.
1733 */
1734 xfs_bmap_cancel(&free_list);
1735 return error;
1736 }
1737
1738 /*
1739 * Duplicate the transaction that has the permanent
1740 * reservation and commit the old transaction.
1741 */
Eric Sandeenf7c99b62007-02-10 18:37:16 +11001742 error = xfs_bmap_finish(tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 ntp = *tp;
1744 if (error) {
1745 /*
1746 * If the bmap finish call encounters an error,
1747 * return to the caller where the transaction
1748 * can be properly aborted. We just need to
1749 * make sure we're not holding any resources
1750 * that we were not when we came in.
1751 *
1752 * Aborting from this point might lose some
1753 * blocks in the file system, but oh well.
1754 */
1755 xfs_bmap_cancel(&free_list);
1756 if (committed) {
1757 /*
1758 * If the passed in transaction committed
1759 * in xfs_bmap_finish(), then we want to
1760 * add the inode to this one before returning.
1761 * This keeps things simple for the higher
1762 * level code, because it always knows that
1763 * the inode is locked and held in the
1764 * transaction that returns to it whether
1765 * errors occur or not. We don't mark the
1766 * inode dirty so that this transaction can
1767 * be easily aborted if possible.
1768 */
1769 xfs_trans_ijoin(ntp, ip,
1770 XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1771 xfs_trans_ihold(ntp, ip);
1772 }
1773 return error;
1774 }
1775
1776 if (committed) {
1777 /*
1778 * The first xact was committed,
1779 * so add the inode to the new one.
1780 * Mark it dirty so it will be logged
1781 * and moved forward in the log as
1782 * part of every commit.
1783 */
1784 xfs_trans_ijoin(ntp, ip,
1785 XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1786 xfs_trans_ihold(ntp, ip);
1787 xfs_trans_log_inode(ntp, ip, XFS_ILOG_CORE);
1788 }
1789 ntp = xfs_trans_dup(ntp);
Eric Sandeen1c72bf92007-05-08 13:48:42 +10001790 (void) xfs_trans_commit(*tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 *tp = ntp;
1792 error = xfs_trans_reserve(ntp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
1793 XFS_TRANS_PERM_LOG_RES,
1794 XFS_ITRUNCATE_LOG_COUNT);
1795 /*
1796 * Add the inode being truncated to the next chained
1797 * transaction.
1798 */
1799 xfs_trans_ijoin(ntp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1800 xfs_trans_ihold(ntp, ip);
1801 if (error)
1802 return (error);
1803 }
1804 /*
1805 * Only update the size in the case of the data fork, but
1806 * always re-log the inode so that our permanent transaction
1807 * can keep on rolling it forward in the log.
1808 */
1809 if (fork == XFS_DATA_FORK) {
1810 xfs_isize_check(mp, ip, new_size);
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10001811 /*
1812 * If we are not changing the file size then do
1813 * not update the on-disk file size - we may be
1814 * called from xfs_inactive_free_eofblocks(). If we
1815 * update the on-disk file size and then the system
1816 * crashes before the contents of the file are
1817 * flushed to disk then the files may be full of
1818 * holes (ie NULL files bug).
1819 */
1820 if (ip->i_size != new_size) {
1821 ip->i_d.di_size = new_size;
1822 ip->i_size = new_size;
1823 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 }
1825 xfs_trans_log_inode(ntp, ip, XFS_ILOG_CORE);
1826 ASSERT((new_size != 0) ||
1827 (fork == XFS_ATTR_FORK) ||
1828 (ip->i_delayed_blks == 0));
1829 ASSERT((new_size != 0) ||
1830 (fork == XFS_ATTR_FORK) ||
1831 (ip->i_d.di_nextents == 0));
1832 xfs_itrunc_trace(XFS_ITRUNC_FINISH2, ip, 0, new_size, 0, 0);
1833 return 0;
1834}
1835
1836
1837/*
1838 * xfs_igrow_start
1839 *
1840 * Do the first part of growing a file: zero any data in the last
1841 * block that is beyond the old EOF. We need to do this before
1842 * the inode is joined to the transaction to modify the i_size.
1843 * That way we can drop the inode lock and call into the buffer
1844 * cache to get the buffer mapping the EOF.
1845 */
1846int
1847xfs_igrow_start(
1848 xfs_inode_t *ip,
1849 xfs_fsize_t new_size,
1850 cred_t *credp)
1851{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 ASSERT(ismrlocked(&(ip->i_lock), MR_UPDATE) != 0);
1853 ASSERT(ismrlocked(&(ip->i_iolock), MR_UPDATE) != 0);
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10001854 ASSERT(new_size > ip->i_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 /*
1857 * Zero any pages that may have been created by
1858 * xfs_write_file() beyond the end of the file
1859 * and any blocks between the old and new file sizes.
1860 */
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10001861 return xfs_zero_eof(ip, new_size, ip->i_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862}
1863
1864/*
1865 * xfs_igrow_finish
1866 *
1867 * This routine is called to extend the size of a file.
1868 * The inode must have both the iolock and the ilock locked
1869 * for update and it must be a part of the current transaction.
1870 * The xfs_igrow_start() function must have been called previously.
1871 * If the change_flag is not zero, the inode change timestamp will
1872 * be updated.
1873 */
1874void
1875xfs_igrow_finish(
1876 xfs_trans_t *tp,
1877 xfs_inode_t *ip,
1878 xfs_fsize_t new_size,
1879 int change_flag)
1880{
1881 ASSERT(ismrlocked(&(ip->i_lock), MR_UPDATE) != 0);
1882 ASSERT(ismrlocked(&(ip->i_iolock), MR_UPDATE) != 0);
1883 ASSERT(ip->i_transp == tp);
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10001884 ASSERT(new_size > ip->i_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885
1886 /*
1887 * Update the file size. Update the inode change timestamp
1888 * if change_flag set.
1889 */
1890 ip->i_d.di_size = new_size;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10001891 ip->i_size = new_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 if (change_flag)
1893 xfs_ichgtime(ip, XFS_ICHGTIME_CHG);
1894 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1895
1896}
1897
1898
1899/*
1900 * This is called when the inode's link count goes to 0.
1901 * We place the on-disk inode on a list in the AGI. It
1902 * will be pulled from this list when the inode is freed.
1903 */
1904int
1905xfs_iunlink(
1906 xfs_trans_t *tp,
1907 xfs_inode_t *ip)
1908{
1909 xfs_mount_t *mp;
1910 xfs_agi_t *agi;
1911 xfs_dinode_t *dip;
1912 xfs_buf_t *agibp;
1913 xfs_buf_t *ibp;
1914 xfs_agnumber_t agno;
1915 xfs_daddr_t agdaddr;
1916 xfs_agino_t agino;
1917 short bucket_index;
1918 int offset;
1919 int error;
1920 int agi_ok;
1921
1922 ASSERT(ip->i_d.di_nlink == 0);
1923 ASSERT(ip->i_d.di_mode != 0);
1924 ASSERT(ip->i_transp == tp);
1925
1926 mp = tp->t_mountp;
1927
1928 agno = XFS_INO_TO_AGNO(mp, ip->i_ino);
1929 agdaddr = XFS_AG_DADDR(mp, agno, XFS_AGI_DADDR(mp));
1930
1931 /*
1932 * Get the agi buffer first. It ensures lock ordering
1933 * on the list.
1934 */
1935 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, agdaddr,
1936 XFS_FSS_TO_BB(mp, 1), 0, &agibp);
Vlad Apostolov859d7182007-10-11 17:44:18 +10001937 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 return error;
Vlad Apostolov859d7182007-10-11 17:44:18 +10001939
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 /*
1941 * Validate the magic number of the agi block.
1942 */
1943 agi = XFS_BUF_TO_AGI(agibp);
1944 agi_ok =
Christoph Hellwig16259e72005-11-02 15:11:25 +11001945 be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC &&
1946 XFS_AGI_GOOD_VERSION(be32_to_cpu(agi->agi_versionnum));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 if (unlikely(XFS_TEST_ERROR(!agi_ok, mp, XFS_ERRTAG_IUNLINK,
1948 XFS_RANDOM_IUNLINK))) {
1949 XFS_CORRUPTION_ERROR("xfs_iunlink", XFS_ERRLEVEL_LOW, mp, agi);
1950 xfs_trans_brelse(tp, agibp);
1951 return XFS_ERROR(EFSCORRUPTED);
1952 }
1953 /*
1954 * Get the index into the agi hash table for the
1955 * list this inode will go on.
1956 */
1957 agino = XFS_INO_TO_AGINO(mp, ip->i_ino);
1958 ASSERT(agino != 0);
1959 bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS;
1960 ASSERT(agi->agi_unlinked[bucket_index]);
Christoph Hellwig16259e72005-11-02 15:11:25 +11001961 ASSERT(be32_to_cpu(agi->agi_unlinked[bucket_index]) != agino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962
Christoph Hellwig16259e72005-11-02 15:11:25 +11001963 if (be32_to_cpu(agi->agi_unlinked[bucket_index]) != NULLAGINO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 /*
1965 * There is already another inode in the bucket we need
1966 * to add ourselves to. Add us at the front of the list.
1967 * Here we put the head pointer into our next pointer,
1968 * and then we fall through to point the head at us.
1969 */
Vlad Apostolovc319b582007-11-23 16:27:51 +11001970 error = xfs_itobp(mp, tp, ip, &dip, &ibp, 0, 0);
1971 if (error)
1972 return error;
1973
Christoph Hellwig347d1c02007-08-28 13:57:51 +10001974 ASSERT(be32_to_cpu(dip->di_next_unlinked) == NULLAGINO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 /* both on-disk, don't endian flip twice */
1976 dip->di_next_unlinked = agi->agi_unlinked[bucket_index];
1977 offset = ip->i_boffset +
1978 offsetof(xfs_dinode_t, di_next_unlinked);
1979 xfs_trans_inode_buf(tp, ibp);
1980 xfs_trans_log_buf(tp, ibp, offset,
1981 (offset + sizeof(xfs_agino_t) - 1));
1982 xfs_inobp_check(mp, ibp);
1983 }
1984
1985 /*
1986 * Point the bucket head pointer at the inode being inserted.
1987 */
1988 ASSERT(agino != 0);
Christoph Hellwig16259e72005-11-02 15:11:25 +11001989 agi->agi_unlinked[bucket_index] = cpu_to_be32(agino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 offset = offsetof(xfs_agi_t, agi_unlinked) +
1991 (sizeof(xfs_agino_t) * bucket_index);
1992 xfs_trans_log_buf(tp, agibp, offset,
1993 (offset + sizeof(xfs_agino_t) - 1));
1994 return 0;
1995}
1996
1997/*
1998 * Pull the on-disk inode from the AGI unlinked list.
1999 */
2000STATIC int
2001xfs_iunlink_remove(
2002 xfs_trans_t *tp,
2003 xfs_inode_t *ip)
2004{
2005 xfs_ino_t next_ino;
2006 xfs_mount_t *mp;
2007 xfs_agi_t *agi;
2008 xfs_dinode_t *dip;
2009 xfs_buf_t *agibp;
2010 xfs_buf_t *ibp;
2011 xfs_agnumber_t agno;
2012 xfs_daddr_t agdaddr;
2013 xfs_agino_t agino;
2014 xfs_agino_t next_agino;
2015 xfs_buf_t *last_ibp;
Nathan Scott6fdf8cc2006-06-28 10:13:52 +10002016 xfs_dinode_t *last_dip = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 short bucket_index;
Nathan Scott6fdf8cc2006-06-28 10:13:52 +10002018 int offset, last_offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 int error;
2020 int agi_ok;
2021
2022 /*
2023 * First pull the on-disk inode from the AGI unlinked list.
2024 */
2025 mp = tp->t_mountp;
2026
2027 agno = XFS_INO_TO_AGNO(mp, ip->i_ino);
2028 agdaddr = XFS_AG_DADDR(mp, agno, XFS_AGI_DADDR(mp));
2029
2030 /*
2031 * Get the agi buffer first. It ensures lock ordering
2032 * on the list.
2033 */
2034 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, agdaddr,
2035 XFS_FSS_TO_BB(mp, 1), 0, &agibp);
2036 if (error) {
2037 cmn_err(CE_WARN,
2038 "xfs_iunlink_remove: xfs_trans_read_buf() returned an error %d on %s. Returning error.",
2039 error, mp->m_fsname);
2040 return error;
2041 }
2042 /*
2043 * Validate the magic number of the agi block.
2044 */
2045 agi = XFS_BUF_TO_AGI(agibp);
2046 agi_ok =
Christoph Hellwig16259e72005-11-02 15:11:25 +11002047 be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC &&
2048 XFS_AGI_GOOD_VERSION(be32_to_cpu(agi->agi_versionnum));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 if (unlikely(XFS_TEST_ERROR(!agi_ok, mp, XFS_ERRTAG_IUNLINK_REMOVE,
2050 XFS_RANDOM_IUNLINK_REMOVE))) {
2051 XFS_CORRUPTION_ERROR("xfs_iunlink_remove", XFS_ERRLEVEL_LOW,
2052 mp, agi);
2053 xfs_trans_brelse(tp, agibp);
2054 cmn_err(CE_WARN,
2055 "xfs_iunlink_remove: XFS_TEST_ERROR() returned an error on %s. Returning EFSCORRUPTED.",
2056 mp->m_fsname);
2057 return XFS_ERROR(EFSCORRUPTED);
2058 }
2059 /*
2060 * Get the index into the agi hash table for the
2061 * list this inode will go on.
2062 */
2063 agino = XFS_INO_TO_AGINO(mp, ip->i_ino);
2064 ASSERT(agino != 0);
2065 bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS;
Christoph Hellwig16259e72005-11-02 15:11:25 +11002066 ASSERT(be32_to_cpu(agi->agi_unlinked[bucket_index]) != NULLAGINO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 ASSERT(agi->agi_unlinked[bucket_index]);
2068
Christoph Hellwig16259e72005-11-02 15:11:25 +11002069 if (be32_to_cpu(agi->agi_unlinked[bucket_index]) == agino) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 /*
2071 * We're at the head of the list. Get the inode's
2072 * on-disk buffer to see if there is anyone after us
2073 * on the list. Only modify our next pointer if it
2074 * is not already NULLAGINO. This saves us the overhead
2075 * of dealing with the buffer when there is no need to
2076 * change it.
2077 */
Nathan Scottb12dd342006-03-17 17:26:04 +11002078 error = xfs_itobp(mp, tp, ip, &dip, &ibp, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 if (error) {
2080 cmn_err(CE_WARN,
2081 "xfs_iunlink_remove: xfs_itobp() returned an error %d on %s. Returning error.",
2082 error, mp->m_fsname);
2083 return error;
2084 }
Christoph Hellwig347d1c02007-08-28 13:57:51 +10002085 next_agino = be32_to_cpu(dip->di_next_unlinked);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 ASSERT(next_agino != 0);
2087 if (next_agino != NULLAGINO) {
Christoph Hellwig347d1c02007-08-28 13:57:51 +10002088 dip->di_next_unlinked = cpu_to_be32(NULLAGINO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 offset = ip->i_boffset +
2090 offsetof(xfs_dinode_t, di_next_unlinked);
2091 xfs_trans_inode_buf(tp, ibp);
2092 xfs_trans_log_buf(tp, ibp, offset,
2093 (offset + sizeof(xfs_agino_t) - 1));
2094 xfs_inobp_check(mp, ibp);
2095 } else {
2096 xfs_trans_brelse(tp, ibp);
2097 }
2098 /*
2099 * Point the bucket head pointer at the next inode.
2100 */
2101 ASSERT(next_agino != 0);
2102 ASSERT(next_agino != agino);
Christoph Hellwig16259e72005-11-02 15:11:25 +11002103 agi->agi_unlinked[bucket_index] = cpu_to_be32(next_agino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 offset = offsetof(xfs_agi_t, agi_unlinked) +
2105 (sizeof(xfs_agino_t) * bucket_index);
2106 xfs_trans_log_buf(tp, agibp, offset,
2107 (offset + sizeof(xfs_agino_t) - 1));
2108 } else {
2109 /*
2110 * We need to search the list for the inode being freed.
2111 */
Christoph Hellwig16259e72005-11-02 15:11:25 +11002112 next_agino = be32_to_cpu(agi->agi_unlinked[bucket_index]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 last_ibp = NULL;
2114 while (next_agino != agino) {
2115 /*
2116 * If the last inode wasn't the one pointing to
2117 * us, then release its buffer since we're not
2118 * going to do anything with it.
2119 */
2120 if (last_ibp != NULL) {
2121 xfs_trans_brelse(tp, last_ibp);
2122 }
2123 next_ino = XFS_AGINO_TO_INO(mp, agno, next_agino);
2124 error = xfs_inotobp(mp, tp, next_ino, &last_dip,
2125 &last_ibp, &last_offset);
2126 if (error) {
2127 cmn_err(CE_WARN,
2128 "xfs_iunlink_remove: xfs_inotobp() returned an error %d on %s. Returning error.",
2129 error, mp->m_fsname);
2130 return error;
2131 }
Christoph Hellwig347d1c02007-08-28 13:57:51 +10002132 next_agino = be32_to_cpu(last_dip->di_next_unlinked);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 ASSERT(next_agino != NULLAGINO);
2134 ASSERT(next_agino != 0);
2135 }
2136 /*
2137 * Now last_ibp points to the buffer previous to us on
2138 * the unlinked list. Pull us from the list.
2139 */
Nathan Scottb12dd342006-03-17 17:26:04 +11002140 error = xfs_itobp(mp, tp, ip, &dip, &ibp, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 if (error) {
2142 cmn_err(CE_WARN,
2143 "xfs_iunlink_remove: xfs_itobp() returned an error %d on %s. Returning error.",
2144 error, mp->m_fsname);
2145 return error;
2146 }
Christoph Hellwig347d1c02007-08-28 13:57:51 +10002147 next_agino = be32_to_cpu(dip->di_next_unlinked);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 ASSERT(next_agino != 0);
2149 ASSERT(next_agino != agino);
2150 if (next_agino != NULLAGINO) {
Christoph Hellwig347d1c02007-08-28 13:57:51 +10002151 dip->di_next_unlinked = cpu_to_be32(NULLAGINO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 offset = ip->i_boffset +
2153 offsetof(xfs_dinode_t, di_next_unlinked);
2154 xfs_trans_inode_buf(tp, ibp);
2155 xfs_trans_log_buf(tp, ibp, offset,
2156 (offset + sizeof(xfs_agino_t) - 1));
2157 xfs_inobp_check(mp, ibp);
2158 } else {
2159 xfs_trans_brelse(tp, ibp);
2160 }
2161 /*
2162 * Point the previous inode on the list to the next inode.
2163 */
Christoph Hellwig347d1c02007-08-28 13:57:51 +10002164 last_dip->di_next_unlinked = cpu_to_be32(next_agino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 ASSERT(next_agino != 0);
2166 offset = last_offset + offsetof(xfs_dinode_t, di_next_unlinked);
2167 xfs_trans_inode_buf(tp, last_ibp);
2168 xfs_trans_log_buf(tp, last_ibp, offset,
2169 (offset + sizeof(xfs_agino_t) - 1));
2170 xfs_inobp_check(mp, last_ibp);
2171 }
2172 return 0;
2173}
2174
David Chinner7989cb82007-02-10 18:34:56 +11002175STATIC_INLINE int xfs_inode_clean(xfs_inode_t *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176{
2177 return (((ip->i_itemp == NULL) ||
2178 !(ip->i_itemp->ili_format.ilf_fields & XFS_ILOG_ALL)) &&
2179 (ip->i_update_core == 0));
2180}
2181
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10002182STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183xfs_ifree_cluster(
2184 xfs_inode_t *free_ip,
2185 xfs_trans_t *tp,
2186 xfs_ino_t inum)
2187{
2188 xfs_mount_t *mp = free_ip->i_mount;
2189 int blks_per_cluster;
2190 int nbufs;
2191 int ninodes;
2192 int i, j, found, pre_flushed;
2193 xfs_daddr_t blkno;
2194 xfs_buf_t *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 xfs_inode_t *ip, **ip_found;
2196 xfs_inode_log_item_t *iip;
2197 xfs_log_item_t *lip;
David Chinnerda353b02007-08-28 14:00:13 +10002198 xfs_perag_t *pag = xfs_get_perag(mp, inum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199
2200 if (mp->m_sb.sb_blocksize >= XFS_INODE_CLUSTER_SIZE(mp)) {
2201 blks_per_cluster = 1;
2202 ninodes = mp->m_sb.sb_inopblock;
2203 nbufs = XFS_IALLOC_BLOCKS(mp);
2204 } else {
2205 blks_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) /
2206 mp->m_sb.sb_blocksize;
2207 ninodes = blks_per_cluster * mp->m_sb.sb_inopblock;
2208 nbufs = XFS_IALLOC_BLOCKS(mp) / blks_per_cluster;
2209 }
2210
2211 ip_found = kmem_alloc(ninodes * sizeof(xfs_inode_t *), KM_NOFS);
2212
2213 for (j = 0; j < nbufs; j++, inum += ninodes) {
2214 blkno = XFS_AGB_TO_DADDR(mp, XFS_INO_TO_AGNO(mp, inum),
2215 XFS_INO_TO_AGBNO(mp, inum));
2216
2217
2218 /*
2219 * Look for each inode in memory and attempt to lock it,
2220 * we can be racing with flush and tail pushing here.
2221 * any inode we get the locks on, add to an array of
2222 * inode items to process later.
2223 *
2224 * The get the buffer lock, we could beat a flush
2225 * or tail pushing thread to the lock here, in which
2226 * case they will go looking for the inode buffer
2227 * and fail, we need some other form of interlock
2228 * here.
2229 */
2230 found = 0;
2231 for (i = 0; i < ninodes; i++) {
David Chinnerda353b02007-08-28 14:00:13 +10002232 read_lock(&pag->pag_ici_lock);
2233 ip = radix_tree_lookup(&pag->pag_ici_root,
2234 XFS_INO_TO_AGINO(mp, (inum + i)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235
2236 /* Inode not in memory or we found it already,
2237 * nothing to do
2238 */
David Chinner7a18c382006-11-11 18:04:54 +11002239 if (!ip || xfs_iflags_test(ip, XFS_ISTALE)) {
David Chinnerda353b02007-08-28 14:00:13 +10002240 read_unlock(&pag->pag_ici_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 continue;
2242 }
2243
2244 if (xfs_inode_clean(ip)) {
David Chinnerda353b02007-08-28 14:00:13 +10002245 read_unlock(&pag->pag_ici_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 continue;
2247 }
2248
2249 /* If we can get the locks then add it to the
2250 * list, otherwise by the time we get the bp lock
2251 * below it will already be attached to the
2252 * inode buffer.
2253 */
2254
2255 /* This inode will already be locked - by us, lets
2256 * keep it that way.
2257 */
2258
2259 if (ip == free_ip) {
2260 if (xfs_iflock_nowait(ip)) {
David Chinner7a18c382006-11-11 18:04:54 +11002261 xfs_iflags_set(ip, XFS_ISTALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 if (xfs_inode_clean(ip)) {
2263 xfs_ifunlock(ip);
2264 } else {
2265 ip_found[found++] = ip;
2266 }
2267 }
David Chinnerda353b02007-08-28 14:00:13 +10002268 read_unlock(&pag->pag_ici_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 continue;
2270 }
2271
2272 if (xfs_ilock_nowait(ip, XFS_ILOCK_EXCL)) {
2273 if (xfs_iflock_nowait(ip)) {
David Chinner7a18c382006-11-11 18:04:54 +11002274 xfs_iflags_set(ip, XFS_ISTALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275
2276 if (xfs_inode_clean(ip)) {
2277 xfs_ifunlock(ip);
2278 xfs_iunlock(ip, XFS_ILOCK_EXCL);
2279 } else {
2280 ip_found[found++] = ip;
2281 }
2282 } else {
2283 xfs_iunlock(ip, XFS_ILOCK_EXCL);
2284 }
2285 }
David Chinnerda353b02007-08-28 14:00:13 +10002286 read_unlock(&pag->pag_ici_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 }
2288
2289 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, blkno,
2290 mp->m_bsize * blks_per_cluster,
2291 XFS_BUF_LOCK);
2292
2293 pre_flushed = 0;
2294 lip = XFS_BUF_FSPRIVATE(bp, xfs_log_item_t *);
2295 while (lip) {
2296 if (lip->li_type == XFS_LI_INODE) {
2297 iip = (xfs_inode_log_item_t *)lip;
2298 ASSERT(iip->ili_logged == 1);
2299 lip->li_cb = (void(*)(xfs_buf_t*,xfs_log_item_t*)) xfs_istale_done;
Donald Douwsma287f3da2007-10-11 17:36:05 +10002300 spin_lock(&mp->m_ail_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 iip->ili_flush_lsn = iip->ili_item.li_lsn;
Donald Douwsma287f3da2007-10-11 17:36:05 +10002302 spin_unlock(&mp->m_ail_lock);
David Chinnere5ffd2b2006-11-21 18:55:33 +11002303 xfs_iflags_set(iip->ili_inode, XFS_ISTALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 pre_flushed++;
2305 }
2306 lip = lip->li_bio_list;
2307 }
2308
2309 for (i = 0; i < found; i++) {
2310 ip = ip_found[i];
2311 iip = ip->i_itemp;
2312
2313 if (!iip) {
2314 ip->i_update_core = 0;
2315 xfs_ifunlock(ip);
2316 xfs_iunlock(ip, XFS_ILOCK_EXCL);
2317 continue;
2318 }
2319
2320 iip->ili_last_fields = iip->ili_format.ilf_fields;
2321 iip->ili_format.ilf_fields = 0;
2322 iip->ili_logged = 1;
Donald Douwsma287f3da2007-10-11 17:36:05 +10002323 spin_lock(&mp->m_ail_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 iip->ili_flush_lsn = iip->ili_item.li_lsn;
Donald Douwsma287f3da2007-10-11 17:36:05 +10002325 spin_unlock(&mp->m_ail_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326
2327 xfs_buf_attach_iodone(bp,
2328 (void(*)(xfs_buf_t*,xfs_log_item_t*))
2329 xfs_istale_done, (xfs_log_item_t *)iip);
2330 if (ip != free_ip) {
2331 xfs_iunlock(ip, XFS_ILOCK_EXCL);
2332 }
2333 }
2334
2335 if (found || pre_flushed)
2336 xfs_trans_stale_inode_buf(tp, bp);
2337 xfs_trans_binval(tp, bp);
2338 }
2339
2340 kmem_free(ip_found, ninodes * sizeof(xfs_inode_t *));
David Chinnerda353b02007-08-28 14:00:13 +10002341 xfs_put_perag(mp, pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342}
2343
2344/*
2345 * This is called to return an inode to the inode free list.
2346 * The inode should already be truncated to 0 length and have
2347 * no pages associated with it. This routine also assumes that
2348 * the inode is already a part of the transaction.
2349 *
2350 * The on-disk copy of the inode will have been added to the list
2351 * of unlinked inodes in the AGI. We need to remove the inode from
2352 * that list atomically with respect to freeing it here.
2353 */
2354int
2355xfs_ifree(
2356 xfs_trans_t *tp,
2357 xfs_inode_t *ip,
2358 xfs_bmap_free_t *flist)
2359{
2360 int error;
2361 int delete;
2362 xfs_ino_t first_ino;
Vlad Apostolovc319b582007-11-23 16:27:51 +11002363 xfs_dinode_t *dip;
2364 xfs_buf_t *ibp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365
2366 ASSERT(ismrlocked(&ip->i_lock, MR_UPDATE));
2367 ASSERT(ip->i_transp == tp);
2368 ASSERT(ip->i_d.di_nlink == 0);
2369 ASSERT(ip->i_d.di_nextents == 0);
2370 ASSERT(ip->i_d.di_anextents == 0);
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10002371 ASSERT((ip->i_d.di_size == 0 && ip->i_size == 0) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 ((ip->i_d.di_mode & S_IFMT) != S_IFREG));
2373 ASSERT(ip->i_d.di_nblocks == 0);
2374
2375 /*
2376 * Pull the on-disk inode from the AGI unlinked list.
2377 */
2378 error = xfs_iunlink_remove(tp, ip);
2379 if (error != 0) {
2380 return error;
2381 }
2382
2383 error = xfs_difree(tp, ip->i_ino, flist, &delete, &first_ino);
2384 if (error != 0) {
2385 return error;
2386 }
2387 ip->i_d.di_mode = 0; /* mark incore inode as free */
2388 ip->i_d.di_flags = 0;
2389 ip->i_d.di_dmevmask = 0;
2390 ip->i_d.di_forkoff = 0; /* mark the attr fork not in use */
2391 ip->i_df.if_ext_max =
2392 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
2393 ip->i_d.di_format = XFS_DINODE_FMT_EXTENTS;
2394 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
2395 /*
2396 * Bump the generation count so no one will be confused
2397 * by reincarnations of this inode.
2398 */
2399 ip->i_d.di_gen++;
Vlad Apostolovc319b582007-11-23 16:27:51 +11002400
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
2402
Vlad Apostolovc319b582007-11-23 16:27:51 +11002403 error = xfs_itobp(ip->i_mount, tp, ip, &dip, &ibp, 0, 0);
2404 if (error)
2405 return error;
2406
2407 /*
2408 * Clear the on-disk di_mode. This is to prevent xfs_bulkstat
2409 * from picking up this inode when it is reclaimed (its incore state
2410 * initialzed but not flushed to disk yet). The in-core di_mode is
2411 * already cleared and a corresponding transaction logged.
2412 * The hack here just synchronizes the in-core to on-disk
2413 * di_mode value in advance before the actual inode sync to disk.
2414 * This is OK because the inode is already unlinked and would never
2415 * change its di_mode again for this inode generation.
2416 * This is a temporary hack that would require a proper fix
2417 * in the future.
2418 */
2419 dip->di_core.di_mode = 0;
2420
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 if (delete) {
2422 xfs_ifree_cluster(ip, tp, first_ino);
2423 }
2424
2425 return 0;
2426}
2427
2428/*
2429 * Reallocate the space for if_broot based on the number of records
2430 * being added or deleted as indicated in rec_diff. Move the records
2431 * and pointers in if_broot to fit the new size. When shrinking this
2432 * will eliminate holes between the records and pointers created by
2433 * the caller. When growing this will create holes to be filled in
2434 * by the caller.
2435 *
2436 * The caller must not request to add more records than would fit in
2437 * the on-disk inode root. If the if_broot is currently NULL, then
2438 * if we adding records one will be allocated. The caller must also
2439 * not request that the number of records go below zero, although
2440 * it can go to zero.
2441 *
2442 * ip -- the inode whose if_broot area is changing
2443 * ext_diff -- the change in the number of records, positive or negative,
2444 * requested for the if_broot array.
2445 */
2446void
2447xfs_iroot_realloc(
2448 xfs_inode_t *ip,
2449 int rec_diff,
2450 int whichfork)
2451{
2452 int cur_max;
2453 xfs_ifork_t *ifp;
2454 xfs_bmbt_block_t *new_broot;
2455 int new_max;
2456 size_t new_size;
2457 char *np;
2458 char *op;
2459
2460 /*
2461 * Handle the degenerate case quietly.
2462 */
2463 if (rec_diff == 0) {
2464 return;
2465 }
2466
2467 ifp = XFS_IFORK_PTR(ip, whichfork);
2468 if (rec_diff > 0) {
2469 /*
2470 * If there wasn't any memory allocated before, just
2471 * allocate it now and get out.
2472 */
2473 if (ifp->if_broot_bytes == 0) {
2474 new_size = (size_t)XFS_BMAP_BROOT_SPACE_CALC(rec_diff);
2475 ifp->if_broot = (xfs_bmbt_block_t*)kmem_alloc(new_size,
2476 KM_SLEEP);
2477 ifp->if_broot_bytes = (int)new_size;
2478 return;
2479 }
2480
2481 /*
2482 * If there is already an existing if_broot, then we need
2483 * to realloc() it and shift the pointers to their new
2484 * location. The records don't change location because
2485 * they are kept butted up against the btree block header.
2486 */
2487 cur_max = XFS_BMAP_BROOT_MAXRECS(ifp->if_broot_bytes);
2488 new_max = cur_max + rec_diff;
2489 new_size = (size_t)XFS_BMAP_BROOT_SPACE_CALC(new_max);
2490 ifp->if_broot = (xfs_bmbt_block_t *)
2491 kmem_realloc(ifp->if_broot,
2492 new_size,
2493 (size_t)XFS_BMAP_BROOT_SPACE_CALC(cur_max), /* old size */
2494 KM_SLEEP);
2495 op = (char *)XFS_BMAP_BROOT_PTR_ADDR(ifp->if_broot, 1,
2496 ifp->if_broot_bytes);
2497 np = (char *)XFS_BMAP_BROOT_PTR_ADDR(ifp->if_broot, 1,
2498 (int)new_size);
2499 ifp->if_broot_bytes = (int)new_size;
2500 ASSERT(ifp->if_broot_bytes <=
2501 XFS_IFORK_SIZE(ip, whichfork) + XFS_BROOT_SIZE_ADJ);
2502 memmove(np, op, cur_max * (uint)sizeof(xfs_dfsbno_t));
2503 return;
2504 }
2505
2506 /*
2507 * rec_diff is less than 0. In this case, we are shrinking the
2508 * if_broot buffer. It must already exist. If we go to zero
2509 * records, just get rid of the root and clear the status bit.
2510 */
2511 ASSERT((ifp->if_broot != NULL) && (ifp->if_broot_bytes > 0));
2512 cur_max = XFS_BMAP_BROOT_MAXRECS(ifp->if_broot_bytes);
2513 new_max = cur_max + rec_diff;
2514 ASSERT(new_max >= 0);
2515 if (new_max > 0)
2516 new_size = (size_t)XFS_BMAP_BROOT_SPACE_CALC(new_max);
2517 else
2518 new_size = 0;
2519 if (new_size > 0) {
2520 new_broot = (xfs_bmbt_block_t *)kmem_alloc(new_size, KM_SLEEP);
2521 /*
2522 * First copy over the btree block header.
2523 */
2524 memcpy(new_broot, ifp->if_broot, sizeof(xfs_bmbt_block_t));
2525 } else {
2526 new_broot = NULL;
2527 ifp->if_flags &= ~XFS_IFBROOT;
2528 }
2529
2530 /*
2531 * Only copy the records and pointers if there are any.
2532 */
2533 if (new_max > 0) {
2534 /*
2535 * First copy the records.
2536 */
2537 op = (char *)XFS_BMAP_BROOT_REC_ADDR(ifp->if_broot, 1,
2538 ifp->if_broot_bytes);
2539 np = (char *)XFS_BMAP_BROOT_REC_ADDR(new_broot, 1,
2540 (int)new_size);
2541 memcpy(np, op, new_max * (uint)sizeof(xfs_bmbt_rec_t));
2542
2543 /*
2544 * Then copy the pointers.
2545 */
2546 op = (char *)XFS_BMAP_BROOT_PTR_ADDR(ifp->if_broot, 1,
2547 ifp->if_broot_bytes);
2548 np = (char *)XFS_BMAP_BROOT_PTR_ADDR(new_broot, 1,
2549 (int)new_size);
2550 memcpy(np, op, new_max * (uint)sizeof(xfs_dfsbno_t));
2551 }
2552 kmem_free(ifp->if_broot, ifp->if_broot_bytes);
2553 ifp->if_broot = new_broot;
2554 ifp->if_broot_bytes = (int)new_size;
2555 ASSERT(ifp->if_broot_bytes <=
2556 XFS_IFORK_SIZE(ip, whichfork) + XFS_BROOT_SIZE_ADJ);
2557 return;
2558}
2559
2560
2561/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 * This is called when the amount of space needed for if_data
2563 * is increased or decreased. The change in size is indicated by
2564 * the number of bytes that need to be added or deleted in the
2565 * byte_diff parameter.
2566 *
2567 * If the amount of space needed has decreased below the size of the
2568 * inline buffer, then switch to using the inline buffer. Otherwise,
2569 * use kmem_realloc() or kmem_alloc() to adjust the size of the buffer
2570 * to what is needed.
2571 *
2572 * ip -- the inode whose if_data area is changing
2573 * byte_diff -- the change in the number of bytes, positive or negative,
2574 * requested for the if_data array.
2575 */
2576void
2577xfs_idata_realloc(
2578 xfs_inode_t *ip,
2579 int byte_diff,
2580 int whichfork)
2581{
2582 xfs_ifork_t *ifp;
2583 int new_size;
2584 int real_size;
2585
2586 if (byte_diff == 0) {
2587 return;
2588 }
2589
2590 ifp = XFS_IFORK_PTR(ip, whichfork);
2591 new_size = (int)ifp->if_bytes + byte_diff;
2592 ASSERT(new_size >= 0);
2593
2594 if (new_size == 0) {
2595 if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) {
2596 kmem_free(ifp->if_u1.if_data, ifp->if_real_bytes);
2597 }
2598 ifp->if_u1.if_data = NULL;
2599 real_size = 0;
2600 } else if (new_size <= sizeof(ifp->if_u2.if_inline_data)) {
2601 /*
2602 * If the valid extents/data can fit in if_inline_ext/data,
2603 * copy them from the malloc'd vector and free it.
2604 */
2605 if (ifp->if_u1.if_data == NULL) {
2606 ifp->if_u1.if_data = ifp->if_u2.if_inline_data;
2607 } else if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) {
2608 ASSERT(ifp->if_real_bytes != 0);
2609 memcpy(ifp->if_u2.if_inline_data, ifp->if_u1.if_data,
2610 new_size);
2611 kmem_free(ifp->if_u1.if_data, ifp->if_real_bytes);
2612 ifp->if_u1.if_data = ifp->if_u2.if_inline_data;
2613 }
2614 real_size = 0;
2615 } else {
2616 /*
2617 * Stuck with malloc/realloc.
2618 * For inline data, the underlying buffer must be
2619 * a multiple of 4 bytes in size so that it can be
2620 * logged and stay on word boundaries. We enforce
2621 * that here.
2622 */
2623 real_size = roundup(new_size, 4);
2624 if (ifp->if_u1.if_data == NULL) {
2625 ASSERT(ifp->if_real_bytes == 0);
2626 ifp->if_u1.if_data = kmem_alloc(real_size, KM_SLEEP);
2627 } else if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) {
2628 /*
2629 * Only do the realloc if the underlying size
2630 * is really changing.
2631 */
2632 if (ifp->if_real_bytes != real_size) {
2633 ifp->if_u1.if_data =
2634 kmem_realloc(ifp->if_u1.if_data,
2635 real_size,
2636 ifp->if_real_bytes,
2637 KM_SLEEP);
2638 }
2639 } else {
2640 ASSERT(ifp->if_real_bytes == 0);
2641 ifp->if_u1.if_data = kmem_alloc(real_size, KM_SLEEP);
2642 memcpy(ifp->if_u1.if_data, ifp->if_u2.if_inline_data,
2643 ifp->if_bytes);
2644 }
2645 }
2646 ifp->if_real_bytes = real_size;
2647 ifp->if_bytes = new_size;
2648 ASSERT(ifp->if_bytes <= XFS_IFORK_SIZE(ip, whichfork));
2649}
2650
2651
2652
2653
2654/*
2655 * Map inode to disk block and offset.
2656 *
2657 * mp -- the mount point structure for the current file system
2658 * tp -- the current transaction
2659 * ino -- the inode number of the inode to be located
2660 * imap -- this structure is filled in with the information necessary
2661 * to retrieve the given inode from disk
2662 * flags -- flags to pass to xfs_dilocate indicating whether or not
2663 * lookups in the inode btree were OK or not
2664 */
2665int
2666xfs_imap(
2667 xfs_mount_t *mp,
2668 xfs_trans_t *tp,
2669 xfs_ino_t ino,
2670 xfs_imap_t *imap,
2671 uint flags)
2672{
2673 xfs_fsblock_t fsbno;
2674 int len;
2675 int off;
2676 int error;
2677
2678 fsbno = imap->im_blkno ?
2679 XFS_DADDR_TO_FSB(mp, imap->im_blkno) : NULLFSBLOCK;
2680 error = xfs_dilocate(mp, tp, ino, &fsbno, &len, &off, flags);
2681 if (error != 0) {
2682 return error;
2683 }
2684 imap->im_blkno = XFS_FSB_TO_DADDR(mp, fsbno);
2685 imap->im_len = XFS_FSB_TO_BB(mp, len);
2686 imap->im_agblkno = XFS_FSB_TO_AGBNO(mp, fsbno);
2687 imap->im_ioffset = (ushort)off;
2688 imap->im_boffset = (ushort)(off << mp->m_sb.sb_inodelog);
2689 return 0;
2690}
2691
2692void
2693xfs_idestroy_fork(
2694 xfs_inode_t *ip,
2695 int whichfork)
2696{
2697 xfs_ifork_t *ifp;
2698
2699 ifp = XFS_IFORK_PTR(ip, whichfork);
2700 if (ifp->if_broot != NULL) {
2701 kmem_free(ifp->if_broot, ifp->if_broot_bytes);
2702 ifp->if_broot = NULL;
2703 }
2704
2705 /*
2706 * If the format is local, then we can't have an extents
2707 * array so just look for an inline data array. If we're
2708 * not local then we may or may not have an extents list,
2709 * so check and free it up if we do.
2710 */
2711 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
2712 if ((ifp->if_u1.if_data != ifp->if_u2.if_inline_data) &&
2713 (ifp->if_u1.if_data != NULL)) {
2714 ASSERT(ifp->if_real_bytes != 0);
2715 kmem_free(ifp->if_u1.if_data, ifp->if_real_bytes);
2716 ifp->if_u1.if_data = NULL;
2717 ifp->if_real_bytes = 0;
2718 }
2719 } else if ((ifp->if_flags & XFS_IFEXTENTS) &&
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11002720 ((ifp->if_flags & XFS_IFEXTIREC) ||
2721 ((ifp->if_u1.if_extents != NULL) &&
2722 (ifp->if_u1.if_extents != ifp->if_u2.if_inline_ext)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 ASSERT(ifp->if_real_bytes != 0);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002724 xfs_iext_destroy(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 }
2726 ASSERT(ifp->if_u1.if_extents == NULL ||
2727 ifp->if_u1.if_extents == ifp->if_u2.if_inline_ext);
2728 ASSERT(ifp->if_real_bytes == 0);
2729 if (whichfork == XFS_ATTR_FORK) {
2730 kmem_zone_free(xfs_ifork_zone, ip->i_afp);
2731 ip->i_afp = NULL;
2732 }
2733}
2734
2735/*
2736 * This is called free all the memory associated with an inode.
2737 * It must free the inode itself and any buffers allocated for
2738 * if_extents/if_data and if_broot. It must also free the lock
2739 * associated with the inode.
2740 */
2741void
2742xfs_idestroy(
2743 xfs_inode_t *ip)
2744{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 switch (ip->i_d.di_mode & S_IFMT) {
2746 case S_IFREG:
2747 case S_IFDIR:
2748 case S_IFLNK:
2749 xfs_idestroy_fork(ip, XFS_DATA_FORK);
2750 break;
2751 }
2752 if (ip->i_afp)
2753 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
2754 mrfree(&ip->i_lock);
2755 mrfree(&ip->i_iolock);
2756 freesema(&ip->i_flock);
Christoph Hellwig1543d792007-08-29 11:46:47 +10002757
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002758#ifdef XFS_INODE_TRACE
Christoph Hellwig1543d792007-08-29 11:46:47 +10002759 ktrace_free(ip->i_trace);
2760#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761#ifdef XFS_BMAP_TRACE
2762 ktrace_free(ip->i_xtrace);
2763#endif
2764#ifdef XFS_BMBT_TRACE
2765 ktrace_free(ip->i_btrace);
2766#endif
2767#ifdef XFS_RW_TRACE
2768 ktrace_free(ip->i_rwtrace);
2769#endif
2770#ifdef XFS_ILOCK_TRACE
2771 ktrace_free(ip->i_lock_trace);
2772#endif
2773#ifdef XFS_DIR2_TRACE
2774 ktrace_free(ip->i_dir_trace);
2775#endif
2776 if (ip->i_itemp) {
David Chinnerf74eaf52007-02-10 18:36:04 +11002777 /*
2778 * Only if we are shutting down the fs will we see an
2779 * inode still in the AIL. If it is there, we should remove
2780 * it to prevent a use-after-free from occurring.
2781 */
2782 xfs_mount_t *mp = ip->i_mount;
2783 xfs_log_item_t *lip = &ip->i_itemp->ili_item;
David Chinnerf74eaf52007-02-10 18:36:04 +11002784
2785 ASSERT(((lip->li_flags & XFS_LI_IN_AIL) == 0) ||
2786 XFS_FORCED_SHUTDOWN(ip->i_mount));
2787 if (lip->li_flags & XFS_LI_IN_AIL) {
Donald Douwsma287f3da2007-10-11 17:36:05 +10002788 spin_lock(&mp->m_ail_lock);
David Chinnerf74eaf52007-02-10 18:36:04 +11002789 if (lip->li_flags & XFS_LI_IN_AIL)
Donald Douwsma287f3da2007-10-11 17:36:05 +10002790 xfs_trans_delete_ail(mp, lip);
David Chinnerf74eaf52007-02-10 18:36:04 +11002791 else
Donald Douwsma287f3da2007-10-11 17:36:05 +10002792 spin_unlock(&mp->m_ail_lock);
David Chinnerf74eaf52007-02-10 18:36:04 +11002793 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 xfs_inode_item_destroy(ip);
2795 }
2796 kmem_zone_free(xfs_inode_zone, ip);
2797}
2798
2799
2800/*
2801 * Increment the pin count of the given buffer.
2802 * This value is protected by ipinlock spinlock in the mount structure.
2803 */
2804void
2805xfs_ipin(
2806 xfs_inode_t *ip)
2807{
2808 ASSERT(ismrlocked(&ip->i_lock, MR_UPDATE));
2809
2810 atomic_inc(&ip->i_pincount);
2811}
2812
2813/*
2814 * Decrement the pin count of the given inode, and wake up
2815 * anyone in xfs_iwait_unpin() if the count goes to 0. The
Nathan Scottc41564b2006-03-29 08:55:14 +10002816 * inode must have been previously pinned with a call to xfs_ipin().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 */
2818void
2819xfs_iunpin(
2820 xfs_inode_t *ip)
2821{
2822 ASSERT(atomic_read(&ip->i_pincount) > 0);
2823
David Chinner5d51eff2007-11-23 16:29:18 +11002824 if (atomic_dec_and_test(&ip->i_pincount))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 wake_up(&ip->i_ipin_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826}
2827
2828/*
2829 * This is called to wait for the given inode to be unpinned.
2830 * It will sleep until this happens. The caller must have the
2831 * inode locked in at least shared mode so that the buffer cannot
2832 * be subsequently pinned once someone is waiting for it to be
2833 * unpinned.
2834 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10002835STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836xfs_iunpin_wait(
2837 xfs_inode_t *ip)
2838{
2839 xfs_inode_log_item_t *iip;
2840 xfs_lsn_t lsn;
2841
2842 ASSERT(ismrlocked(&ip->i_lock, MR_UPDATE | MR_ACCESS));
2843
2844 if (atomic_read(&ip->i_pincount) == 0) {
2845 return;
2846 }
2847
2848 iip = ip->i_itemp;
2849 if (iip && iip->ili_last_lsn) {
2850 lsn = iip->ili_last_lsn;
2851 } else {
2852 lsn = (xfs_lsn_t)0;
2853 }
2854
2855 /*
2856 * Give the log a push so we don't wait here too long.
2857 */
2858 xfs_log_force(ip->i_mount, lsn, XFS_LOG_FORCE);
2859
2860 wait_event(ip->i_ipin_wait, (atomic_read(&ip->i_pincount) == 0));
2861}
2862
2863
2864/*
2865 * xfs_iextents_copy()
2866 *
2867 * This is called to copy the REAL extents (as opposed to the delayed
2868 * allocation extents) from the inode into the given buffer. It
2869 * returns the number of bytes copied into the buffer.
2870 *
2871 * If there are no delayed allocation extents, then we can just
2872 * memcpy() the extents into the buffer. Otherwise, we need to
2873 * examine each extent in turn and skip those which are delayed.
2874 */
2875int
2876xfs_iextents_copy(
2877 xfs_inode_t *ip,
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10002878 xfs_bmbt_rec_t *dp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 int whichfork)
2880{
2881 int copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 int i;
2883 xfs_ifork_t *ifp;
2884 int nrecs;
2885 xfs_fsblock_t start_block;
2886
2887 ifp = XFS_IFORK_PTR(ip, whichfork);
2888 ASSERT(ismrlocked(&ip->i_lock, MR_UPDATE|MR_ACCESS));
2889 ASSERT(ifp->if_bytes > 0);
2890
2891 nrecs = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
Eric Sandeen3a59c942007-07-11 11:09:47 +10002892 XFS_BMAP_TRACE_EXLIST(ip, nrecs, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 ASSERT(nrecs > 0);
2894
2895 /*
2896 * There are some delayed allocation extents in the
2897 * inode, so copy the extents one at a time and skip
2898 * the delayed ones. There must be at least one
2899 * non-delayed extent.
2900 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 copied = 0;
2902 for (i = 0; i < nrecs; i++) {
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10002903 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 start_block = xfs_bmbt_get_startblock(ep);
2905 if (ISNULLSTARTBLOCK(start_block)) {
2906 /*
2907 * It's a delayed allocation extent, so skip it.
2908 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 continue;
2910 }
2911
2912 /* Translate to on disk format */
Christoph Hellwigcd8b0a92007-08-16 16:24:15 +10002913 put_unaligned(cpu_to_be64(ep->l0), &dp->l0);
2914 put_unaligned(cpu_to_be64(ep->l1), &dp->l1);
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10002915 dp++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 copied++;
2917 }
2918 ASSERT(copied != 0);
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10002919 xfs_validate_extents(ifp, copied, XFS_EXTFMT_INODE(ip));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920
2921 return (copied * (uint)sizeof(xfs_bmbt_rec_t));
2922}
2923
2924/*
2925 * Each of the following cases stores data into the same region
2926 * of the on-disk inode, so only one of them can be valid at
2927 * any given time. While it is possible to have conflicting formats
2928 * and log flags, e.g. having XFS_ILOG_?DATA set when the fork is
2929 * in EXTENTS format, this can only happen when the fork has
2930 * changed formats after being modified but before being flushed.
2931 * In these cases, the format always takes precedence, because the
2932 * format indicates the current state of the fork.
2933 */
2934/*ARGSUSED*/
2935STATIC int
2936xfs_iflush_fork(
2937 xfs_inode_t *ip,
2938 xfs_dinode_t *dip,
2939 xfs_inode_log_item_t *iip,
2940 int whichfork,
2941 xfs_buf_t *bp)
2942{
2943 char *cp;
2944 xfs_ifork_t *ifp;
2945 xfs_mount_t *mp;
2946#ifdef XFS_TRANS_DEBUG
2947 int first;
2948#endif
2949 static const short brootflag[2] =
2950 { XFS_ILOG_DBROOT, XFS_ILOG_ABROOT };
2951 static const short dataflag[2] =
2952 { XFS_ILOG_DDATA, XFS_ILOG_ADATA };
2953 static const short extflag[2] =
2954 { XFS_ILOG_DEXT, XFS_ILOG_AEXT };
2955
2956 if (iip == NULL)
2957 return 0;
2958 ifp = XFS_IFORK_PTR(ip, whichfork);
2959 /*
2960 * This can happen if we gave up in iformat in an error path,
2961 * for the attribute fork.
2962 */
2963 if (ifp == NULL) {
2964 ASSERT(whichfork == XFS_ATTR_FORK);
2965 return 0;
2966 }
2967 cp = XFS_DFORK_PTR(dip, whichfork);
2968 mp = ip->i_mount;
2969 switch (XFS_IFORK_FORMAT(ip, whichfork)) {
2970 case XFS_DINODE_FMT_LOCAL:
2971 if ((iip->ili_format.ilf_fields & dataflag[whichfork]) &&
2972 (ifp->if_bytes > 0)) {
2973 ASSERT(ifp->if_u1.if_data != NULL);
2974 ASSERT(ifp->if_bytes <= XFS_IFORK_SIZE(ip, whichfork));
2975 memcpy(cp, ifp->if_u1.if_data, ifp->if_bytes);
2976 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 break;
2978
2979 case XFS_DINODE_FMT_EXTENTS:
2980 ASSERT((ifp->if_flags & XFS_IFEXTENTS) ||
2981 !(iip->ili_format.ilf_fields & extflag[whichfork]));
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002982 ASSERT((xfs_iext_get_ext(ifp, 0) != NULL) ||
2983 (ifp->if_bytes == 0));
2984 ASSERT((xfs_iext_get_ext(ifp, 0) == NULL) ||
2985 (ifp->if_bytes > 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 if ((iip->ili_format.ilf_fields & extflag[whichfork]) &&
2987 (ifp->if_bytes > 0)) {
2988 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) > 0);
2989 (void)xfs_iextents_copy(ip, (xfs_bmbt_rec_t *)cp,
2990 whichfork);
2991 }
2992 break;
2993
2994 case XFS_DINODE_FMT_BTREE:
2995 if ((iip->ili_format.ilf_fields & brootflag[whichfork]) &&
2996 (ifp->if_broot_bytes > 0)) {
2997 ASSERT(ifp->if_broot != NULL);
2998 ASSERT(ifp->if_broot_bytes <=
2999 (XFS_IFORK_SIZE(ip, whichfork) +
3000 XFS_BROOT_SIZE_ADJ));
3001 xfs_bmbt_to_bmdr(ifp->if_broot, ifp->if_broot_bytes,
3002 (xfs_bmdr_block_t *)cp,
3003 XFS_DFORK_SIZE(dip, mp, whichfork));
3004 }
3005 break;
3006
3007 case XFS_DINODE_FMT_DEV:
3008 if (iip->ili_format.ilf_fields & XFS_ILOG_DEV) {
3009 ASSERT(whichfork == XFS_DATA_FORK);
Christoph Hellwig347d1c02007-08-28 13:57:51 +10003010 dip->di_u.di_dev = cpu_to_be32(ip->i_df.if_u2.if_rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011 }
3012 break;
3013
3014 case XFS_DINODE_FMT_UUID:
3015 if (iip->ili_format.ilf_fields & XFS_ILOG_UUID) {
3016 ASSERT(whichfork == XFS_DATA_FORK);
3017 memcpy(&dip->di_u.di_muuid, &ip->i_df.if_u2.if_uuid,
3018 sizeof(uuid_t));
3019 }
3020 break;
3021
3022 default:
3023 ASSERT(0);
3024 break;
3025 }
3026
3027 return 0;
3028}
3029
3030/*
3031 * xfs_iflush() will write a modified inode's changes out to the
3032 * inode's on disk home. The caller must have the inode lock held
3033 * in at least shared mode and the inode flush semaphore must be
3034 * held as well. The inode lock will still be held upon return from
3035 * the call and the caller is free to unlock it.
3036 * The inode flush lock will be unlocked when the inode reaches the disk.
3037 * The flags indicate how the inode's buffer should be written out.
3038 */
3039int
3040xfs_iflush(
3041 xfs_inode_t *ip,
3042 uint flags)
3043{
3044 xfs_inode_log_item_t *iip;
3045 xfs_buf_t *bp;
3046 xfs_dinode_t *dip;
3047 xfs_mount_t *mp;
3048 int error;
3049 /* REFERENCED */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 xfs_inode_t *iq;
3051 int clcount; /* count of inodes clustered */
3052 int bufwasdelwri;
David Chinnerda353b02007-08-28 14:00:13 +10003053 struct hlist_node *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 enum { INT_DELWRI = (1 << 0), INT_ASYNC = (1 << 1) };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055
3056 XFS_STATS_INC(xs_iflush_count);
3057
3058 ASSERT(ismrlocked(&ip->i_lock, MR_UPDATE|MR_ACCESS));
Al Viro0d8fee32006-06-19 08:41:30 +10003059 ASSERT(issemalocked(&(ip->i_flock)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE ||
3061 ip->i_d.di_nextents > ip->i_df.if_ext_max);
3062
3063 iip = ip->i_itemp;
3064 mp = ip->i_mount;
3065
3066 /*
3067 * If the inode isn't dirty, then just release the inode
3068 * flush lock and do nothing.
3069 */
3070 if ((ip->i_update_core == 0) &&
3071 ((iip == NULL) || !(iip->ili_format.ilf_fields & XFS_ILOG_ALL))) {
3072 ASSERT((iip != NULL) ?
3073 !(iip->ili_item.li_flags & XFS_LI_IN_AIL) : 1);
3074 xfs_ifunlock(ip);
3075 return 0;
3076 }
3077
3078 /*
3079 * We can't flush the inode until it is unpinned, so
3080 * wait for it. We know noone new can pin it, because
3081 * we are holding the inode lock shared and you need
3082 * to hold it exclusively to pin the inode.
3083 */
3084 xfs_iunpin_wait(ip);
3085
3086 /*
3087 * This may have been unpinned because the filesystem is shutting
3088 * down forcibly. If that's the case we must not write this inode
3089 * to disk, because the log record didn't make it to disk!
3090 */
3091 if (XFS_FORCED_SHUTDOWN(mp)) {
3092 ip->i_update_core = 0;
3093 if (iip)
3094 iip->ili_format.ilf_fields = 0;
3095 xfs_ifunlock(ip);
3096 return XFS_ERROR(EIO);
3097 }
3098
3099 /*
3100 * Get the buffer containing the on-disk inode.
3101 */
Nathan Scottb12dd342006-03-17 17:26:04 +11003102 error = xfs_itobp(mp, NULL, ip, &dip, &bp, 0, 0);
3103 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104 xfs_ifunlock(ip);
3105 return error;
3106 }
3107
3108 /*
3109 * Decide how buffer will be flushed out. This is done before
3110 * the call to xfs_iflush_int because this field is zeroed by it.
3111 */
3112 if (iip != NULL && iip->ili_format.ilf_fields != 0) {
3113 /*
3114 * Flush out the inode buffer according to the directions
3115 * of the caller. In the cases where the caller has given
3116 * us a choice choose the non-delwri case. This is because
3117 * the inode is in the AIL and we need to get it out soon.
3118 */
3119 switch (flags) {
3120 case XFS_IFLUSH_SYNC:
3121 case XFS_IFLUSH_DELWRI_ELSE_SYNC:
3122 flags = 0;
3123 break;
3124 case XFS_IFLUSH_ASYNC:
3125 case XFS_IFLUSH_DELWRI_ELSE_ASYNC:
3126 flags = INT_ASYNC;
3127 break;
3128 case XFS_IFLUSH_DELWRI:
3129 flags = INT_DELWRI;
3130 break;
3131 default:
3132 ASSERT(0);
3133 flags = 0;
3134 break;
3135 }
3136 } else {
3137 switch (flags) {
3138 case XFS_IFLUSH_DELWRI_ELSE_SYNC:
3139 case XFS_IFLUSH_DELWRI_ELSE_ASYNC:
3140 case XFS_IFLUSH_DELWRI:
3141 flags = INT_DELWRI;
3142 break;
3143 case XFS_IFLUSH_ASYNC:
3144 flags = INT_ASYNC;
3145 break;
3146 case XFS_IFLUSH_SYNC:
3147 flags = 0;
3148 break;
3149 default:
3150 ASSERT(0);
3151 flags = 0;
3152 break;
3153 }
3154 }
3155
3156 /*
3157 * First flush out the inode that xfs_iflush was called with.
3158 */
3159 error = xfs_iflush_int(ip, bp);
3160 if (error) {
3161 goto corrupt_out;
3162 }
3163
3164 /*
3165 * inode clustering:
3166 * see if other inodes can be gathered into this write
3167 */
David Chinnerda353b02007-08-28 14:00:13 +10003168 spin_lock(&ip->i_cluster->icl_lock);
3169 ip->i_cluster->icl_buf = bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170
3171 clcount = 0;
David Chinnerda353b02007-08-28 14:00:13 +10003172 hlist_for_each_entry(iq, entry, &ip->i_cluster->icl_inodes, i_cnode) {
3173 if (iq == ip)
3174 continue;
3175
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176 /*
3177 * Do an un-protected check to see if the inode is dirty and
3178 * is a candidate for flushing. These checks will be repeated
3179 * later after the appropriate locks are acquired.
3180 */
3181 iip = iq->i_itemp;
3182 if ((iq->i_update_core == 0) &&
3183 ((iip == NULL) ||
3184 !(iip->ili_format.ilf_fields & XFS_ILOG_ALL)) &&
3185 xfs_ipincount(iq) == 0) {
3186 continue;
3187 }
3188
3189 /*
3190 * Try to get locks. If any are unavailable,
3191 * then this inode cannot be flushed and is skipped.
3192 */
3193
3194 /* get inode locks (just i_lock) */
3195 if (xfs_ilock_nowait(iq, XFS_ILOCK_SHARED)) {
3196 /* get inode flush lock */
3197 if (xfs_iflock_nowait(iq)) {
3198 /* check if pinned */
3199 if (xfs_ipincount(iq) == 0) {
3200 /* arriving here means that
3201 * this inode can be flushed.
3202 * first re-check that it's
3203 * dirty
3204 */
3205 iip = iq->i_itemp;
3206 if ((iq->i_update_core != 0)||
3207 ((iip != NULL) &&
3208 (iip->ili_format.ilf_fields & XFS_ILOG_ALL))) {
3209 clcount++;
3210 error = xfs_iflush_int(iq, bp);
3211 if (error) {
3212 xfs_iunlock(iq,
3213 XFS_ILOCK_SHARED);
3214 goto cluster_corrupt_out;
3215 }
3216 } else {
3217 xfs_ifunlock(iq);
3218 }
3219 } else {
3220 xfs_ifunlock(iq);
3221 }
3222 }
3223 xfs_iunlock(iq, XFS_ILOCK_SHARED);
3224 }
3225 }
David Chinnerda353b02007-08-28 14:00:13 +10003226 spin_unlock(&ip->i_cluster->icl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227
3228 if (clcount) {
3229 XFS_STATS_INC(xs_icluster_flushcnt);
3230 XFS_STATS_ADD(xs_icluster_flushinode, clcount);
3231 }
3232
3233 /*
3234 * If the buffer is pinned then push on the log so we won't
3235 * get stuck waiting in the write for too long.
3236 */
3237 if (XFS_BUF_ISPINNED(bp)){
3238 xfs_log_force(mp, (xfs_lsn_t)0, XFS_LOG_FORCE);
3239 }
3240
3241 if (flags & INT_DELWRI) {
3242 xfs_bdwrite(mp, bp);
3243 } else if (flags & INT_ASYNC) {
3244 xfs_bawrite(mp, bp);
3245 } else {
3246 error = xfs_bwrite(mp, bp);
3247 }
3248 return error;
3249
3250corrupt_out:
3251 xfs_buf_relse(bp);
Nathan Scott7d04a332006-06-09 14:58:38 +10003252 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 xfs_iflush_abort(ip);
3254 /*
3255 * Unlocks the flush lock
3256 */
3257 return XFS_ERROR(EFSCORRUPTED);
3258
3259cluster_corrupt_out:
3260 /* Corruption detected in the clustering loop. Invalidate the
3261 * inode buffer and shut down the filesystem.
3262 */
David Chinnerda353b02007-08-28 14:00:13 +10003263 spin_unlock(&ip->i_cluster->icl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264
3265 /*
3266 * Clean up the buffer. If it was B_DELWRI, just release it --
3267 * brelse can handle it with no problems. If not, shut down the
3268 * filesystem before releasing the buffer.
3269 */
3270 if ((bufwasdelwri= XFS_BUF_ISDELAYWRITE(bp))) {
3271 xfs_buf_relse(bp);
3272 }
3273
Nathan Scott7d04a332006-06-09 14:58:38 +10003274 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275
3276 if(!bufwasdelwri) {
3277 /*
3278 * Just like incore_relse: if we have b_iodone functions,
3279 * mark the buffer as an error and call them. Otherwise
3280 * mark it as stale and brelse.
3281 */
3282 if (XFS_BUF_IODONE_FUNC(bp)) {
3283 XFS_BUF_CLR_BDSTRAT_FUNC(bp);
3284 XFS_BUF_UNDONE(bp);
3285 XFS_BUF_STALE(bp);
3286 XFS_BUF_SHUT(bp);
3287 XFS_BUF_ERROR(bp,EIO);
3288 xfs_biodone(bp);
3289 } else {
3290 XFS_BUF_STALE(bp);
3291 xfs_buf_relse(bp);
3292 }
3293 }
3294
3295 xfs_iflush_abort(iq);
3296 /*
3297 * Unlocks the flush lock
3298 */
3299 return XFS_ERROR(EFSCORRUPTED);
3300}
3301
3302
3303STATIC int
3304xfs_iflush_int(
3305 xfs_inode_t *ip,
3306 xfs_buf_t *bp)
3307{
3308 xfs_inode_log_item_t *iip;
3309 xfs_dinode_t *dip;
3310 xfs_mount_t *mp;
3311#ifdef XFS_TRANS_DEBUG
3312 int first;
3313#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314
3315 ASSERT(ismrlocked(&ip->i_lock, MR_UPDATE|MR_ACCESS));
Al Viro0d8fee32006-06-19 08:41:30 +10003316 ASSERT(issemalocked(&(ip->i_flock)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317 ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE ||
3318 ip->i_d.di_nextents > ip->i_df.if_ext_max);
3319
3320 iip = ip->i_itemp;
3321 mp = ip->i_mount;
3322
3323
3324 /*
3325 * If the inode isn't dirty, then just release the inode
3326 * flush lock and do nothing.
3327 */
3328 if ((ip->i_update_core == 0) &&
3329 ((iip == NULL) || !(iip->ili_format.ilf_fields & XFS_ILOG_ALL))) {
3330 xfs_ifunlock(ip);
3331 return 0;
3332 }
3333
3334 /* set *dip = inode's place in the buffer */
3335 dip = (xfs_dinode_t *)xfs_buf_offset(bp, ip->i_boffset);
3336
3337 /*
3338 * Clear i_update_core before copying out the data.
3339 * This is for coordination with our timestamp updates
3340 * that don't hold the inode lock. They will always
3341 * update the timestamps BEFORE setting i_update_core,
3342 * so if we clear i_update_core after they set it we
3343 * are guaranteed to see their updates to the timestamps.
3344 * I believe that this depends on strongly ordered memory
3345 * semantics, but we have that. We use the SYNCHRONIZE
3346 * macro to make sure that the compiler does not reorder
3347 * the i_update_core access below the data copy below.
3348 */
3349 ip->i_update_core = 0;
3350 SYNCHRONIZE();
3351
Christoph Hellwig42fe2b12006-01-11 15:35:17 +11003352 /*
3353 * Make sure to get the latest atime from the Linux inode.
3354 */
3355 xfs_synchronize_atime(ip);
3356
Christoph Hellwig347d1c02007-08-28 13:57:51 +10003357 if (XFS_TEST_ERROR(be16_to_cpu(dip->di_core.di_magic) != XFS_DINODE_MAGIC,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358 mp, XFS_ERRTAG_IFLUSH_1, XFS_RANDOM_IFLUSH_1)) {
3359 xfs_cmn_err(XFS_PTAG_IFLUSH, CE_ALERT, mp,
3360 "xfs_iflush: Bad inode %Lu magic number 0x%x, ptr 0x%p",
Christoph Hellwig347d1c02007-08-28 13:57:51 +10003361 ip->i_ino, be16_to_cpu(dip->di_core.di_magic), dip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362 goto corrupt_out;
3363 }
3364 if (XFS_TEST_ERROR(ip->i_d.di_magic != XFS_DINODE_MAGIC,
3365 mp, XFS_ERRTAG_IFLUSH_2, XFS_RANDOM_IFLUSH_2)) {
3366 xfs_cmn_err(XFS_PTAG_IFLUSH, CE_ALERT, mp,
3367 "xfs_iflush: Bad inode %Lu, ptr 0x%p, magic number 0x%x",
3368 ip->i_ino, ip, ip->i_d.di_magic);
3369 goto corrupt_out;
3370 }
3371 if ((ip->i_d.di_mode & S_IFMT) == S_IFREG) {
3372 if (XFS_TEST_ERROR(
3373 (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS) &&
3374 (ip->i_d.di_format != XFS_DINODE_FMT_BTREE),
3375 mp, XFS_ERRTAG_IFLUSH_3, XFS_RANDOM_IFLUSH_3)) {
3376 xfs_cmn_err(XFS_PTAG_IFLUSH, CE_ALERT, mp,
3377 "xfs_iflush: Bad regular inode %Lu, ptr 0x%p",
3378 ip->i_ino, ip);
3379 goto corrupt_out;
3380 }
3381 } else if ((ip->i_d.di_mode & S_IFMT) == S_IFDIR) {
3382 if (XFS_TEST_ERROR(
3383 (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS) &&
3384 (ip->i_d.di_format != XFS_DINODE_FMT_BTREE) &&
3385 (ip->i_d.di_format != XFS_DINODE_FMT_LOCAL),
3386 mp, XFS_ERRTAG_IFLUSH_4, XFS_RANDOM_IFLUSH_4)) {
3387 xfs_cmn_err(XFS_PTAG_IFLUSH, CE_ALERT, mp,
3388 "xfs_iflush: Bad directory inode %Lu, ptr 0x%p",
3389 ip->i_ino, ip);
3390 goto corrupt_out;
3391 }
3392 }
3393 if (XFS_TEST_ERROR(ip->i_d.di_nextents + ip->i_d.di_anextents >
3394 ip->i_d.di_nblocks, mp, XFS_ERRTAG_IFLUSH_5,
3395 XFS_RANDOM_IFLUSH_5)) {
3396 xfs_cmn_err(XFS_PTAG_IFLUSH, CE_ALERT, mp,
3397 "xfs_iflush: detected corrupt incore inode %Lu, total extents = %d, nblocks = %Ld, ptr 0x%p",
3398 ip->i_ino,
3399 ip->i_d.di_nextents + ip->i_d.di_anextents,
3400 ip->i_d.di_nblocks,
3401 ip);
3402 goto corrupt_out;
3403 }
3404 if (XFS_TEST_ERROR(ip->i_d.di_forkoff > mp->m_sb.sb_inodesize,
3405 mp, XFS_ERRTAG_IFLUSH_6, XFS_RANDOM_IFLUSH_6)) {
3406 xfs_cmn_err(XFS_PTAG_IFLUSH, CE_ALERT, mp,
3407 "xfs_iflush: bad inode %Lu, forkoff 0x%x, ptr 0x%p",
3408 ip->i_ino, ip->i_d.di_forkoff, ip);
3409 goto corrupt_out;
3410 }
3411 /*
3412 * bump the flush iteration count, used to detect flushes which
3413 * postdate a log record during recovery.
3414 */
3415
3416 ip->i_d.di_flushiter++;
3417
3418 /*
3419 * Copy the dirty parts of the inode into the on-disk
3420 * inode. We always copy out the core of the inode,
3421 * because if the inode is dirty at all the core must
3422 * be.
3423 */
Christoph Hellwig347d1c02007-08-28 13:57:51 +10003424 xfs_dinode_to_disk(&dip->di_core, &ip->i_d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425
3426 /* Wrap, we never let the log put out DI_MAX_FLUSH */
3427 if (ip->i_d.di_flushiter == DI_MAX_FLUSH)
3428 ip->i_d.di_flushiter = 0;
3429
3430 /*
3431 * If this is really an old format inode and the superblock version
3432 * has not been updated to support only new format inodes, then
3433 * convert back to the old inode format. If the superblock version
3434 * has been updated, then make the conversion permanent.
3435 */
3436 ASSERT(ip->i_d.di_version == XFS_DINODE_VERSION_1 ||
3437 XFS_SB_VERSION_HASNLINK(&mp->m_sb));
3438 if (ip->i_d.di_version == XFS_DINODE_VERSION_1) {
3439 if (!XFS_SB_VERSION_HASNLINK(&mp->m_sb)) {
3440 /*
3441 * Convert it back.
3442 */
3443 ASSERT(ip->i_d.di_nlink <= XFS_MAXLINK_1);
Christoph Hellwig347d1c02007-08-28 13:57:51 +10003444 dip->di_core.di_onlink = cpu_to_be16(ip->i_d.di_nlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445 } else {
3446 /*
3447 * The superblock version has already been bumped,
3448 * so just make the conversion to the new inode
3449 * format permanent.
3450 */
3451 ip->i_d.di_version = XFS_DINODE_VERSION_2;
Christoph Hellwig347d1c02007-08-28 13:57:51 +10003452 dip->di_core.di_version = XFS_DINODE_VERSION_2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453 ip->i_d.di_onlink = 0;
3454 dip->di_core.di_onlink = 0;
3455 memset(&(ip->i_d.di_pad[0]), 0, sizeof(ip->i_d.di_pad));
3456 memset(&(dip->di_core.di_pad[0]), 0,
3457 sizeof(dip->di_core.di_pad));
3458 ASSERT(ip->i_d.di_projid == 0);
3459 }
3460 }
3461
3462 if (xfs_iflush_fork(ip, dip, iip, XFS_DATA_FORK, bp) == EFSCORRUPTED) {
3463 goto corrupt_out;
3464 }
3465
3466 if (XFS_IFORK_Q(ip)) {
3467 /*
3468 * The only error from xfs_iflush_fork is on the data fork.
3469 */
3470 (void) xfs_iflush_fork(ip, dip, iip, XFS_ATTR_FORK, bp);
3471 }
3472 xfs_inobp_check(mp, bp);
3473
3474 /*
3475 * We've recorded everything logged in the inode, so we'd
3476 * like to clear the ilf_fields bits so we don't log and
3477 * flush things unnecessarily. However, we can't stop
3478 * logging all this information until the data we've copied
3479 * into the disk buffer is written to disk. If we did we might
3480 * overwrite the copy of the inode in the log with all the
3481 * data after re-logging only part of it, and in the face of
3482 * a crash we wouldn't have all the data we need to recover.
3483 *
3484 * What we do is move the bits to the ili_last_fields field.
3485 * When logging the inode, these bits are moved back to the
3486 * ilf_fields field. In the xfs_iflush_done() routine we
3487 * clear ili_last_fields, since we know that the information
3488 * those bits represent is permanently on disk. As long as
3489 * the flush completes before the inode is logged again, then
3490 * both ilf_fields and ili_last_fields will be cleared.
3491 *
3492 * We can play with the ilf_fields bits here, because the inode
3493 * lock must be held exclusively in order to set bits there
3494 * and the flush lock protects the ili_last_fields bits.
3495 * Set ili_logged so the flush done
3496 * routine can tell whether or not to look in the AIL.
3497 * Also, store the current LSN of the inode so that we can tell
3498 * whether the item has moved in the AIL from xfs_iflush_done().
3499 * In order to read the lsn we need the AIL lock, because
3500 * it is a 64 bit value that cannot be read atomically.
3501 */
3502 if (iip != NULL && iip->ili_format.ilf_fields != 0) {
3503 iip->ili_last_fields = iip->ili_format.ilf_fields;
3504 iip->ili_format.ilf_fields = 0;
3505 iip->ili_logged = 1;
3506
3507 ASSERT(sizeof(xfs_lsn_t) == 8); /* don't lock if it shrinks */
Donald Douwsma287f3da2007-10-11 17:36:05 +10003508 spin_lock(&mp->m_ail_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509 iip->ili_flush_lsn = iip->ili_item.li_lsn;
Donald Douwsma287f3da2007-10-11 17:36:05 +10003510 spin_unlock(&mp->m_ail_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511
3512 /*
3513 * Attach the function xfs_iflush_done to the inode's
3514 * buffer. This will remove the inode from the AIL
3515 * and unlock the inode's flush lock when the inode is
3516 * completely written to disk.
3517 */
3518 xfs_buf_attach_iodone(bp, (void(*)(xfs_buf_t*,xfs_log_item_t*))
3519 xfs_iflush_done, (xfs_log_item_t *)iip);
3520
3521 ASSERT(XFS_BUF_FSPRIVATE(bp, void *) != NULL);
3522 ASSERT(XFS_BUF_IODONE_FUNC(bp) != NULL);
3523 } else {
3524 /*
3525 * We're flushing an inode which is not in the AIL and has
3526 * not been logged but has i_update_core set. For this
3527 * case we can use a B_DELWRI flush and immediately drop
3528 * the inode flush lock because we can avoid the whole
3529 * AIL state thing. It's OK to drop the flush lock now,
3530 * because we've already locked the buffer and to do anything
3531 * you really need both.
3532 */
3533 if (iip != NULL) {
3534 ASSERT(iip->ili_logged == 0);
3535 ASSERT(iip->ili_last_fields == 0);
3536 ASSERT((iip->ili_item.li_flags & XFS_LI_IN_AIL) == 0);
3537 }
3538 xfs_ifunlock(ip);
3539 }
3540
3541 return 0;
3542
3543corrupt_out:
3544 return XFS_ERROR(EFSCORRUPTED);
3545}
3546
3547
3548/*
Christoph Hellwigefa80272005-06-21 15:37:17 +10003549 * Flush all inactive inodes in mp.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550 */
Christoph Hellwigefa80272005-06-21 15:37:17 +10003551void
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552xfs_iflush_all(
Christoph Hellwigefa80272005-06-21 15:37:17 +10003553 xfs_mount_t *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555 xfs_inode_t *ip;
Nathan Scott67fcaa72006-06-09 17:00:52 +10003556 bhv_vnode_t *vp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557
Christoph Hellwigefa80272005-06-21 15:37:17 +10003558 again:
3559 XFS_MOUNT_ILOCK(mp);
3560 ip = mp->m_inodes;
3561 if (ip == NULL)
3562 goto out;
3563
3564 do {
3565 /* Make sure we skip markers inserted by sync */
3566 if (ip->i_mount == NULL) {
3567 ip = ip->i_mnext;
3568 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570
Christoph Hellwigefa80272005-06-21 15:37:17 +10003571 vp = XFS_ITOV_NULL(ip);
3572 if (!vp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573 XFS_MOUNT_IUNLOCK(mp);
Christoph Hellwigefa80272005-06-21 15:37:17 +10003574 xfs_finish_reclaim(ip, 0, XFS_IFLUSH_ASYNC);
3575 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577
Christoph Hellwigefa80272005-06-21 15:37:17 +10003578 ASSERT(vn_count(vp) == 0);
3579
3580 ip = ip->i_mnext;
3581 } while (ip != mp->m_inodes);
3582 out:
3583 XFS_MOUNT_IUNLOCK(mp);
3584}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586#ifdef XFS_ILOCK_TRACE
3587ktrace_t *xfs_ilock_trace_buf;
3588
3589void
3590xfs_ilock_trace(xfs_inode_t *ip, int lock, unsigned int lockflags, inst_t *ra)
3591{
3592 ktrace_enter(ip->i_lock_trace,
3593 (void *)ip,
3594 (void *)(unsigned long)lock, /* 1 = LOCK, 3=UNLOCK, etc */
3595 (void *)(unsigned long)lockflags, /* XFS_ILOCK_EXCL etc */
3596 (void *)ra, /* caller of ilock */
3597 (void *)(unsigned long)current_cpu(),
3598 (void *)(unsigned long)current_pid(),
3599 NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL);
3600}
3601#endif
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003602
3603/*
3604 * Return a pointer to the extent record at file index idx.
3605 */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003606xfs_bmbt_rec_host_t *
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003607xfs_iext_get_ext(
3608 xfs_ifork_t *ifp, /* inode fork pointer */
3609 xfs_extnum_t idx) /* index of target extent */
3610{
3611 ASSERT(idx >= 0);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003612 if ((ifp->if_flags & XFS_IFEXTIREC) && (idx == 0)) {
3613 return ifp->if_u1.if_ext_irec->er_extbuf;
3614 } else if (ifp->if_flags & XFS_IFEXTIREC) {
3615 xfs_ext_irec_t *erp; /* irec pointer */
3616 int erp_idx = 0; /* irec index */
3617 xfs_extnum_t page_idx = idx; /* ext index in target list */
3618
3619 erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 0);
3620 return &erp->er_extbuf[page_idx];
3621 } else if (ifp->if_bytes) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003622 return &ifp->if_u1.if_extents[idx];
3623 } else {
3624 return NULL;
3625 }
3626}
3627
3628/*
3629 * Insert new item(s) into the extent records for incore inode
3630 * fork 'ifp'. 'count' new items are inserted at index 'idx'.
3631 */
3632void
3633xfs_iext_insert(
3634 xfs_ifork_t *ifp, /* inode fork pointer */
3635 xfs_extnum_t idx, /* starting index of new items */
3636 xfs_extnum_t count, /* number of inserted items */
3637 xfs_bmbt_irec_t *new) /* items to insert */
3638{
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003639 xfs_extnum_t i; /* extent record index */
3640
3641 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
3642 xfs_iext_add(ifp, idx, count);
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003643 for (i = idx; i < idx + count; i++, new++)
3644 xfs_bmbt_set_all(xfs_iext_get_ext(ifp, i), new);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003645}
3646
3647/*
3648 * This is called when the amount of space required for incore file
3649 * extents needs to be increased. The ext_diff parameter stores the
3650 * number of new extents being added and the idx parameter contains
3651 * the extent index where the new extents will be added. If the new
3652 * extents are being appended, then we just need to (re)allocate and
3653 * initialize the space. Otherwise, if the new extents are being
3654 * inserted into the middle of the existing entries, a bit more work
3655 * is required to make room for the new extents to be inserted. The
3656 * caller is responsible for filling in the new extent entries upon
3657 * return.
3658 */
3659void
3660xfs_iext_add(
3661 xfs_ifork_t *ifp, /* inode fork pointer */
3662 xfs_extnum_t idx, /* index to begin adding exts */
Nathan Scottc41564b2006-03-29 08:55:14 +10003663 int ext_diff) /* number of extents to add */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003664{
3665 int byte_diff; /* new bytes being added */
3666 int new_size; /* size of extents after adding */
3667 xfs_extnum_t nextents; /* number of extents in file */
3668
3669 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3670 ASSERT((idx >= 0) && (idx <= nextents));
3671 byte_diff = ext_diff * sizeof(xfs_bmbt_rec_t);
3672 new_size = ifp->if_bytes + byte_diff;
3673 /*
3674 * If the new number of extents (nextents + ext_diff)
3675 * fits inside the inode, then continue to use the inline
3676 * extent buffer.
3677 */
3678 if (nextents + ext_diff <= XFS_INLINE_EXTS) {
3679 if (idx < nextents) {
3680 memmove(&ifp->if_u2.if_inline_ext[idx + ext_diff],
3681 &ifp->if_u2.if_inline_ext[idx],
3682 (nextents - idx) * sizeof(xfs_bmbt_rec_t));
3683 memset(&ifp->if_u2.if_inline_ext[idx], 0, byte_diff);
3684 }
3685 ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext;
3686 ifp->if_real_bytes = 0;
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003687 ifp->if_lastex = nextents + ext_diff;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003688 }
3689 /*
3690 * Otherwise use a linear (direct) extent list.
3691 * If the extents are currently inside the inode,
3692 * xfs_iext_realloc_direct will switch us from
3693 * inline to direct extent allocation mode.
3694 */
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003695 else if (nextents + ext_diff <= XFS_LINEAR_EXTS) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003696 xfs_iext_realloc_direct(ifp, new_size);
3697 if (idx < nextents) {
3698 memmove(&ifp->if_u1.if_extents[idx + ext_diff],
3699 &ifp->if_u1.if_extents[idx],
3700 (nextents - idx) * sizeof(xfs_bmbt_rec_t));
3701 memset(&ifp->if_u1.if_extents[idx], 0, byte_diff);
3702 }
3703 }
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003704 /* Indirection array */
3705 else {
3706 xfs_ext_irec_t *erp;
3707 int erp_idx = 0;
3708 int page_idx = idx;
3709
3710 ASSERT(nextents + ext_diff > XFS_LINEAR_EXTS);
3711 if (ifp->if_flags & XFS_IFEXTIREC) {
3712 erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 1);
3713 } else {
3714 xfs_iext_irec_init(ifp);
3715 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3716 erp = ifp->if_u1.if_ext_irec;
3717 }
3718 /* Extents fit in target extent page */
3719 if (erp && erp->er_extcount + ext_diff <= XFS_LINEAR_EXTS) {
3720 if (page_idx < erp->er_extcount) {
3721 memmove(&erp->er_extbuf[page_idx + ext_diff],
3722 &erp->er_extbuf[page_idx],
3723 (erp->er_extcount - page_idx) *
3724 sizeof(xfs_bmbt_rec_t));
3725 memset(&erp->er_extbuf[page_idx], 0, byte_diff);
3726 }
3727 erp->er_extcount += ext_diff;
3728 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff);
3729 }
3730 /* Insert a new extent page */
3731 else if (erp) {
3732 xfs_iext_add_indirect_multi(ifp,
3733 erp_idx, page_idx, ext_diff);
3734 }
3735 /*
3736 * If extent(s) are being appended to the last page in
3737 * the indirection array and the new extent(s) don't fit
3738 * in the page, then erp is NULL and erp_idx is set to
3739 * the next index needed in the indirection array.
3740 */
3741 else {
3742 int count = ext_diff;
3743
3744 while (count) {
3745 erp = xfs_iext_irec_new(ifp, erp_idx);
3746 erp->er_extcount = count;
3747 count -= MIN(count, (int)XFS_LINEAR_EXTS);
3748 if (count) {
3749 erp_idx++;
3750 }
3751 }
3752 }
3753 }
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003754 ifp->if_bytes = new_size;
3755}
3756
3757/*
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003758 * This is called when incore extents are being added to the indirection
3759 * array and the new extents do not fit in the target extent list. The
3760 * erp_idx parameter contains the irec index for the target extent list
3761 * in the indirection array, and the idx parameter contains the extent
3762 * index within the list. The number of extents being added is stored
3763 * in the count parameter.
3764 *
3765 * |-------| |-------|
3766 * | | | | idx - number of extents before idx
3767 * | idx | | count |
3768 * | | | | count - number of extents being inserted at idx
3769 * |-------| |-------|
3770 * | count | | nex2 | nex2 - number of extents after idx + count
3771 * |-------| |-------|
3772 */
3773void
3774xfs_iext_add_indirect_multi(
3775 xfs_ifork_t *ifp, /* inode fork pointer */
3776 int erp_idx, /* target extent irec index */
3777 xfs_extnum_t idx, /* index within target list */
3778 int count) /* new extents being added */
3779{
3780 int byte_diff; /* new bytes being added */
3781 xfs_ext_irec_t *erp; /* pointer to irec entry */
3782 xfs_extnum_t ext_diff; /* number of extents to add */
3783 xfs_extnum_t ext_cnt; /* new extents still needed */
3784 xfs_extnum_t nex2; /* extents after idx + count */
3785 xfs_bmbt_rec_t *nex2_ep = NULL; /* temp list for nex2 extents */
3786 int nlists; /* number of irec's (lists) */
3787
3788 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3789 erp = &ifp->if_u1.if_ext_irec[erp_idx];
3790 nex2 = erp->er_extcount - idx;
3791 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3792
3793 /*
3794 * Save second part of target extent list
3795 * (all extents past */
3796 if (nex2) {
3797 byte_diff = nex2 * sizeof(xfs_bmbt_rec_t);
3798 nex2_ep = (xfs_bmbt_rec_t *) kmem_alloc(byte_diff, KM_SLEEP);
3799 memmove(nex2_ep, &erp->er_extbuf[idx], byte_diff);
3800 erp->er_extcount -= nex2;
3801 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, -nex2);
3802 memset(&erp->er_extbuf[idx], 0, byte_diff);
3803 }
3804
3805 /*
3806 * Add the new extents to the end of the target
3807 * list, then allocate new irec record(s) and
3808 * extent buffer(s) as needed to store the rest
3809 * of the new extents.
3810 */
3811 ext_cnt = count;
3812 ext_diff = MIN(ext_cnt, (int)XFS_LINEAR_EXTS - erp->er_extcount);
3813 if (ext_diff) {
3814 erp->er_extcount += ext_diff;
3815 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff);
3816 ext_cnt -= ext_diff;
3817 }
3818 while (ext_cnt) {
3819 erp_idx++;
3820 erp = xfs_iext_irec_new(ifp, erp_idx);
3821 ext_diff = MIN(ext_cnt, (int)XFS_LINEAR_EXTS);
3822 erp->er_extcount = ext_diff;
3823 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff);
3824 ext_cnt -= ext_diff;
3825 }
3826
3827 /* Add nex2 extents back to indirection array */
3828 if (nex2) {
3829 xfs_extnum_t ext_avail;
3830 int i;
3831
3832 byte_diff = nex2 * sizeof(xfs_bmbt_rec_t);
3833 ext_avail = XFS_LINEAR_EXTS - erp->er_extcount;
3834 i = 0;
3835 /*
3836 * If nex2 extents fit in the current page, append
3837 * nex2_ep after the new extents.
3838 */
3839 if (nex2 <= ext_avail) {
3840 i = erp->er_extcount;
3841 }
3842 /*
3843 * Otherwise, check if space is available in the
3844 * next page.
3845 */
3846 else if ((erp_idx < nlists - 1) &&
3847 (nex2 <= (ext_avail = XFS_LINEAR_EXTS -
3848 ifp->if_u1.if_ext_irec[erp_idx+1].er_extcount))) {
3849 erp_idx++;
3850 erp++;
3851 /* Create a hole for nex2 extents */
3852 memmove(&erp->er_extbuf[nex2], erp->er_extbuf,
3853 erp->er_extcount * sizeof(xfs_bmbt_rec_t));
3854 }
3855 /*
3856 * Final choice, create a new extent page for
3857 * nex2 extents.
3858 */
3859 else {
3860 erp_idx++;
3861 erp = xfs_iext_irec_new(ifp, erp_idx);
3862 }
3863 memmove(&erp->er_extbuf[i], nex2_ep, byte_diff);
3864 kmem_free(nex2_ep, byte_diff);
3865 erp->er_extcount += nex2;
3866 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, nex2);
3867 }
3868}
3869
3870/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003871 * This is called when the amount of space required for incore file
3872 * extents needs to be decreased. The ext_diff parameter stores the
3873 * number of extents to be removed and the idx parameter contains
3874 * the extent index where the extents will be removed from.
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003875 *
3876 * If the amount of space needed has decreased below the linear
3877 * limit, XFS_IEXT_BUFSZ, then switch to using the contiguous
3878 * extent array. Otherwise, use kmem_realloc() to adjust the
3879 * size to what is needed.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003880 */
3881void
3882xfs_iext_remove(
3883 xfs_ifork_t *ifp, /* inode fork pointer */
3884 xfs_extnum_t idx, /* index to begin removing exts */
3885 int ext_diff) /* number of extents to remove */
3886{
3887 xfs_extnum_t nextents; /* number of extents in file */
3888 int new_size; /* size of extents after removal */
3889
3890 ASSERT(ext_diff > 0);
3891 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3892 new_size = (nextents - ext_diff) * sizeof(xfs_bmbt_rec_t);
3893
3894 if (new_size == 0) {
3895 xfs_iext_destroy(ifp);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003896 } else if (ifp->if_flags & XFS_IFEXTIREC) {
3897 xfs_iext_remove_indirect(ifp, idx, ext_diff);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003898 } else if (ifp->if_real_bytes) {
3899 xfs_iext_remove_direct(ifp, idx, ext_diff);
3900 } else {
3901 xfs_iext_remove_inline(ifp, idx, ext_diff);
3902 }
3903 ifp->if_bytes = new_size;
3904}
3905
3906/*
3907 * This removes ext_diff extents from the inline buffer, beginning
3908 * at extent index idx.
3909 */
3910void
3911xfs_iext_remove_inline(
3912 xfs_ifork_t *ifp, /* inode fork pointer */
3913 xfs_extnum_t idx, /* index to begin removing exts */
3914 int ext_diff) /* number of extents to remove */
3915{
3916 int nextents; /* number of extents in file */
3917
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003918 ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003919 ASSERT(idx < XFS_INLINE_EXTS);
3920 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3921 ASSERT(((nextents - ext_diff) > 0) &&
3922 (nextents - ext_diff) < XFS_INLINE_EXTS);
3923
3924 if (idx + ext_diff < nextents) {
3925 memmove(&ifp->if_u2.if_inline_ext[idx],
3926 &ifp->if_u2.if_inline_ext[idx + ext_diff],
3927 (nextents - (idx + ext_diff)) *
3928 sizeof(xfs_bmbt_rec_t));
3929 memset(&ifp->if_u2.if_inline_ext[nextents - ext_diff],
3930 0, ext_diff * sizeof(xfs_bmbt_rec_t));
3931 } else {
3932 memset(&ifp->if_u2.if_inline_ext[idx], 0,
3933 ext_diff * sizeof(xfs_bmbt_rec_t));
3934 }
3935}
3936
3937/*
3938 * This removes ext_diff extents from a linear (direct) extent list,
3939 * beginning at extent index idx. If the extents are being removed
3940 * from the end of the list (ie. truncate) then we just need to re-
3941 * allocate the list to remove the extra space. Otherwise, if the
3942 * extents are being removed from the middle of the existing extent
3943 * entries, then we first need to move the extent records beginning
3944 * at idx + ext_diff up in the list to overwrite the records being
3945 * removed, then remove the extra space via kmem_realloc.
3946 */
3947void
3948xfs_iext_remove_direct(
3949 xfs_ifork_t *ifp, /* inode fork pointer */
3950 xfs_extnum_t idx, /* index to begin removing exts */
3951 int ext_diff) /* number of extents to remove */
3952{
3953 xfs_extnum_t nextents; /* number of extents in file */
3954 int new_size; /* size of extents after removal */
3955
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003956 ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003957 new_size = ifp->if_bytes -
3958 (ext_diff * sizeof(xfs_bmbt_rec_t));
3959 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3960
3961 if (new_size == 0) {
3962 xfs_iext_destroy(ifp);
3963 return;
3964 }
3965 /* Move extents up in the list (if needed) */
3966 if (idx + ext_diff < nextents) {
3967 memmove(&ifp->if_u1.if_extents[idx],
3968 &ifp->if_u1.if_extents[idx + ext_diff],
3969 (nextents - (idx + ext_diff)) *
3970 sizeof(xfs_bmbt_rec_t));
3971 }
3972 memset(&ifp->if_u1.if_extents[nextents - ext_diff],
3973 0, ext_diff * sizeof(xfs_bmbt_rec_t));
3974 /*
3975 * Reallocate the direct extent list. If the extents
3976 * will fit inside the inode then xfs_iext_realloc_direct
3977 * will switch from direct to inline extent allocation
3978 * mode for us.
3979 */
3980 xfs_iext_realloc_direct(ifp, new_size);
3981 ifp->if_bytes = new_size;
3982}
3983
3984/*
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003985 * This is called when incore extents are being removed from the
3986 * indirection array and the extents being removed span multiple extent
3987 * buffers. The idx parameter contains the file extent index where we
3988 * want to begin removing extents, and the count parameter contains
3989 * how many extents need to be removed.
3990 *
3991 * |-------| |-------|
3992 * | nex1 | | | nex1 - number of extents before idx
3993 * |-------| | count |
3994 * | | | | count - number of extents being removed at idx
3995 * | count | |-------|
3996 * | | | nex2 | nex2 - number of extents after idx + count
3997 * |-------| |-------|
3998 */
3999void
4000xfs_iext_remove_indirect(
4001 xfs_ifork_t *ifp, /* inode fork pointer */
4002 xfs_extnum_t idx, /* index to begin removing extents */
4003 int count) /* number of extents to remove */
4004{
4005 xfs_ext_irec_t *erp; /* indirection array pointer */
4006 int erp_idx = 0; /* indirection array index */
4007 xfs_extnum_t ext_cnt; /* extents left to remove */
4008 xfs_extnum_t ext_diff; /* extents to remove in current list */
4009 xfs_extnum_t nex1; /* number of extents before idx */
4010 xfs_extnum_t nex2; /* extents after idx + count */
Nathan Scottc41564b2006-03-29 08:55:14 +10004011 int nlists; /* entries in indirection array */
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11004012 int page_idx = idx; /* index in target extent list */
4013
4014 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
4015 erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 0);
4016 ASSERT(erp != NULL);
4017 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
4018 nex1 = page_idx;
4019 ext_cnt = count;
4020 while (ext_cnt) {
4021 nex2 = MAX((erp->er_extcount - (nex1 + ext_cnt)), 0);
4022 ext_diff = MIN(ext_cnt, (erp->er_extcount - nex1));
4023 /*
4024 * Check for deletion of entire list;
4025 * xfs_iext_irec_remove() updates extent offsets.
4026 */
4027 if (ext_diff == erp->er_extcount) {
4028 xfs_iext_irec_remove(ifp, erp_idx);
4029 ext_cnt -= ext_diff;
4030 nex1 = 0;
4031 if (ext_cnt) {
4032 ASSERT(erp_idx < ifp->if_real_bytes /
4033 XFS_IEXT_BUFSZ);
4034 erp = &ifp->if_u1.if_ext_irec[erp_idx];
4035 nex1 = 0;
4036 continue;
4037 } else {
4038 break;
4039 }
4040 }
4041 /* Move extents up (if needed) */
4042 if (nex2) {
4043 memmove(&erp->er_extbuf[nex1],
4044 &erp->er_extbuf[nex1 + ext_diff],
4045 nex2 * sizeof(xfs_bmbt_rec_t));
4046 }
4047 /* Zero out rest of page */
4048 memset(&erp->er_extbuf[nex1 + nex2], 0, (XFS_IEXT_BUFSZ -
4049 ((nex1 + nex2) * sizeof(xfs_bmbt_rec_t))));
4050 /* Update remaining counters */
4051 erp->er_extcount -= ext_diff;
4052 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, -ext_diff);
4053 ext_cnt -= ext_diff;
4054 nex1 = 0;
4055 erp_idx++;
4056 erp++;
4057 }
4058 ifp->if_bytes -= count * sizeof(xfs_bmbt_rec_t);
4059 xfs_iext_irec_compact(ifp);
4060}
4061
4062/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004063 * Create, destroy, or resize a linear (direct) block of extents.
4064 */
4065void
4066xfs_iext_realloc_direct(
4067 xfs_ifork_t *ifp, /* inode fork pointer */
4068 int new_size) /* new size of extents */
4069{
4070 int rnew_size; /* real new size of extents */
4071
4072 rnew_size = new_size;
4073
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11004074 ASSERT(!(ifp->if_flags & XFS_IFEXTIREC) ||
4075 ((new_size >= 0) && (new_size <= XFS_IEXT_BUFSZ) &&
4076 (new_size != ifp->if_real_bytes)));
4077
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004078 /* Free extent records */
4079 if (new_size == 0) {
4080 xfs_iext_destroy(ifp);
4081 }
4082 /* Resize direct extent list and zero any new bytes */
4083 else if (ifp->if_real_bytes) {
4084 /* Check if extents will fit inside the inode */
4085 if (new_size <= XFS_INLINE_EXTS * sizeof(xfs_bmbt_rec_t)) {
4086 xfs_iext_direct_to_inline(ifp, new_size /
4087 (uint)sizeof(xfs_bmbt_rec_t));
4088 ifp->if_bytes = new_size;
4089 return;
4090 }
Vignesh Babu16a087d2007-06-28 16:46:37 +10004091 if (!is_power_of_2(new_size)){
Robert P. J. Day40ebd812007-11-23 16:30:51 +11004092 rnew_size = roundup_pow_of_two(new_size);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004093 }
4094 if (rnew_size != ifp->if_real_bytes) {
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10004095 ifp->if_u1.if_extents =
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004096 kmem_realloc(ifp->if_u1.if_extents,
4097 rnew_size,
4098 ifp->if_real_bytes,
4099 KM_SLEEP);
4100 }
4101 if (rnew_size > ifp->if_real_bytes) {
4102 memset(&ifp->if_u1.if_extents[ifp->if_bytes /
4103 (uint)sizeof(xfs_bmbt_rec_t)], 0,
4104 rnew_size - ifp->if_real_bytes);
4105 }
4106 }
4107 /*
4108 * Switch from the inline extent buffer to a direct
4109 * extent list. Be sure to include the inline extent
4110 * bytes in new_size.
4111 */
4112 else {
4113 new_size += ifp->if_bytes;
Vignesh Babu16a087d2007-06-28 16:46:37 +10004114 if (!is_power_of_2(new_size)) {
Robert P. J. Day40ebd812007-11-23 16:30:51 +11004115 rnew_size = roundup_pow_of_two(new_size);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004116 }
4117 xfs_iext_inline_to_direct(ifp, rnew_size);
4118 }
4119 ifp->if_real_bytes = rnew_size;
4120 ifp->if_bytes = new_size;
4121}
4122
4123/*
4124 * Switch from linear (direct) extent records to inline buffer.
4125 */
4126void
4127xfs_iext_direct_to_inline(
4128 xfs_ifork_t *ifp, /* inode fork pointer */
4129 xfs_extnum_t nextents) /* number of extents in file */
4130{
4131 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
4132 ASSERT(nextents <= XFS_INLINE_EXTS);
4133 /*
4134 * The inline buffer was zeroed when we switched
4135 * from inline to direct extent allocation mode,
4136 * so we don't need to clear it here.
4137 */
4138 memcpy(ifp->if_u2.if_inline_ext, ifp->if_u1.if_extents,
4139 nextents * sizeof(xfs_bmbt_rec_t));
Mandy Kirkconnellfe6c1e72006-06-09 14:51:25 +10004140 kmem_free(ifp->if_u1.if_extents, ifp->if_real_bytes);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004141 ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext;
4142 ifp->if_real_bytes = 0;
4143}
4144
4145/*
4146 * Switch from inline buffer to linear (direct) extent records.
4147 * new_size should already be rounded up to the next power of 2
4148 * by the caller (when appropriate), so use new_size as it is.
4149 * However, since new_size may be rounded up, we can't update
4150 * if_bytes here. It is the caller's responsibility to update
4151 * if_bytes upon return.
4152 */
4153void
4154xfs_iext_inline_to_direct(
4155 xfs_ifork_t *ifp, /* inode fork pointer */
4156 int new_size) /* number of extents in file */
4157{
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10004158 ifp->if_u1.if_extents = kmem_alloc(new_size, KM_SLEEP);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004159 memset(ifp->if_u1.if_extents, 0, new_size);
4160 if (ifp->if_bytes) {
4161 memcpy(ifp->if_u1.if_extents, ifp->if_u2.if_inline_ext,
4162 ifp->if_bytes);
4163 memset(ifp->if_u2.if_inline_ext, 0, XFS_INLINE_EXTS *
4164 sizeof(xfs_bmbt_rec_t));
4165 }
4166 ifp->if_real_bytes = new_size;
4167}
4168
4169/*
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11004170 * Resize an extent indirection array to new_size bytes.
4171 */
4172void
4173xfs_iext_realloc_indirect(
4174 xfs_ifork_t *ifp, /* inode fork pointer */
4175 int new_size) /* new indirection array size */
4176{
4177 int nlists; /* number of irec's (ex lists) */
4178 int size; /* current indirection array size */
4179
4180 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
4181 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
4182 size = nlists * sizeof(xfs_ext_irec_t);
4183 ASSERT(ifp->if_real_bytes);
4184 ASSERT((new_size >= 0) && (new_size != size));
4185 if (new_size == 0) {
4186 xfs_iext_destroy(ifp);
4187 } else {
4188 ifp->if_u1.if_ext_irec = (xfs_ext_irec_t *)
4189 kmem_realloc(ifp->if_u1.if_ext_irec,
4190 new_size, size, KM_SLEEP);
4191 }
4192}
4193
4194/*
4195 * Switch from indirection array to linear (direct) extent allocations.
4196 */
4197void
4198xfs_iext_indirect_to_direct(
4199 xfs_ifork_t *ifp) /* inode fork pointer */
4200{
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10004201 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11004202 xfs_extnum_t nextents; /* number of extents in file */
4203 int size; /* size of file extents */
4204
4205 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
4206 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4207 ASSERT(nextents <= XFS_LINEAR_EXTS);
4208 size = nextents * sizeof(xfs_bmbt_rec_t);
4209
4210 xfs_iext_irec_compact_full(ifp);
4211 ASSERT(ifp->if_real_bytes == XFS_IEXT_BUFSZ);
4212
4213 ep = ifp->if_u1.if_ext_irec->er_extbuf;
4214 kmem_free(ifp->if_u1.if_ext_irec, sizeof(xfs_ext_irec_t));
4215 ifp->if_flags &= ~XFS_IFEXTIREC;
4216 ifp->if_u1.if_extents = ep;
4217 ifp->if_bytes = size;
4218 if (nextents < XFS_LINEAR_EXTS) {
4219 xfs_iext_realloc_direct(ifp, size);
4220 }
4221}
4222
4223/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004224 * Free incore file extents.
4225 */
4226void
4227xfs_iext_destroy(
4228 xfs_ifork_t *ifp) /* inode fork pointer */
4229{
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11004230 if (ifp->if_flags & XFS_IFEXTIREC) {
4231 int erp_idx;
4232 int nlists;
4233
4234 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
4235 for (erp_idx = nlists - 1; erp_idx >= 0 ; erp_idx--) {
4236 xfs_iext_irec_remove(ifp, erp_idx);
4237 }
4238 ifp->if_flags &= ~XFS_IFEXTIREC;
4239 } else if (ifp->if_real_bytes) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004240 kmem_free(ifp->if_u1.if_extents, ifp->if_real_bytes);
4241 } else if (ifp->if_bytes) {
4242 memset(ifp->if_u2.if_inline_ext, 0, XFS_INLINE_EXTS *
4243 sizeof(xfs_bmbt_rec_t));
4244 }
4245 ifp->if_u1.if_extents = NULL;
4246 ifp->if_real_bytes = 0;
4247 ifp->if_bytes = 0;
4248}
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11004249
4250/*
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11004251 * Return a pointer to the extent record for file system block bno.
4252 */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10004253xfs_bmbt_rec_host_t * /* pointer to found extent record */
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11004254xfs_iext_bno_to_ext(
4255 xfs_ifork_t *ifp, /* inode fork pointer */
4256 xfs_fileoff_t bno, /* block number to search for */
4257 xfs_extnum_t *idxp) /* index of target extent */
4258{
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10004259 xfs_bmbt_rec_host_t *base; /* pointer to first extent */
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11004260 xfs_filblks_t blockcount = 0; /* number of blocks in extent */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10004261 xfs_bmbt_rec_host_t *ep = NULL; /* pointer to target extent */
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11004262 xfs_ext_irec_t *erp = NULL; /* indirection array pointer */
Nathan Scottc41564b2006-03-29 08:55:14 +10004263 int high; /* upper boundary in search */
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11004264 xfs_extnum_t idx = 0; /* index of target extent */
Nathan Scottc41564b2006-03-29 08:55:14 +10004265 int low; /* lower boundary in search */
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11004266 xfs_extnum_t nextents; /* number of file extents */
4267 xfs_fileoff_t startoff = 0; /* start offset of extent */
4268
4269 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4270 if (nextents == 0) {
4271 *idxp = 0;
4272 return NULL;
4273 }
4274 low = 0;
4275 if (ifp->if_flags & XFS_IFEXTIREC) {
4276 /* Find target extent list */
4277 int erp_idx = 0;
4278 erp = xfs_iext_bno_to_irec(ifp, bno, &erp_idx);
4279 base = erp->er_extbuf;
4280 high = erp->er_extcount - 1;
4281 } else {
4282 base = ifp->if_u1.if_extents;
4283 high = nextents - 1;
4284 }
4285 /* Binary search extent records */
4286 while (low <= high) {
4287 idx = (low + high) >> 1;
4288 ep = base + idx;
4289 startoff = xfs_bmbt_get_startoff(ep);
4290 blockcount = xfs_bmbt_get_blockcount(ep);
4291 if (bno < startoff) {
4292 high = idx - 1;
4293 } else if (bno >= startoff + blockcount) {
4294 low = idx + 1;
4295 } else {
4296 /* Convert back to file-based extent index */
4297 if (ifp->if_flags & XFS_IFEXTIREC) {
4298 idx += erp->er_extoff;
4299 }
4300 *idxp = idx;
4301 return ep;
4302 }
4303 }
4304 /* Convert back to file-based extent index */
4305 if (ifp->if_flags & XFS_IFEXTIREC) {
4306 idx += erp->er_extoff;
4307 }
4308 if (bno >= startoff + blockcount) {
4309 if (++idx == nextents) {
4310 ep = NULL;
4311 } else {
4312 ep = xfs_iext_get_ext(ifp, idx);
4313 }
4314 }
4315 *idxp = idx;
4316 return ep;
4317}
4318
4319/*
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11004320 * Return a pointer to the indirection array entry containing the
4321 * extent record for filesystem block bno. Store the index of the
4322 * target irec in *erp_idxp.
4323 */
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11004324xfs_ext_irec_t * /* pointer to found extent record */
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11004325xfs_iext_bno_to_irec(
4326 xfs_ifork_t *ifp, /* inode fork pointer */
4327 xfs_fileoff_t bno, /* block number to search for */
4328 int *erp_idxp) /* irec index of target ext list */
4329{
4330 xfs_ext_irec_t *erp = NULL; /* indirection array pointer */
4331 xfs_ext_irec_t *erp_next; /* next indirection array entry */
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11004332 int erp_idx; /* indirection array index */
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11004333 int nlists; /* number of extent irec's (lists) */
4334 int high; /* binary search upper limit */
4335 int low; /* binary search lower limit */
4336
4337 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
4338 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
4339 erp_idx = 0;
4340 low = 0;
4341 high = nlists - 1;
4342 while (low <= high) {
4343 erp_idx = (low + high) >> 1;
4344 erp = &ifp->if_u1.if_ext_irec[erp_idx];
4345 erp_next = erp_idx < nlists - 1 ? erp + 1 : NULL;
4346 if (bno < xfs_bmbt_get_startoff(erp->er_extbuf)) {
4347 high = erp_idx - 1;
4348 } else if (erp_next && bno >=
4349 xfs_bmbt_get_startoff(erp_next->er_extbuf)) {
4350 low = erp_idx + 1;
4351 } else {
4352 break;
4353 }
4354 }
4355 *erp_idxp = erp_idx;
4356 return erp;
4357}
4358
4359/*
4360 * Return a pointer to the indirection array entry containing the
4361 * extent record at file extent index *idxp. Store the index of the
4362 * target irec in *erp_idxp and store the page index of the target
4363 * extent record in *idxp.
4364 */
4365xfs_ext_irec_t *
4366xfs_iext_idx_to_irec(
4367 xfs_ifork_t *ifp, /* inode fork pointer */
4368 xfs_extnum_t *idxp, /* extent index (file -> page) */
4369 int *erp_idxp, /* pointer to target irec */
4370 int realloc) /* new bytes were just added */
4371{
4372 xfs_ext_irec_t *prev; /* pointer to previous irec */
4373 xfs_ext_irec_t *erp = NULL; /* pointer to current irec */
4374 int erp_idx; /* indirection array index */
4375 int nlists; /* number of irec's (ex lists) */
4376 int high; /* binary search upper limit */
4377 int low; /* binary search lower limit */
4378 xfs_extnum_t page_idx = *idxp; /* extent index in target list */
4379
4380 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
4381 ASSERT(page_idx >= 0 && page_idx <=
4382 ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t));
4383 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
4384 erp_idx = 0;
4385 low = 0;
4386 high = nlists - 1;
4387
4388 /* Binary search extent irec's */
4389 while (low <= high) {
4390 erp_idx = (low + high) >> 1;
4391 erp = &ifp->if_u1.if_ext_irec[erp_idx];
4392 prev = erp_idx > 0 ? erp - 1 : NULL;
4393 if (page_idx < erp->er_extoff || (page_idx == erp->er_extoff &&
4394 realloc && prev && prev->er_extcount < XFS_LINEAR_EXTS)) {
4395 high = erp_idx - 1;
4396 } else if (page_idx > erp->er_extoff + erp->er_extcount ||
4397 (page_idx == erp->er_extoff + erp->er_extcount &&
4398 !realloc)) {
4399 low = erp_idx + 1;
4400 } else if (page_idx == erp->er_extoff + erp->er_extcount &&
4401 erp->er_extcount == XFS_LINEAR_EXTS) {
4402 ASSERT(realloc);
4403 page_idx = 0;
4404 erp_idx++;
4405 erp = erp_idx < nlists ? erp + 1 : NULL;
4406 break;
4407 } else {
4408 page_idx -= erp->er_extoff;
4409 break;
4410 }
4411 }
4412 *idxp = page_idx;
4413 *erp_idxp = erp_idx;
4414 return(erp);
4415}
4416
4417/*
4418 * Allocate and initialize an indirection array once the space needed
4419 * for incore extents increases above XFS_IEXT_BUFSZ.
4420 */
4421void
4422xfs_iext_irec_init(
4423 xfs_ifork_t *ifp) /* inode fork pointer */
4424{
4425 xfs_ext_irec_t *erp; /* indirection array pointer */
4426 xfs_extnum_t nextents; /* number of extents in file */
4427
4428 ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
4429 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4430 ASSERT(nextents <= XFS_LINEAR_EXTS);
4431
4432 erp = (xfs_ext_irec_t *)
4433 kmem_alloc(sizeof(xfs_ext_irec_t), KM_SLEEP);
4434
4435 if (nextents == 0) {
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10004436 ifp->if_u1.if_extents = kmem_alloc(XFS_IEXT_BUFSZ, KM_SLEEP);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11004437 } else if (!ifp->if_real_bytes) {
4438 xfs_iext_inline_to_direct(ifp, XFS_IEXT_BUFSZ);
4439 } else if (ifp->if_real_bytes < XFS_IEXT_BUFSZ) {
4440 xfs_iext_realloc_direct(ifp, XFS_IEXT_BUFSZ);
4441 }
4442 erp->er_extbuf = ifp->if_u1.if_extents;
4443 erp->er_extcount = nextents;
4444 erp->er_extoff = 0;
4445
4446 ifp->if_flags |= XFS_IFEXTIREC;
4447 ifp->if_real_bytes = XFS_IEXT_BUFSZ;
4448 ifp->if_bytes = nextents * sizeof(xfs_bmbt_rec_t);
4449 ifp->if_u1.if_ext_irec = erp;
4450
4451 return;
4452}
4453
4454/*
4455 * Allocate and initialize a new entry in the indirection array.
4456 */
4457xfs_ext_irec_t *
4458xfs_iext_irec_new(
4459 xfs_ifork_t *ifp, /* inode fork pointer */
4460 int erp_idx) /* index for new irec */
4461{
4462 xfs_ext_irec_t *erp; /* indirection array pointer */
4463 int i; /* loop counter */
4464 int nlists; /* number of irec's (ex lists) */
4465
4466 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
4467 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
4468
4469 /* Resize indirection array */
4470 xfs_iext_realloc_indirect(ifp, ++nlists *
4471 sizeof(xfs_ext_irec_t));
4472 /*
4473 * Move records down in the array so the
4474 * new page can use erp_idx.
4475 */
4476 erp = ifp->if_u1.if_ext_irec;
4477 for (i = nlists - 1; i > erp_idx; i--) {
4478 memmove(&erp[i], &erp[i-1], sizeof(xfs_ext_irec_t));
4479 }
4480 ASSERT(i == erp_idx);
4481
4482 /* Initialize new extent record */
4483 erp = ifp->if_u1.if_ext_irec;
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10004484 erp[erp_idx].er_extbuf = kmem_alloc(XFS_IEXT_BUFSZ, KM_SLEEP);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11004485 ifp->if_real_bytes = nlists * XFS_IEXT_BUFSZ;
4486 memset(erp[erp_idx].er_extbuf, 0, XFS_IEXT_BUFSZ);
4487 erp[erp_idx].er_extcount = 0;
4488 erp[erp_idx].er_extoff = erp_idx > 0 ?
4489 erp[erp_idx-1].er_extoff + erp[erp_idx-1].er_extcount : 0;
4490 return (&erp[erp_idx]);
4491}
4492
4493/*
4494 * Remove a record from the indirection array.
4495 */
4496void
4497xfs_iext_irec_remove(
4498 xfs_ifork_t *ifp, /* inode fork pointer */
4499 int erp_idx) /* irec index to remove */
4500{
4501 xfs_ext_irec_t *erp; /* indirection array pointer */
4502 int i; /* loop counter */
4503 int nlists; /* number of irec's (ex lists) */
4504
4505 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
4506 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
4507 erp = &ifp->if_u1.if_ext_irec[erp_idx];
4508 if (erp->er_extbuf) {
4509 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1,
4510 -erp->er_extcount);
4511 kmem_free(erp->er_extbuf, XFS_IEXT_BUFSZ);
4512 }
4513 /* Compact extent records */
4514 erp = ifp->if_u1.if_ext_irec;
4515 for (i = erp_idx; i < nlists - 1; i++) {
4516 memmove(&erp[i], &erp[i+1], sizeof(xfs_ext_irec_t));
4517 }
4518 /*
4519 * Manually free the last extent record from the indirection
4520 * array. A call to xfs_iext_realloc_indirect() with a size
4521 * of zero would result in a call to xfs_iext_destroy() which
4522 * would in turn call this function again, creating a nasty
4523 * infinite loop.
4524 */
4525 if (--nlists) {
4526 xfs_iext_realloc_indirect(ifp,
4527 nlists * sizeof(xfs_ext_irec_t));
4528 } else {
4529 kmem_free(ifp->if_u1.if_ext_irec,
4530 sizeof(xfs_ext_irec_t));
4531 }
4532 ifp->if_real_bytes = nlists * XFS_IEXT_BUFSZ;
4533}
4534
4535/*
4536 * This is called to clean up large amounts of unused memory allocated
4537 * by the indirection array. Before compacting anything though, verify
4538 * that the indirection array is still needed and switch back to the
4539 * linear extent list (or even the inline buffer) if possible. The
4540 * compaction policy is as follows:
4541 *
4542 * Full Compaction: Extents fit into a single page (or inline buffer)
4543 * Full Compaction: Extents occupy less than 10% of allocated space
4544 * Partial Compaction: Extents occupy > 10% and < 50% of allocated space
4545 * No Compaction: Extents occupy at least 50% of allocated space
4546 */
4547void
4548xfs_iext_irec_compact(
4549 xfs_ifork_t *ifp) /* inode fork pointer */
4550{
4551 xfs_extnum_t nextents; /* number of extents in file */
4552 int nlists; /* number of irec's (ex lists) */
4553
4554 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
4555 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
4556 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4557
4558 if (nextents == 0) {
4559 xfs_iext_destroy(ifp);
4560 } else if (nextents <= XFS_INLINE_EXTS) {
4561 xfs_iext_indirect_to_direct(ifp);
4562 xfs_iext_direct_to_inline(ifp, nextents);
4563 } else if (nextents <= XFS_LINEAR_EXTS) {
4564 xfs_iext_indirect_to_direct(ifp);
4565 } else if (nextents < (nlists * XFS_LINEAR_EXTS) >> 3) {
4566 xfs_iext_irec_compact_full(ifp);
4567 } else if (nextents < (nlists * XFS_LINEAR_EXTS) >> 1) {
4568 xfs_iext_irec_compact_pages(ifp);
4569 }
4570}
4571
4572/*
4573 * Combine extents from neighboring extent pages.
4574 */
4575void
4576xfs_iext_irec_compact_pages(
4577 xfs_ifork_t *ifp) /* inode fork pointer */
4578{
4579 xfs_ext_irec_t *erp, *erp_next;/* pointers to irec entries */
4580 int erp_idx = 0; /* indirection array index */
4581 int nlists; /* number of irec's (ex lists) */
4582
4583 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
4584 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
4585 while (erp_idx < nlists - 1) {
4586 erp = &ifp->if_u1.if_ext_irec[erp_idx];
4587 erp_next = erp + 1;
4588 if (erp_next->er_extcount <=
4589 (XFS_LINEAR_EXTS - erp->er_extcount)) {
4590 memmove(&erp->er_extbuf[erp->er_extcount],
4591 erp_next->er_extbuf, erp_next->er_extcount *
4592 sizeof(xfs_bmbt_rec_t));
4593 erp->er_extcount += erp_next->er_extcount;
4594 /*
4595 * Free page before removing extent record
4596 * so er_extoffs don't get modified in
4597 * xfs_iext_irec_remove.
4598 */
4599 kmem_free(erp_next->er_extbuf, XFS_IEXT_BUFSZ);
4600 erp_next->er_extbuf = NULL;
4601 xfs_iext_irec_remove(ifp, erp_idx + 1);
4602 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
4603 } else {
4604 erp_idx++;
4605 }
4606 }
4607}
4608
4609/*
4610 * Fully compact the extent records managed by the indirection array.
4611 */
4612void
4613xfs_iext_irec_compact_full(
4614 xfs_ifork_t *ifp) /* inode fork pointer */
4615{
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10004616 xfs_bmbt_rec_host_t *ep, *ep_next; /* extent record pointers */
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11004617 xfs_ext_irec_t *erp, *erp_next; /* extent irec pointers */
4618 int erp_idx = 0; /* extent irec index */
4619 int ext_avail; /* empty entries in ex list */
4620 int ext_diff; /* number of exts to add */
4621 int nlists; /* number of irec's (ex lists) */
4622
4623 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
4624 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
4625 erp = ifp->if_u1.if_ext_irec;
4626 ep = &erp->er_extbuf[erp->er_extcount];
4627 erp_next = erp + 1;
4628 ep_next = erp_next->er_extbuf;
4629 while (erp_idx < nlists - 1) {
4630 ext_avail = XFS_LINEAR_EXTS - erp->er_extcount;
4631 ext_diff = MIN(ext_avail, erp_next->er_extcount);
4632 memcpy(ep, ep_next, ext_diff * sizeof(xfs_bmbt_rec_t));
4633 erp->er_extcount += ext_diff;
4634 erp_next->er_extcount -= ext_diff;
4635 /* Remove next page */
4636 if (erp_next->er_extcount == 0) {
4637 /*
4638 * Free page before removing extent record
4639 * so er_extoffs don't get modified in
4640 * xfs_iext_irec_remove.
4641 */
4642 kmem_free(erp_next->er_extbuf,
4643 erp_next->er_extcount * sizeof(xfs_bmbt_rec_t));
4644 erp_next->er_extbuf = NULL;
4645 xfs_iext_irec_remove(ifp, erp_idx + 1);
4646 erp = &ifp->if_u1.if_ext_irec[erp_idx];
4647 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
4648 /* Update next page */
4649 } else {
4650 /* Move rest of page up to become next new page */
4651 memmove(erp_next->er_extbuf, ep_next,
4652 erp_next->er_extcount * sizeof(xfs_bmbt_rec_t));
4653 ep_next = erp_next->er_extbuf;
4654 memset(&ep_next[erp_next->er_extcount], 0,
4655 (XFS_LINEAR_EXTS - erp_next->er_extcount) *
4656 sizeof(xfs_bmbt_rec_t));
4657 }
4658 if (erp->er_extcount == XFS_LINEAR_EXTS) {
4659 erp_idx++;
4660 if (erp_idx < nlists)
4661 erp = &ifp->if_u1.if_ext_irec[erp_idx];
4662 else
4663 break;
4664 }
4665 ep = &erp->er_extbuf[erp->er_extcount];
4666 erp_next = erp + 1;
4667 ep_next = erp_next->er_extbuf;
4668 }
4669}
4670
4671/*
4672 * This is called to update the er_extoff field in the indirection
4673 * array when extents have been added or removed from one of the
4674 * extent lists. erp_idx contains the irec index to begin updating
4675 * at and ext_diff contains the number of extents that were added
4676 * or removed.
4677 */
4678void
4679xfs_iext_irec_update_extoffs(
4680 xfs_ifork_t *ifp, /* inode fork pointer */
4681 int erp_idx, /* irec index to update */
4682 int ext_diff) /* number of new extents */
4683{
4684 int i; /* loop counter */
4685 int nlists; /* number of irec's (ex lists */
4686
4687 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
4688 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
4689 for (i = erp_idx; i < nlists; i++) {
4690 ifp->if_u1.if_ext_irec[i].er_extoff += ext_diff;
4691 }
4692}