blob: 59829523ab0708dc06440be55e3aeafc3af78a08 [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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110019#include "xfs_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "xfs_types.h"
Nathan Scotta844f452005-11-02 14:38:42 +110021#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "xfs_log.h"
Nathan Scotta844f452005-11-02 14:38:42 +110023#include "xfs_inum.h"
24#include "xfs_imap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include "xfs_trans.h"
26#include "xfs_trans_priv.h"
27#include "xfs_sb.h"
28#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include "xfs_dir2.h"
30#include "xfs_dmapi.h"
31#include "xfs_mount.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "xfs_bmap_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110033#include "xfs_alloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "xfs_ialloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "xfs_dir2_sf.h"
Nathan Scotta844f452005-11-02 14:38:42 +110036#include "xfs_attr_sf.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include "xfs_dinode.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include "xfs_inode.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include "xfs_buf_item.h"
Nathan Scotta844f452005-11-02 14:38:42 +110040#include "xfs_inode_item.h"
41#include "xfs_btree.h"
42#include "xfs_alloc.h"
43#include "xfs_ialloc.h"
44#include "xfs_bmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include "xfs_rw.h"
46#include "xfs_error.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include "xfs_utils.h"
48#include "xfs_dir2_trace.h"
49#include "xfs_quota.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include "xfs_acl.h"
David Chinner2a82b8b2007-07-11 11:09:12 +100051#include "xfs_filestream.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Vignesh Babu16a087d2007-06-28 16:46:37 +100053#include <linux/log2.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55kmem_zone_t *xfs_ifork_zone;
56kmem_zone_t *xfs_inode_zone;
57kmem_zone_t *xfs_chashlist_zone;
58
59/*
60 * Used in xfs_itruncate(). This is the maximum number of extents
61 * freed from a file in a single transaction.
62 */
63#define XFS_ITRUNC_MAX_EXTENTS 2
64
65STATIC int xfs_iflush_int(xfs_inode_t *, xfs_buf_t *);
66STATIC int xfs_iformat_local(xfs_inode_t *, xfs_dinode_t *, int, int);
67STATIC int xfs_iformat_extents(xfs_inode_t *, xfs_dinode_t *, int);
68STATIC int xfs_iformat_btree(xfs_inode_t *, xfs_dinode_t *, int);
69
70
71#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,
80 int disk,
81 xfs_exntfmt_t fmt)
82{
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +110083 xfs_bmbt_rec_t *ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 xfs_bmbt_irec_t irec;
85 xfs_bmbt_rec_t rec;
86 int i;
87
88 for (i = 0; i < nrecs; i++) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +110089 ep = xfs_iext_get_ext(ifp, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 rec.l0 = get_unaligned((__uint64_t*)&ep->l0);
91 rec.l1 = get_unaligned((__uint64_t*)&ep->l1);
92 if (disk)
93 xfs_bmbt_disk_get_all(&rec, &irec);
94 else
95 xfs_bmbt_get_all(&rec, &irec);
96 if (fmt == XFS_EXTFMT_NOSTATE)
97 ASSERT(irec.br_state == XFS_EXT_NORM);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 }
99}
100#else /* DEBUG */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100101#define xfs_validate_extents(ifp, nrecs, disk, fmt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102#endif /* DEBUG */
103
104/*
105 * Check that none of the inode's in the buffer have a next
106 * unlinked field of 0.
107 */
108#if defined(DEBUG)
109void
110xfs_inobp_check(
111 xfs_mount_t *mp,
112 xfs_buf_t *bp)
113{
114 int i;
115 int j;
116 xfs_dinode_t *dip;
117
118 j = mp->m_inode_cluster_size >> mp->m_sb.sb_inodelog;
119
120 for (i = 0; i < j; i++) {
121 dip = (xfs_dinode_t *)xfs_buf_offset(bp,
122 i * mp->m_sb.sb_inodesize);
123 if (!dip->di_next_unlinked) {
124 xfs_fs_cmn_err(CE_ALERT, mp,
125 "Detected a bogus zero next_unlinked field in incore inode buffer 0x%p. About to pop an ASSERT.",
126 bp);
127 ASSERT(dip->di_next_unlinked);
128 }
129 }
130}
131#endif
132
133/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 * This routine is called to map an inode number within a file
135 * system to the buffer containing the on-disk version of the
136 * inode. It returns a pointer to the buffer containing the
137 * on-disk inode in the bpp parameter, and in the dip parameter
138 * it returns a pointer to the on-disk inode within that buffer.
139 *
140 * If a non-zero error is returned, then the contents of bpp and
141 * dipp are undefined.
142 *
143 * Use xfs_imap() to determine the size and location of the
144 * buffer to read from disk.
145 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000146STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147xfs_inotobp(
148 xfs_mount_t *mp,
149 xfs_trans_t *tp,
150 xfs_ino_t ino,
151 xfs_dinode_t **dipp,
152 xfs_buf_t **bpp,
153 int *offset)
154{
155 int di_ok;
156 xfs_imap_t imap;
157 xfs_buf_t *bp;
158 int error;
159 xfs_dinode_t *dip;
160
161 /*
Nathan Scottc41564b2006-03-29 08:55:14 +1000162 * Call the space management code to find the location of the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 * inode on disk.
164 */
165 imap.im_blkno = 0;
166 error = xfs_imap(mp, tp, ino, &imap, XFS_IMAP_LOOKUP);
167 if (error != 0) {
168 cmn_err(CE_WARN,
169 "xfs_inotobp: xfs_imap() returned an "
170 "error %d on %s. Returning error.", error, mp->m_fsname);
171 return error;
172 }
173
174 /*
175 * If the inode number maps to a block outside the bounds of the
176 * file system then return NULL rather than calling read_buf
177 * and panicing when we get an error from the driver.
178 */
179 if ((imap.im_blkno + imap.im_len) >
180 XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks)) {
181 cmn_err(CE_WARN,
Christoph Hellwigda1650a2005-11-02 10:21:35 +1100182 "xfs_inotobp: inode number (%llu + %d) maps to a block outside the bounds "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 "of the file system %s. Returning EINVAL.",
Christoph Hellwigda1650a2005-11-02 10:21:35 +1100184 (unsigned long long)imap.im_blkno,
185 imap.im_len, mp->m_fsname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 return XFS_ERROR(EINVAL);
187 }
188
189 /*
190 * Read in the buffer. If tp is NULL, xfs_trans_read_buf() will
191 * default to just a read_buf() call.
192 */
193 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, imap.im_blkno,
194 (int)imap.im_len, XFS_BUF_LOCK, &bp);
195
196 if (error) {
197 cmn_err(CE_WARN,
198 "xfs_inotobp: xfs_trans_read_buf() returned an "
199 "error %d on %s. Returning error.", error, mp->m_fsname);
200 return error;
201 }
202 dip = (xfs_dinode_t *)xfs_buf_offset(bp, 0);
203 di_ok =
204 INT_GET(dip->di_core.di_magic, ARCH_CONVERT) == XFS_DINODE_MAGIC &&
205 XFS_DINODE_GOOD_VERSION(INT_GET(dip->di_core.di_version, ARCH_CONVERT));
206 if (unlikely(XFS_TEST_ERROR(!di_ok, mp, XFS_ERRTAG_ITOBP_INOTOBP,
207 XFS_RANDOM_ITOBP_INOTOBP))) {
208 XFS_CORRUPTION_ERROR("xfs_inotobp", XFS_ERRLEVEL_LOW, mp, dip);
209 xfs_trans_brelse(tp, bp);
210 cmn_err(CE_WARN,
211 "xfs_inotobp: XFS_TEST_ERROR() returned an "
212 "error on %s. Returning EFSCORRUPTED.", mp->m_fsname);
213 return XFS_ERROR(EFSCORRUPTED);
214 }
215
216 xfs_inobp_check(mp, bp);
217
218 /*
219 * Set *dipp to point to the on-disk inode in the buffer.
220 */
221 *dipp = (xfs_dinode_t *)xfs_buf_offset(bp, imap.im_boffset);
222 *bpp = bp;
223 *offset = imap.im_boffset;
224 return 0;
225}
226
227
228/*
229 * This routine is called to map an inode to the buffer containing
230 * the on-disk version of the inode. It returns a pointer to the
231 * buffer containing the on-disk inode in the bpp parameter, and in
232 * the dip parameter it returns a pointer to the on-disk inode within
233 * that buffer.
234 *
235 * If a non-zero error is returned, then the contents of bpp and
236 * dipp are undefined.
237 *
238 * If the inode is new and has not yet been initialized, use xfs_imap()
239 * to determine the size and location of the buffer to read from disk.
240 * If the inode has already been mapped to its buffer and read in once,
241 * then use the mapping information stored in the inode rather than
242 * calling xfs_imap(). This allows us to avoid the overhead of looking
243 * at the inode btree for small block file systems (see xfs_dilocate()).
244 * We can tell whether the inode has been mapped in before by comparing
245 * its disk block address to 0. Only uninitialized inodes will have
246 * 0 for the disk block address.
247 */
248int
249xfs_itobp(
250 xfs_mount_t *mp,
251 xfs_trans_t *tp,
252 xfs_inode_t *ip,
253 xfs_dinode_t **dipp,
254 xfs_buf_t **bpp,
Nathan Scottb12dd342006-03-17 17:26:04 +1100255 xfs_daddr_t bno,
256 uint imap_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257{
Nathan Scott4d1a2ed2006-06-09 17:12:28 +1000258 xfs_imap_t imap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 xfs_buf_t *bp;
260 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 int i;
262 int ni;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
264 if (ip->i_blkno == (xfs_daddr_t)0) {
265 /*
266 * Call the space management code to find the location of the
267 * inode on disk.
268 */
269 imap.im_blkno = bno;
Nathan Scottb12dd342006-03-17 17:26:04 +1100270 if ((error = xfs_imap(mp, tp, ip->i_ino, &imap,
271 XFS_IMAP_LOOKUP | imap_flags)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
274 /*
275 * If the inode number maps to a block outside the bounds
276 * of the file system then return NULL rather than calling
277 * read_buf and panicing when we get an error from the
278 * driver.
279 */
280 if ((imap.im_blkno + imap.im_len) >
281 XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks)) {
282#ifdef DEBUG
283 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_itobp: "
284 "(imap.im_blkno (0x%llx) "
285 "+ imap.im_len (0x%llx)) > "
286 " XFS_FSB_TO_BB(mp, "
287 "mp->m_sb.sb_dblocks) (0x%llx)",
288 (unsigned long long) imap.im_blkno,
289 (unsigned long long) imap.im_len,
290 XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks));
291#endif /* DEBUG */
292 return XFS_ERROR(EINVAL);
293 }
294
295 /*
296 * Fill in the fields in the inode that will be used to
297 * map the inode to its buffer from now on.
298 */
299 ip->i_blkno = imap.im_blkno;
300 ip->i_len = imap.im_len;
301 ip->i_boffset = imap.im_boffset;
302 } else {
303 /*
304 * We've already mapped the inode once, so just use the
305 * mapping that we saved the first time.
306 */
307 imap.im_blkno = ip->i_blkno;
308 imap.im_len = ip->i_len;
309 imap.im_boffset = ip->i_boffset;
310 }
311 ASSERT(bno == 0 || bno == imap.im_blkno);
312
313 /*
314 * Read in the buffer. If tp is NULL, xfs_trans_read_buf() will
315 * default to just a read_buf() call.
316 */
317 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, imap.im_blkno,
318 (int)imap.im_len, XFS_BUF_LOCK, &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 if (error) {
320#ifdef DEBUG
321 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_itobp: "
322 "xfs_trans_read_buf() returned error %d, "
323 "imap.im_blkno 0x%llx, imap.im_len 0x%llx",
324 error, (unsigned long long) imap.im_blkno,
325 (unsigned long long) imap.im_len);
326#endif /* DEBUG */
327 return error;
328 }
Nathan Scott4d1a2ed2006-06-09 17:12:28 +1000329
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 /*
331 * Validate the magic number and version of every inode in the buffer
332 * (if DEBUG kernel) or the first inode in the buffer, otherwise.
Nathan Scott4d1a2ed2006-06-09 17:12:28 +1000333 * No validation is done here in userspace (xfs_repair).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 */
Nathan Scott4d1a2ed2006-06-09 17:12:28 +1000335#if !defined(__KERNEL__)
336 ni = 0;
337#elif defined(DEBUG)
Nathan Scott41ff7152006-07-28 17:05:51 +1000338 ni = BBTOB(imap.im_len) >> mp->m_sb.sb_inodelog;
Nathan Scott4d1a2ed2006-06-09 17:12:28 +1000339#else /* usual case */
Nathan Scott41ff7152006-07-28 17:05:51 +1000340 ni = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341#endif
Nathan Scott4d1a2ed2006-06-09 17:12:28 +1000342
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 for (i = 0; i < ni; i++) {
344 int di_ok;
345 xfs_dinode_t *dip;
346
347 dip = (xfs_dinode_t *)xfs_buf_offset(bp,
348 (i << mp->m_sb.sb_inodelog));
349 di_ok = INT_GET(dip->di_core.di_magic, ARCH_CONVERT) == XFS_DINODE_MAGIC &&
350 XFS_DINODE_GOOD_VERSION(INT_GET(dip->di_core.di_version, ARCH_CONVERT));
Nathan Scott41ff7152006-07-28 17:05:51 +1000351 if (unlikely(XFS_TEST_ERROR(!di_ok, mp,
352 XFS_ERRTAG_ITOBP_INOTOBP,
353 XFS_RANDOM_ITOBP_INOTOBP))) {
354 if (imap_flags & XFS_IMAP_BULKSTAT) {
355 xfs_trans_brelse(tp, bp);
356 return XFS_ERROR(EINVAL);
357 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358#ifdef DEBUG
Nathan Scott41ff7152006-07-28 17:05:51 +1000359 cmn_err(CE_ALERT,
Nathan Scott4d1a2ed2006-06-09 17:12:28 +1000360 "Device %s - bad inode magic/vsn "
361 "daddr %lld #%d (magic=%x)",
Nathan Scottb6574522006-06-09 15:29:40 +1000362 XFS_BUFTARG_NAME(mp->m_ddev_targp),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 (unsigned long long)imap.im_blkno, i,
364 INT_GET(dip->di_core.di_magic, ARCH_CONVERT));
365#endif
366 XFS_CORRUPTION_ERROR("xfs_itobp", XFS_ERRLEVEL_HIGH,
367 mp, dip);
368 xfs_trans_brelse(tp, bp);
369 return XFS_ERROR(EFSCORRUPTED);
370 }
371 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
373 xfs_inobp_check(mp, bp);
374
375 /*
376 * Mark the buffer as an inode buffer now that it looks good
377 */
378 XFS_BUF_SET_VTYPE(bp, B_FS_INO);
379
380 /*
381 * Set *dipp to point to the on-disk inode in the buffer.
382 */
383 *dipp = (xfs_dinode_t *)xfs_buf_offset(bp, imap.im_boffset);
384 *bpp = bp;
385 return 0;
386}
387
388/*
389 * Move inode type and inode format specific information from the
390 * on-disk inode to the in-core inode. For fifos, devs, and sockets
391 * this means set if_rdev to the proper value. For files, directories,
392 * and symlinks this means to bring in the in-line data or extent
393 * pointers. For a file in B-tree format, only the root is immediately
394 * brought in-core. The rest will be in-lined in if_extents when it
395 * is first referenced (see xfs_iread_extents()).
396 */
397STATIC int
398xfs_iformat(
399 xfs_inode_t *ip,
400 xfs_dinode_t *dip)
401{
402 xfs_attr_shortform_t *atp;
403 int size;
404 int error;
405 xfs_fsize_t di_size;
406 ip->i_df.if_ext_max =
407 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
408 error = 0;
409
410 if (unlikely(
411 INT_GET(dip->di_core.di_nextents, ARCH_CONVERT) +
412 INT_GET(dip->di_core.di_anextents, ARCH_CONVERT) >
413 INT_GET(dip->di_core.di_nblocks, ARCH_CONVERT))) {
Nathan Scott3762ec62006-01-12 10:29:53 +1100414 xfs_fs_repair_cmn_err(CE_WARN, ip->i_mount,
415 "corrupt dinode %Lu, extent total = %d, nblocks = %Lu.",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 (unsigned long long)ip->i_ino,
417 (int)(INT_GET(dip->di_core.di_nextents, ARCH_CONVERT)
418 + INT_GET(dip->di_core.di_anextents, ARCH_CONVERT)),
419 (unsigned long long)
420 INT_GET(dip->di_core.di_nblocks, ARCH_CONVERT));
421 XFS_CORRUPTION_ERROR("xfs_iformat(1)", XFS_ERRLEVEL_LOW,
422 ip->i_mount, dip);
423 return XFS_ERROR(EFSCORRUPTED);
424 }
425
426 if (unlikely(INT_GET(dip->di_core.di_forkoff, ARCH_CONVERT) > ip->i_mount->m_sb.sb_inodesize)) {
Nathan Scott3762ec62006-01-12 10:29:53 +1100427 xfs_fs_repair_cmn_err(CE_WARN, ip->i_mount,
428 "corrupt dinode %Lu, forkoff = 0x%x.",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 (unsigned long long)ip->i_ino,
430 (int)(INT_GET(dip->di_core.di_forkoff, ARCH_CONVERT)));
431 XFS_CORRUPTION_ERROR("xfs_iformat(2)", XFS_ERRLEVEL_LOW,
432 ip->i_mount, dip);
433 return XFS_ERROR(EFSCORRUPTED);
434 }
435
436 switch (ip->i_d.di_mode & S_IFMT) {
437 case S_IFIFO:
438 case S_IFCHR:
439 case S_IFBLK:
440 case S_IFSOCK:
441 if (unlikely(INT_GET(dip->di_core.di_format, ARCH_CONVERT) != XFS_DINODE_FMT_DEV)) {
442 XFS_CORRUPTION_ERROR("xfs_iformat(3)", XFS_ERRLEVEL_LOW,
443 ip->i_mount, dip);
444 return XFS_ERROR(EFSCORRUPTED);
445 }
446 ip->i_d.di_size = 0;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000447 ip->i_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 ip->i_df.if_u2.if_rdev = INT_GET(dip->di_u.di_dev, ARCH_CONVERT);
449 break;
450
451 case S_IFREG:
452 case S_IFLNK:
453 case S_IFDIR:
454 switch (INT_GET(dip->di_core.di_format, ARCH_CONVERT)) {
455 case XFS_DINODE_FMT_LOCAL:
456 /*
457 * no local regular files yet
458 */
459 if (unlikely((INT_GET(dip->di_core.di_mode, ARCH_CONVERT) & S_IFMT) == S_IFREG)) {
Nathan Scott3762ec62006-01-12 10:29:53 +1100460 xfs_fs_repair_cmn_err(CE_WARN, ip->i_mount,
461 "corrupt inode %Lu "
462 "(local format for regular file).",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 (unsigned long long) ip->i_ino);
464 XFS_CORRUPTION_ERROR("xfs_iformat(4)",
465 XFS_ERRLEVEL_LOW,
466 ip->i_mount, dip);
467 return XFS_ERROR(EFSCORRUPTED);
468 }
469
470 di_size = INT_GET(dip->di_core.di_size, ARCH_CONVERT);
471 if (unlikely(di_size > XFS_DFORK_DSIZE(dip, ip->i_mount))) {
Nathan Scott3762ec62006-01-12 10:29:53 +1100472 xfs_fs_repair_cmn_err(CE_WARN, ip->i_mount,
473 "corrupt inode %Lu "
474 "(bad size %Ld for local inode).",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 (unsigned long long) ip->i_ino,
476 (long long) di_size);
477 XFS_CORRUPTION_ERROR("xfs_iformat(5)",
478 XFS_ERRLEVEL_LOW,
479 ip->i_mount, dip);
480 return XFS_ERROR(EFSCORRUPTED);
481 }
482
483 size = (int)di_size;
484 error = xfs_iformat_local(ip, dip, XFS_DATA_FORK, size);
485 break;
486 case XFS_DINODE_FMT_EXTENTS:
487 error = xfs_iformat_extents(ip, dip, XFS_DATA_FORK);
488 break;
489 case XFS_DINODE_FMT_BTREE:
490 error = xfs_iformat_btree(ip, dip, XFS_DATA_FORK);
491 break;
492 default:
493 XFS_ERROR_REPORT("xfs_iformat(6)", XFS_ERRLEVEL_LOW,
494 ip->i_mount);
495 return XFS_ERROR(EFSCORRUPTED);
496 }
497 break;
498
499 default:
500 XFS_ERROR_REPORT("xfs_iformat(7)", XFS_ERRLEVEL_LOW, ip->i_mount);
501 return XFS_ERROR(EFSCORRUPTED);
502 }
503 if (error) {
504 return error;
505 }
506 if (!XFS_DFORK_Q(dip))
507 return 0;
508 ASSERT(ip->i_afp == NULL);
509 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP);
510 ip->i_afp->if_ext_max =
511 XFS_IFORK_ASIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
512 switch (INT_GET(dip->di_core.di_aformat, ARCH_CONVERT)) {
513 case XFS_DINODE_FMT_LOCAL:
514 atp = (xfs_attr_shortform_t *)XFS_DFORK_APTR(dip);
Nathan Scott3b244aa2006-03-17 17:29:25 +1100515 size = be16_to_cpu(atp->hdr.totsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 error = xfs_iformat_local(ip, dip, XFS_ATTR_FORK, size);
517 break;
518 case XFS_DINODE_FMT_EXTENTS:
519 error = xfs_iformat_extents(ip, dip, XFS_ATTR_FORK);
520 break;
521 case XFS_DINODE_FMT_BTREE:
522 error = xfs_iformat_btree(ip, dip, XFS_ATTR_FORK);
523 break;
524 default:
525 error = XFS_ERROR(EFSCORRUPTED);
526 break;
527 }
528 if (error) {
529 kmem_zone_free(xfs_ifork_zone, ip->i_afp);
530 ip->i_afp = NULL;
531 xfs_idestroy_fork(ip, XFS_DATA_FORK);
532 }
533 return error;
534}
535
536/*
537 * The file is in-lined in the on-disk inode.
538 * If it fits into if_inline_data, then copy
539 * it there, otherwise allocate a buffer for it
540 * and copy the data there. Either way, set
541 * if_data to point at the data.
542 * If we allocate a buffer for the data, make
543 * sure that its size is a multiple of 4 and
544 * record the real size in i_real_bytes.
545 */
546STATIC int
547xfs_iformat_local(
548 xfs_inode_t *ip,
549 xfs_dinode_t *dip,
550 int whichfork,
551 int size)
552{
553 xfs_ifork_t *ifp;
554 int real_size;
555
556 /*
557 * If the size is unreasonable, then something
558 * is wrong and we just bail out rather than crash in
559 * kmem_alloc() or memcpy() below.
560 */
561 if (unlikely(size > XFS_DFORK_SIZE(dip, ip->i_mount, whichfork))) {
Nathan Scott3762ec62006-01-12 10:29:53 +1100562 xfs_fs_repair_cmn_err(CE_WARN, ip->i_mount,
563 "corrupt inode %Lu "
564 "(bad size %d for local fork, size = %d).",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 (unsigned long long) ip->i_ino, size,
566 XFS_DFORK_SIZE(dip, ip->i_mount, whichfork));
567 XFS_CORRUPTION_ERROR("xfs_iformat_local", XFS_ERRLEVEL_LOW,
568 ip->i_mount, dip);
569 return XFS_ERROR(EFSCORRUPTED);
570 }
571 ifp = XFS_IFORK_PTR(ip, whichfork);
572 real_size = 0;
573 if (size == 0)
574 ifp->if_u1.if_data = NULL;
575 else if (size <= sizeof(ifp->if_u2.if_inline_data))
576 ifp->if_u1.if_data = ifp->if_u2.if_inline_data;
577 else {
578 real_size = roundup(size, 4);
579 ifp->if_u1.if_data = kmem_alloc(real_size, KM_SLEEP);
580 }
581 ifp->if_bytes = size;
582 ifp->if_real_bytes = real_size;
583 if (size)
584 memcpy(ifp->if_u1.if_data, XFS_DFORK_PTR(dip, whichfork), size);
585 ifp->if_flags &= ~XFS_IFEXTENTS;
586 ifp->if_flags |= XFS_IFINLINE;
587 return 0;
588}
589
590/*
591 * The file consists of a set of extents all
592 * of which fit into the on-disk inode.
593 * If there are few enough extents to fit into
594 * the if_inline_ext, then copy them there.
595 * Otherwise allocate a buffer for them and copy
596 * them into it. Either way, set if_extents
597 * to point at the extents.
598 */
599STATIC int
600xfs_iformat_extents(
601 xfs_inode_t *ip,
602 xfs_dinode_t *dip,
603 int whichfork)
604{
605 xfs_bmbt_rec_t *ep, *dp;
606 xfs_ifork_t *ifp;
607 int nex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 int size;
609 int i;
610
611 ifp = XFS_IFORK_PTR(ip, whichfork);
612 nex = XFS_DFORK_NEXTENTS(dip, whichfork);
613 size = nex * (uint)sizeof(xfs_bmbt_rec_t);
614
615 /*
616 * If the number of extents is unreasonable, then something
617 * is wrong and we just bail out rather than crash in
618 * kmem_alloc() or memcpy() below.
619 */
620 if (unlikely(size < 0 || size > XFS_DFORK_SIZE(dip, ip->i_mount, whichfork))) {
Nathan Scott3762ec62006-01-12 10:29:53 +1100621 xfs_fs_repair_cmn_err(CE_WARN, ip->i_mount,
622 "corrupt inode %Lu ((a)extents = %d).",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 (unsigned long long) ip->i_ino, nex);
624 XFS_CORRUPTION_ERROR("xfs_iformat_extents(1)", XFS_ERRLEVEL_LOW,
625 ip->i_mount, dip);
626 return XFS_ERROR(EFSCORRUPTED);
627 }
628
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100629 ifp->if_real_bytes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 if (nex == 0)
631 ifp->if_u1.if_extents = NULL;
632 else if (nex <= XFS_INLINE_EXTS)
633 ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100634 else
635 xfs_iext_add(ifp, 0, nex);
636
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 ifp->if_bytes = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 if (size) {
639 dp = (xfs_bmbt_rec_t *) XFS_DFORK_PTR(dip, whichfork);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100640 xfs_validate_extents(ifp, nex, 1, XFS_EXTFMT_INODE(ip));
641 for (i = 0; i < nex; i++, dp++) {
642 ep = xfs_iext_get_ext(ifp, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 ep->l0 = INT_GET(get_unaligned((__uint64_t*)&dp->l0),
644 ARCH_CONVERT);
645 ep->l1 = INT_GET(get_unaligned((__uint64_t*)&dp->l1),
646 ARCH_CONVERT);
647 }
648 xfs_bmap_trace_exlist("xfs_iformat_extents", ip, nex,
649 whichfork);
650 if (whichfork != XFS_DATA_FORK ||
651 XFS_EXTFMT_INODE(ip) == XFS_EXTFMT_NOSTATE)
652 if (unlikely(xfs_check_nostate_extents(
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100653 ifp, 0, nex))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 XFS_ERROR_REPORT("xfs_iformat_extents(2)",
655 XFS_ERRLEVEL_LOW,
656 ip->i_mount);
657 return XFS_ERROR(EFSCORRUPTED);
658 }
659 }
660 ifp->if_flags |= XFS_IFEXTENTS;
661 return 0;
662}
663
664/*
665 * The file has too many extents to fit into
666 * the inode, so they are in B-tree format.
667 * Allocate a buffer for the root of the B-tree
668 * and copy the root into it. The i_extents
669 * field will remain NULL until all of the
670 * extents are read in (when they are needed).
671 */
672STATIC int
673xfs_iformat_btree(
674 xfs_inode_t *ip,
675 xfs_dinode_t *dip,
676 int whichfork)
677{
678 xfs_bmdr_block_t *dfp;
679 xfs_ifork_t *ifp;
680 /* REFERENCED */
681 int nrecs;
682 int size;
683
684 ifp = XFS_IFORK_PTR(ip, whichfork);
685 dfp = (xfs_bmdr_block_t *)XFS_DFORK_PTR(dip, whichfork);
686 size = XFS_BMAP_BROOT_SPACE(dfp);
687 nrecs = XFS_BMAP_BROOT_NUMRECS(dfp);
688
689 /*
690 * blow out if -- fork has less extents than can fit in
691 * fork (fork shouldn't be a btree format), root btree
692 * block has more records than can fit into the fork,
693 * or the number of extents is greater than the number of
694 * blocks.
695 */
696 if (unlikely(XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max
697 || XFS_BMDR_SPACE_CALC(nrecs) >
698 XFS_DFORK_SIZE(dip, ip->i_mount, whichfork)
699 || XFS_IFORK_NEXTENTS(ip, whichfork) > ip->i_d.di_nblocks)) {
Nathan Scott3762ec62006-01-12 10:29:53 +1100700 xfs_fs_repair_cmn_err(CE_WARN, ip->i_mount,
701 "corrupt inode %Lu (btree).",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 (unsigned long long) ip->i_ino);
703 XFS_ERROR_REPORT("xfs_iformat_btree", XFS_ERRLEVEL_LOW,
704 ip->i_mount);
705 return XFS_ERROR(EFSCORRUPTED);
706 }
707
708 ifp->if_broot_bytes = size;
709 ifp->if_broot = kmem_alloc(size, KM_SLEEP);
710 ASSERT(ifp->if_broot != NULL);
711 /*
712 * Copy and convert from the on-disk structure
713 * to the in-memory structure.
714 */
715 xfs_bmdr_to_bmbt(dfp, XFS_DFORK_SIZE(dip, ip->i_mount, whichfork),
716 ifp->if_broot, size);
717 ifp->if_flags &= ~XFS_IFEXTENTS;
718 ifp->if_flags |= XFS_IFBROOT;
719
720 return 0;
721}
722
723/*
724 * xfs_xlate_dinode_core - translate an xfs_inode_core_t between ondisk
725 * and native format
726 *
727 * buf = on-disk representation
728 * dip = native representation
729 * dir = direction - +ve -> disk to native
730 * -ve -> native to disk
731 */
732void
733xfs_xlate_dinode_core(
734 xfs_caddr_t buf,
735 xfs_dinode_core_t *dip,
736 int dir)
737{
738 xfs_dinode_core_t *buf_core = (xfs_dinode_core_t *)buf;
739 xfs_dinode_core_t *mem_core = (xfs_dinode_core_t *)dip;
740 xfs_arch_t arch = ARCH_CONVERT;
741
742 ASSERT(dir);
743
744 INT_XLATE(buf_core->di_magic, mem_core->di_magic, dir, arch);
745 INT_XLATE(buf_core->di_mode, mem_core->di_mode, dir, arch);
746 INT_XLATE(buf_core->di_version, mem_core->di_version, dir, arch);
747 INT_XLATE(buf_core->di_format, mem_core->di_format, dir, arch);
748 INT_XLATE(buf_core->di_onlink, mem_core->di_onlink, dir, arch);
749 INT_XLATE(buf_core->di_uid, mem_core->di_uid, dir, arch);
750 INT_XLATE(buf_core->di_gid, mem_core->di_gid, dir, arch);
751 INT_XLATE(buf_core->di_nlink, mem_core->di_nlink, dir, arch);
752 INT_XLATE(buf_core->di_projid, mem_core->di_projid, dir, arch);
753
754 if (dir > 0) {
755 memcpy(mem_core->di_pad, buf_core->di_pad,
756 sizeof(buf_core->di_pad));
757 } else {
758 memcpy(buf_core->di_pad, mem_core->di_pad,
759 sizeof(buf_core->di_pad));
760 }
761
762 INT_XLATE(buf_core->di_flushiter, mem_core->di_flushiter, dir, arch);
763
764 INT_XLATE(buf_core->di_atime.t_sec, mem_core->di_atime.t_sec,
765 dir, arch);
766 INT_XLATE(buf_core->di_atime.t_nsec, mem_core->di_atime.t_nsec,
767 dir, arch);
768 INT_XLATE(buf_core->di_mtime.t_sec, mem_core->di_mtime.t_sec,
769 dir, arch);
770 INT_XLATE(buf_core->di_mtime.t_nsec, mem_core->di_mtime.t_nsec,
771 dir, arch);
772 INT_XLATE(buf_core->di_ctime.t_sec, mem_core->di_ctime.t_sec,
773 dir, arch);
774 INT_XLATE(buf_core->di_ctime.t_nsec, mem_core->di_ctime.t_nsec,
775 dir, arch);
776 INT_XLATE(buf_core->di_size, mem_core->di_size, dir, arch);
777 INT_XLATE(buf_core->di_nblocks, mem_core->di_nblocks, dir, arch);
778 INT_XLATE(buf_core->di_extsize, mem_core->di_extsize, dir, arch);
779 INT_XLATE(buf_core->di_nextents, mem_core->di_nextents, dir, arch);
780 INT_XLATE(buf_core->di_anextents, mem_core->di_anextents, dir, arch);
781 INT_XLATE(buf_core->di_forkoff, mem_core->di_forkoff, dir, arch);
782 INT_XLATE(buf_core->di_aformat, mem_core->di_aformat, dir, arch);
783 INT_XLATE(buf_core->di_dmevmask, mem_core->di_dmevmask, dir, arch);
784 INT_XLATE(buf_core->di_dmstate, mem_core->di_dmstate, dir, arch);
785 INT_XLATE(buf_core->di_flags, mem_core->di_flags, dir, arch);
786 INT_XLATE(buf_core->di_gen, mem_core->di_gen, dir, arch);
787}
788
789STATIC uint
790_xfs_dic2xflags(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 __uint16_t di_flags)
792{
793 uint flags = 0;
794
795 if (di_flags & XFS_DIFLAG_ANY) {
796 if (di_flags & XFS_DIFLAG_REALTIME)
797 flags |= XFS_XFLAG_REALTIME;
798 if (di_flags & XFS_DIFLAG_PREALLOC)
799 flags |= XFS_XFLAG_PREALLOC;
800 if (di_flags & XFS_DIFLAG_IMMUTABLE)
801 flags |= XFS_XFLAG_IMMUTABLE;
802 if (di_flags & XFS_DIFLAG_APPEND)
803 flags |= XFS_XFLAG_APPEND;
804 if (di_flags & XFS_DIFLAG_SYNC)
805 flags |= XFS_XFLAG_SYNC;
806 if (di_flags & XFS_DIFLAG_NOATIME)
807 flags |= XFS_XFLAG_NOATIME;
808 if (di_flags & XFS_DIFLAG_NODUMP)
809 flags |= XFS_XFLAG_NODUMP;
810 if (di_flags & XFS_DIFLAG_RTINHERIT)
811 flags |= XFS_XFLAG_RTINHERIT;
812 if (di_flags & XFS_DIFLAG_PROJINHERIT)
813 flags |= XFS_XFLAG_PROJINHERIT;
814 if (di_flags & XFS_DIFLAG_NOSYMLINKS)
815 flags |= XFS_XFLAG_NOSYMLINKS;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100816 if (di_flags & XFS_DIFLAG_EXTSIZE)
817 flags |= XFS_XFLAG_EXTSIZE;
818 if (di_flags & XFS_DIFLAG_EXTSZINHERIT)
819 flags |= XFS_XFLAG_EXTSZINHERIT;
Barry Naujokd3446ea2006-06-09 14:54:19 +1000820 if (di_flags & XFS_DIFLAG_NODEFRAG)
821 flags |= XFS_XFLAG_NODEFRAG;
David Chinner2a82b8b2007-07-11 11:09:12 +1000822 if (di_flags & XFS_DIFLAG_FILESTREAM)
823 flags |= XFS_XFLAG_FILESTREAM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 }
825
826 return flags;
827}
828
829uint
830xfs_ip2xflags(
831 xfs_inode_t *ip)
832{
833 xfs_dinode_core_t *dic = &ip->i_d;
834
Nathan Scotta916e2b2006-06-09 17:12:17 +1000835 return _xfs_dic2xflags(dic->di_flags) |
836 (XFS_CFORK_Q(dic) ? XFS_XFLAG_HASATTR : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837}
838
839uint
840xfs_dic2xflags(
841 xfs_dinode_core_t *dic)
842{
Nathan Scotta916e2b2006-06-09 17:12:17 +1000843 return _xfs_dic2xflags(INT_GET(dic->di_flags, ARCH_CONVERT)) |
844 (XFS_CFORK_Q_DISK(dic) ? XFS_XFLAG_HASATTR : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845}
846
847/*
848 * Given a mount structure and an inode number, return a pointer
Nathan Scottc41564b2006-03-29 08:55:14 +1000849 * to a newly allocated in-core inode corresponding to the given
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 * inode number.
851 *
852 * Initialize the inode's attributes and extent pointers if it
853 * already has them (it will not if the inode has no links).
854 */
855int
856xfs_iread(
857 xfs_mount_t *mp,
858 xfs_trans_t *tp,
859 xfs_ino_t ino,
860 xfs_inode_t **ipp,
Nathan Scott745b1f472006-09-28 11:02:23 +1000861 xfs_daddr_t bno,
862 uint imap_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863{
864 xfs_buf_t *bp;
865 xfs_dinode_t *dip;
866 xfs_inode_t *ip;
867 int error;
868
869 ASSERT(xfs_inode_zone != NULL);
870
871 ip = kmem_zone_zalloc(xfs_inode_zone, KM_SLEEP);
872 ip->i_ino = ino;
873 ip->i_mount = mp;
David Chinnerf273ab82006-09-28 11:06:03 +1000874 spin_lock_init(&ip->i_flags_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
876 /*
877 * Get pointer's to the on-disk inode and the buffer containing it.
878 * If the inode number refers to a block outside the file system
879 * then xfs_itobp() will return NULL. In this case we should
880 * return NULL as well. Set i_blkno to 0 so that xfs_itobp() will
881 * know that this is a new incore inode.
882 */
Nathan Scott745b1f472006-09-28 11:02:23 +1000883 error = xfs_itobp(mp, tp, ip, &dip, &bp, bno, imap_flags);
Nathan Scottb12dd342006-03-17 17:26:04 +1100884 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 kmem_zone_free(xfs_inode_zone, ip);
886 return error;
887 }
888
889 /*
890 * Initialize inode's trace buffers.
891 * Do this before xfs_iformat in case it adds entries.
892 */
893#ifdef XFS_BMAP_TRACE
894 ip->i_xtrace = ktrace_alloc(XFS_BMAP_KTRACE_SIZE, KM_SLEEP);
895#endif
896#ifdef XFS_BMBT_TRACE
897 ip->i_btrace = ktrace_alloc(XFS_BMBT_KTRACE_SIZE, KM_SLEEP);
898#endif
899#ifdef XFS_RW_TRACE
900 ip->i_rwtrace = ktrace_alloc(XFS_RW_KTRACE_SIZE, KM_SLEEP);
901#endif
902#ifdef XFS_ILOCK_TRACE
903 ip->i_lock_trace = ktrace_alloc(XFS_ILOCK_KTRACE_SIZE, KM_SLEEP);
904#endif
905#ifdef XFS_DIR2_TRACE
906 ip->i_dir_trace = ktrace_alloc(XFS_DIR2_KTRACE_SIZE, KM_SLEEP);
907#endif
908
909 /*
910 * If we got something that isn't an inode it means someone
911 * (nfs or dmi) has a stale handle.
912 */
913 if (INT_GET(dip->di_core.di_magic, ARCH_CONVERT) != XFS_DINODE_MAGIC) {
914 kmem_zone_free(xfs_inode_zone, ip);
915 xfs_trans_brelse(tp, bp);
916#ifdef DEBUG
917 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_iread: "
918 "dip->di_core.di_magic (0x%x) != "
919 "XFS_DINODE_MAGIC (0x%x)",
920 INT_GET(dip->di_core.di_magic, ARCH_CONVERT),
921 XFS_DINODE_MAGIC);
922#endif /* DEBUG */
923 return XFS_ERROR(EINVAL);
924 }
925
926 /*
927 * If the on-disk inode is already linked to a directory
928 * entry, copy all of the inode into the in-core inode.
929 * xfs_iformat() handles copying in the inode format
930 * specific information.
931 * Otherwise, just get the truly permanent information.
932 */
933 if (dip->di_core.di_mode) {
934 xfs_xlate_dinode_core((xfs_caddr_t)&dip->di_core,
935 &(ip->i_d), 1);
936 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 {
948 ip->i_d.di_magic = INT_GET(dip->di_core.di_magic, ARCH_CONVERT);
949 ip->i_d.di_version = INT_GET(dip->di_core.di_version, ARCH_CONVERT);
950 ip->i_d.di_gen = INT_GET(dip->di_core.di_gen, ARCH_CONVERT);
951 ip->i_d.di_flushiter = INT_GET(dip->di_core.di_flushiter, ARCH_CONVERT);
952 /*
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 }
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001052 xfs_validate_extents(ifp, nextents, 0, 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
David Chinnerb11f94d2007-07-11 11:09:33 +10001165 if (pip && XFS_INHERIT_GID(pip, vp->v_vfsp)) {
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) {
Nathan Scott365ca832005-06-21 15:39:12 +10001227 if (pip->i_d.di_flags & XFS_DIFLAG_RTINHERIT) {
1228 di_flags |= XFS_DIFLAG_REALTIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 ip->i_iocore.io_flags |= XFS_IOCORE_RT;
1230 }
Nathan Scottdd9f4382006-01-11 15:28:28 +11001231 if (pip->i_d.di_flags & XFS_DIFLAG_EXTSZINHERIT) {
1232 di_flags |= XFS_DIFLAG_EXTSIZE;
1233 ip->i_d.di_extsize = pip->i_d.di_extsize;
1234 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 }
1236 if ((pip->i_d.di_flags & XFS_DIFLAG_NOATIME) &&
1237 xfs_inherit_noatime)
Nathan Scott365ca832005-06-21 15:39:12 +10001238 di_flags |= XFS_DIFLAG_NOATIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 if ((pip->i_d.di_flags & XFS_DIFLAG_NODUMP) &&
1240 xfs_inherit_nodump)
Nathan Scott365ca832005-06-21 15:39:12 +10001241 di_flags |= XFS_DIFLAG_NODUMP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 if ((pip->i_d.di_flags & XFS_DIFLAG_SYNC) &&
1243 xfs_inherit_sync)
Nathan Scott365ca832005-06-21 15:39:12 +10001244 di_flags |= XFS_DIFLAG_SYNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 if ((pip->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) &&
1246 xfs_inherit_nosymlinks)
Nathan Scott365ca832005-06-21 15:39:12 +10001247 di_flags |= XFS_DIFLAG_NOSYMLINKS;
1248 if (pip->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
1249 di_flags |= XFS_DIFLAG_PROJINHERIT;
Barry Naujokd3446ea2006-06-09 14:54:19 +10001250 if ((pip->i_d.di_flags & XFS_DIFLAG_NODEFRAG) &&
1251 xfs_inherit_nodefrag)
1252 di_flags |= XFS_DIFLAG_NODEFRAG;
David Chinner2a82b8b2007-07-11 11:09:12 +10001253 if (pip->i_d.di_flags & XFS_DIFLAG_FILESTREAM)
1254 di_flags |= XFS_DIFLAG_FILESTREAM;
Nathan Scott365ca832005-06-21 15:39:12 +10001255 ip->i_d.di_flags |= di_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 }
1257 /* FALLTHROUGH */
1258 case S_IFLNK:
1259 ip->i_d.di_format = XFS_DINODE_FMT_EXTENTS;
1260 ip->i_df.if_flags = XFS_IFEXTENTS;
1261 ip->i_df.if_bytes = ip->i_df.if_real_bytes = 0;
1262 ip->i_df.if_u1.if_extents = NULL;
1263 break;
1264 default:
1265 ASSERT(0);
1266 }
1267 /*
1268 * Attribute fork settings for new inode.
1269 */
1270 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
1271 ip->i_d.di_anextents = 0;
1272
1273 /*
1274 * Log the new values stuffed into the inode.
1275 */
1276 xfs_trans_log_inode(tp, ip, flags);
1277
Nathan Scottb83bd132006-06-09 16:48:30 +10001278 /* now that we have an i_mode we can setup inode ops and unlock */
1279 bhv_vfs_init_vnode(XFS_MTOVFS(tp->t_mountp), vp, XFS_ITOBHV(ip), 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
1281 *ipp = ip;
1282 return 0;
1283}
1284
1285/*
1286 * Check to make sure that there are no blocks allocated to the
1287 * file beyond the size of the file. We don't check this for
1288 * files with fixed size extents or real time extents, but we
1289 * at least do it for regular files.
1290 */
1291#ifdef DEBUG
1292void
1293xfs_isize_check(
1294 xfs_mount_t *mp,
1295 xfs_inode_t *ip,
1296 xfs_fsize_t isize)
1297{
1298 xfs_fileoff_t map_first;
1299 int nimaps;
1300 xfs_bmbt_irec_t imaps[2];
1301
1302 if ((ip->i_d.di_mode & S_IFMT) != S_IFREG)
1303 return;
1304
Nathan Scottdd9f4382006-01-11 15:28:28 +11001305 if (ip->i_d.di_flags & (XFS_DIFLAG_REALTIME | XFS_DIFLAG_EXTSIZE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 return;
1307
1308 nimaps = 2;
1309 map_first = XFS_B_TO_FSB(mp, (xfs_ufsize_t)isize);
1310 /*
1311 * The filesystem could be shutting down, so bmapi may return
1312 * an error.
1313 */
1314 if (xfs_bmapi(NULL, ip, map_first,
1315 (XFS_B_TO_FSB(mp,
1316 (xfs_ufsize_t)XFS_MAXIOFFSET(mp)) -
1317 map_first),
1318 XFS_BMAPI_ENTIRE, NULL, 0, imaps, &nimaps,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001319 NULL, NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 return;
1321 ASSERT(nimaps == 1);
1322 ASSERT(imaps[0].br_startblock == HOLESTARTBLOCK);
1323}
1324#endif /* DEBUG */
1325
1326/*
1327 * Calculate the last possible buffered byte in a file. This must
1328 * include data that was buffered beyond the EOF by the write code.
1329 * This also needs to deal with overflowing the xfs_fsize_t type
1330 * which can happen for sizes near the limit.
1331 *
1332 * We also need to take into account any blocks beyond the EOF. It
1333 * may be the case that they were buffered by a write which failed.
1334 * In that case the pages will still be in memory, but the inode size
1335 * will never have been updated.
1336 */
1337xfs_fsize_t
1338xfs_file_last_byte(
1339 xfs_inode_t *ip)
1340{
1341 xfs_mount_t *mp;
1342 xfs_fsize_t last_byte;
1343 xfs_fileoff_t last_block;
1344 xfs_fileoff_t size_last_block;
1345 int error;
1346
1347 ASSERT(ismrlocked(&(ip->i_iolock), MR_UPDATE | MR_ACCESS));
1348
1349 mp = ip->i_mount;
1350 /*
1351 * Only check for blocks beyond the EOF if the extents have
1352 * been read in. This eliminates the need for the inode lock,
1353 * and it also saves us from looking when it really isn't
1354 * necessary.
1355 */
1356 if (ip->i_df.if_flags & XFS_IFEXTENTS) {
1357 error = xfs_bmap_last_offset(NULL, ip, &last_block,
1358 XFS_DATA_FORK);
1359 if (error) {
1360 last_block = 0;
1361 }
1362 } else {
1363 last_block = 0;
1364 }
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10001365 size_last_block = XFS_B_TO_FSB(mp, (xfs_ufsize_t)ip->i_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 last_block = XFS_FILEOFF_MAX(last_block, size_last_block);
1367
1368 last_byte = XFS_FSB_TO_B(mp, last_block);
1369 if (last_byte < 0) {
1370 return XFS_MAXIOFFSET(mp);
1371 }
1372 last_byte += (1 << mp->m_writeio_log);
1373 if (last_byte < 0) {
1374 return XFS_MAXIOFFSET(mp);
1375 }
1376 return last_byte;
1377}
1378
1379#if defined(XFS_RW_TRACE)
1380STATIC void
1381xfs_itrunc_trace(
1382 int tag,
1383 xfs_inode_t *ip,
1384 int flag,
1385 xfs_fsize_t new_size,
1386 xfs_off_t toss_start,
1387 xfs_off_t toss_finish)
1388{
1389 if (ip->i_rwtrace == NULL) {
1390 return;
1391 }
1392
1393 ktrace_enter(ip->i_rwtrace,
1394 (void*)((long)tag),
1395 (void*)ip,
1396 (void*)(unsigned long)((ip->i_d.di_size >> 32) & 0xffffffff),
1397 (void*)(unsigned long)(ip->i_d.di_size & 0xffffffff),
1398 (void*)((long)flag),
1399 (void*)(unsigned long)((new_size >> 32) & 0xffffffff),
1400 (void*)(unsigned long)(new_size & 0xffffffff),
1401 (void*)(unsigned long)((toss_start >> 32) & 0xffffffff),
1402 (void*)(unsigned long)(toss_start & 0xffffffff),
1403 (void*)(unsigned long)((toss_finish >> 32) & 0xffffffff),
1404 (void*)(unsigned long)(toss_finish & 0xffffffff),
1405 (void*)(unsigned long)current_cpu(),
Yingping Luf1fdc842006-03-22 12:44:15 +11001406 (void*)(unsigned long)current_pid(),
1407 (void*)NULL,
1408 (void*)NULL,
1409 (void*)NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410}
1411#else
1412#define xfs_itrunc_trace(tag, ip, flag, new_size, toss_start, toss_finish)
1413#endif
1414
1415/*
1416 * Start the truncation of the file to new_size. The new size
1417 * must be smaller than the current size. This routine will
1418 * clear the buffer and page caches of file data in the removed
1419 * range, and xfs_itruncate_finish() will remove the underlying
1420 * disk blocks.
1421 *
1422 * The inode must have its I/O lock locked EXCLUSIVELY, and it
1423 * must NOT have the inode lock held at all. This is because we're
1424 * calling into the buffer/page cache code and we can't hold the
1425 * inode lock when we do so.
1426 *
David Chinner38e22992006-03-22 12:47:15 +11001427 * We need to wait for any direct I/Os in flight to complete before we
1428 * proceed with the truncate. This is needed to prevent the extents
1429 * being read or written by the direct I/Os from being removed while the
1430 * I/O is in flight as there is no other method of synchronising
1431 * direct I/O with the truncate operation. Also, because we hold
1432 * the IOLOCK in exclusive mode, we prevent new direct I/Os from being
1433 * started until the truncate completes and drops the lock. Essentially,
1434 * the vn_iowait() call forms an I/O barrier that provides strict ordering
1435 * between direct I/Os and the truncate operation.
1436 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 * The flags parameter can have either the value XFS_ITRUNC_DEFINITE
1438 * or XFS_ITRUNC_MAYBE. The XFS_ITRUNC_MAYBE value should be used
1439 * in the case that the caller is locking things out of order and
1440 * may not be able to call xfs_itruncate_finish() with the inode lock
1441 * held without dropping the I/O lock. If the caller must drop the
1442 * I/O lock before calling xfs_itruncate_finish(), then xfs_itruncate_start()
1443 * must be called again with all the same restrictions as the initial
1444 * call.
1445 */
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +10001446int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447xfs_itruncate_start(
1448 xfs_inode_t *ip,
1449 uint flags,
1450 xfs_fsize_t new_size)
1451{
1452 xfs_fsize_t last_byte;
1453 xfs_off_t toss_start;
1454 xfs_mount_t *mp;
Nathan Scott67fcaa72006-06-09 17:00:52 +10001455 bhv_vnode_t *vp;
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +10001456 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457
1458 ASSERT(ismrlocked(&ip->i_iolock, MR_UPDATE) != 0);
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10001459 ASSERT((new_size == 0) || (new_size <= ip->i_size));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 ASSERT((flags == XFS_ITRUNC_DEFINITE) ||
1461 (flags == XFS_ITRUNC_MAYBE));
1462
1463 mp = ip->i_mount;
1464 vp = XFS_ITOV(ip);
Yingping Lu9fa80462006-03-22 12:44:35 +11001465
1466 vn_iowait(vp); /* wait for the completion of any pending DIOs */
1467
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 /*
Nathan Scott67fcaa72006-06-09 17:00:52 +10001469 * Call toss_pages or flushinval_pages to get rid of pages
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 * overlapping the region being removed. We have to use
Nathan Scott67fcaa72006-06-09 17:00:52 +10001471 * the less efficient flushinval_pages in the case that the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 * caller may not be able to finish the truncate without
1473 * dropping the inode's I/O lock. Make sure
1474 * to catch any pages brought in by buffers overlapping
1475 * the EOF by searching out beyond the isize by our
1476 * block size. We round new_size up to a block boundary
1477 * so that we don't toss things on the same block as
1478 * new_size but before it.
1479 *
Nathan Scott67fcaa72006-06-09 17:00:52 +10001480 * Before calling toss_page or flushinval_pages, make sure to
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 * call remapf() over the same region if the file is mapped.
1482 * This frees up mapped file references to the pages in the
Nathan Scott67fcaa72006-06-09 17:00:52 +10001483 * given range and for the flushinval_pages case it ensures
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 * that we get the latest mapped changes flushed out.
1485 */
1486 toss_start = XFS_B_TO_FSB(mp, (xfs_ufsize_t)new_size);
1487 toss_start = XFS_FSB_TO_B(mp, toss_start);
1488 if (toss_start < 0) {
1489 /*
1490 * The place to start tossing is beyond our maximum
1491 * file size, so there is no way that the data extended
1492 * out there.
1493 */
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +10001494 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 }
1496 last_byte = xfs_file_last_byte(ip);
1497 xfs_itrunc_trace(XFS_ITRUNC_START, ip, flags, new_size, toss_start,
1498 last_byte);
1499 if (last_byte > toss_start) {
1500 if (flags & XFS_ITRUNC_DEFINITE) {
Nathan Scott67fcaa72006-06-09 17:00:52 +10001501 bhv_vop_toss_pages(vp, toss_start, -1, FI_REMAPF_LOCKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 } else {
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +10001503 error = bhv_vop_flushinval_pages(vp, toss_start, -1, FI_REMAPF_LOCKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 }
1505 }
1506
1507#ifdef DEBUG
1508 if (new_size == 0) {
1509 ASSERT(VN_CACHED(vp) == 0);
1510 }
1511#endif
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +10001512 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513}
1514
1515/*
1516 * Shrink the file to the given new_size. The new
1517 * size must be smaller than the current size.
1518 * This will free up the underlying blocks
1519 * in the removed range after a call to xfs_itruncate_start()
1520 * or xfs_atruncate_start().
1521 *
1522 * The transaction passed to this routine must have made
1523 * a permanent log reservation of at least XFS_ITRUNCATE_LOG_RES.
1524 * This routine may commit the given transaction and
1525 * start new ones, so make sure everything involved in
1526 * the transaction is tidy before calling here.
1527 * Some transaction will be returned to the caller to be
1528 * committed. The incoming transaction must already include
1529 * the inode, and both inode locks must be held exclusively.
1530 * The inode must also be "held" within the transaction. On
1531 * return the inode will be "held" within the returned transaction.
1532 * This routine does NOT require any disk space to be reserved
1533 * for it within the transaction.
1534 *
1535 * The fork parameter must be either xfs_attr_fork or xfs_data_fork,
1536 * and it indicates the fork which is to be truncated. For the
1537 * attribute fork we only support truncation to size 0.
1538 *
1539 * We use the sync parameter to indicate whether or not the first
1540 * transaction we perform might have to be synchronous. For the attr fork,
1541 * it needs to be so if the unlink of the inode is not yet known to be
1542 * permanent in the log. This keeps us from freeing and reusing the
1543 * blocks of the attribute fork before the unlink of the inode becomes
1544 * permanent.
1545 *
1546 * For the data fork, we normally have to run synchronously if we're
1547 * being called out of the inactive path or we're being called
1548 * out of the create path where we're truncating an existing file.
1549 * Either way, the truncate needs to be sync so blocks don't reappear
1550 * in the file with altered data in case of a crash. wsync filesystems
1551 * can run the first case async because anything that shrinks the inode
1552 * has to run sync so by the time we're called here from inactive, the
1553 * inode size is permanently set to 0.
1554 *
1555 * Calls from the truncate path always need to be sync unless we're
1556 * in a wsync filesystem and the file has already been unlinked.
1557 *
1558 * The caller is responsible for correctly setting the sync parameter.
1559 * It gets too hard for us to guess here which path we're being called
1560 * out of just based on inode state.
1561 */
1562int
1563xfs_itruncate_finish(
1564 xfs_trans_t **tp,
1565 xfs_inode_t *ip,
1566 xfs_fsize_t new_size,
1567 int fork,
1568 int sync)
1569{
1570 xfs_fsblock_t first_block;
1571 xfs_fileoff_t first_unmap_block;
1572 xfs_fileoff_t last_block;
1573 xfs_filblks_t unmap_len=0;
1574 xfs_mount_t *mp;
1575 xfs_trans_t *ntp;
1576 int done;
1577 int committed;
1578 xfs_bmap_free_t free_list;
1579 int error;
1580
1581 ASSERT(ismrlocked(&ip->i_iolock, MR_UPDATE) != 0);
1582 ASSERT(ismrlocked(&ip->i_lock, MR_UPDATE) != 0);
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10001583 ASSERT((new_size == 0) || (new_size <= ip->i_size));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 ASSERT(*tp != NULL);
1585 ASSERT((*tp)->t_flags & XFS_TRANS_PERM_LOG_RES);
1586 ASSERT(ip->i_transp == *tp);
1587 ASSERT(ip->i_itemp != NULL);
1588 ASSERT(ip->i_itemp->ili_flags & XFS_ILI_HOLD);
1589
1590
1591 ntp = *tp;
1592 mp = (ntp)->t_mountp;
1593 ASSERT(! XFS_NOT_DQATTACHED(mp, ip));
1594
1595 /*
1596 * We only support truncating the entire attribute fork.
1597 */
1598 if (fork == XFS_ATTR_FORK) {
1599 new_size = 0LL;
1600 }
1601 first_unmap_block = XFS_B_TO_FSB(mp, (xfs_ufsize_t)new_size);
1602 xfs_itrunc_trace(XFS_ITRUNC_FINISH1, ip, 0, new_size, 0, 0);
1603 /*
1604 * The first thing we do is set the size to new_size permanently
1605 * on disk. This way we don't have to worry about anyone ever
1606 * being able to look at the data being freed even in the face
1607 * of a crash. What we're getting around here is the case where
1608 * we free a block, it is allocated to another file, it is written
1609 * to, and then we crash. If the new data gets written to the
1610 * file but the log buffers containing the free and reallocation
1611 * don't, then we'd end up with garbage in the blocks being freed.
1612 * As long as we make the new_size permanent before actually
1613 * freeing any blocks it doesn't matter if they get writtten to.
1614 *
1615 * The callers must signal into us whether or not the size
1616 * setting here must be synchronous. There are a few cases
1617 * where it doesn't have to be synchronous. Those cases
1618 * occur if the file is unlinked and we know the unlink is
1619 * permanent or if the blocks being truncated are guaranteed
1620 * to be beyond the inode eof (regardless of the link count)
1621 * and the eof value is permanent. Both of these cases occur
1622 * only on wsync-mounted filesystems. In those cases, we're
1623 * guaranteed that no user will ever see the data in the blocks
1624 * that are being truncated so the truncate can run async.
1625 * In the free beyond eof case, the file may wind up with
1626 * more blocks allocated to it than it needs if we crash
1627 * and that won't get fixed until the next time the file
1628 * is re-opened and closed but that's ok as that shouldn't
1629 * be too many blocks.
1630 *
1631 * However, we can't just make all wsync xactions run async
1632 * because there's one call out of the create path that needs
1633 * to run sync where it's truncating an existing file to size
1634 * 0 whose size is > 0.
1635 *
1636 * It's probably possible to come up with a test in this
1637 * routine that would correctly distinguish all the above
1638 * cases from the values of the function parameters and the
1639 * inode state but for sanity's sake, I've decided to let the
1640 * layers above just tell us. It's simpler to correctly figure
1641 * out in the layer above exactly under what conditions we
1642 * can run async and I think it's easier for others read and
1643 * follow the logic in case something has to be changed.
1644 * cscope is your friend -- rcc.
1645 *
1646 * The attribute fork is much simpler.
1647 *
1648 * For the attribute fork we allow the caller to tell us whether
1649 * the unlink of the inode that led to this call is yet permanent
1650 * in the on disk log. If it is not and we will be freeing extents
1651 * in this inode then we make the first transaction synchronous
1652 * to make sure that the unlink is permanent by the time we free
1653 * the blocks.
1654 */
1655 if (fork == XFS_DATA_FORK) {
1656 if (ip->i_d.di_nextents > 0) {
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10001657 /*
1658 * If we are not changing the file size then do
1659 * not update the on-disk file size - we may be
1660 * called from xfs_inactive_free_eofblocks(). If we
1661 * update the on-disk file size and then the system
1662 * crashes before the contents of the file are
1663 * flushed to disk then the files may be full of
1664 * holes (ie NULL files bug).
1665 */
1666 if (ip->i_size != new_size) {
1667 ip->i_d.di_size = new_size;
1668 ip->i_size = new_size;
1669 xfs_trans_log_inode(ntp, ip, XFS_ILOG_CORE);
1670 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 }
1672 } else if (sync) {
1673 ASSERT(!(mp->m_flags & XFS_MOUNT_WSYNC));
1674 if (ip->i_d.di_anextents > 0)
1675 xfs_trans_set_sync(ntp);
1676 }
1677 ASSERT(fork == XFS_DATA_FORK ||
1678 (fork == XFS_ATTR_FORK &&
1679 ((sync && !(mp->m_flags & XFS_MOUNT_WSYNC)) ||
1680 (sync == 0 && (mp->m_flags & XFS_MOUNT_WSYNC)))));
1681
1682 /*
1683 * Since it is possible for space to become allocated beyond
1684 * the end of the file (in a crash where the space is allocated
1685 * but the inode size is not yet updated), simply remove any
1686 * blocks which show up between the new EOF and the maximum
1687 * possible file size. If the first block to be removed is
1688 * beyond the maximum file size (ie it is the same as last_block),
1689 * then there is nothing to do.
1690 */
1691 last_block = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp));
1692 ASSERT(first_unmap_block <= last_block);
1693 done = 0;
1694 if (last_block == first_unmap_block) {
1695 done = 1;
1696 } else {
1697 unmap_len = last_block - first_unmap_block + 1;
1698 }
1699 while (!done) {
1700 /*
1701 * Free up up to XFS_ITRUNC_MAX_EXTENTS. xfs_bunmapi()
1702 * will tell us whether it freed the entire range or
1703 * not. If this is a synchronous mount (wsync),
1704 * then we can tell bunmapi to keep all the
1705 * transactions asynchronous since the unlink
1706 * transaction that made this inode inactive has
1707 * already hit the disk. There's no danger of
1708 * the freed blocks being reused, there being a
1709 * crash, and the reused blocks suddenly reappearing
1710 * in this file with garbage in them once recovery
1711 * runs.
1712 */
1713 XFS_BMAP_INIT(&free_list, &first_block);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001714 error = XFS_BUNMAPI(mp, ntp, &ip->i_iocore,
1715 first_unmap_block, unmap_len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 XFS_BMAPI_AFLAG(fork) |
1717 (sync ? 0 : XFS_BMAPI_ASYNC),
1718 XFS_ITRUNC_MAX_EXTENTS,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001719 &first_block, &free_list,
1720 NULL, &done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 if (error) {
1722 /*
1723 * If the bunmapi call encounters an error,
1724 * return to the caller where the transaction
1725 * can be properly aborted. We just need to
1726 * make sure we're not holding any resources
1727 * that we were not when we came in.
1728 */
1729 xfs_bmap_cancel(&free_list);
1730 return error;
1731 }
1732
1733 /*
1734 * Duplicate the transaction that has the permanent
1735 * reservation and commit the old transaction.
1736 */
Eric Sandeenf7c99b62007-02-10 18:37:16 +11001737 error = xfs_bmap_finish(tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 ntp = *tp;
1739 if (error) {
1740 /*
1741 * If the bmap finish call encounters an error,
1742 * return to the caller where the transaction
1743 * can be properly aborted. We just need to
1744 * make sure we're not holding any resources
1745 * that we were not when we came in.
1746 *
1747 * Aborting from this point might lose some
1748 * blocks in the file system, but oh well.
1749 */
1750 xfs_bmap_cancel(&free_list);
1751 if (committed) {
1752 /*
1753 * If the passed in transaction committed
1754 * in xfs_bmap_finish(), then we want to
1755 * add the inode to this one before returning.
1756 * This keeps things simple for the higher
1757 * level code, because it always knows that
1758 * the inode is locked and held in the
1759 * transaction that returns to it whether
1760 * errors occur or not. We don't mark the
1761 * inode dirty so that this transaction can
1762 * be easily aborted if possible.
1763 */
1764 xfs_trans_ijoin(ntp, ip,
1765 XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1766 xfs_trans_ihold(ntp, ip);
1767 }
1768 return error;
1769 }
1770
1771 if (committed) {
1772 /*
1773 * The first xact was committed,
1774 * so add the inode to the new one.
1775 * Mark it dirty so it will be logged
1776 * and moved forward in the log as
1777 * part of every commit.
1778 */
1779 xfs_trans_ijoin(ntp, ip,
1780 XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1781 xfs_trans_ihold(ntp, ip);
1782 xfs_trans_log_inode(ntp, ip, XFS_ILOG_CORE);
1783 }
1784 ntp = xfs_trans_dup(ntp);
Eric Sandeen1c72bf92007-05-08 13:48:42 +10001785 (void) xfs_trans_commit(*tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 *tp = ntp;
1787 error = xfs_trans_reserve(ntp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
1788 XFS_TRANS_PERM_LOG_RES,
1789 XFS_ITRUNCATE_LOG_COUNT);
1790 /*
1791 * Add the inode being truncated to the next chained
1792 * transaction.
1793 */
1794 xfs_trans_ijoin(ntp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1795 xfs_trans_ihold(ntp, ip);
1796 if (error)
1797 return (error);
1798 }
1799 /*
1800 * Only update the size in the case of the data fork, but
1801 * always re-log the inode so that our permanent transaction
1802 * can keep on rolling it forward in the log.
1803 */
1804 if (fork == XFS_DATA_FORK) {
1805 xfs_isize_check(mp, ip, new_size);
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10001806 /*
1807 * If we are not changing the file size then do
1808 * not update the on-disk file size - we may be
1809 * called from xfs_inactive_free_eofblocks(). If we
1810 * update the on-disk file size and then the system
1811 * crashes before the contents of the file are
1812 * flushed to disk then the files may be full of
1813 * holes (ie NULL files bug).
1814 */
1815 if (ip->i_size != new_size) {
1816 ip->i_d.di_size = new_size;
1817 ip->i_size = new_size;
1818 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 }
1820 xfs_trans_log_inode(ntp, ip, XFS_ILOG_CORE);
1821 ASSERT((new_size != 0) ||
1822 (fork == XFS_ATTR_FORK) ||
1823 (ip->i_delayed_blks == 0));
1824 ASSERT((new_size != 0) ||
1825 (fork == XFS_ATTR_FORK) ||
1826 (ip->i_d.di_nextents == 0));
1827 xfs_itrunc_trace(XFS_ITRUNC_FINISH2, ip, 0, new_size, 0, 0);
1828 return 0;
1829}
1830
1831
1832/*
1833 * xfs_igrow_start
1834 *
1835 * Do the first part of growing a file: zero any data in the last
1836 * block that is beyond the old EOF. We need to do this before
1837 * the inode is joined to the transaction to modify the i_size.
1838 * That way we can drop the inode lock and call into the buffer
1839 * cache to get the buffer mapping the EOF.
1840 */
1841int
1842xfs_igrow_start(
1843 xfs_inode_t *ip,
1844 xfs_fsize_t new_size,
1845 cred_t *credp)
1846{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 int error;
1848
1849 ASSERT(ismrlocked(&(ip->i_lock), MR_UPDATE) != 0);
1850 ASSERT(ismrlocked(&(ip->i_iolock), MR_UPDATE) != 0);
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10001851 ASSERT(new_size > ip->i_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 /*
1854 * Zero any pages that may have been created by
1855 * xfs_write_file() beyond the end of the file
1856 * and any blocks between the old and new file sizes.
1857 */
Eric Sandeen24ee8082006-01-11 15:34:32 +11001858 error = xfs_zero_eof(XFS_ITOV(ip), &ip->i_iocore, new_size,
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10001859 ip->i_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 return error;
1861}
1862
1863/*
1864 * xfs_igrow_finish
1865 *
1866 * This routine is called to extend the size of a file.
1867 * The inode must have both the iolock and the ilock locked
1868 * for update and it must be a part of the current transaction.
1869 * The xfs_igrow_start() function must have been called previously.
1870 * If the change_flag is not zero, the inode change timestamp will
1871 * be updated.
1872 */
1873void
1874xfs_igrow_finish(
1875 xfs_trans_t *tp,
1876 xfs_inode_t *ip,
1877 xfs_fsize_t new_size,
1878 int change_flag)
1879{
1880 ASSERT(ismrlocked(&(ip->i_lock), MR_UPDATE) != 0);
1881 ASSERT(ismrlocked(&(ip->i_iolock), MR_UPDATE) != 0);
1882 ASSERT(ip->i_transp == tp);
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10001883 ASSERT(new_size > ip->i_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884
1885 /*
1886 * Update the file size. Update the inode change timestamp
1887 * if change_flag set.
1888 */
1889 ip->i_d.di_size = new_size;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10001890 ip->i_size = new_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 if (change_flag)
1892 xfs_ichgtime(ip, XFS_ICHGTIME_CHG);
1893 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1894
1895}
1896
1897
1898/*
1899 * This is called when the inode's link count goes to 0.
1900 * We place the on-disk inode on a list in the AGI. It
1901 * will be pulled from this list when the inode is freed.
1902 */
1903int
1904xfs_iunlink(
1905 xfs_trans_t *tp,
1906 xfs_inode_t *ip)
1907{
1908 xfs_mount_t *mp;
1909 xfs_agi_t *agi;
1910 xfs_dinode_t *dip;
1911 xfs_buf_t *agibp;
1912 xfs_buf_t *ibp;
1913 xfs_agnumber_t agno;
1914 xfs_daddr_t agdaddr;
1915 xfs_agino_t agino;
1916 short bucket_index;
1917 int offset;
1918 int error;
1919 int agi_ok;
1920
1921 ASSERT(ip->i_d.di_nlink == 0);
1922 ASSERT(ip->i_d.di_mode != 0);
1923 ASSERT(ip->i_transp == tp);
1924
1925 mp = tp->t_mountp;
1926
1927 agno = XFS_INO_TO_AGNO(mp, ip->i_ino);
1928 agdaddr = XFS_AG_DADDR(mp, agno, XFS_AGI_DADDR(mp));
1929
1930 /*
1931 * Get the agi buffer first. It ensures lock ordering
1932 * on the list.
1933 */
1934 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, agdaddr,
1935 XFS_FSS_TO_BB(mp, 1), 0, &agibp);
1936 if (error) {
1937 return error;
1938 }
1939 /*
1940 * Validate the magic number of the agi block.
1941 */
1942 agi = XFS_BUF_TO_AGI(agibp);
1943 agi_ok =
Christoph Hellwig16259e72005-11-02 15:11:25 +11001944 be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC &&
1945 XFS_AGI_GOOD_VERSION(be32_to_cpu(agi->agi_versionnum));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 if (unlikely(XFS_TEST_ERROR(!agi_ok, mp, XFS_ERRTAG_IUNLINK,
1947 XFS_RANDOM_IUNLINK))) {
1948 XFS_CORRUPTION_ERROR("xfs_iunlink", XFS_ERRLEVEL_LOW, mp, agi);
1949 xfs_trans_brelse(tp, agibp);
1950 return XFS_ERROR(EFSCORRUPTED);
1951 }
1952 /*
1953 * Get the index into the agi hash table for the
1954 * list this inode will go on.
1955 */
1956 agino = XFS_INO_TO_AGINO(mp, ip->i_ino);
1957 ASSERT(agino != 0);
1958 bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS;
1959 ASSERT(agi->agi_unlinked[bucket_index]);
Christoph Hellwig16259e72005-11-02 15:11:25 +11001960 ASSERT(be32_to_cpu(agi->agi_unlinked[bucket_index]) != agino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961
Christoph Hellwig16259e72005-11-02 15:11:25 +11001962 if (be32_to_cpu(agi->agi_unlinked[bucket_index]) != NULLAGINO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 /*
1964 * There is already another inode in the bucket we need
1965 * to add ourselves to. Add us at the front of the list.
1966 * Here we put the head pointer into our next pointer,
1967 * and then we fall through to point the head at us.
1968 */
Nathan Scottb12dd342006-03-17 17:26:04 +11001969 error = xfs_itobp(mp, tp, ip, &dip, &ibp, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 if (error) {
1971 return error;
1972 }
1973 ASSERT(INT_GET(dip->di_next_unlinked, ARCH_CONVERT) == NULLAGINO);
1974 ASSERT(dip->di_next_unlinked);
1975 /* 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 }
2085 next_agino = INT_GET(dip->di_next_unlinked, ARCH_CONVERT);
2086 ASSERT(next_agino != 0);
2087 if (next_agino != NULLAGINO) {
2088 INT_SET(dip->di_next_unlinked, ARCH_CONVERT, NULLAGINO);
2089 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 }
2132 next_agino = INT_GET(last_dip->di_next_unlinked, ARCH_CONVERT);
2133 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 }
2147 next_agino = INT_GET(dip->di_next_unlinked, ARCH_CONVERT);
2148 ASSERT(next_agino != 0);
2149 ASSERT(next_agino != agino);
2150 if (next_agino != NULLAGINO) {
2151 INT_SET(dip->di_next_unlinked, ARCH_CONVERT, NULLAGINO);
2152 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 */
2164 INT_SET(last_dip->di_next_unlinked, ARCH_CONVERT, next_agino);
2165 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;
2195 xfs_ihash_t *ih;
2196 xfs_inode_t *ip, **ip_found;
2197 xfs_inode_log_item_t *iip;
2198 xfs_log_item_t *lip;
2199 SPLDECL(s);
2200
2201 if (mp->m_sb.sb_blocksize >= XFS_INODE_CLUSTER_SIZE(mp)) {
2202 blks_per_cluster = 1;
2203 ninodes = mp->m_sb.sb_inopblock;
2204 nbufs = XFS_IALLOC_BLOCKS(mp);
2205 } else {
2206 blks_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) /
2207 mp->m_sb.sb_blocksize;
2208 ninodes = blks_per_cluster * mp->m_sb.sb_inopblock;
2209 nbufs = XFS_IALLOC_BLOCKS(mp) / blks_per_cluster;
2210 }
2211
2212 ip_found = kmem_alloc(ninodes * sizeof(xfs_inode_t *), KM_NOFS);
2213
2214 for (j = 0; j < nbufs; j++, inum += ninodes) {
2215 blkno = XFS_AGB_TO_DADDR(mp, XFS_INO_TO_AGNO(mp, inum),
2216 XFS_INO_TO_AGBNO(mp, inum));
2217
2218
2219 /*
2220 * Look for each inode in memory and attempt to lock it,
2221 * we can be racing with flush and tail pushing here.
2222 * any inode we get the locks on, add to an array of
2223 * inode items to process later.
2224 *
2225 * The get the buffer lock, we could beat a flush
2226 * or tail pushing thread to the lock here, in which
2227 * case they will go looking for the inode buffer
2228 * and fail, we need some other form of interlock
2229 * here.
2230 */
2231 found = 0;
2232 for (i = 0; i < ninodes; i++) {
2233 ih = XFS_IHASH(mp, inum + i);
2234 read_lock(&ih->ih_lock);
2235 for (ip = ih->ih_next; ip != NULL; ip = ip->i_next) {
2236 if (ip->i_ino == inum + i)
2237 break;
2238 }
2239
2240 /* Inode not in memory or we found it already,
2241 * nothing to do
2242 */
David Chinner7a18c382006-11-11 18:04:54 +11002243 if (!ip || xfs_iflags_test(ip, XFS_ISTALE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 read_unlock(&ih->ih_lock);
2245 continue;
2246 }
2247
2248 if (xfs_inode_clean(ip)) {
2249 read_unlock(&ih->ih_lock);
2250 continue;
2251 }
2252
2253 /* If we can get the locks then add it to the
2254 * list, otherwise by the time we get the bp lock
2255 * below it will already be attached to the
2256 * inode buffer.
2257 */
2258
2259 /* This inode will already be locked - by us, lets
2260 * keep it that way.
2261 */
2262
2263 if (ip == free_ip) {
2264 if (xfs_iflock_nowait(ip)) {
David Chinner7a18c382006-11-11 18:04:54 +11002265 xfs_iflags_set(ip, XFS_ISTALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 if (xfs_inode_clean(ip)) {
2267 xfs_ifunlock(ip);
2268 } else {
2269 ip_found[found++] = ip;
2270 }
2271 }
2272 read_unlock(&ih->ih_lock);
2273 continue;
2274 }
2275
2276 if (xfs_ilock_nowait(ip, XFS_ILOCK_EXCL)) {
2277 if (xfs_iflock_nowait(ip)) {
David Chinner7a18c382006-11-11 18:04:54 +11002278 xfs_iflags_set(ip, XFS_ISTALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279
2280 if (xfs_inode_clean(ip)) {
2281 xfs_ifunlock(ip);
2282 xfs_iunlock(ip, XFS_ILOCK_EXCL);
2283 } else {
2284 ip_found[found++] = ip;
2285 }
2286 } else {
2287 xfs_iunlock(ip, XFS_ILOCK_EXCL);
2288 }
2289 }
2290
2291 read_unlock(&ih->ih_lock);
2292 }
2293
2294 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, blkno,
2295 mp->m_bsize * blks_per_cluster,
2296 XFS_BUF_LOCK);
2297
2298 pre_flushed = 0;
2299 lip = XFS_BUF_FSPRIVATE(bp, xfs_log_item_t *);
2300 while (lip) {
2301 if (lip->li_type == XFS_LI_INODE) {
2302 iip = (xfs_inode_log_item_t *)lip;
2303 ASSERT(iip->ili_logged == 1);
2304 lip->li_cb = (void(*)(xfs_buf_t*,xfs_log_item_t*)) xfs_istale_done;
2305 AIL_LOCK(mp,s);
2306 iip->ili_flush_lsn = iip->ili_item.li_lsn;
2307 AIL_UNLOCK(mp, s);
David Chinnere5ffd2b2006-11-21 18:55:33 +11002308 xfs_iflags_set(iip->ili_inode, XFS_ISTALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 pre_flushed++;
2310 }
2311 lip = lip->li_bio_list;
2312 }
2313
2314 for (i = 0; i < found; i++) {
2315 ip = ip_found[i];
2316 iip = ip->i_itemp;
2317
2318 if (!iip) {
2319 ip->i_update_core = 0;
2320 xfs_ifunlock(ip);
2321 xfs_iunlock(ip, XFS_ILOCK_EXCL);
2322 continue;
2323 }
2324
2325 iip->ili_last_fields = iip->ili_format.ilf_fields;
2326 iip->ili_format.ilf_fields = 0;
2327 iip->ili_logged = 1;
2328 AIL_LOCK(mp,s);
2329 iip->ili_flush_lsn = iip->ili_item.li_lsn;
2330 AIL_UNLOCK(mp, s);
2331
2332 xfs_buf_attach_iodone(bp,
2333 (void(*)(xfs_buf_t*,xfs_log_item_t*))
2334 xfs_istale_done, (xfs_log_item_t *)iip);
2335 if (ip != free_ip) {
2336 xfs_iunlock(ip, XFS_ILOCK_EXCL);
2337 }
2338 }
2339
2340 if (found || pre_flushed)
2341 xfs_trans_stale_inode_buf(tp, bp);
2342 xfs_trans_binval(tp, bp);
2343 }
2344
2345 kmem_free(ip_found, ninodes * sizeof(xfs_inode_t *));
2346}
2347
2348/*
2349 * This is called to return an inode to the inode free list.
2350 * The inode should already be truncated to 0 length and have
2351 * no pages associated with it. This routine also assumes that
2352 * the inode is already a part of the transaction.
2353 *
2354 * The on-disk copy of the inode will have been added to the list
2355 * of unlinked inodes in the AGI. We need to remove the inode from
2356 * that list atomically with respect to freeing it here.
2357 */
2358int
2359xfs_ifree(
2360 xfs_trans_t *tp,
2361 xfs_inode_t *ip,
2362 xfs_bmap_free_t *flist)
2363{
2364 int error;
2365 int delete;
2366 xfs_ino_t first_ino;
2367
2368 ASSERT(ismrlocked(&ip->i_lock, MR_UPDATE));
2369 ASSERT(ip->i_transp == tp);
2370 ASSERT(ip->i_d.di_nlink == 0);
2371 ASSERT(ip->i_d.di_nextents == 0);
2372 ASSERT(ip->i_d.di_anextents == 0);
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10002373 ASSERT((ip->i_d.di_size == 0 && ip->i_size == 0) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 ((ip->i_d.di_mode & S_IFMT) != S_IFREG));
2375 ASSERT(ip->i_d.di_nblocks == 0);
2376
2377 /*
2378 * Pull the on-disk inode from the AGI unlinked list.
2379 */
2380 error = xfs_iunlink_remove(tp, ip);
2381 if (error != 0) {
2382 return error;
2383 }
2384
2385 error = xfs_difree(tp, ip->i_ino, flist, &delete, &first_ino);
2386 if (error != 0) {
2387 return error;
2388 }
2389 ip->i_d.di_mode = 0; /* mark incore inode as free */
2390 ip->i_d.di_flags = 0;
2391 ip->i_d.di_dmevmask = 0;
2392 ip->i_d.di_forkoff = 0; /* mark the attr fork not in use */
2393 ip->i_df.if_ext_max =
2394 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
2395 ip->i_d.di_format = XFS_DINODE_FMT_EXTENTS;
2396 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
2397 /*
2398 * Bump the generation count so no one will be confused
2399 * by reincarnations of this inode.
2400 */
2401 ip->i_d.di_gen++;
2402 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
2403
2404 if (delete) {
2405 xfs_ifree_cluster(ip, tp, first_ino);
2406 }
2407
2408 return 0;
2409}
2410
2411/*
2412 * Reallocate the space for if_broot based on the number of records
2413 * being added or deleted as indicated in rec_diff. Move the records
2414 * and pointers in if_broot to fit the new size. When shrinking this
2415 * will eliminate holes between the records and pointers created by
2416 * the caller. When growing this will create holes to be filled in
2417 * by the caller.
2418 *
2419 * The caller must not request to add more records than would fit in
2420 * the on-disk inode root. If the if_broot is currently NULL, then
2421 * if we adding records one will be allocated. The caller must also
2422 * not request that the number of records go below zero, although
2423 * it can go to zero.
2424 *
2425 * ip -- the inode whose if_broot area is changing
2426 * ext_diff -- the change in the number of records, positive or negative,
2427 * requested for the if_broot array.
2428 */
2429void
2430xfs_iroot_realloc(
2431 xfs_inode_t *ip,
2432 int rec_diff,
2433 int whichfork)
2434{
2435 int cur_max;
2436 xfs_ifork_t *ifp;
2437 xfs_bmbt_block_t *new_broot;
2438 int new_max;
2439 size_t new_size;
2440 char *np;
2441 char *op;
2442
2443 /*
2444 * Handle the degenerate case quietly.
2445 */
2446 if (rec_diff == 0) {
2447 return;
2448 }
2449
2450 ifp = XFS_IFORK_PTR(ip, whichfork);
2451 if (rec_diff > 0) {
2452 /*
2453 * If there wasn't any memory allocated before, just
2454 * allocate it now and get out.
2455 */
2456 if (ifp->if_broot_bytes == 0) {
2457 new_size = (size_t)XFS_BMAP_BROOT_SPACE_CALC(rec_diff);
2458 ifp->if_broot = (xfs_bmbt_block_t*)kmem_alloc(new_size,
2459 KM_SLEEP);
2460 ifp->if_broot_bytes = (int)new_size;
2461 return;
2462 }
2463
2464 /*
2465 * If there is already an existing if_broot, then we need
2466 * to realloc() it and shift the pointers to their new
2467 * location. The records don't change location because
2468 * they are kept butted up against the btree block header.
2469 */
2470 cur_max = XFS_BMAP_BROOT_MAXRECS(ifp->if_broot_bytes);
2471 new_max = cur_max + rec_diff;
2472 new_size = (size_t)XFS_BMAP_BROOT_SPACE_CALC(new_max);
2473 ifp->if_broot = (xfs_bmbt_block_t *)
2474 kmem_realloc(ifp->if_broot,
2475 new_size,
2476 (size_t)XFS_BMAP_BROOT_SPACE_CALC(cur_max), /* old size */
2477 KM_SLEEP);
2478 op = (char *)XFS_BMAP_BROOT_PTR_ADDR(ifp->if_broot, 1,
2479 ifp->if_broot_bytes);
2480 np = (char *)XFS_BMAP_BROOT_PTR_ADDR(ifp->if_broot, 1,
2481 (int)new_size);
2482 ifp->if_broot_bytes = (int)new_size;
2483 ASSERT(ifp->if_broot_bytes <=
2484 XFS_IFORK_SIZE(ip, whichfork) + XFS_BROOT_SIZE_ADJ);
2485 memmove(np, op, cur_max * (uint)sizeof(xfs_dfsbno_t));
2486 return;
2487 }
2488
2489 /*
2490 * rec_diff is less than 0. In this case, we are shrinking the
2491 * if_broot buffer. It must already exist. If we go to zero
2492 * records, just get rid of the root and clear the status bit.
2493 */
2494 ASSERT((ifp->if_broot != NULL) && (ifp->if_broot_bytes > 0));
2495 cur_max = XFS_BMAP_BROOT_MAXRECS(ifp->if_broot_bytes);
2496 new_max = cur_max + rec_diff;
2497 ASSERT(new_max >= 0);
2498 if (new_max > 0)
2499 new_size = (size_t)XFS_BMAP_BROOT_SPACE_CALC(new_max);
2500 else
2501 new_size = 0;
2502 if (new_size > 0) {
2503 new_broot = (xfs_bmbt_block_t *)kmem_alloc(new_size, KM_SLEEP);
2504 /*
2505 * First copy over the btree block header.
2506 */
2507 memcpy(new_broot, ifp->if_broot, sizeof(xfs_bmbt_block_t));
2508 } else {
2509 new_broot = NULL;
2510 ifp->if_flags &= ~XFS_IFBROOT;
2511 }
2512
2513 /*
2514 * Only copy the records and pointers if there are any.
2515 */
2516 if (new_max > 0) {
2517 /*
2518 * First copy the records.
2519 */
2520 op = (char *)XFS_BMAP_BROOT_REC_ADDR(ifp->if_broot, 1,
2521 ifp->if_broot_bytes);
2522 np = (char *)XFS_BMAP_BROOT_REC_ADDR(new_broot, 1,
2523 (int)new_size);
2524 memcpy(np, op, new_max * (uint)sizeof(xfs_bmbt_rec_t));
2525
2526 /*
2527 * Then copy the pointers.
2528 */
2529 op = (char *)XFS_BMAP_BROOT_PTR_ADDR(ifp->if_broot, 1,
2530 ifp->if_broot_bytes);
2531 np = (char *)XFS_BMAP_BROOT_PTR_ADDR(new_broot, 1,
2532 (int)new_size);
2533 memcpy(np, op, new_max * (uint)sizeof(xfs_dfsbno_t));
2534 }
2535 kmem_free(ifp->if_broot, ifp->if_broot_bytes);
2536 ifp->if_broot = new_broot;
2537 ifp->if_broot_bytes = (int)new_size;
2538 ASSERT(ifp->if_broot_bytes <=
2539 XFS_IFORK_SIZE(ip, whichfork) + XFS_BROOT_SIZE_ADJ);
2540 return;
2541}
2542
2543
2544/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 * This is called when the amount of space needed for if_data
2546 * is increased or decreased. The change in size is indicated by
2547 * the number of bytes that need to be added or deleted in the
2548 * byte_diff parameter.
2549 *
2550 * If the amount of space needed has decreased below the size of the
2551 * inline buffer, then switch to using the inline buffer. Otherwise,
2552 * use kmem_realloc() or kmem_alloc() to adjust the size of the buffer
2553 * to what is needed.
2554 *
2555 * ip -- the inode whose if_data area is changing
2556 * byte_diff -- the change in the number of bytes, positive or negative,
2557 * requested for the if_data array.
2558 */
2559void
2560xfs_idata_realloc(
2561 xfs_inode_t *ip,
2562 int byte_diff,
2563 int whichfork)
2564{
2565 xfs_ifork_t *ifp;
2566 int new_size;
2567 int real_size;
2568
2569 if (byte_diff == 0) {
2570 return;
2571 }
2572
2573 ifp = XFS_IFORK_PTR(ip, whichfork);
2574 new_size = (int)ifp->if_bytes + byte_diff;
2575 ASSERT(new_size >= 0);
2576
2577 if (new_size == 0) {
2578 if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) {
2579 kmem_free(ifp->if_u1.if_data, ifp->if_real_bytes);
2580 }
2581 ifp->if_u1.if_data = NULL;
2582 real_size = 0;
2583 } else if (new_size <= sizeof(ifp->if_u2.if_inline_data)) {
2584 /*
2585 * If the valid extents/data can fit in if_inline_ext/data,
2586 * copy them from the malloc'd vector and free it.
2587 */
2588 if (ifp->if_u1.if_data == NULL) {
2589 ifp->if_u1.if_data = ifp->if_u2.if_inline_data;
2590 } else if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) {
2591 ASSERT(ifp->if_real_bytes != 0);
2592 memcpy(ifp->if_u2.if_inline_data, ifp->if_u1.if_data,
2593 new_size);
2594 kmem_free(ifp->if_u1.if_data, ifp->if_real_bytes);
2595 ifp->if_u1.if_data = ifp->if_u2.if_inline_data;
2596 }
2597 real_size = 0;
2598 } else {
2599 /*
2600 * Stuck with malloc/realloc.
2601 * For inline data, the underlying buffer must be
2602 * a multiple of 4 bytes in size so that it can be
2603 * logged and stay on word boundaries. We enforce
2604 * that here.
2605 */
2606 real_size = roundup(new_size, 4);
2607 if (ifp->if_u1.if_data == NULL) {
2608 ASSERT(ifp->if_real_bytes == 0);
2609 ifp->if_u1.if_data = kmem_alloc(real_size, KM_SLEEP);
2610 } else if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) {
2611 /*
2612 * Only do the realloc if the underlying size
2613 * is really changing.
2614 */
2615 if (ifp->if_real_bytes != real_size) {
2616 ifp->if_u1.if_data =
2617 kmem_realloc(ifp->if_u1.if_data,
2618 real_size,
2619 ifp->if_real_bytes,
2620 KM_SLEEP);
2621 }
2622 } else {
2623 ASSERT(ifp->if_real_bytes == 0);
2624 ifp->if_u1.if_data = kmem_alloc(real_size, KM_SLEEP);
2625 memcpy(ifp->if_u1.if_data, ifp->if_u2.if_inline_data,
2626 ifp->if_bytes);
2627 }
2628 }
2629 ifp->if_real_bytes = real_size;
2630 ifp->if_bytes = new_size;
2631 ASSERT(ifp->if_bytes <= XFS_IFORK_SIZE(ip, whichfork));
2632}
2633
2634
2635
2636
2637/*
2638 * Map inode to disk block and offset.
2639 *
2640 * mp -- the mount point structure for the current file system
2641 * tp -- the current transaction
2642 * ino -- the inode number of the inode to be located
2643 * imap -- this structure is filled in with the information necessary
2644 * to retrieve the given inode from disk
2645 * flags -- flags to pass to xfs_dilocate indicating whether or not
2646 * lookups in the inode btree were OK or not
2647 */
2648int
2649xfs_imap(
2650 xfs_mount_t *mp,
2651 xfs_trans_t *tp,
2652 xfs_ino_t ino,
2653 xfs_imap_t *imap,
2654 uint flags)
2655{
2656 xfs_fsblock_t fsbno;
2657 int len;
2658 int off;
2659 int error;
2660
2661 fsbno = imap->im_blkno ?
2662 XFS_DADDR_TO_FSB(mp, imap->im_blkno) : NULLFSBLOCK;
2663 error = xfs_dilocate(mp, tp, ino, &fsbno, &len, &off, flags);
2664 if (error != 0) {
2665 return error;
2666 }
2667 imap->im_blkno = XFS_FSB_TO_DADDR(mp, fsbno);
2668 imap->im_len = XFS_FSB_TO_BB(mp, len);
2669 imap->im_agblkno = XFS_FSB_TO_AGBNO(mp, fsbno);
2670 imap->im_ioffset = (ushort)off;
2671 imap->im_boffset = (ushort)(off << mp->m_sb.sb_inodelog);
2672 return 0;
2673}
2674
2675void
2676xfs_idestroy_fork(
2677 xfs_inode_t *ip,
2678 int whichfork)
2679{
2680 xfs_ifork_t *ifp;
2681
2682 ifp = XFS_IFORK_PTR(ip, whichfork);
2683 if (ifp->if_broot != NULL) {
2684 kmem_free(ifp->if_broot, ifp->if_broot_bytes);
2685 ifp->if_broot = NULL;
2686 }
2687
2688 /*
2689 * If the format is local, then we can't have an extents
2690 * array so just look for an inline data array. If we're
2691 * not local then we may or may not have an extents list,
2692 * so check and free it up if we do.
2693 */
2694 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
2695 if ((ifp->if_u1.if_data != ifp->if_u2.if_inline_data) &&
2696 (ifp->if_u1.if_data != NULL)) {
2697 ASSERT(ifp->if_real_bytes != 0);
2698 kmem_free(ifp->if_u1.if_data, ifp->if_real_bytes);
2699 ifp->if_u1.if_data = NULL;
2700 ifp->if_real_bytes = 0;
2701 }
2702 } else if ((ifp->if_flags & XFS_IFEXTENTS) &&
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11002703 ((ifp->if_flags & XFS_IFEXTIREC) ||
2704 ((ifp->if_u1.if_extents != NULL) &&
2705 (ifp->if_u1.if_extents != ifp->if_u2.if_inline_ext)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 ASSERT(ifp->if_real_bytes != 0);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002707 xfs_iext_destroy(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 }
2709 ASSERT(ifp->if_u1.if_extents == NULL ||
2710 ifp->if_u1.if_extents == ifp->if_u2.if_inline_ext);
2711 ASSERT(ifp->if_real_bytes == 0);
2712 if (whichfork == XFS_ATTR_FORK) {
2713 kmem_zone_free(xfs_ifork_zone, ip->i_afp);
2714 ip->i_afp = NULL;
2715 }
2716}
2717
2718/*
2719 * This is called free all the memory associated with an inode.
2720 * It must free the inode itself and any buffers allocated for
2721 * if_extents/if_data and if_broot. It must also free the lock
2722 * associated with the inode.
2723 */
2724void
2725xfs_idestroy(
2726 xfs_inode_t *ip)
2727{
2728
2729 switch (ip->i_d.di_mode & S_IFMT) {
2730 case S_IFREG:
2731 case S_IFDIR:
2732 case S_IFLNK:
2733 xfs_idestroy_fork(ip, XFS_DATA_FORK);
2734 break;
2735 }
2736 if (ip->i_afp)
2737 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
2738 mrfree(&ip->i_lock);
2739 mrfree(&ip->i_iolock);
2740 freesema(&ip->i_flock);
2741#ifdef XFS_BMAP_TRACE
2742 ktrace_free(ip->i_xtrace);
2743#endif
2744#ifdef XFS_BMBT_TRACE
2745 ktrace_free(ip->i_btrace);
2746#endif
2747#ifdef XFS_RW_TRACE
2748 ktrace_free(ip->i_rwtrace);
2749#endif
2750#ifdef XFS_ILOCK_TRACE
2751 ktrace_free(ip->i_lock_trace);
2752#endif
2753#ifdef XFS_DIR2_TRACE
2754 ktrace_free(ip->i_dir_trace);
2755#endif
2756 if (ip->i_itemp) {
David Chinnerf74eaf52007-02-10 18:36:04 +11002757 /*
2758 * Only if we are shutting down the fs will we see an
2759 * inode still in the AIL. If it is there, we should remove
2760 * it to prevent a use-after-free from occurring.
2761 */
2762 xfs_mount_t *mp = ip->i_mount;
2763 xfs_log_item_t *lip = &ip->i_itemp->ili_item;
2764 int s;
2765
2766 ASSERT(((lip->li_flags & XFS_LI_IN_AIL) == 0) ||
2767 XFS_FORCED_SHUTDOWN(ip->i_mount));
2768 if (lip->li_flags & XFS_LI_IN_AIL) {
2769 AIL_LOCK(mp, s);
2770 if (lip->li_flags & XFS_LI_IN_AIL)
2771 xfs_trans_delete_ail(mp, lip, s);
2772 else
2773 AIL_UNLOCK(mp, s);
2774 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 xfs_inode_item_destroy(ip);
2776 }
2777 kmem_zone_free(xfs_inode_zone, ip);
2778}
2779
2780
2781/*
2782 * Increment the pin count of the given buffer.
2783 * This value is protected by ipinlock spinlock in the mount structure.
2784 */
2785void
2786xfs_ipin(
2787 xfs_inode_t *ip)
2788{
2789 ASSERT(ismrlocked(&ip->i_lock, MR_UPDATE));
2790
2791 atomic_inc(&ip->i_pincount);
2792}
2793
2794/*
2795 * Decrement the pin count of the given inode, and wake up
2796 * anyone in xfs_iwait_unpin() if the count goes to 0. The
Nathan Scottc41564b2006-03-29 08:55:14 +10002797 * inode must have been previously pinned with a call to xfs_ipin().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 */
2799void
2800xfs_iunpin(
2801 xfs_inode_t *ip)
2802{
2803 ASSERT(atomic_read(&ip->i_pincount) > 0);
2804
David Chinner4c606582006-11-11 18:05:00 +11002805 if (atomic_dec_and_lock(&ip->i_pincount, &ip->i_flags_lock)) {
David Chinnerf273ab82006-09-28 11:06:03 +10002806
David Chinner4c606582006-11-11 18:05:00 +11002807 /*
2808 * If the inode is currently being reclaimed, the link between
2809 * the bhv_vnode and the xfs_inode will be broken after the
2810 * XFS_IRECLAIM* flag is set. Hence, if these flags are not
2811 * set, then we can move forward and mark the linux inode dirty
2812 * knowing that it is still valid as it won't freed until after
2813 * the bhv_vnode<->xfs_inode link is broken in xfs_reclaim. The
2814 * i_flags_lock is used to synchronise the setting of the
2815 * XFS_IRECLAIM* flags and the breaking of the link, and so we
2816 * can execute atomically w.r.t to reclaim by holding this lock
2817 * here.
2818 *
2819 * However, we still need to issue the unpin wakeup call as the
2820 * inode reclaim may be blocked waiting for the inode to become
2821 * unpinned.
2822 */
2823
David Chinner7a18c382006-11-11 18:04:54 +11002824 if (!__xfs_iflags_test(ip, XFS_IRECLAIM|XFS_IRECLAIMABLE)) {
Nathan Scott67fcaa72006-06-09 17:00:52 +10002825 bhv_vnode_t *vp = XFS_ITOV_NULL(ip);
David Chinner4c606582006-11-11 18:05:00 +11002826 struct inode *inode = NULL;
2827
2828 BUG_ON(vp == NULL);
2829 inode = vn_to_inode(vp);
2830 BUG_ON(inode->i_state & I_CLEAR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831
David Chinner58829e42006-04-11 15:11:20 +10002832 /* make sync come back and flush this inode */
David Chinner4c606582006-11-11 18:05:00 +11002833 if (!(inode->i_state & (I_NEW|I_FREEING)))
2834 mark_inode_dirty_sync(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 }
David Chinnerf273ab82006-09-28 11:06:03 +10002836 spin_unlock(&ip->i_flags_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 wake_up(&ip->i_ipin_wait);
2838 }
2839}
2840
2841/*
2842 * This is called to wait for the given inode to be unpinned.
2843 * It will sleep until this happens. The caller must have the
2844 * inode locked in at least shared mode so that the buffer cannot
2845 * be subsequently pinned once someone is waiting for it to be
2846 * unpinned.
2847 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10002848STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849xfs_iunpin_wait(
2850 xfs_inode_t *ip)
2851{
2852 xfs_inode_log_item_t *iip;
2853 xfs_lsn_t lsn;
2854
2855 ASSERT(ismrlocked(&ip->i_lock, MR_UPDATE | MR_ACCESS));
2856
2857 if (atomic_read(&ip->i_pincount) == 0) {
2858 return;
2859 }
2860
2861 iip = ip->i_itemp;
2862 if (iip && iip->ili_last_lsn) {
2863 lsn = iip->ili_last_lsn;
2864 } else {
2865 lsn = (xfs_lsn_t)0;
2866 }
2867
2868 /*
2869 * Give the log a push so we don't wait here too long.
2870 */
2871 xfs_log_force(ip->i_mount, lsn, XFS_LOG_FORCE);
2872
2873 wait_event(ip->i_ipin_wait, (atomic_read(&ip->i_pincount) == 0));
2874}
2875
2876
2877/*
2878 * xfs_iextents_copy()
2879 *
2880 * This is called to copy the REAL extents (as opposed to the delayed
2881 * allocation extents) from the inode into the given buffer. It
2882 * returns the number of bytes copied into the buffer.
2883 *
2884 * If there are no delayed allocation extents, then we can just
2885 * memcpy() the extents into the buffer. Otherwise, we need to
2886 * examine each extent in turn and skip those which are delayed.
2887 */
2888int
2889xfs_iextents_copy(
2890 xfs_inode_t *ip,
2891 xfs_bmbt_rec_t *buffer,
2892 int whichfork)
2893{
2894 int copied;
2895 xfs_bmbt_rec_t *dest_ep;
2896 xfs_bmbt_rec_t *ep;
2897#ifdef XFS_BMAP_TRACE
2898 static char fname[] = "xfs_iextents_copy";
2899#endif
2900 int i;
2901 xfs_ifork_t *ifp;
2902 int nrecs;
2903 xfs_fsblock_t start_block;
2904
2905 ifp = XFS_IFORK_PTR(ip, whichfork);
2906 ASSERT(ismrlocked(&ip->i_lock, MR_UPDATE|MR_ACCESS));
2907 ASSERT(ifp->if_bytes > 0);
2908
2909 nrecs = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
2910 xfs_bmap_trace_exlist(fname, ip, nrecs, whichfork);
2911 ASSERT(nrecs > 0);
2912
2913 /*
2914 * There are some delayed allocation extents in the
2915 * inode, so copy the extents one at a time and skip
2916 * the delayed ones. There must be at least one
2917 * non-delayed extent.
2918 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 dest_ep = buffer;
2920 copied = 0;
2921 for (i = 0; i < nrecs; i++) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002922 ep = xfs_iext_get_ext(ifp, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 start_block = xfs_bmbt_get_startblock(ep);
2924 if (ISNULLSTARTBLOCK(start_block)) {
2925 /*
2926 * It's a delayed allocation extent, so skip it.
2927 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 continue;
2929 }
2930
2931 /* Translate to on disk format */
2932 put_unaligned(INT_GET(ep->l0, ARCH_CONVERT),
2933 (__uint64_t*)&dest_ep->l0);
2934 put_unaligned(INT_GET(ep->l1, ARCH_CONVERT),
2935 (__uint64_t*)&dest_ep->l1);
2936 dest_ep++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 copied++;
2938 }
2939 ASSERT(copied != 0);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002940 xfs_validate_extents(ifp, copied, 1, XFS_EXTFMT_INODE(ip));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941
2942 return (copied * (uint)sizeof(xfs_bmbt_rec_t));
2943}
2944
2945/*
2946 * Each of the following cases stores data into the same region
2947 * of the on-disk inode, so only one of them can be valid at
2948 * any given time. While it is possible to have conflicting formats
2949 * and log flags, e.g. having XFS_ILOG_?DATA set when the fork is
2950 * in EXTENTS format, this can only happen when the fork has
2951 * changed formats after being modified but before being flushed.
2952 * In these cases, the format always takes precedence, because the
2953 * format indicates the current state of the fork.
2954 */
2955/*ARGSUSED*/
2956STATIC int
2957xfs_iflush_fork(
2958 xfs_inode_t *ip,
2959 xfs_dinode_t *dip,
2960 xfs_inode_log_item_t *iip,
2961 int whichfork,
2962 xfs_buf_t *bp)
2963{
2964 char *cp;
2965 xfs_ifork_t *ifp;
2966 xfs_mount_t *mp;
2967#ifdef XFS_TRANS_DEBUG
2968 int first;
2969#endif
2970 static const short brootflag[2] =
2971 { XFS_ILOG_DBROOT, XFS_ILOG_ABROOT };
2972 static const short dataflag[2] =
2973 { XFS_ILOG_DDATA, XFS_ILOG_ADATA };
2974 static const short extflag[2] =
2975 { XFS_ILOG_DEXT, XFS_ILOG_AEXT };
2976
2977 if (iip == NULL)
2978 return 0;
2979 ifp = XFS_IFORK_PTR(ip, whichfork);
2980 /*
2981 * This can happen if we gave up in iformat in an error path,
2982 * for the attribute fork.
2983 */
2984 if (ifp == NULL) {
2985 ASSERT(whichfork == XFS_ATTR_FORK);
2986 return 0;
2987 }
2988 cp = XFS_DFORK_PTR(dip, whichfork);
2989 mp = ip->i_mount;
2990 switch (XFS_IFORK_FORMAT(ip, whichfork)) {
2991 case XFS_DINODE_FMT_LOCAL:
2992 if ((iip->ili_format.ilf_fields & dataflag[whichfork]) &&
2993 (ifp->if_bytes > 0)) {
2994 ASSERT(ifp->if_u1.if_data != NULL);
2995 ASSERT(ifp->if_bytes <= XFS_IFORK_SIZE(ip, whichfork));
2996 memcpy(cp, ifp->if_u1.if_data, ifp->if_bytes);
2997 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 break;
2999
3000 case XFS_DINODE_FMT_EXTENTS:
3001 ASSERT((ifp->if_flags & XFS_IFEXTENTS) ||
3002 !(iip->ili_format.ilf_fields & extflag[whichfork]));
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003003 ASSERT((xfs_iext_get_ext(ifp, 0) != NULL) ||
3004 (ifp->if_bytes == 0));
3005 ASSERT((xfs_iext_get_ext(ifp, 0) == NULL) ||
3006 (ifp->if_bytes > 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007 if ((iip->ili_format.ilf_fields & extflag[whichfork]) &&
3008 (ifp->if_bytes > 0)) {
3009 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) > 0);
3010 (void)xfs_iextents_copy(ip, (xfs_bmbt_rec_t *)cp,
3011 whichfork);
3012 }
3013 break;
3014
3015 case XFS_DINODE_FMT_BTREE:
3016 if ((iip->ili_format.ilf_fields & brootflag[whichfork]) &&
3017 (ifp->if_broot_bytes > 0)) {
3018 ASSERT(ifp->if_broot != NULL);
3019 ASSERT(ifp->if_broot_bytes <=
3020 (XFS_IFORK_SIZE(ip, whichfork) +
3021 XFS_BROOT_SIZE_ADJ));
3022 xfs_bmbt_to_bmdr(ifp->if_broot, ifp->if_broot_bytes,
3023 (xfs_bmdr_block_t *)cp,
3024 XFS_DFORK_SIZE(dip, mp, whichfork));
3025 }
3026 break;
3027
3028 case XFS_DINODE_FMT_DEV:
3029 if (iip->ili_format.ilf_fields & XFS_ILOG_DEV) {
3030 ASSERT(whichfork == XFS_DATA_FORK);
3031 INT_SET(dip->di_u.di_dev, ARCH_CONVERT, ip->i_df.if_u2.if_rdev);
3032 }
3033 break;
3034
3035 case XFS_DINODE_FMT_UUID:
3036 if (iip->ili_format.ilf_fields & XFS_ILOG_UUID) {
3037 ASSERT(whichfork == XFS_DATA_FORK);
3038 memcpy(&dip->di_u.di_muuid, &ip->i_df.if_u2.if_uuid,
3039 sizeof(uuid_t));
3040 }
3041 break;
3042
3043 default:
3044 ASSERT(0);
3045 break;
3046 }
3047
3048 return 0;
3049}
3050
3051/*
3052 * xfs_iflush() will write a modified inode's changes out to the
3053 * inode's on disk home. The caller must have the inode lock held
3054 * in at least shared mode and the inode flush semaphore must be
3055 * held as well. The inode lock will still be held upon return from
3056 * the call and the caller is free to unlock it.
3057 * The inode flush lock will be unlocked when the inode reaches the disk.
3058 * The flags indicate how the inode's buffer should be written out.
3059 */
3060int
3061xfs_iflush(
3062 xfs_inode_t *ip,
3063 uint flags)
3064{
3065 xfs_inode_log_item_t *iip;
3066 xfs_buf_t *bp;
3067 xfs_dinode_t *dip;
3068 xfs_mount_t *mp;
3069 int error;
3070 /* REFERENCED */
3071 xfs_chash_t *ch;
3072 xfs_inode_t *iq;
3073 int clcount; /* count of inodes clustered */
3074 int bufwasdelwri;
3075 enum { INT_DELWRI = (1 << 0), INT_ASYNC = (1 << 1) };
3076 SPLDECL(s);
3077
3078 XFS_STATS_INC(xs_iflush_count);
3079
3080 ASSERT(ismrlocked(&ip->i_lock, MR_UPDATE|MR_ACCESS));
Al Viro0d8fee32006-06-19 08:41:30 +10003081 ASSERT(issemalocked(&(ip->i_flock)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE ||
3083 ip->i_d.di_nextents > ip->i_df.if_ext_max);
3084
3085 iip = ip->i_itemp;
3086 mp = ip->i_mount;
3087
3088 /*
3089 * If the inode isn't dirty, then just release the inode
3090 * flush lock and do nothing.
3091 */
3092 if ((ip->i_update_core == 0) &&
3093 ((iip == NULL) || !(iip->ili_format.ilf_fields & XFS_ILOG_ALL))) {
3094 ASSERT((iip != NULL) ?
3095 !(iip->ili_item.li_flags & XFS_LI_IN_AIL) : 1);
3096 xfs_ifunlock(ip);
3097 return 0;
3098 }
3099
3100 /*
3101 * We can't flush the inode until it is unpinned, so
3102 * wait for it. We know noone new can pin it, because
3103 * we are holding the inode lock shared and you need
3104 * to hold it exclusively to pin the inode.
3105 */
3106 xfs_iunpin_wait(ip);
3107
3108 /*
3109 * This may have been unpinned because the filesystem is shutting
3110 * down forcibly. If that's the case we must not write this inode
3111 * to disk, because the log record didn't make it to disk!
3112 */
3113 if (XFS_FORCED_SHUTDOWN(mp)) {
3114 ip->i_update_core = 0;
3115 if (iip)
3116 iip->ili_format.ilf_fields = 0;
3117 xfs_ifunlock(ip);
3118 return XFS_ERROR(EIO);
3119 }
3120
3121 /*
3122 * Get the buffer containing the on-disk inode.
3123 */
Nathan Scottb12dd342006-03-17 17:26:04 +11003124 error = xfs_itobp(mp, NULL, ip, &dip, &bp, 0, 0);
3125 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126 xfs_ifunlock(ip);
3127 return error;
3128 }
3129
3130 /*
3131 * Decide how buffer will be flushed out. This is done before
3132 * the call to xfs_iflush_int because this field is zeroed by it.
3133 */
3134 if (iip != NULL && iip->ili_format.ilf_fields != 0) {
3135 /*
3136 * Flush out the inode buffer according to the directions
3137 * of the caller. In the cases where the caller has given
3138 * us a choice choose the non-delwri case. This is because
3139 * the inode is in the AIL and we need to get it out soon.
3140 */
3141 switch (flags) {
3142 case XFS_IFLUSH_SYNC:
3143 case XFS_IFLUSH_DELWRI_ELSE_SYNC:
3144 flags = 0;
3145 break;
3146 case XFS_IFLUSH_ASYNC:
3147 case XFS_IFLUSH_DELWRI_ELSE_ASYNC:
3148 flags = INT_ASYNC;
3149 break;
3150 case XFS_IFLUSH_DELWRI:
3151 flags = INT_DELWRI;
3152 break;
3153 default:
3154 ASSERT(0);
3155 flags = 0;
3156 break;
3157 }
3158 } else {
3159 switch (flags) {
3160 case XFS_IFLUSH_DELWRI_ELSE_SYNC:
3161 case XFS_IFLUSH_DELWRI_ELSE_ASYNC:
3162 case XFS_IFLUSH_DELWRI:
3163 flags = INT_DELWRI;
3164 break;
3165 case XFS_IFLUSH_ASYNC:
3166 flags = INT_ASYNC;
3167 break;
3168 case XFS_IFLUSH_SYNC:
3169 flags = 0;
3170 break;
3171 default:
3172 ASSERT(0);
3173 flags = 0;
3174 break;
3175 }
3176 }
3177
3178 /*
3179 * First flush out the inode that xfs_iflush was called with.
3180 */
3181 error = xfs_iflush_int(ip, bp);
3182 if (error) {
3183 goto corrupt_out;
3184 }
3185
3186 /*
3187 * inode clustering:
3188 * see if other inodes can be gathered into this write
3189 */
3190
3191 ip->i_chash->chl_buf = bp;
3192
3193 ch = XFS_CHASH(mp, ip->i_blkno);
3194 s = mutex_spinlock(&ch->ch_lock);
3195
3196 clcount = 0;
3197 for (iq = ip->i_cnext; iq != ip; iq = iq->i_cnext) {
3198 /*
3199 * Do an un-protected check to see if the inode is dirty and
3200 * is a candidate for flushing. These checks will be repeated
3201 * later after the appropriate locks are acquired.
3202 */
3203 iip = iq->i_itemp;
3204 if ((iq->i_update_core == 0) &&
3205 ((iip == NULL) ||
3206 !(iip->ili_format.ilf_fields & XFS_ILOG_ALL)) &&
3207 xfs_ipincount(iq) == 0) {
3208 continue;
3209 }
3210
3211 /*
3212 * Try to get locks. If any are unavailable,
3213 * then this inode cannot be flushed and is skipped.
3214 */
3215
3216 /* get inode locks (just i_lock) */
3217 if (xfs_ilock_nowait(iq, XFS_ILOCK_SHARED)) {
3218 /* get inode flush lock */
3219 if (xfs_iflock_nowait(iq)) {
3220 /* check if pinned */
3221 if (xfs_ipincount(iq) == 0) {
3222 /* arriving here means that
3223 * this inode can be flushed.
3224 * first re-check that it's
3225 * dirty
3226 */
3227 iip = iq->i_itemp;
3228 if ((iq->i_update_core != 0)||
3229 ((iip != NULL) &&
3230 (iip->ili_format.ilf_fields & XFS_ILOG_ALL))) {
3231 clcount++;
3232 error = xfs_iflush_int(iq, bp);
3233 if (error) {
3234 xfs_iunlock(iq,
3235 XFS_ILOCK_SHARED);
3236 goto cluster_corrupt_out;
3237 }
3238 } else {
3239 xfs_ifunlock(iq);
3240 }
3241 } else {
3242 xfs_ifunlock(iq);
3243 }
3244 }
3245 xfs_iunlock(iq, XFS_ILOCK_SHARED);
3246 }
3247 }
3248 mutex_spinunlock(&ch->ch_lock, s);
3249
3250 if (clcount) {
3251 XFS_STATS_INC(xs_icluster_flushcnt);
3252 XFS_STATS_ADD(xs_icluster_flushinode, clcount);
3253 }
3254
3255 /*
3256 * If the buffer is pinned then push on the log so we won't
3257 * get stuck waiting in the write for too long.
3258 */
3259 if (XFS_BUF_ISPINNED(bp)){
3260 xfs_log_force(mp, (xfs_lsn_t)0, XFS_LOG_FORCE);
3261 }
3262
3263 if (flags & INT_DELWRI) {
3264 xfs_bdwrite(mp, bp);
3265 } else if (flags & INT_ASYNC) {
3266 xfs_bawrite(mp, bp);
3267 } else {
3268 error = xfs_bwrite(mp, bp);
3269 }
3270 return error;
3271
3272corrupt_out:
3273 xfs_buf_relse(bp);
Nathan Scott7d04a332006-06-09 14:58:38 +10003274 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275 xfs_iflush_abort(ip);
3276 /*
3277 * Unlocks the flush lock
3278 */
3279 return XFS_ERROR(EFSCORRUPTED);
3280
3281cluster_corrupt_out:
3282 /* Corruption detected in the clustering loop. Invalidate the
3283 * inode buffer and shut down the filesystem.
3284 */
3285 mutex_spinunlock(&ch->ch_lock, s);
3286
3287 /*
3288 * Clean up the buffer. If it was B_DELWRI, just release it --
3289 * brelse can handle it with no problems. If not, shut down the
3290 * filesystem before releasing the buffer.
3291 */
3292 if ((bufwasdelwri= XFS_BUF_ISDELAYWRITE(bp))) {
3293 xfs_buf_relse(bp);
3294 }
3295
Nathan Scott7d04a332006-06-09 14:58:38 +10003296 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297
3298 if(!bufwasdelwri) {
3299 /*
3300 * Just like incore_relse: if we have b_iodone functions,
3301 * mark the buffer as an error and call them. Otherwise
3302 * mark it as stale and brelse.
3303 */
3304 if (XFS_BUF_IODONE_FUNC(bp)) {
3305 XFS_BUF_CLR_BDSTRAT_FUNC(bp);
3306 XFS_BUF_UNDONE(bp);
3307 XFS_BUF_STALE(bp);
3308 XFS_BUF_SHUT(bp);
3309 XFS_BUF_ERROR(bp,EIO);
3310 xfs_biodone(bp);
3311 } else {
3312 XFS_BUF_STALE(bp);
3313 xfs_buf_relse(bp);
3314 }
3315 }
3316
3317 xfs_iflush_abort(iq);
3318 /*
3319 * Unlocks the flush lock
3320 */
3321 return XFS_ERROR(EFSCORRUPTED);
3322}
3323
3324
3325STATIC int
3326xfs_iflush_int(
3327 xfs_inode_t *ip,
3328 xfs_buf_t *bp)
3329{
3330 xfs_inode_log_item_t *iip;
3331 xfs_dinode_t *dip;
3332 xfs_mount_t *mp;
3333#ifdef XFS_TRANS_DEBUG
3334 int first;
3335#endif
3336 SPLDECL(s);
3337
3338 ASSERT(ismrlocked(&ip->i_lock, MR_UPDATE|MR_ACCESS));
Al Viro0d8fee32006-06-19 08:41:30 +10003339 ASSERT(issemalocked(&(ip->i_flock)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340 ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE ||
3341 ip->i_d.di_nextents > ip->i_df.if_ext_max);
3342
3343 iip = ip->i_itemp;
3344 mp = ip->i_mount;
3345
3346
3347 /*
3348 * If the inode isn't dirty, then just release the inode
3349 * flush lock and do nothing.
3350 */
3351 if ((ip->i_update_core == 0) &&
3352 ((iip == NULL) || !(iip->ili_format.ilf_fields & XFS_ILOG_ALL))) {
3353 xfs_ifunlock(ip);
3354 return 0;
3355 }
3356
3357 /* set *dip = inode's place in the buffer */
3358 dip = (xfs_dinode_t *)xfs_buf_offset(bp, ip->i_boffset);
3359
3360 /*
3361 * Clear i_update_core before copying out the data.
3362 * This is for coordination with our timestamp updates
3363 * that don't hold the inode lock. They will always
3364 * update the timestamps BEFORE setting i_update_core,
3365 * so if we clear i_update_core after they set it we
3366 * are guaranteed to see their updates to the timestamps.
3367 * I believe that this depends on strongly ordered memory
3368 * semantics, but we have that. We use the SYNCHRONIZE
3369 * macro to make sure that the compiler does not reorder
3370 * the i_update_core access below the data copy below.
3371 */
3372 ip->i_update_core = 0;
3373 SYNCHRONIZE();
3374
Christoph Hellwig42fe2b12006-01-11 15:35:17 +11003375 /*
3376 * Make sure to get the latest atime from the Linux inode.
3377 */
3378 xfs_synchronize_atime(ip);
3379
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380 if (XFS_TEST_ERROR(INT_GET(dip->di_core.di_magic,ARCH_CONVERT) != XFS_DINODE_MAGIC,
3381 mp, XFS_ERRTAG_IFLUSH_1, XFS_RANDOM_IFLUSH_1)) {
3382 xfs_cmn_err(XFS_PTAG_IFLUSH, CE_ALERT, mp,
3383 "xfs_iflush: Bad inode %Lu magic number 0x%x, ptr 0x%p",
3384 ip->i_ino, (int) INT_GET(dip->di_core.di_magic, ARCH_CONVERT), dip);
3385 goto corrupt_out;
3386 }
3387 if (XFS_TEST_ERROR(ip->i_d.di_magic != XFS_DINODE_MAGIC,
3388 mp, XFS_ERRTAG_IFLUSH_2, XFS_RANDOM_IFLUSH_2)) {
3389 xfs_cmn_err(XFS_PTAG_IFLUSH, CE_ALERT, mp,
3390 "xfs_iflush: Bad inode %Lu, ptr 0x%p, magic number 0x%x",
3391 ip->i_ino, ip, ip->i_d.di_magic);
3392 goto corrupt_out;
3393 }
3394 if ((ip->i_d.di_mode & S_IFMT) == S_IFREG) {
3395 if (XFS_TEST_ERROR(
3396 (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS) &&
3397 (ip->i_d.di_format != XFS_DINODE_FMT_BTREE),
3398 mp, XFS_ERRTAG_IFLUSH_3, XFS_RANDOM_IFLUSH_3)) {
3399 xfs_cmn_err(XFS_PTAG_IFLUSH, CE_ALERT, mp,
3400 "xfs_iflush: Bad regular inode %Lu, ptr 0x%p",
3401 ip->i_ino, ip);
3402 goto corrupt_out;
3403 }
3404 } else if ((ip->i_d.di_mode & S_IFMT) == S_IFDIR) {
3405 if (XFS_TEST_ERROR(
3406 (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS) &&
3407 (ip->i_d.di_format != XFS_DINODE_FMT_BTREE) &&
3408 (ip->i_d.di_format != XFS_DINODE_FMT_LOCAL),
3409 mp, XFS_ERRTAG_IFLUSH_4, XFS_RANDOM_IFLUSH_4)) {
3410 xfs_cmn_err(XFS_PTAG_IFLUSH, CE_ALERT, mp,
3411 "xfs_iflush: Bad directory inode %Lu, ptr 0x%p",
3412 ip->i_ino, ip);
3413 goto corrupt_out;
3414 }
3415 }
3416 if (XFS_TEST_ERROR(ip->i_d.di_nextents + ip->i_d.di_anextents >
3417 ip->i_d.di_nblocks, mp, XFS_ERRTAG_IFLUSH_5,
3418 XFS_RANDOM_IFLUSH_5)) {
3419 xfs_cmn_err(XFS_PTAG_IFLUSH, CE_ALERT, mp,
3420 "xfs_iflush: detected corrupt incore inode %Lu, total extents = %d, nblocks = %Ld, ptr 0x%p",
3421 ip->i_ino,
3422 ip->i_d.di_nextents + ip->i_d.di_anextents,
3423 ip->i_d.di_nblocks,
3424 ip);
3425 goto corrupt_out;
3426 }
3427 if (XFS_TEST_ERROR(ip->i_d.di_forkoff > mp->m_sb.sb_inodesize,
3428 mp, XFS_ERRTAG_IFLUSH_6, XFS_RANDOM_IFLUSH_6)) {
3429 xfs_cmn_err(XFS_PTAG_IFLUSH, CE_ALERT, mp,
3430 "xfs_iflush: bad inode %Lu, forkoff 0x%x, ptr 0x%p",
3431 ip->i_ino, ip->i_d.di_forkoff, ip);
3432 goto corrupt_out;
3433 }
3434 /*
3435 * bump the flush iteration count, used to detect flushes which
3436 * postdate a log record during recovery.
3437 */
3438
3439 ip->i_d.di_flushiter++;
3440
3441 /*
3442 * Copy the dirty parts of the inode into the on-disk
3443 * inode. We always copy out the core of the inode,
3444 * because if the inode is dirty at all the core must
3445 * be.
3446 */
3447 xfs_xlate_dinode_core((xfs_caddr_t)&(dip->di_core), &(ip->i_d), -1);
3448
3449 /* Wrap, we never let the log put out DI_MAX_FLUSH */
3450 if (ip->i_d.di_flushiter == DI_MAX_FLUSH)
3451 ip->i_d.di_flushiter = 0;
3452
3453 /*
3454 * If this is really an old format inode and the superblock version
3455 * has not been updated to support only new format inodes, then
3456 * convert back to the old inode format. If the superblock version
3457 * has been updated, then make the conversion permanent.
3458 */
3459 ASSERT(ip->i_d.di_version == XFS_DINODE_VERSION_1 ||
3460 XFS_SB_VERSION_HASNLINK(&mp->m_sb));
3461 if (ip->i_d.di_version == XFS_DINODE_VERSION_1) {
3462 if (!XFS_SB_VERSION_HASNLINK(&mp->m_sb)) {
3463 /*
3464 * Convert it back.
3465 */
3466 ASSERT(ip->i_d.di_nlink <= XFS_MAXLINK_1);
3467 INT_SET(dip->di_core.di_onlink, ARCH_CONVERT, ip->i_d.di_nlink);
3468 } else {
3469 /*
3470 * The superblock version has already been bumped,
3471 * so just make the conversion to the new inode
3472 * format permanent.
3473 */
3474 ip->i_d.di_version = XFS_DINODE_VERSION_2;
3475 INT_SET(dip->di_core.di_version, ARCH_CONVERT, XFS_DINODE_VERSION_2);
3476 ip->i_d.di_onlink = 0;
3477 dip->di_core.di_onlink = 0;
3478 memset(&(ip->i_d.di_pad[0]), 0, sizeof(ip->i_d.di_pad));
3479 memset(&(dip->di_core.di_pad[0]), 0,
3480 sizeof(dip->di_core.di_pad));
3481 ASSERT(ip->i_d.di_projid == 0);
3482 }
3483 }
3484
3485 if (xfs_iflush_fork(ip, dip, iip, XFS_DATA_FORK, bp) == EFSCORRUPTED) {
3486 goto corrupt_out;
3487 }
3488
3489 if (XFS_IFORK_Q(ip)) {
3490 /*
3491 * The only error from xfs_iflush_fork is on the data fork.
3492 */
3493 (void) xfs_iflush_fork(ip, dip, iip, XFS_ATTR_FORK, bp);
3494 }
3495 xfs_inobp_check(mp, bp);
3496
3497 /*
3498 * We've recorded everything logged in the inode, so we'd
3499 * like to clear the ilf_fields bits so we don't log and
3500 * flush things unnecessarily. However, we can't stop
3501 * logging all this information until the data we've copied
3502 * into the disk buffer is written to disk. If we did we might
3503 * overwrite the copy of the inode in the log with all the
3504 * data after re-logging only part of it, and in the face of
3505 * a crash we wouldn't have all the data we need to recover.
3506 *
3507 * What we do is move the bits to the ili_last_fields field.
3508 * When logging the inode, these bits are moved back to the
3509 * ilf_fields field. In the xfs_iflush_done() routine we
3510 * clear ili_last_fields, since we know that the information
3511 * those bits represent is permanently on disk. As long as
3512 * the flush completes before the inode is logged again, then
3513 * both ilf_fields and ili_last_fields will be cleared.
3514 *
3515 * We can play with the ilf_fields bits here, because the inode
3516 * lock must be held exclusively in order to set bits there
3517 * and the flush lock protects the ili_last_fields bits.
3518 * Set ili_logged so the flush done
3519 * routine can tell whether or not to look in the AIL.
3520 * Also, store the current LSN of the inode so that we can tell
3521 * whether the item has moved in the AIL from xfs_iflush_done().
3522 * In order to read the lsn we need the AIL lock, because
3523 * it is a 64 bit value that cannot be read atomically.
3524 */
3525 if (iip != NULL && iip->ili_format.ilf_fields != 0) {
3526 iip->ili_last_fields = iip->ili_format.ilf_fields;
3527 iip->ili_format.ilf_fields = 0;
3528 iip->ili_logged = 1;
3529
3530 ASSERT(sizeof(xfs_lsn_t) == 8); /* don't lock if it shrinks */
3531 AIL_LOCK(mp,s);
3532 iip->ili_flush_lsn = iip->ili_item.li_lsn;
3533 AIL_UNLOCK(mp, s);
3534
3535 /*
3536 * Attach the function xfs_iflush_done to the inode's
3537 * buffer. This will remove the inode from the AIL
3538 * and unlock the inode's flush lock when the inode is
3539 * completely written to disk.
3540 */
3541 xfs_buf_attach_iodone(bp, (void(*)(xfs_buf_t*,xfs_log_item_t*))
3542 xfs_iflush_done, (xfs_log_item_t *)iip);
3543
3544 ASSERT(XFS_BUF_FSPRIVATE(bp, void *) != NULL);
3545 ASSERT(XFS_BUF_IODONE_FUNC(bp) != NULL);
3546 } else {
3547 /*
3548 * We're flushing an inode which is not in the AIL and has
3549 * not been logged but has i_update_core set. For this
3550 * case we can use a B_DELWRI flush and immediately drop
3551 * the inode flush lock because we can avoid the whole
3552 * AIL state thing. It's OK to drop the flush lock now,
3553 * because we've already locked the buffer and to do anything
3554 * you really need both.
3555 */
3556 if (iip != NULL) {
3557 ASSERT(iip->ili_logged == 0);
3558 ASSERT(iip->ili_last_fields == 0);
3559 ASSERT((iip->ili_item.li_flags & XFS_LI_IN_AIL) == 0);
3560 }
3561 xfs_ifunlock(ip);
3562 }
3563
3564 return 0;
3565
3566corrupt_out:
3567 return XFS_ERROR(EFSCORRUPTED);
3568}
3569
3570
3571/*
Christoph Hellwigefa80272005-06-21 15:37:17 +10003572 * Flush all inactive inodes in mp.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573 */
Christoph Hellwigefa80272005-06-21 15:37:17 +10003574void
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575xfs_iflush_all(
Christoph Hellwigefa80272005-06-21 15:37:17 +10003576 xfs_mount_t *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578 xfs_inode_t *ip;
Nathan Scott67fcaa72006-06-09 17:00:52 +10003579 bhv_vnode_t *vp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580
Christoph Hellwigefa80272005-06-21 15:37:17 +10003581 again:
3582 XFS_MOUNT_ILOCK(mp);
3583 ip = mp->m_inodes;
3584 if (ip == NULL)
3585 goto out;
3586
3587 do {
3588 /* Make sure we skip markers inserted by sync */
3589 if (ip->i_mount == NULL) {
3590 ip = ip->i_mnext;
3591 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593
Christoph Hellwigefa80272005-06-21 15:37:17 +10003594 vp = XFS_ITOV_NULL(ip);
3595 if (!vp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596 XFS_MOUNT_IUNLOCK(mp);
Christoph Hellwigefa80272005-06-21 15:37:17 +10003597 xfs_finish_reclaim(ip, 0, XFS_IFLUSH_ASYNC);
3598 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600
Christoph Hellwigefa80272005-06-21 15:37:17 +10003601 ASSERT(vn_count(vp) == 0);
3602
3603 ip = ip->i_mnext;
3604 } while (ip != mp->m_inodes);
3605 out:
3606 XFS_MOUNT_IUNLOCK(mp);
3607}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608
3609/*
3610 * xfs_iaccess: check accessibility of inode for mode.
3611 */
3612int
3613xfs_iaccess(
3614 xfs_inode_t *ip,
3615 mode_t mode,
3616 cred_t *cr)
3617{
3618 int error;
3619 mode_t orgmode = mode;
Nathan Scottec86dc02006-03-17 17:25:36 +11003620 struct inode *inode = vn_to_inode(XFS_ITOV(ip));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621
3622 if (mode & S_IWUSR) {
3623 umode_t imode = inode->i_mode;
3624
3625 if (IS_RDONLY(inode) &&
3626 (S_ISREG(imode) || S_ISDIR(imode) || S_ISLNK(imode)))
3627 return XFS_ERROR(EROFS);
3628
3629 if (IS_IMMUTABLE(inode))
3630 return XFS_ERROR(EACCES);
3631 }
3632
3633 /*
3634 * If there's an Access Control List it's used instead of
3635 * the mode bits.
3636 */
3637 if ((error = _ACL_XFS_IACCESS(ip, mode, cr)) != -1)
3638 return error ? XFS_ERROR(error) : 0;
3639
3640 if (current_fsuid(cr) != ip->i_d.di_uid) {
3641 mode >>= 3;
3642 if (!in_group_p((gid_t)ip->i_d.di_gid))
3643 mode >>= 3;
3644 }
3645
3646 /*
3647 * If the DACs are ok we don't need any capability check.
3648 */
3649 if ((ip->i_d.di_mode & mode) == mode)
3650 return 0;
3651 /*
3652 * Read/write DACs are always overridable.
3653 * Executable DACs are overridable if at least one exec bit is set.
3654 */
3655 if (!(orgmode & S_IXUSR) ||
3656 (inode->i_mode & S_IXUGO) || S_ISDIR(inode->i_mode))
3657 if (capable_cred(cr, CAP_DAC_OVERRIDE))
3658 return 0;
3659
3660 if ((orgmode == S_IRUSR) ||
3661 (S_ISDIR(inode->i_mode) && (!(orgmode & S_IWUSR)))) {
3662 if (capable_cred(cr, CAP_DAC_READ_SEARCH))
3663 return 0;
3664#ifdef NOISE
3665 cmn_err(CE_NOTE, "Ick: mode=%o, orgmode=%o", mode, orgmode);
3666#endif /* NOISE */
3667 return XFS_ERROR(EACCES);
3668 }
3669 return XFS_ERROR(EACCES);
3670}
3671
3672/*
3673 * xfs_iroundup: round up argument to next power of two
3674 */
3675uint
3676xfs_iroundup(
3677 uint v)
3678{
3679 int i;
3680 uint m;
3681
3682 if ((v & (v - 1)) == 0)
3683 return v;
3684 ASSERT((v & 0x80000000) == 0);
3685 if ((v & (v + 1)) == 0)
3686 return v + 1;
3687 for (i = 0, m = 1; i < 31; i++, m <<= 1) {
3688 if (v & m)
3689 continue;
3690 v |= m;
3691 if ((v & (v + 1)) == 0)
3692 return v + 1;
3693 }
3694 ASSERT(0);
3695 return( 0 );
3696}
3697
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698#ifdef XFS_ILOCK_TRACE
3699ktrace_t *xfs_ilock_trace_buf;
3700
3701void
3702xfs_ilock_trace(xfs_inode_t *ip, int lock, unsigned int lockflags, inst_t *ra)
3703{
3704 ktrace_enter(ip->i_lock_trace,
3705 (void *)ip,
3706 (void *)(unsigned long)lock, /* 1 = LOCK, 3=UNLOCK, etc */
3707 (void *)(unsigned long)lockflags, /* XFS_ILOCK_EXCL etc */
3708 (void *)ra, /* caller of ilock */
3709 (void *)(unsigned long)current_cpu(),
3710 (void *)(unsigned long)current_pid(),
3711 NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL);
3712}
3713#endif
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003714
3715/*
3716 * Return a pointer to the extent record at file index idx.
3717 */
3718xfs_bmbt_rec_t *
3719xfs_iext_get_ext(
3720 xfs_ifork_t *ifp, /* inode fork pointer */
3721 xfs_extnum_t idx) /* index of target extent */
3722{
3723 ASSERT(idx >= 0);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003724 if ((ifp->if_flags & XFS_IFEXTIREC) && (idx == 0)) {
3725 return ifp->if_u1.if_ext_irec->er_extbuf;
3726 } else if (ifp->if_flags & XFS_IFEXTIREC) {
3727 xfs_ext_irec_t *erp; /* irec pointer */
3728 int erp_idx = 0; /* irec index */
3729 xfs_extnum_t page_idx = idx; /* ext index in target list */
3730
3731 erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 0);
3732 return &erp->er_extbuf[page_idx];
3733 } else if (ifp->if_bytes) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003734 return &ifp->if_u1.if_extents[idx];
3735 } else {
3736 return NULL;
3737 }
3738}
3739
3740/*
3741 * Insert new item(s) into the extent records for incore inode
3742 * fork 'ifp'. 'count' new items are inserted at index 'idx'.
3743 */
3744void
3745xfs_iext_insert(
3746 xfs_ifork_t *ifp, /* inode fork pointer */
3747 xfs_extnum_t idx, /* starting index of new items */
3748 xfs_extnum_t count, /* number of inserted items */
3749 xfs_bmbt_irec_t *new) /* items to insert */
3750{
3751 xfs_bmbt_rec_t *ep; /* extent record pointer */
3752 xfs_extnum_t i; /* extent record index */
3753
3754 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
3755 xfs_iext_add(ifp, idx, count);
3756 for (i = idx; i < idx + count; i++, new++) {
3757 ep = xfs_iext_get_ext(ifp, i);
3758 xfs_bmbt_set_all(ep, new);
3759 }
3760}
3761
3762/*
3763 * This is called when the amount of space required for incore file
3764 * extents needs to be increased. The ext_diff parameter stores the
3765 * number of new extents being added and the idx parameter contains
3766 * the extent index where the new extents will be added. If the new
3767 * extents are being appended, then we just need to (re)allocate and
3768 * initialize the space. Otherwise, if the new extents are being
3769 * inserted into the middle of the existing entries, a bit more work
3770 * is required to make room for the new extents to be inserted. The
3771 * caller is responsible for filling in the new extent entries upon
3772 * return.
3773 */
3774void
3775xfs_iext_add(
3776 xfs_ifork_t *ifp, /* inode fork pointer */
3777 xfs_extnum_t idx, /* index to begin adding exts */
Nathan Scottc41564b2006-03-29 08:55:14 +10003778 int ext_diff) /* number of extents to add */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003779{
3780 int byte_diff; /* new bytes being added */
3781 int new_size; /* size of extents after adding */
3782 xfs_extnum_t nextents; /* number of extents in file */
3783
3784 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3785 ASSERT((idx >= 0) && (idx <= nextents));
3786 byte_diff = ext_diff * sizeof(xfs_bmbt_rec_t);
3787 new_size = ifp->if_bytes + byte_diff;
3788 /*
3789 * If the new number of extents (nextents + ext_diff)
3790 * fits inside the inode, then continue to use the inline
3791 * extent buffer.
3792 */
3793 if (nextents + ext_diff <= XFS_INLINE_EXTS) {
3794 if (idx < nextents) {
3795 memmove(&ifp->if_u2.if_inline_ext[idx + ext_diff],
3796 &ifp->if_u2.if_inline_ext[idx],
3797 (nextents - idx) * sizeof(xfs_bmbt_rec_t));
3798 memset(&ifp->if_u2.if_inline_ext[idx], 0, byte_diff);
3799 }
3800 ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext;
3801 ifp->if_real_bytes = 0;
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003802 ifp->if_lastex = nextents + ext_diff;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003803 }
3804 /*
3805 * Otherwise use a linear (direct) extent list.
3806 * If the extents are currently inside the inode,
3807 * xfs_iext_realloc_direct will switch us from
3808 * inline to direct extent allocation mode.
3809 */
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003810 else if (nextents + ext_diff <= XFS_LINEAR_EXTS) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003811 xfs_iext_realloc_direct(ifp, new_size);
3812 if (idx < nextents) {
3813 memmove(&ifp->if_u1.if_extents[idx + ext_diff],
3814 &ifp->if_u1.if_extents[idx],
3815 (nextents - idx) * sizeof(xfs_bmbt_rec_t));
3816 memset(&ifp->if_u1.if_extents[idx], 0, byte_diff);
3817 }
3818 }
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003819 /* Indirection array */
3820 else {
3821 xfs_ext_irec_t *erp;
3822 int erp_idx = 0;
3823 int page_idx = idx;
3824
3825 ASSERT(nextents + ext_diff > XFS_LINEAR_EXTS);
3826 if (ifp->if_flags & XFS_IFEXTIREC) {
3827 erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 1);
3828 } else {
3829 xfs_iext_irec_init(ifp);
3830 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3831 erp = ifp->if_u1.if_ext_irec;
3832 }
3833 /* Extents fit in target extent page */
3834 if (erp && erp->er_extcount + ext_diff <= XFS_LINEAR_EXTS) {
3835 if (page_idx < erp->er_extcount) {
3836 memmove(&erp->er_extbuf[page_idx + ext_diff],
3837 &erp->er_extbuf[page_idx],
3838 (erp->er_extcount - page_idx) *
3839 sizeof(xfs_bmbt_rec_t));
3840 memset(&erp->er_extbuf[page_idx], 0, byte_diff);
3841 }
3842 erp->er_extcount += ext_diff;
3843 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff);
3844 }
3845 /* Insert a new extent page */
3846 else if (erp) {
3847 xfs_iext_add_indirect_multi(ifp,
3848 erp_idx, page_idx, ext_diff);
3849 }
3850 /*
3851 * If extent(s) are being appended to the last page in
3852 * the indirection array and the new extent(s) don't fit
3853 * in the page, then erp is NULL and erp_idx is set to
3854 * the next index needed in the indirection array.
3855 */
3856 else {
3857 int count = ext_diff;
3858
3859 while (count) {
3860 erp = xfs_iext_irec_new(ifp, erp_idx);
3861 erp->er_extcount = count;
3862 count -= MIN(count, (int)XFS_LINEAR_EXTS);
3863 if (count) {
3864 erp_idx++;
3865 }
3866 }
3867 }
3868 }
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003869 ifp->if_bytes = new_size;
3870}
3871
3872/*
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003873 * This is called when incore extents are being added to the indirection
3874 * array and the new extents do not fit in the target extent list. The
3875 * erp_idx parameter contains the irec index for the target extent list
3876 * in the indirection array, and the idx parameter contains the extent
3877 * index within the list. The number of extents being added is stored
3878 * in the count parameter.
3879 *
3880 * |-------| |-------|
3881 * | | | | idx - number of extents before idx
3882 * | idx | | count |
3883 * | | | | count - number of extents being inserted at idx
3884 * |-------| |-------|
3885 * | count | | nex2 | nex2 - number of extents after idx + count
3886 * |-------| |-------|
3887 */
3888void
3889xfs_iext_add_indirect_multi(
3890 xfs_ifork_t *ifp, /* inode fork pointer */
3891 int erp_idx, /* target extent irec index */
3892 xfs_extnum_t idx, /* index within target list */
3893 int count) /* new extents being added */
3894{
3895 int byte_diff; /* new bytes being added */
3896 xfs_ext_irec_t *erp; /* pointer to irec entry */
3897 xfs_extnum_t ext_diff; /* number of extents to add */
3898 xfs_extnum_t ext_cnt; /* new extents still needed */
3899 xfs_extnum_t nex2; /* extents after idx + count */
3900 xfs_bmbt_rec_t *nex2_ep = NULL; /* temp list for nex2 extents */
3901 int nlists; /* number of irec's (lists) */
3902
3903 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3904 erp = &ifp->if_u1.if_ext_irec[erp_idx];
3905 nex2 = erp->er_extcount - idx;
3906 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3907
3908 /*
3909 * Save second part of target extent list
3910 * (all extents past */
3911 if (nex2) {
3912 byte_diff = nex2 * sizeof(xfs_bmbt_rec_t);
3913 nex2_ep = (xfs_bmbt_rec_t *) kmem_alloc(byte_diff, KM_SLEEP);
3914 memmove(nex2_ep, &erp->er_extbuf[idx], byte_diff);
3915 erp->er_extcount -= nex2;
3916 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, -nex2);
3917 memset(&erp->er_extbuf[idx], 0, byte_diff);
3918 }
3919
3920 /*
3921 * Add the new extents to the end of the target
3922 * list, then allocate new irec record(s) and
3923 * extent buffer(s) as needed to store the rest
3924 * of the new extents.
3925 */
3926 ext_cnt = count;
3927 ext_diff = MIN(ext_cnt, (int)XFS_LINEAR_EXTS - erp->er_extcount);
3928 if (ext_diff) {
3929 erp->er_extcount += ext_diff;
3930 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff);
3931 ext_cnt -= ext_diff;
3932 }
3933 while (ext_cnt) {
3934 erp_idx++;
3935 erp = xfs_iext_irec_new(ifp, erp_idx);
3936 ext_diff = MIN(ext_cnt, (int)XFS_LINEAR_EXTS);
3937 erp->er_extcount = ext_diff;
3938 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff);
3939 ext_cnt -= ext_diff;
3940 }
3941
3942 /* Add nex2 extents back to indirection array */
3943 if (nex2) {
3944 xfs_extnum_t ext_avail;
3945 int i;
3946
3947 byte_diff = nex2 * sizeof(xfs_bmbt_rec_t);
3948 ext_avail = XFS_LINEAR_EXTS - erp->er_extcount;
3949 i = 0;
3950 /*
3951 * If nex2 extents fit in the current page, append
3952 * nex2_ep after the new extents.
3953 */
3954 if (nex2 <= ext_avail) {
3955 i = erp->er_extcount;
3956 }
3957 /*
3958 * Otherwise, check if space is available in the
3959 * next page.
3960 */
3961 else if ((erp_idx < nlists - 1) &&
3962 (nex2 <= (ext_avail = XFS_LINEAR_EXTS -
3963 ifp->if_u1.if_ext_irec[erp_idx+1].er_extcount))) {
3964 erp_idx++;
3965 erp++;
3966 /* Create a hole for nex2 extents */
3967 memmove(&erp->er_extbuf[nex2], erp->er_extbuf,
3968 erp->er_extcount * sizeof(xfs_bmbt_rec_t));
3969 }
3970 /*
3971 * Final choice, create a new extent page for
3972 * nex2 extents.
3973 */
3974 else {
3975 erp_idx++;
3976 erp = xfs_iext_irec_new(ifp, erp_idx);
3977 }
3978 memmove(&erp->er_extbuf[i], nex2_ep, byte_diff);
3979 kmem_free(nex2_ep, byte_diff);
3980 erp->er_extcount += nex2;
3981 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, nex2);
3982 }
3983}
3984
3985/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003986 * This is called when the amount of space required for incore file
3987 * extents needs to be decreased. The ext_diff parameter stores the
3988 * number of extents to be removed and the idx parameter contains
3989 * the extent index where the extents will be removed from.
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003990 *
3991 * If the amount of space needed has decreased below the linear
3992 * limit, XFS_IEXT_BUFSZ, then switch to using the contiguous
3993 * extent array. Otherwise, use kmem_realloc() to adjust the
3994 * size to what is needed.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003995 */
3996void
3997xfs_iext_remove(
3998 xfs_ifork_t *ifp, /* inode fork pointer */
3999 xfs_extnum_t idx, /* index to begin removing exts */
4000 int ext_diff) /* number of extents to remove */
4001{
4002 xfs_extnum_t nextents; /* number of extents in file */
4003 int new_size; /* size of extents after removal */
4004
4005 ASSERT(ext_diff > 0);
4006 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4007 new_size = (nextents - ext_diff) * sizeof(xfs_bmbt_rec_t);
4008
4009 if (new_size == 0) {
4010 xfs_iext_destroy(ifp);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11004011 } else if (ifp->if_flags & XFS_IFEXTIREC) {
4012 xfs_iext_remove_indirect(ifp, idx, ext_diff);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004013 } else if (ifp->if_real_bytes) {
4014 xfs_iext_remove_direct(ifp, idx, ext_diff);
4015 } else {
4016 xfs_iext_remove_inline(ifp, idx, ext_diff);
4017 }
4018 ifp->if_bytes = new_size;
4019}
4020
4021/*
4022 * This removes ext_diff extents from the inline buffer, beginning
4023 * at extent index idx.
4024 */
4025void
4026xfs_iext_remove_inline(
4027 xfs_ifork_t *ifp, /* inode fork pointer */
4028 xfs_extnum_t idx, /* index to begin removing exts */
4029 int ext_diff) /* number of extents to remove */
4030{
4031 int nextents; /* number of extents in file */
4032
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11004033 ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004034 ASSERT(idx < XFS_INLINE_EXTS);
4035 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4036 ASSERT(((nextents - ext_diff) > 0) &&
4037 (nextents - ext_diff) < XFS_INLINE_EXTS);
4038
4039 if (idx + ext_diff < nextents) {
4040 memmove(&ifp->if_u2.if_inline_ext[idx],
4041 &ifp->if_u2.if_inline_ext[idx + ext_diff],
4042 (nextents - (idx + ext_diff)) *
4043 sizeof(xfs_bmbt_rec_t));
4044 memset(&ifp->if_u2.if_inline_ext[nextents - ext_diff],
4045 0, ext_diff * sizeof(xfs_bmbt_rec_t));
4046 } else {
4047 memset(&ifp->if_u2.if_inline_ext[idx], 0,
4048 ext_diff * sizeof(xfs_bmbt_rec_t));
4049 }
4050}
4051
4052/*
4053 * This removes ext_diff extents from a linear (direct) extent list,
4054 * beginning at extent index idx. If the extents are being removed
4055 * from the end of the list (ie. truncate) then we just need to re-
4056 * allocate the list to remove the extra space. Otherwise, if the
4057 * extents are being removed from the middle of the existing extent
4058 * entries, then we first need to move the extent records beginning
4059 * at idx + ext_diff up in the list to overwrite the records being
4060 * removed, then remove the extra space via kmem_realloc.
4061 */
4062void
4063xfs_iext_remove_direct(
4064 xfs_ifork_t *ifp, /* inode fork pointer */
4065 xfs_extnum_t idx, /* index to begin removing exts */
4066 int ext_diff) /* number of extents to remove */
4067{
4068 xfs_extnum_t nextents; /* number of extents in file */
4069 int new_size; /* size of extents after removal */
4070
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11004071 ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004072 new_size = ifp->if_bytes -
4073 (ext_diff * sizeof(xfs_bmbt_rec_t));
4074 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4075
4076 if (new_size == 0) {
4077 xfs_iext_destroy(ifp);
4078 return;
4079 }
4080 /* Move extents up in the list (if needed) */
4081 if (idx + ext_diff < nextents) {
4082 memmove(&ifp->if_u1.if_extents[idx],
4083 &ifp->if_u1.if_extents[idx + ext_diff],
4084 (nextents - (idx + ext_diff)) *
4085 sizeof(xfs_bmbt_rec_t));
4086 }
4087 memset(&ifp->if_u1.if_extents[nextents - ext_diff],
4088 0, ext_diff * sizeof(xfs_bmbt_rec_t));
4089 /*
4090 * Reallocate the direct extent list. If the extents
4091 * will fit inside the inode then xfs_iext_realloc_direct
4092 * will switch from direct to inline extent allocation
4093 * mode for us.
4094 */
4095 xfs_iext_realloc_direct(ifp, new_size);
4096 ifp->if_bytes = new_size;
4097}
4098
4099/*
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11004100 * This is called when incore extents are being removed from the
4101 * indirection array and the extents being removed span multiple extent
4102 * buffers. The idx parameter contains the file extent index where we
4103 * want to begin removing extents, and the count parameter contains
4104 * how many extents need to be removed.
4105 *
4106 * |-------| |-------|
4107 * | nex1 | | | nex1 - number of extents before idx
4108 * |-------| | count |
4109 * | | | | count - number of extents being removed at idx
4110 * | count | |-------|
4111 * | | | nex2 | nex2 - number of extents after idx + count
4112 * |-------| |-------|
4113 */
4114void
4115xfs_iext_remove_indirect(
4116 xfs_ifork_t *ifp, /* inode fork pointer */
4117 xfs_extnum_t idx, /* index to begin removing extents */
4118 int count) /* number of extents to remove */
4119{
4120 xfs_ext_irec_t *erp; /* indirection array pointer */
4121 int erp_idx = 0; /* indirection array index */
4122 xfs_extnum_t ext_cnt; /* extents left to remove */
4123 xfs_extnum_t ext_diff; /* extents to remove in current list */
4124 xfs_extnum_t nex1; /* number of extents before idx */
4125 xfs_extnum_t nex2; /* extents after idx + count */
Nathan Scottc41564b2006-03-29 08:55:14 +10004126 int nlists; /* entries in indirection array */
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11004127 int page_idx = idx; /* index in target extent list */
4128
4129 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
4130 erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 0);
4131 ASSERT(erp != NULL);
4132 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
4133 nex1 = page_idx;
4134 ext_cnt = count;
4135 while (ext_cnt) {
4136 nex2 = MAX((erp->er_extcount - (nex1 + ext_cnt)), 0);
4137 ext_diff = MIN(ext_cnt, (erp->er_extcount - nex1));
4138 /*
4139 * Check for deletion of entire list;
4140 * xfs_iext_irec_remove() updates extent offsets.
4141 */
4142 if (ext_diff == erp->er_extcount) {
4143 xfs_iext_irec_remove(ifp, erp_idx);
4144 ext_cnt -= ext_diff;
4145 nex1 = 0;
4146 if (ext_cnt) {
4147 ASSERT(erp_idx < ifp->if_real_bytes /
4148 XFS_IEXT_BUFSZ);
4149 erp = &ifp->if_u1.if_ext_irec[erp_idx];
4150 nex1 = 0;
4151 continue;
4152 } else {
4153 break;
4154 }
4155 }
4156 /* Move extents up (if needed) */
4157 if (nex2) {
4158 memmove(&erp->er_extbuf[nex1],
4159 &erp->er_extbuf[nex1 + ext_diff],
4160 nex2 * sizeof(xfs_bmbt_rec_t));
4161 }
4162 /* Zero out rest of page */
4163 memset(&erp->er_extbuf[nex1 + nex2], 0, (XFS_IEXT_BUFSZ -
4164 ((nex1 + nex2) * sizeof(xfs_bmbt_rec_t))));
4165 /* Update remaining counters */
4166 erp->er_extcount -= ext_diff;
4167 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, -ext_diff);
4168 ext_cnt -= ext_diff;
4169 nex1 = 0;
4170 erp_idx++;
4171 erp++;
4172 }
4173 ifp->if_bytes -= count * sizeof(xfs_bmbt_rec_t);
4174 xfs_iext_irec_compact(ifp);
4175}
4176
4177/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004178 * Create, destroy, or resize a linear (direct) block of extents.
4179 */
4180void
4181xfs_iext_realloc_direct(
4182 xfs_ifork_t *ifp, /* inode fork pointer */
4183 int new_size) /* new size of extents */
4184{
4185 int rnew_size; /* real new size of extents */
4186
4187 rnew_size = new_size;
4188
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11004189 ASSERT(!(ifp->if_flags & XFS_IFEXTIREC) ||
4190 ((new_size >= 0) && (new_size <= XFS_IEXT_BUFSZ) &&
4191 (new_size != ifp->if_real_bytes)));
4192
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004193 /* Free extent records */
4194 if (new_size == 0) {
4195 xfs_iext_destroy(ifp);
4196 }
4197 /* Resize direct extent list and zero any new bytes */
4198 else if (ifp->if_real_bytes) {
4199 /* Check if extents will fit inside the inode */
4200 if (new_size <= XFS_INLINE_EXTS * sizeof(xfs_bmbt_rec_t)) {
4201 xfs_iext_direct_to_inline(ifp, new_size /
4202 (uint)sizeof(xfs_bmbt_rec_t));
4203 ifp->if_bytes = new_size;
4204 return;
4205 }
Vignesh Babu16a087d2007-06-28 16:46:37 +10004206 if (!is_power_of_2(new_size)){
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004207 rnew_size = xfs_iroundup(new_size);
4208 }
4209 if (rnew_size != ifp->if_real_bytes) {
4210 ifp->if_u1.if_extents = (xfs_bmbt_rec_t *)
4211 kmem_realloc(ifp->if_u1.if_extents,
4212 rnew_size,
4213 ifp->if_real_bytes,
4214 KM_SLEEP);
4215 }
4216 if (rnew_size > ifp->if_real_bytes) {
4217 memset(&ifp->if_u1.if_extents[ifp->if_bytes /
4218 (uint)sizeof(xfs_bmbt_rec_t)], 0,
4219 rnew_size - ifp->if_real_bytes);
4220 }
4221 }
4222 /*
4223 * Switch from the inline extent buffer to a direct
4224 * extent list. Be sure to include the inline extent
4225 * bytes in new_size.
4226 */
4227 else {
4228 new_size += ifp->if_bytes;
Vignesh Babu16a087d2007-06-28 16:46:37 +10004229 if (!is_power_of_2(new_size)) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004230 rnew_size = xfs_iroundup(new_size);
4231 }
4232 xfs_iext_inline_to_direct(ifp, rnew_size);
4233 }
4234 ifp->if_real_bytes = rnew_size;
4235 ifp->if_bytes = new_size;
4236}
4237
4238/*
4239 * Switch from linear (direct) extent records to inline buffer.
4240 */
4241void
4242xfs_iext_direct_to_inline(
4243 xfs_ifork_t *ifp, /* inode fork pointer */
4244 xfs_extnum_t nextents) /* number of extents in file */
4245{
4246 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
4247 ASSERT(nextents <= XFS_INLINE_EXTS);
4248 /*
4249 * The inline buffer was zeroed when we switched
4250 * from inline to direct extent allocation mode,
4251 * so we don't need to clear it here.
4252 */
4253 memcpy(ifp->if_u2.if_inline_ext, ifp->if_u1.if_extents,
4254 nextents * sizeof(xfs_bmbt_rec_t));
Mandy Kirkconnellfe6c1e72006-06-09 14:51:25 +10004255 kmem_free(ifp->if_u1.if_extents, ifp->if_real_bytes);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004256 ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext;
4257 ifp->if_real_bytes = 0;
4258}
4259
4260/*
4261 * Switch from inline buffer to linear (direct) extent records.
4262 * new_size should already be rounded up to the next power of 2
4263 * by the caller (when appropriate), so use new_size as it is.
4264 * However, since new_size may be rounded up, we can't update
4265 * if_bytes here. It is the caller's responsibility to update
4266 * if_bytes upon return.
4267 */
4268void
4269xfs_iext_inline_to_direct(
4270 xfs_ifork_t *ifp, /* inode fork pointer */
4271 int new_size) /* number of extents in file */
4272{
4273 ifp->if_u1.if_extents = (xfs_bmbt_rec_t *)
4274 kmem_alloc(new_size, KM_SLEEP);
4275 memset(ifp->if_u1.if_extents, 0, new_size);
4276 if (ifp->if_bytes) {
4277 memcpy(ifp->if_u1.if_extents, ifp->if_u2.if_inline_ext,
4278 ifp->if_bytes);
4279 memset(ifp->if_u2.if_inline_ext, 0, XFS_INLINE_EXTS *
4280 sizeof(xfs_bmbt_rec_t));
4281 }
4282 ifp->if_real_bytes = new_size;
4283}
4284
4285/*
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11004286 * Resize an extent indirection array to new_size bytes.
4287 */
4288void
4289xfs_iext_realloc_indirect(
4290 xfs_ifork_t *ifp, /* inode fork pointer */
4291 int new_size) /* new indirection array size */
4292{
4293 int nlists; /* number of irec's (ex lists) */
4294 int size; /* current indirection array size */
4295
4296 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
4297 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
4298 size = nlists * sizeof(xfs_ext_irec_t);
4299 ASSERT(ifp->if_real_bytes);
4300 ASSERT((new_size >= 0) && (new_size != size));
4301 if (new_size == 0) {
4302 xfs_iext_destroy(ifp);
4303 } else {
4304 ifp->if_u1.if_ext_irec = (xfs_ext_irec_t *)
4305 kmem_realloc(ifp->if_u1.if_ext_irec,
4306 new_size, size, KM_SLEEP);
4307 }
4308}
4309
4310/*
4311 * Switch from indirection array to linear (direct) extent allocations.
4312 */
4313void
4314xfs_iext_indirect_to_direct(
4315 xfs_ifork_t *ifp) /* inode fork pointer */
4316{
4317 xfs_bmbt_rec_t *ep; /* extent record pointer */
4318 xfs_extnum_t nextents; /* number of extents in file */
4319 int size; /* size of file extents */
4320
4321 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
4322 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4323 ASSERT(nextents <= XFS_LINEAR_EXTS);
4324 size = nextents * sizeof(xfs_bmbt_rec_t);
4325
4326 xfs_iext_irec_compact_full(ifp);
4327 ASSERT(ifp->if_real_bytes == XFS_IEXT_BUFSZ);
4328
4329 ep = ifp->if_u1.if_ext_irec->er_extbuf;
4330 kmem_free(ifp->if_u1.if_ext_irec, sizeof(xfs_ext_irec_t));
4331 ifp->if_flags &= ~XFS_IFEXTIREC;
4332 ifp->if_u1.if_extents = ep;
4333 ifp->if_bytes = size;
4334 if (nextents < XFS_LINEAR_EXTS) {
4335 xfs_iext_realloc_direct(ifp, size);
4336 }
4337}
4338
4339/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004340 * Free incore file extents.
4341 */
4342void
4343xfs_iext_destroy(
4344 xfs_ifork_t *ifp) /* inode fork pointer */
4345{
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11004346 if (ifp->if_flags & XFS_IFEXTIREC) {
4347 int erp_idx;
4348 int nlists;
4349
4350 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
4351 for (erp_idx = nlists - 1; erp_idx >= 0 ; erp_idx--) {
4352 xfs_iext_irec_remove(ifp, erp_idx);
4353 }
4354 ifp->if_flags &= ~XFS_IFEXTIREC;
4355 } else if (ifp->if_real_bytes) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004356 kmem_free(ifp->if_u1.if_extents, ifp->if_real_bytes);
4357 } else if (ifp->if_bytes) {
4358 memset(ifp->if_u2.if_inline_ext, 0, XFS_INLINE_EXTS *
4359 sizeof(xfs_bmbt_rec_t));
4360 }
4361 ifp->if_u1.if_extents = NULL;
4362 ifp->if_real_bytes = 0;
4363 ifp->if_bytes = 0;
4364}
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11004365
4366/*
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11004367 * Return a pointer to the extent record for file system block bno.
4368 */
4369xfs_bmbt_rec_t * /* pointer to found extent record */
4370xfs_iext_bno_to_ext(
4371 xfs_ifork_t *ifp, /* inode fork pointer */
4372 xfs_fileoff_t bno, /* block number to search for */
4373 xfs_extnum_t *idxp) /* index of target extent */
4374{
4375 xfs_bmbt_rec_t *base; /* pointer to first extent */
4376 xfs_filblks_t blockcount = 0; /* number of blocks in extent */
4377 xfs_bmbt_rec_t *ep = NULL; /* pointer to target extent */
4378 xfs_ext_irec_t *erp = NULL; /* indirection array pointer */
Nathan Scottc41564b2006-03-29 08:55:14 +10004379 int high; /* upper boundary in search */
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11004380 xfs_extnum_t idx = 0; /* index of target extent */
Nathan Scottc41564b2006-03-29 08:55:14 +10004381 int low; /* lower boundary in search */
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11004382 xfs_extnum_t nextents; /* number of file extents */
4383 xfs_fileoff_t startoff = 0; /* start offset of extent */
4384
4385 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4386 if (nextents == 0) {
4387 *idxp = 0;
4388 return NULL;
4389 }
4390 low = 0;
4391 if (ifp->if_flags & XFS_IFEXTIREC) {
4392 /* Find target extent list */
4393 int erp_idx = 0;
4394 erp = xfs_iext_bno_to_irec(ifp, bno, &erp_idx);
4395 base = erp->er_extbuf;
4396 high = erp->er_extcount - 1;
4397 } else {
4398 base = ifp->if_u1.if_extents;
4399 high = nextents - 1;
4400 }
4401 /* Binary search extent records */
4402 while (low <= high) {
4403 idx = (low + high) >> 1;
4404 ep = base + idx;
4405 startoff = xfs_bmbt_get_startoff(ep);
4406 blockcount = xfs_bmbt_get_blockcount(ep);
4407 if (bno < startoff) {
4408 high = idx - 1;
4409 } else if (bno >= startoff + blockcount) {
4410 low = idx + 1;
4411 } else {
4412 /* Convert back to file-based extent index */
4413 if (ifp->if_flags & XFS_IFEXTIREC) {
4414 idx += erp->er_extoff;
4415 }
4416 *idxp = idx;
4417 return ep;
4418 }
4419 }
4420 /* Convert back to file-based extent index */
4421 if (ifp->if_flags & XFS_IFEXTIREC) {
4422 idx += erp->er_extoff;
4423 }
4424 if (bno >= startoff + blockcount) {
4425 if (++idx == nextents) {
4426 ep = NULL;
4427 } else {
4428 ep = xfs_iext_get_ext(ifp, idx);
4429 }
4430 }
4431 *idxp = idx;
4432 return ep;
4433}
4434
4435/*
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11004436 * Return a pointer to the indirection array entry containing the
4437 * extent record for filesystem block bno. Store the index of the
4438 * target irec in *erp_idxp.
4439 */
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11004440xfs_ext_irec_t * /* pointer to found extent record */
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11004441xfs_iext_bno_to_irec(
4442 xfs_ifork_t *ifp, /* inode fork pointer */
4443 xfs_fileoff_t bno, /* block number to search for */
4444 int *erp_idxp) /* irec index of target ext list */
4445{
4446 xfs_ext_irec_t *erp = NULL; /* indirection array pointer */
4447 xfs_ext_irec_t *erp_next; /* next indirection array entry */
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11004448 int erp_idx; /* indirection array index */
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11004449 int nlists; /* number of extent irec's (lists) */
4450 int high; /* binary search upper limit */
4451 int low; /* binary search lower limit */
4452
4453 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
4454 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
4455 erp_idx = 0;
4456 low = 0;
4457 high = nlists - 1;
4458 while (low <= high) {
4459 erp_idx = (low + high) >> 1;
4460 erp = &ifp->if_u1.if_ext_irec[erp_idx];
4461 erp_next = erp_idx < nlists - 1 ? erp + 1 : NULL;
4462 if (bno < xfs_bmbt_get_startoff(erp->er_extbuf)) {
4463 high = erp_idx - 1;
4464 } else if (erp_next && bno >=
4465 xfs_bmbt_get_startoff(erp_next->er_extbuf)) {
4466 low = erp_idx + 1;
4467 } else {
4468 break;
4469 }
4470 }
4471 *erp_idxp = erp_idx;
4472 return erp;
4473}
4474
4475/*
4476 * Return a pointer to the indirection array entry containing the
4477 * extent record at file extent index *idxp. Store the index of the
4478 * target irec in *erp_idxp and store the page index of the target
4479 * extent record in *idxp.
4480 */
4481xfs_ext_irec_t *
4482xfs_iext_idx_to_irec(
4483 xfs_ifork_t *ifp, /* inode fork pointer */
4484 xfs_extnum_t *idxp, /* extent index (file -> page) */
4485 int *erp_idxp, /* pointer to target irec */
4486 int realloc) /* new bytes were just added */
4487{
4488 xfs_ext_irec_t *prev; /* pointer to previous irec */
4489 xfs_ext_irec_t *erp = NULL; /* pointer to current irec */
4490 int erp_idx; /* indirection array index */
4491 int nlists; /* number of irec's (ex lists) */
4492 int high; /* binary search upper limit */
4493 int low; /* binary search lower limit */
4494 xfs_extnum_t page_idx = *idxp; /* extent index in target list */
4495
4496 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
4497 ASSERT(page_idx >= 0 && page_idx <=
4498 ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t));
4499 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
4500 erp_idx = 0;
4501 low = 0;
4502 high = nlists - 1;
4503
4504 /* Binary search extent irec's */
4505 while (low <= high) {
4506 erp_idx = (low + high) >> 1;
4507 erp = &ifp->if_u1.if_ext_irec[erp_idx];
4508 prev = erp_idx > 0 ? erp - 1 : NULL;
4509 if (page_idx < erp->er_extoff || (page_idx == erp->er_extoff &&
4510 realloc && prev && prev->er_extcount < XFS_LINEAR_EXTS)) {
4511 high = erp_idx - 1;
4512 } else if (page_idx > erp->er_extoff + erp->er_extcount ||
4513 (page_idx == erp->er_extoff + erp->er_extcount &&
4514 !realloc)) {
4515 low = erp_idx + 1;
4516 } else if (page_idx == erp->er_extoff + erp->er_extcount &&
4517 erp->er_extcount == XFS_LINEAR_EXTS) {
4518 ASSERT(realloc);
4519 page_idx = 0;
4520 erp_idx++;
4521 erp = erp_idx < nlists ? erp + 1 : NULL;
4522 break;
4523 } else {
4524 page_idx -= erp->er_extoff;
4525 break;
4526 }
4527 }
4528 *idxp = page_idx;
4529 *erp_idxp = erp_idx;
4530 return(erp);
4531}
4532
4533/*
4534 * Allocate and initialize an indirection array once the space needed
4535 * for incore extents increases above XFS_IEXT_BUFSZ.
4536 */
4537void
4538xfs_iext_irec_init(
4539 xfs_ifork_t *ifp) /* inode fork pointer */
4540{
4541 xfs_ext_irec_t *erp; /* indirection array pointer */
4542 xfs_extnum_t nextents; /* number of extents in file */
4543
4544 ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
4545 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4546 ASSERT(nextents <= XFS_LINEAR_EXTS);
4547
4548 erp = (xfs_ext_irec_t *)
4549 kmem_alloc(sizeof(xfs_ext_irec_t), KM_SLEEP);
4550
4551 if (nextents == 0) {
4552 ifp->if_u1.if_extents = (xfs_bmbt_rec_t *)
4553 kmem_alloc(XFS_IEXT_BUFSZ, KM_SLEEP);
4554 } else if (!ifp->if_real_bytes) {
4555 xfs_iext_inline_to_direct(ifp, XFS_IEXT_BUFSZ);
4556 } else if (ifp->if_real_bytes < XFS_IEXT_BUFSZ) {
4557 xfs_iext_realloc_direct(ifp, XFS_IEXT_BUFSZ);
4558 }
4559 erp->er_extbuf = ifp->if_u1.if_extents;
4560 erp->er_extcount = nextents;
4561 erp->er_extoff = 0;
4562
4563 ifp->if_flags |= XFS_IFEXTIREC;
4564 ifp->if_real_bytes = XFS_IEXT_BUFSZ;
4565 ifp->if_bytes = nextents * sizeof(xfs_bmbt_rec_t);
4566 ifp->if_u1.if_ext_irec = erp;
4567
4568 return;
4569}
4570
4571/*
4572 * Allocate and initialize a new entry in the indirection array.
4573 */
4574xfs_ext_irec_t *
4575xfs_iext_irec_new(
4576 xfs_ifork_t *ifp, /* inode fork pointer */
4577 int erp_idx) /* index for new irec */
4578{
4579 xfs_ext_irec_t *erp; /* indirection array pointer */
4580 int i; /* loop counter */
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
4586 /* Resize indirection array */
4587 xfs_iext_realloc_indirect(ifp, ++nlists *
4588 sizeof(xfs_ext_irec_t));
4589 /*
4590 * Move records down in the array so the
4591 * new page can use erp_idx.
4592 */
4593 erp = ifp->if_u1.if_ext_irec;
4594 for (i = nlists - 1; i > erp_idx; i--) {
4595 memmove(&erp[i], &erp[i-1], sizeof(xfs_ext_irec_t));
4596 }
4597 ASSERT(i == erp_idx);
4598
4599 /* Initialize new extent record */
4600 erp = ifp->if_u1.if_ext_irec;
4601 erp[erp_idx].er_extbuf = (xfs_bmbt_rec_t *)
4602 kmem_alloc(XFS_IEXT_BUFSZ, KM_SLEEP);
4603 ifp->if_real_bytes = nlists * XFS_IEXT_BUFSZ;
4604 memset(erp[erp_idx].er_extbuf, 0, XFS_IEXT_BUFSZ);
4605 erp[erp_idx].er_extcount = 0;
4606 erp[erp_idx].er_extoff = erp_idx > 0 ?
4607 erp[erp_idx-1].er_extoff + erp[erp_idx-1].er_extcount : 0;
4608 return (&erp[erp_idx]);
4609}
4610
4611/*
4612 * Remove a record from the indirection array.
4613 */
4614void
4615xfs_iext_irec_remove(
4616 xfs_ifork_t *ifp, /* inode fork pointer */
4617 int erp_idx) /* irec index to remove */
4618{
4619 xfs_ext_irec_t *erp; /* indirection array pointer */
4620 int i; /* loop counter */
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[erp_idx];
4626 if (erp->er_extbuf) {
4627 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1,
4628 -erp->er_extcount);
4629 kmem_free(erp->er_extbuf, XFS_IEXT_BUFSZ);
4630 }
4631 /* Compact extent records */
4632 erp = ifp->if_u1.if_ext_irec;
4633 for (i = erp_idx; i < nlists - 1; i++) {
4634 memmove(&erp[i], &erp[i+1], sizeof(xfs_ext_irec_t));
4635 }
4636 /*
4637 * Manually free the last extent record from the indirection
4638 * array. A call to xfs_iext_realloc_indirect() with a size
4639 * of zero would result in a call to xfs_iext_destroy() which
4640 * would in turn call this function again, creating a nasty
4641 * infinite loop.
4642 */
4643 if (--nlists) {
4644 xfs_iext_realloc_indirect(ifp,
4645 nlists * sizeof(xfs_ext_irec_t));
4646 } else {
4647 kmem_free(ifp->if_u1.if_ext_irec,
4648 sizeof(xfs_ext_irec_t));
4649 }
4650 ifp->if_real_bytes = nlists * XFS_IEXT_BUFSZ;
4651}
4652
4653/*
4654 * This is called to clean up large amounts of unused memory allocated
4655 * by the indirection array. Before compacting anything though, verify
4656 * that the indirection array is still needed and switch back to the
4657 * linear extent list (or even the inline buffer) if possible. The
4658 * compaction policy is as follows:
4659 *
4660 * Full Compaction: Extents fit into a single page (or inline buffer)
4661 * Full Compaction: Extents occupy less than 10% of allocated space
4662 * Partial Compaction: Extents occupy > 10% and < 50% of allocated space
4663 * No Compaction: Extents occupy at least 50% of allocated space
4664 */
4665void
4666xfs_iext_irec_compact(
4667 xfs_ifork_t *ifp) /* inode fork pointer */
4668{
4669 xfs_extnum_t nextents; /* number of extents in file */
4670 int nlists; /* number of irec's (ex lists) */
4671
4672 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
4673 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
4674 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4675
4676 if (nextents == 0) {
4677 xfs_iext_destroy(ifp);
4678 } else if (nextents <= XFS_INLINE_EXTS) {
4679 xfs_iext_indirect_to_direct(ifp);
4680 xfs_iext_direct_to_inline(ifp, nextents);
4681 } else if (nextents <= XFS_LINEAR_EXTS) {
4682 xfs_iext_indirect_to_direct(ifp);
4683 } else if (nextents < (nlists * XFS_LINEAR_EXTS) >> 3) {
4684 xfs_iext_irec_compact_full(ifp);
4685 } else if (nextents < (nlists * XFS_LINEAR_EXTS) >> 1) {
4686 xfs_iext_irec_compact_pages(ifp);
4687 }
4688}
4689
4690/*
4691 * Combine extents from neighboring extent pages.
4692 */
4693void
4694xfs_iext_irec_compact_pages(
4695 xfs_ifork_t *ifp) /* inode fork pointer */
4696{
4697 xfs_ext_irec_t *erp, *erp_next;/* pointers to irec entries */
4698 int erp_idx = 0; /* indirection array index */
4699 int nlists; /* number of irec's (ex lists) */
4700
4701 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
4702 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
4703 while (erp_idx < nlists - 1) {
4704 erp = &ifp->if_u1.if_ext_irec[erp_idx];
4705 erp_next = erp + 1;
4706 if (erp_next->er_extcount <=
4707 (XFS_LINEAR_EXTS - erp->er_extcount)) {
4708 memmove(&erp->er_extbuf[erp->er_extcount],
4709 erp_next->er_extbuf, erp_next->er_extcount *
4710 sizeof(xfs_bmbt_rec_t));
4711 erp->er_extcount += erp_next->er_extcount;
4712 /*
4713 * Free page before removing extent record
4714 * so er_extoffs don't get modified in
4715 * xfs_iext_irec_remove.
4716 */
4717 kmem_free(erp_next->er_extbuf, XFS_IEXT_BUFSZ);
4718 erp_next->er_extbuf = NULL;
4719 xfs_iext_irec_remove(ifp, erp_idx + 1);
4720 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
4721 } else {
4722 erp_idx++;
4723 }
4724 }
4725}
4726
4727/*
4728 * Fully compact the extent records managed by the indirection array.
4729 */
4730void
4731xfs_iext_irec_compact_full(
4732 xfs_ifork_t *ifp) /* inode fork pointer */
4733{
4734 xfs_bmbt_rec_t *ep, *ep_next; /* extent record pointers */
4735 xfs_ext_irec_t *erp, *erp_next; /* extent irec pointers */
4736 int erp_idx = 0; /* extent irec index */
4737 int ext_avail; /* empty entries in ex list */
4738 int ext_diff; /* number of exts to add */
4739 int nlists; /* number of irec's (ex lists) */
4740
4741 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
4742 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
4743 erp = ifp->if_u1.if_ext_irec;
4744 ep = &erp->er_extbuf[erp->er_extcount];
4745 erp_next = erp + 1;
4746 ep_next = erp_next->er_extbuf;
4747 while (erp_idx < nlists - 1) {
4748 ext_avail = XFS_LINEAR_EXTS - erp->er_extcount;
4749 ext_diff = MIN(ext_avail, erp_next->er_extcount);
4750 memcpy(ep, ep_next, ext_diff * sizeof(xfs_bmbt_rec_t));
4751 erp->er_extcount += ext_diff;
4752 erp_next->er_extcount -= ext_diff;
4753 /* Remove next page */
4754 if (erp_next->er_extcount == 0) {
4755 /*
4756 * Free page before removing extent record
4757 * so er_extoffs don't get modified in
4758 * xfs_iext_irec_remove.
4759 */
4760 kmem_free(erp_next->er_extbuf,
4761 erp_next->er_extcount * sizeof(xfs_bmbt_rec_t));
4762 erp_next->er_extbuf = NULL;
4763 xfs_iext_irec_remove(ifp, erp_idx + 1);
4764 erp = &ifp->if_u1.if_ext_irec[erp_idx];
4765 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
4766 /* Update next page */
4767 } else {
4768 /* Move rest of page up to become next new page */
4769 memmove(erp_next->er_extbuf, ep_next,
4770 erp_next->er_extcount * sizeof(xfs_bmbt_rec_t));
4771 ep_next = erp_next->er_extbuf;
4772 memset(&ep_next[erp_next->er_extcount], 0,
4773 (XFS_LINEAR_EXTS - erp_next->er_extcount) *
4774 sizeof(xfs_bmbt_rec_t));
4775 }
4776 if (erp->er_extcount == XFS_LINEAR_EXTS) {
4777 erp_idx++;
4778 if (erp_idx < nlists)
4779 erp = &ifp->if_u1.if_ext_irec[erp_idx];
4780 else
4781 break;
4782 }
4783 ep = &erp->er_extbuf[erp->er_extcount];
4784 erp_next = erp + 1;
4785 ep_next = erp_next->er_extbuf;
4786 }
4787}
4788
4789/*
4790 * This is called to update the er_extoff field in the indirection
4791 * array when extents have been added or removed from one of the
4792 * extent lists. erp_idx contains the irec index to begin updating
4793 * at and ext_diff contains the number of extents that were added
4794 * or removed.
4795 */
4796void
4797xfs_iext_irec_update_extoffs(
4798 xfs_ifork_t *ifp, /* inode fork pointer */
4799 int erp_idx, /* irec index to update */
4800 int ext_diff) /* number of new extents */
4801{
4802 int i; /* loop counter */
4803 int nlists; /* number of irec's (ex lists */
4804
4805 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
4806 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
4807 for (i = erp_idx; i < nlists; i++) {
4808 ifp->if_u1.if_ext_irec[i].er_extoff += ext_diff;
4809 }
4810}