blob: 72ec1a4cc477cd01c1c598607e40dd223e4ab85f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
Nathan Scott7b718762005-11-02 14:58:39 +11003 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Robert P. J. Day40ebd812007-11-23 16:30:51 +110018#include <linux/log2.h>
19
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110021#include "xfs_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "xfs_types.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include "xfs_log.h"
Nathan Scotta844f452005-11-02 14:38:42 +110024#include "xfs_inum.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_mount.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include "xfs_bmap_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110031#include "xfs_alloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "xfs_ialloc_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110033#include "xfs_attr_sf.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "xfs_dinode.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "xfs_inode.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include "xfs_buf_item.h"
Nathan Scotta844f452005-11-02 14:38:42 +110037#include "xfs_inode_item.h"
38#include "xfs_btree.h"
39#include "xfs_alloc.h"
40#include "xfs_ialloc.h"
41#include "xfs_bmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include "xfs_error.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include "xfs_utils.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include "xfs_quota.h"
David Chinner2a82b8b2007-07-11 11:09:12 +100045#include "xfs_filestream.h"
Christoph Hellwig739bfb22007-08-29 10:58:01 +100046#include "xfs_vnodeops.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000047#include "xfs_trace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Linus Torvalds1da177e2005-04-16 15:20:36 -070049kmem_zone_t *xfs_ifork_zone;
50kmem_zone_t *xfs_inode_zone;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52/*
Christoph Hellwig8f04c472011-07-08 14:34:34 +020053 * Used in xfs_itruncate_extents(). This is the maximum number of extents
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 * freed from a file in a single transaction.
55 */
56#define XFS_ITRUNC_MAX_EXTENTS 2
57
58STATIC int xfs_iflush_int(xfs_inode_t *, xfs_buf_t *);
59STATIC int xfs_iformat_local(xfs_inode_t *, xfs_dinode_t *, int, int);
60STATIC int xfs_iformat_extents(xfs_inode_t *, xfs_dinode_t *, int);
61STATIC int xfs_iformat_btree(xfs_inode_t *, xfs_dinode_t *, int);
62
Dave Chinner2a0ec1d2012-04-23 15:59:02 +100063/*
64 * helper function to extract extent size hint from inode
65 */
66xfs_extlen_t
67xfs_get_extsz_hint(
68 struct xfs_inode *ip)
69{
70 if ((ip->i_d.di_flags & XFS_DIFLAG_EXTSIZE) && ip->i_d.di_extsize)
71 return ip->i_d.di_extsize;
72 if (XFS_IS_REALTIME_INODE(ip))
73 return ip->i_mount->m_sb.sb_rextsize;
74 return 0;
75}
76
Linus Torvalds1da177e2005-04-16 15:20:36 -070077#ifdef DEBUG
78/*
79 * Make sure that the extents in the given memory buffer
80 * are valid.
81 */
82STATIC void
83xfs_validate_extents(
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +110084 xfs_ifork_t *ifp,
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 int nrecs,
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 xfs_exntfmt_t fmt)
87{
88 xfs_bmbt_irec_t irec;
Christoph Hellwiga6f64d42007-08-16 16:23:40 +100089 xfs_bmbt_rec_host_t rec;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 int i;
91
92 for (i = 0; i < nrecs; i++) {
Christoph Hellwiga6f64d42007-08-16 16:23:40 +100093 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, i);
94 rec.l0 = get_unaligned(&ep->l0);
95 rec.l1 = get_unaligned(&ep->l1);
96 xfs_bmbt_get_all(&rec, &irec);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 if (fmt == XFS_EXTFMT_NOSTATE)
98 ASSERT(irec.br_state == XFS_EXT_NORM);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 }
100}
101#else /* DEBUG */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +1000102#define xfs_validate_extents(ifp, nrecs, fmt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103#endif /* DEBUG */
104
105/*
106 * Check that none of the inode's in the buffer have a next
107 * unlinked field of 0.
108 */
109#if defined(DEBUG)
110void
111xfs_inobp_check(
112 xfs_mount_t *mp,
113 xfs_buf_t *bp)
114{
115 int i;
116 int j;
117 xfs_dinode_t *dip;
118
119 j = mp->m_inode_cluster_size >> mp->m_sb.sb_inodelog;
120
121 for (i = 0; i < j; i++) {
122 dip = (xfs_dinode_t *)xfs_buf_offset(bp,
123 i * mp->m_sb.sb_inodesize);
124 if (!dip->di_next_unlinked) {
Dave Chinner53487782011-03-07 10:05:35 +1100125 xfs_alert(mp,
126 "Detected bogus zero next_unlinked field in incore inode buffer 0x%p.",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 bp);
128 ASSERT(dip->di_next_unlinked);
129 }
130 }
131}
132#endif
133
134/*
David Chinner4ae29b42008-03-06 13:43:34 +1100135 * Find the buffer associated with the given inode map
136 * We do basic validation checks on the buffer once it has been
137 * retrieved from disk.
138 */
139STATIC int
140xfs_imap_to_bp(
141 xfs_mount_t *mp,
142 xfs_trans_t *tp,
Christoph Hellwig92bfc6e2008-11-28 14:23:41 +1100143 struct xfs_imap *imap,
David Chinner4ae29b42008-03-06 13:43:34 +1100144 xfs_buf_t **bpp,
145 uint buf_flags,
Christoph Hellwigb48d8d62008-11-28 14:23:41 +1100146 uint iget_flags)
David Chinner4ae29b42008-03-06 13:43:34 +1100147{
148 int error;
149 int i;
150 int ni;
151 xfs_buf_t *bp;
152
153 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, imap->im_blkno,
David Chinnera3f74ff2008-03-06 13:43:42 +1100154 (int)imap->im_len, buf_flags, &bp);
David Chinner4ae29b42008-03-06 13:43:34 +1100155 if (error) {
David Chinnera3f74ff2008-03-06 13:43:42 +1100156 if (error != EAGAIN) {
Dave Chinner0b932cc2011-03-07 10:08:35 +1100157 xfs_warn(mp,
158 "%s: xfs_trans_read_buf() returned error %d.",
159 __func__, error);
David Chinnera3f74ff2008-03-06 13:43:42 +1100160 } else {
Christoph Hellwig0cadda12010-01-19 09:56:44 +0000161 ASSERT(buf_flags & XBF_TRYLOCK);
David Chinnera3f74ff2008-03-06 13:43:42 +1100162 }
David Chinner4ae29b42008-03-06 13:43:34 +1100163 return error;
164 }
165
166 /*
167 * Validate the magic number and version of every inode in the buffer
168 * (if DEBUG kernel) or the first inode in the buffer, otherwise.
169 */
170#ifdef DEBUG
171 ni = BBTOB(imap->im_len) >> mp->m_sb.sb_inodelog;
172#else /* usual case */
173 ni = 1;
174#endif
175
176 for (i = 0; i < ni; i++) {
177 int di_ok;
178 xfs_dinode_t *dip;
179
180 dip = (xfs_dinode_t *)xfs_buf_offset(bp,
181 (i << mp->m_sb.sb_inodelog));
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200182 di_ok = dip->di_magic == cpu_to_be16(XFS_DINODE_MAGIC) &&
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100183 XFS_DINODE_GOOD_VERSION(dip->di_version);
David Chinner4ae29b42008-03-06 13:43:34 +1100184 if (unlikely(XFS_TEST_ERROR(!di_ok, mp,
185 XFS_ERRTAG_ITOBP_INOTOBP,
186 XFS_RANDOM_ITOBP_INOTOBP))) {
Dave Chinner19207792010-06-24 11:15:47 +1000187 if (iget_flags & XFS_IGET_UNTRUSTED) {
David Chinner4ae29b42008-03-06 13:43:34 +1100188 xfs_trans_brelse(tp, bp);
189 return XFS_ERROR(EINVAL);
190 }
191 XFS_CORRUPTION_ERROR("xfs_imap_to_bp",
192 XFS_ERRLEVEL_HIGH, mp, dip);
193#ifdef DEBUG
Dave Chinner0b932cc2011-03-07 10:08:35 +1100194 xfs_emerg(mp,
195 "bad inode magic/vsn daddr %lld #%d (magic=%x)",
David Chinner4ae29b42008-03-06 13:43:34 +1100196 (unsigned long long)imap->im_blkno, i,
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100197 be16_to_cpu(dip->di_magic));
Dave Chinner0b932cc2011-03-07 10:08:35 +1100198 ASSERT(0);
David Chinner4ae29b42008-03-06 13:43:34 +1100199#endif
200 xfs_trans_brelse(tp, bp);
201 return XFS_ERROR(EFSCORRUPTED);
202 }
203 }
204
205 xfs_inobp_check(mp, bp);
David Chinner4ae29b42008-03-06 13:43:34 +1100206 *bpp = bp;
207 return 0;
208}
209
210/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 * This routine is called to map an inode number within a file
212 * system to the buffer containing the on-disk version of the
213 * inode. It returns a pointer to the buffer containing the
214 * on-disk inode in the bpp parameter, and in the dip parameter
215 * it returns a pointer to the on-disk inode within that buffer.
216 *
217 * If a non-zero error is returned, then the contents of bpp and
218 * dipp are undefined.
219 *
220 * Use xfs_imap() to determine the size and location of the
221 * buffer to read from disk.
222 */
Christoph Hellwigc679eef2008-10-30 18:04:13 +1100223int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224xfs_inotobp(
225 xfs_mount_t *mp,
226 xfs_trans_t *tp,
227 xfs_ino_t ino,
228 xfs_dinode_t **dipp,
229 xfs_buf_t **bpp,
Christoph Hellwigc679eef2008-10-30 18:04:13 +1100230 int *offset,
231 uint imap_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232{
Christoph Hellwig92bfc6e2008-11-28 14:23:41 +1100233 struct xfs_imap imap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 xfs_buf_t *bp;
235 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 imap.im_blkno = 0;
Christoph Hellwiga1941892008-11-28 14:23:40 +1100238 error = xfs_imap(mp, tp, ino, &imap, imap_flags);
David Chinner4ae29b42008-03-06 13:43:34 +1100239 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
Dave Chinnera8acad72012-04-23 15:58:54 +1000242 error = xfs_imap_to_bp(mp, tp, &imap, &bp, 0, imap_flags);
David Chinner4ae29b42008-03-06 13:43:34 +1100243 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 *dipp = (xfs_dinode_t *)xfs_buf_offset(bp, imap.im_boffset);
247 *bpp = bp;
248 *offset = imap.im_boffset;
249 return 0;
250}
251
252
253/*
254 * This routine is called to map an inode to the buffer containing
255 * the on-disk version of the inode. It returns a pointer to the
256 * buffer containing the on-disk inode in the bpp parameter, and in
257 * the dip parameter it returns a pointer to the on-disk inode within
258 * that buffer.
259 *
260 * If a non-zero error is returned, then the contents of bpp and
261 * dipp are undefined.
262 *
Christoph Hellwig76d8b272008-11-28 14:23:40 +1100263 * The inode is expected to already been mapped to its buffer and read
264 * in once, thus we can use the mapping information stored in the inode
265 * rather than calling xfs_imap(). This allows us to avoid the overhead
266 * of looking at the inode btree for small block file systems
Christoph Hellwig94e1b692008-11-28 14:23:41 +1100267 * (see xfs_imap()).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 */
269int
270xfs_itobp(
271 xfs_mount_t *mp,
272 xfs_trans_t *tp,
273 xfs_inode_t *ip,
274 xfs_dinode_t **dipp,
275 xfs_buf_t **bpp,
David Chinnera3f74ff2008-03-06 13:43:42 +1100276 uint buf_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
278 xfs_buf_t *bp;
279 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
Christoph Hellwig92bfc6e2008-11-28 14:23:41 +1100281 ASSERT(ip->i_imap.im_blkno != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Christoph Hellwig92bfc6e2008-11-28 14:23:41 +1100283 error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &bp, buf_flags, 0);
David Chinner4ae29b42008-03-06 13:43:34 +1100284 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 return error;
Nathan Scott4d1a2ed2006-06-09 17:12:28 +1000286
David Chinnera3f74ff2008-03-06 13:43:42 +1100287 if (!bp) {
Christoph Hellwig0cadda12010-01-19 09:56:44 +0000288 ASSERT(buf_flags & XBF_TRYLOCK);
David Chinnera3f74ff2008-03-06 13:43:42 +1100289 ASSERT(tp == NULL);
290 *bpp = NULL;
291 return EAGAIN;
292 }
293
Christoph Hellwig92bfc6e2008-11-28 14:23:41 +1100294 *dipp = (xfs_dinode_t *)xfs_buf_offset(bp, ip->i_imap.im_boffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 *bpp = bp;
296 return 0;
297}
298
299/*
300 * Move inode type and inode format specific information from the
301 * on-disk inode to the in-core inode. For fifos, devs, and sockets
302 * this means set if_rdev to the proper value. For files, directories,
303 * and symlinks this means to bring in the in-line data or extent
304 * pointers. For a file in B-tree format, only the root is immediately
305 * brought in-core. The rest will be in-lined in if_extents when it
306 * is first referenced (see xfs_iread_extents()).
307 */
308STATIC int
309xfs_iformat(
310 xfs_inode_t *ip,
311 xfs_dinode_t *dip)
312{
313 xfs_attr_shortform_t *atp;
314 int size;
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000315 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 xfs_fsize_t di_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100318 if (unlikely(be32_to_cpu(dip->di_nextents) +
319 be16_to_cpu(dip->di_anextents) >
320 be64_to_cpu(dip->di_nblocks))) {
Dave Chinner65333b42011-03-07 10:03:35 +1100321 xfs_warn(ip->i_mount,
Nathan Scott3762ec62006-01-12 10:29:53 +1100322 "corrupt dinode %Lu, extent total = %d, nblocks = %Lu.",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 (unsigned long long)ip->i_ino,
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100324 (int)(be32_to_cpu(dip->di_nextents) +
325 be16_to_cpu(dip->di_anextents)),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 (unsigned long long)
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100327 be64_to_cpu(dip->di_nblocks));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 XFS_CORRUPTION_ERROR("xfs_iformat(1)", XFS_ERRLEVEL_LOW,
329 ip->i_mount, dip);
330 return XFS_ERROR(EFSCORRUPTED);
331 }
332
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100333 if (unlikely(dip->di_forkoff > ip->i_mount->m_sb.sb_inodesize)) {
Dave Chinner65333b42011-03-07 10:03:35 +1100334 xfs_warn(ip->i_mount, "corrupt dinode %Lu, forkoff = 0x%x.",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 (unsigned long long)ip->i_ino,
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100336 dip->di_forkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 XFS_CORRUPTION_ERROR("xfs_iformat(2)", XFS_ERRLEVEL_LOW,
338 ip->i_mount, dip);
339 return XFS_ERROR(EFSCORRUPTED);
340 }
341
Christoph Hellwigb89d4202009-08-10 11:32:18 -0300342 if (unlikely((ip->i_d.di_flags & XFS_DIFLAG_REALTIME) &&
343 !ip->i_mount->m_rtdev_targp)) {
Dave Chinner65333b42011-03-07 10:03:35 +1100344 xfs_warn(ip->i_mount,
Christoph Hellwigb89d4202009-08-10 11:32:18 -0300345 "corrupt dinode %Lu, has realtime flag set.",
346 ip->i_ino);
347 XFS_CORRUPTION_ERROR("xfs_iformat(realtime)",
348 XFS_ERRLEVEL_LOW, ip->i_mount, dip);
349 return XFS_ERROR(EFSCORRUPTED);
350 }
351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 switch (ip->i_d.di_mode & S_IFMT) {
353 case S_IFIFO:
354 case S_IFCHR:
355 case S_IFBLK:
356 case S_IFSOCK:
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100357 if (unlikely(dip->di_format != XFS_DINODE_FMT_DEV)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 XFS_CORRUPTION_ERROR("xfs_iformat(3)", XFS_ERRLEVEL_LOW,
359 ip->i_mount, dip);
360 return XFS_ERROR(EFSCORRUPTED);
361 }
362 ip->i_d.di_size = 0;
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100363 ip->i_df.if_u2.if_rdev = xfs_dinode_get_rdev(dip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 break;
365
366 case S_IFREG:
367 case S_IFLNK:
368 case S_IFDIR:
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100369 switch (dip->di_format) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 case XFS_DINODE_FMT_LOCAL:
371 /*
372 * no local regular files yet
373 */
Al Viroabbede12011-07-26 02:31:30 -0400374 if (unlikely(S_ISREG(be16_to_cpu(dip->di_mode)))) {
Dave Chinner65333b42011-03-07 10:03:35 +1100375 xfs_warn(ip->i_mount,
376 "corrupt inode %Lu (local format for regular file).",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 (unsigned long long) ip->i_ino);
378 XFS_CORRUPTION_ERROR("xfs_iformat(4)",
379 XFS_ERRLEVEL_LOW,
380 ip->i_mount, dip);
381 return XFS_ERROR(EFSCORRUPTED);
382 }
383
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100384 di_size = be64_to_cpu(dip->di_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 if (unlikely(di_size > XFS_DFORK_DSIZE(dip, ip->i_mount))) {
Dave Chinner65333b42011-03-07 10:03:35 +1100386 xfs_warn(ip->i_mount,
387 "corrupt inode %Lu (bad size %Ld for local inode).",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 (unsigned long long) ip->i_ino,
389 (long long) di_size);
390 XFS_CORRUPTION_ERROR("xfs_iformat(5)",
391 XFS_ERRLEVEL_LOW,
392 ip->i_mount, dip);
393 return XFS_ERROR(EFSCORRUPTED);
394 }
395
396 size = (int)di_size;
397 error = xfs_iformat_local(ip, dip, XFS_DATA_FORK, size);
398 break;
399 case XFS_DINODE_FMT_EXTENTS:
400 error = xfs_iformat_extents(ip, dip, XFS_DATA_FORK);
401 break;
402 case XFS_DINODE_FMT_BTREE:
403 error = xfs_iformat_btree(ip, dip, XFS_DATA_FORK);
404 break;
405 default:
406 XFS_ERROR_REPORT("xfs_iformat(6)", XFS_ERRLEVEL_LOW,
407 ip->i_mount);
408 return XFS_ERROR(EFSCORRUPTED);
409 }
410 break;
411
412 default:
413 XFS_ERROR_REPORT("xfs_iformat(7)", XFS_ERRLEVEL_LOW, ip->i_mount);
414 return XFS_ERROR(EFSCORRUPTED);
415 }
416 if (error) {
417 return error;
418 }
419 if (!XFS_DFORK_Q(dip))
420 return 0;
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000421
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 ASSERT(ip->i_afp == NULL);
Dave Chinner4a7eddd2010-07-20 17:53:59 +1000423 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP | KM_NOFS);
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000424
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100425 switch (dip->di_aformat) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 case XFS_DINODE_FMT_LOCAL:
427 atp = (xfs_attr_shortform_t *)XFS_DFORK_APTR(dip);
Nathan Scott3b244aa2006-03-17 17:29:25 +1100428 size = be16_to_cpu(atp->hdr.totsize);
Christoph Hellwig2809f762009-01-19 02:04:16 +0100429
430 if (unlikely(size < sizeof(struct xfs_attr_sf_hdr))) {
Dave Chinner65333b42011-03-07 10:03:35 +1100431 xfs_warn(ip->i_mount,
432 "corrupt inode %Lu (bad attr fork size %Ld).",
Christoph Hellwig2809f762009-01-19 02:04:16 +0100433 (unsigned long long) ip->i_ino,
434 (long long) size);
435 XFS_CORRUPTION_ERROR("xfs_iformat(8)",
436 XFS_ERRLEVEL_LOW,
437 ip->i_mount, dip);
438 return XFS_ERROR(EFSCORRUPTED);
439 }
440
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 error = xfs_iformat_local(ip, dip, XFS_ATTR_FORK, size);
442 break;
443 case XFS_DINODE_FMT_EXTENTS:
444 error = xfs_iformat_extents(ip, dip, XFS_ATTR_FORK);
445 break;
446 case XFS_DINODE_FMT_BTREE:
447 error = xfs_iformat_btree(ip, dip, XFS_ATTR_FORK);
448 break;
449 default:
450 error = XFS_ERROR(EFSCORRUPTED);
451 break;
452 }
453 if (error) {
454 kmem_zone_free(xfs_ifork_zone, ip->i_afp);
455 ip->i_afp = NULL;
456 xfs_idestroy_fork(ip, XFS_DATA_FORK);
457 }
458 return error;
459}
460
461/*
462 * The file is in-lined in the on-disk inode.
463 * If it fits into if_inline_data, then copy
464 * it there, otherwise allocate a buffer for it
465 * and copy the data there. Either way, set
466 * if_data to point at the data.
467 * If we allocate a buffer for the data, make
468 * sure that its size is a multiple of 4 and
469 * record the real size in i_real_bytes.
470 */
471STATIC int
472xfs_iformat_local(
473 xfs_inode_t *ip,
474 xfs_dinode_t *dip,
475 int whichfork,
476 int size)
477{
478 xfs_ifork_t *ifp;
479 int real_size;
480
481 /*
482 * If the size is unreasonable, then something
483 * is wrong and we just bail out rather than crash in
484 * kmem_alloc() or memcpy() below.
485 */
486 if (unlikely(size > XFS_DFORK_SIZE(dip, ip->i_mount, whichfork))) {
Dave Chinner65333b42011-03-07 10:03:35 +1100487 xfs_warn(ip->i_mount,
488 "corrupt inode %Lu (bad size %d for local fork, size = %d).",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 (unsigned long long) ip->i_ino, size,
490 XFS_DFORK_SIZE(dip, ip->i_mount, whichfork));
491 XFS_CORRUPTION_ERROR("xfs_iformat_local", XFS_ERRLEVEL_LOW,
492 ip->i_mount, dip);
493 return XFS_ERROR(EFSCORRUPTED);
494 }
495 ifp = XFS_IFORK_PTR(ip, whichfork);
496 real_size = 0;
497 if (size == 0)
498 ifp->if_u1.if_data = NULL;
499 else if (size <= sizeof(ifp->if_u2.if_inline_data))
500 ifp->if_u1.if_data = ifp->if_u2.if_inline_data;
501 else {
502 real_size = roundup(size, 4);
Dave Chinner4a7eddd2010-07-20 17:53:59 +1000503 ifp->if_u1.if_data = kmem_alloc(real_size, KM_SLEEP | KM_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 }
505 ifp->if_bytes = size;
506 ifp->if_real_bytes = real_size;
507 if (size)
508 memcpy(ifp->if_u1.if_data, XFS_DFORK_PTR(dip, whichfork), size);
509 ifp->if_flags &= ~XFS_IFEXTENTS;
510 ifp->if_flags |= XFS_IFINLINE;
511 return 0;
512}
513
514/*
515 * The file consists of a set of extents all
516 * of which fit into the on-disk inode.
517 * If there are few enough extents to fit into
518 * the if_inline_ext, then copy them there.
519 * Otherwise allocate a buffer for them and copy
520 * them into it. Either way, set if_extents
521 * to point at the extents.
522 */
523STATIC int
524xfs_iformat_extents(
525 xfs_inode_t *ip,
526 xfs_dinode_t *dip,
527 int whichfork)
528{
Christoph Hellwiga6f64d42007-08-16 16:23:40 +1000529 xfs_bmbt_rec_t *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 xfs_ifork_t *ifp;
531 int nex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 int size;
533 int i;
534
535 ifp = XFS_IFORK_PTR(ip, whichfork);
536 nex = XFS_DFORK_NEXTENTS(dip, whichfork);
537 size = nex * (uint)sizeof(xfs_bmbt_rec_t);
538
539 /*
540 * If the number of extents is unreasonable, then something
541 * is wrong and we just bail out rather than crash in
542 * kmem_alloc() or memcpy() below.
543 */
544 if (unlikely(size < 0 || size > XFS_DFORK_SIZE(dip, ip->i_mount, whichfork))) {
Dave Chinner65333b42011-03-07 10:03:35 +1100545 xfs_warn(ip->i_mount, "corrupt inode %Lu ((a)extents = %d).",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 (unsigned long long) ip->i_ino, nex);
547 XFS_CORRUPTION_ERROR("xfs_iformat_extents(1)", XFS_ERRLEVEL_LOW,
548 ip->i_mount, dip);
549 return XFS_ERROR(EFSCORRUPTED);
550 }
551
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100552 ifp->if_real_bytes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 if (nex == 0)
554 ifp->if_u1.if_extents = NULL;
555 else if (nex <= XFS_INLINE_EXTS)
556 ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100557 else
558 xfs_iext_add(ifp, 0, nex);
559
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 ifp->if_bytes = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 if (size) {
562 dp = (xfs_bmbt_rec_t *) XFS_DFORK_PTR(dip, whichfork);
Christoph Hellwiga6f64d42007-08-16 16:23:40 +1000563 xfs_validate_extents(ifp, nex, XFS_EXTFMT_INODE(ip));
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100564 for (i = 0; i < nex; i++, dp++) {
Christoph Hellwiga6f64d42007-08-16 16:23:40 +1000565 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, i);
Harvey Harrison597bca62008-08-13 16:29:21 +1000566 ep->l0 = get_unaligned_be64(&dp->l0);
567 ep->l1 = get_unaligned_be64(&dp->l1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 }
Eric Sandeen3a59c942007-07-11 11:09:47 +1000569 XFS_BMAP_TRACE_EXLIST(ip, nex, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 if (whichfork != XFS_DATA_FORK ||
571 XFS_EXTFMT_INODE(ip) == XFS_EXTFMT_NOSTATE)
572 if (unlikely(xfs_check_nostate_extents(
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100573 ifp, 0, nex))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 XFS_ERROR_REPORT("xfs_iformat_extents(2)",
575 XFS_ERRLEVEL_LOW,
576 ip->i_mount);
577 return XFS_ERROR(EFSCORRUPTED);
578 }
579 }
580 ifp->if_flags |= XFS_IFEXTENTS;
581 return 0;
582}
583
584/*
585 * The file has too many extents to fit into
586 * the inode, so they are in B-tree format.
587 * Allocate a buffer for the root of the B-tree
588 * and copy the root into it. The i_extents
589 * field will remain NULL until all of the
590 * extents are read in (when they are needed).
591 */
592STATIC int
593xfs_iformat_btree(
594 xfs_inode_t *ip,
595 xfs_dinode_t *dip,
596 int whichfork)
597{
598 xfs_bmdr_block_t *dfp;
599 xfs_ifork_t *ifp;
600 /* REFERENCED */
601 int nrecs;
602 int size;
603
604 ifp = XFS_IFORK_PTR(ip, whichfork);
605 dfp = (xfs_bmdr_block_t *)XFS_DFORK_PTR(dip, whichfork);
606 size = XFS_BMAP_BROOT_SPACE(dfp);
Christoph Hellwig60197e82008-10-30 17:11:19 +1100607 nrecs = be16_to_cpu(dfp->bb_numrecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
609 /*
610 * blow out if -- fork has less extents than can fit in
611 * fork (fork shouldn't be a btree format), root btree
612 * block has more records than can fit into the fork,
613 * or the number of extents is greater than the number of
614 * blocks.
615 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000616 if (unlikely(XFS_IFORK_NEXTENTS(ip, whichfork) <=
617 XFS_IFORK_MAXEXT(ip, whichfork) ||
618 XFS_BMDR_SPACE_CALC(nrecs) >
619 XFS_DFORK_SIZE(dip, ip->i_mount, whichfork) ||
620 XFS_IFORK_NEXTENTS(ip, whichfork) > ip->i_d.di_nblocks)) {
Dave Chinner65333b42011-03-07 10:03:35 +1100621 xfs_warn(ip->i_mount, "corrupt inode %Lu (btree).",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 (unsigned long long) ip->i_ino);
Dave Chinner65333b42011-03-07 10:03:35 +1100623 XFS_CORRUPTION_ERROR("xfs_iformat_btree", XFS_ERRLEVEL_LOW,
624 ip->i_mount, dip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 return XFS_ERROR(EFSCORRUPTED);
626 }
627
628 ifp->if_broot_bytes = size;
Dave Chinner4a7eddd2010-07-20 17:53:59 +1000629 ifp->if_broot = kmem_alloc(size, KM_SLEEP | KM_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 ASSERT(ifp->if_broot != NULL);
631 /*
632 * Copy and convert from the on-disk structure
633 * to the in-memory structure.
634 */
Christoph Hellwig60197e82008-10-30 17:11:19 +1100635 xfs_bmdr_to_bmbt(ip->i_mount, dfp,
636 XFS_DFORK_SIZE(dip, ip->i_mount, whichfork),
637 ifp->if_broot, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 ifp->if_flags &= ~XFS_IFEXTENTS;
639 ifp->if_flags |= XFS_IFBROOT;
640
641 return 0;
642}
643
Eric Sandeend96f8f82009-07-02 00:09:33 -0500644STATIC void
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000645xfs_dinode_from_disk(
646 xfs_icdinode_t *to,
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100647 xfs_dinode_t *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648{
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000649 to->di_magic = be16_to_cpu(from->di_magic);
650 to->di_mode = be16_to_cpu(from->di_mode);
651 to->di_version = from ->di_version;
652 to->di_format = from->di_format;
653 to->di_onlink = be16_to_cpu(from->di_onlink);
654 to->di_uid = be32_to_cpu(from->di_uid);
655 to->di_gid = be32_to_cpu(from->di_gid);
656 to->di_nlink = be32_to_cpu(from->di_nlink);
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +0000657 to->di_projid_lo = be16_to_cpu(from->di_projid_lo);
658 to->di_projid_hi = be16_to_cpu(from->di_projid_hi);
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000659 memcpy(to->di_pad, from->di_pad, sizeof(to->di_pad));
660 to->di_flushiter = be16_to_cpu(from->di_flushiter);
661 to->di_atime.t_sec = be32_to_cpu(from->di_atime.t_sec);
662 to->di_atime.t_nsec = be32_to_cpu(from->di_atime.t_nsec);
663 to->di_mtime.t_sec = be32_to_cpu(from->di_mtime.t_sec);
664 to->di_mtime.t_nsec = be32_to_cpu(from->di_mtime.t_nsec);
665 to->di_ctime.t_sec = be32_to_cpu(from->di_ctime.t_sec);
666 to->di_ctime.t_nsec = be32_to_cpu(from->di_ctime.t_nsec);
667 to->di_size = be64_to_cpu(from->di_size);
668 to->di_nblocks = be64_to_cpu(from->di_nblocks);
669 to->di_extsize = be32_to_cpu(from->di_extsize);
670 to->di_nextents = be32_to_cpu(from->di_nextents);
671 to->di_anextents = be16_to_cpu(from->di_anextents);
672 to->di_forkoff = from->di_forkoff;
673 to->di_aformat = from->di_aformat;
674 to->di_dmevmask = be32_to_cpu(from->di_dmevmask);
675 to->di_dmstate = be16_to_cpu(from->di_dmstate);
676 to->di_flags = be16_to_cpu(from->di_flags);
677 to->di_gen = be32_to_cpu(from->di_gen);
678}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000680void
681xfs_dinode_to_disk(
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100682 xfs_dinode_t *to,
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000683 xfs_icdinode_t *from)
684{
685 to->di_magic = cpu_to_be16(from->di_magic);
686 to->di_mode = cpu_to_be16(from->di_mode);
687 to->di_version = from ->di_version;
688 to->di_format = from->di_format;
689 to->di_onlink = cpu_to_be16(from->di_onlink);
690 to->di_uid = cpu_to_be32(from->di_uid);
691 to->di_gid = cpu_to_be32(from->di_gid);
692 to->di_nlink = cpu_to_be32(from->di_nlink);
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +0000693 to->di_projid_lo = cpu_to_be16(from->di_projid_lo);
694 to->di_projid_hi = cpu_to_be16(from->di_projid_hi);
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000695 memcpy(to->di_pad, from->di_pad, sizeof(to->di_pad));
696 to->di_flushiter = cpu_to_be16(from->di_flushiter);
697 to->di_atime.t_sec = cpu_to_be32(from->di_atime.t_sec);
698 to->di_atime.t_nsec = cpu_to_be32(from->di_atime.t_nsec);
699 to->di_mtime.t_sec = cpu_to_be32(from->di_mtime.t_sec);
700 to->di_mtime.t_nsec = cpu_to_be32(from->di_mtime.t_nsec);
701 to->di_ctime.t_sec = cpu_to_be32(from->di_ctime.t_sec);
702 to->di_ctime.t_nsec = cpu_to_be32(from->di_ctime.t_nsec);
703 to->di_size = cpu_to_be64(from->di_size);
704 to->di_nblocks = cpu_to_be64(from->di_nblocks);
705 to->di_extsize = cpu_to_be32(from->di_extsize);
706 to->di_nextents = cpu_to_be32(from->di_nextents);
707 to->di_anextents = cpu_to_be16(from->di_anextents);
708 to->di_forkoff = from->di_forkoff;
709 to->di_aformat = from->di_aformat;
710 to->di_dmevmask = cpu_to_be32(from->di_dmevmask);
711 to->di_dmstate = cpu_to_be16(from->di_dmstate);
712 to->di_flags = cpu_to_be16(from->di_flags);
713 to->di_gen = cpu_to_be32(from->di_gen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714}
715
716STATIC uint
717_xfs_dic2xflags(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 __uint16_t di_flags)
719{
720 uint flags = 0;
721
722 if (di_flags & XFS_DIFLAG_ANY) {
723 if (di_flags & XFS_DIFLAG_REALTIME)
724 flags |= XFS_XFLAG_REALTIME;
725 if (di_flags & XFS_DIFLAG_PREALLOC)
726 flags |= XFS_XFLAG_PREALLOC;
727 if (di_flags & XFS_DIFLAG_IMMUTABLE)
728 flags |= XFS_XFLAG_IMMUTABLE;
729 if (di_flags & XFS_DIFLAG_APPEND)
730 flags |= XFS_XFLAG_APPEND;
731 if (di_flags & XFS_DIFLAG_SYNC)
732 flags |= XFS_XFLAG_SYNC;
733 if (di_flags & XFS_DIFLAG_NOATIME)
734 flags |= XFS_XFLAG_NOATIME;
735 if (di_flags & XFS_DIFLAG_NODUMP)
736 flags |= XFS_XFLAG_NODUMP;
737 if (di_flags & XFS_DIFLAG_RTINHERIT)
738 flags |= XFS_XFLAG_RTINHERIT;
739 if (di_flags & XFS_DIFLAG_PROJINHERIT)
740 flags |= XFS_XFLAG_PROJINHERIT;
741 if (di_flags & XFS_DIFLAG_NOSYMLINKS)
742 flags |= XFS_XFLAG_NOSYMLINKS;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100743 if (di_flags & XFS_DIFLAG_EXTSIZE)
744 flags |= XFS_XFLAG_EXTSIZE;
745 if (di_flags & XFS_DIFLAG_EXTSZINHERIT)
746 flags |= XFS_XFLAG_EXTSZINHERIT;
Barry Naujokd3446ea2006-06-09 14:54:19 +1000747 if (di_flags & XFS_DIFLAG_NODEFRAG)
748 flags |= XFS_XFLAG_NODEFRAG;
David Chinner2a82b8b2007-07-11 11:09:12 +1000749 if (di_flags & XFS_DIFLAG_FILESTREAM)
750 flags |= XFS_XFLAG_FILESTREAM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 }
752
753 return flags;
754}
755
756uint
757xfs_ip2xflags(
758 xfs_inode_t *ip)
759{
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000760 xfs_icdinode_t *dic = &ip->i_d;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
Nathan Scotta916e2b2006-06-09 17:12:17 +1000762 return _xfs_dic2xflags(dic->di_flags) |
Christoph Hellwig45ba5982007-12-07 14:07:20 +1100763 (XFS_IFORK_Q(ip) ? XFS_XFLAG_HASATTR : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764}
765
766uint
767xfs_dic2xflags(
Christoph Hellwig45ba5982007-12-07 14:07:20 +1100768 xfs_dinode_t *dip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769{
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100770 return _xfs_dic2xflags(be16_to_cpu(dip->di_flags)) |
Christoph Hellwig45ba5982007-12-07 14:07:20 +1100771 (XFS_DFORK_Q(dip) ? XFS_XFLAG_HASATTR : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772}
773
774/*
Christoph Hellwig24f211b2008-11-28 14:23:42 +1100775 * Read the disk inode attributes into the in-core inode structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 */
777int
778xfs_iread(
779 xfs_mount_t *mp,
780 xfs_trans_t *tp,
Christoph Hellwig24f211b2008-11-28 14:23:42 +1100781 xfs_inode_t *ip,
Christoph Hellwig24f211b2008-11-28 14:23:42 +1100782 uint iget_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783{
784 xfs_buf_t *bp;
785 xfs_dinode_t *dip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 int error;
787
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 /*
Christoph Hellwig92bfc6e2008-11-28 14:23:41 +1100789 * Fill in the location information in the in-core inode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 */
Christoph Hellwig24f211b2008-11-28 14:23:42 +1100791 error = xfs_imap(mp, tp, ip->i_ino, &ip->i_imap, iget_flags);
Christoph Hellwig9ed04512008-10-30 18:26:04 +1100792 if (error)
Christoph Hellwig24f211b2008-11-28 14:23:42 +1100793 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
795 /*
Christoph Hellwig92bfc6e2008-11-28 14:23:41 +1100796 * Get pointers to the on-disk inode and the buffer containing it.
Christoph Hellwig76d8b272008-11-28 14:23:40 +1100797 */
Dave Chinnera8acad72012-04-23 15:58:54 +1000798 error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &bp, 0, iget_flags);
Christoph Hellwig76d8b272008-11-28 14:23:40 +1100799 if (error)
Christoph Hellwig24f211b2008-11-28 14:23:42 +1100800 return error;
Christoph Hellwig92bfc6e2008-11-28 14:23:41 +1100801 dip = (xfs_dinode_t *)xfs_buf_offset(bp, ip->i_imap.im_boffset);
Christoph Hellwig76d8b272008-11-28 14:23:40 +1100802
803 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 * If we got something that isn't an inode it means someone
805 * (nfs or dmi) has a stale handle.
806 */
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200807 if (dip->di_magic != cpu_to_be16(XFS_DINODE_MAGIC)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808#ifdef DEBUG
Dave Chinner53487782011-03-07 10:05:35 +1100809 xfs_alert(mp,
810 "%s: dip->di_magic (0x%x) != XFS_DINODE_MAGIC (0x%x)",
811 __func__, be16_to_cpu(dip->di_magic), XFS_DINODE_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812#endif /* DEBUG */
Christoph Hellwig9ed04512008-10-30 18:26:04 +1100813 error = XFS_ERROR(EINVAL);
814 goto out_brelse;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 }
816
817 /*
818 * If the on-disk inode is already linked to a directory
819 * entry, copy all of the inode into the in-core inode.
820 * xfs_iformat() handles copying in the inode format
821 * specific information.
822 * Otherwise, just get the truly permanent information.
823 */
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100824 if (dip->di_mode) {
825 xfs_dinode_from_disk(&ip->i_d, dip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 error = xfs_iformat(ip, dip);
827 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828#ifdef DEBUG
Dave Chinner53487782011-03-07 10:05:35 +1100829 xfs_alert(mp, "%s: xfs_iformat() returned error %d",
830 __func__, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831#endif /* DEBUG */
Christoph Hellwig9ed04512008-10-30 18:26:04 +1100832 goto out_brelse;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 }
834 } else {
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100835 ip->i_d.di_magic = be16_to_cpu(dip->di_magic);
836 ip->i_d.di_version = dip->di_version;
837 ip->i_d.di_gen = be32_to_cpu(dip->di_gen);
838 ip->i_d.di_flushiter = be16_to_cpu(dip->di_flushiter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 /*
840 * Make sure to pull in the mode here as well in
841 * case the inode is released without being used.
842 * This ensures that xfs_inactive() will see that
843 * the inode is already free and not try to mess
844 * with the uninitialized part of it.
845 */
846 ip->i_d.di_mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 }
848
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 /*
850 * The inode format changed when we moved the link count and
851 * made it 32 bits long. If this is an old format inode,
852 * convert it in memory to look like a new one. If it gets
853 * flushed to disk we will convert back before flushing or
854 * logging it. We zero out the new projid field and the old link
855 * count field. We'll handle clearing the pad field (the remains
856 * of the old uuid field) when we actually convert the inode to
857 * the new format. We don't change the version number so that we
858 * can distinguish this from a real new format inode.
859 */
Christoph Hellwig51ce16d2008-11-28 14:23:39 +1100860 if (ip->i_d.di_version == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 ip->i_d.di_nlink = ip->i_d.di_onlink;
862 ip->i_d.di_onlink = 0;
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +0000863 xfs_set_projid(ip, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 }
865
866 ip->i_delayed_blks = 0;
867
868 /*
869 * Mark the buffer containing the inode as something to keep
870 * around for a while. This helps to keep recently accessed
871 * meta-data in-core longer.
872 */
Dave Chinner821eb212010-12-02 16:31:13 +1100873 xfs_buf_set_ref(bp, XFS_INO_REF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
875 /*
876 * Use xfs_trans_brelse() to release the buffer containing the
877 * on-disk inode, because it was acquired with xfs_trans_read_buf()
878 * in xfs_itobp() above. If tp is NULL, this is just a normal
879 * brelse(). If we're within a transaction, then xfs_trans_brelse()
880 * will only release the buffer if it is not dirty within the
881 * transaction. It will be OK to release the buffer in this case,
882 * because inodes on disk are never destroyed and we will be
883 * locking the new in-core inode before putting it in the hash
884 * table where other processes can find it. Thus we don't have
885 * to worry about the inode being changed just because we released
886 * the buffer.
887 */
Christoph Hellwig9ed04512008-10-30 18:26:04 +1100888 out_brelse:
889 xfs_trans_brelse(tp, bp);
Christoph Hellwig9ed04512008-10-30 18:26:04 +1100890 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891}
892
893/*
894 * Read in extents from a btree-format inode.
895 * Allocate and fill in if_extents. Real work is done in xfs_bmap.c.
896 */
897int
898xfs_iread_extents(
899 xfs_trans_t *tp,
900 xfs_inode_t *ip,
901 int whichfork)
902{
903 int error;
904 xfs_ifork_t *ifp;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100905 xfs_extnum_t nextents;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
907 if (unlikely(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
908 XFS_ERROR_REPORT("xfs_iread_extents", XFS_ERRLEVEL_LOW,
909 ip->i_mount);
910 return XFS_ERROR(EFSCORRUPTED);
911 }
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100912 nextents = XFS_IFORK_NEXTENTS(ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 ifp = XFS_IFORK_PTR(ip, whichfork);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100914
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 /*
916 * We know that the size is valid (it's checked in iformat_btree)
917 */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100918 ifp->if_bytes = ifp->if_real_bytes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 ifp->if_flags |= XFS_IFEXTENTS;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100920 xfs_iext_add(ifp, 0, nextents);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 error = xfs_bmap_read_extents(tp, ip, whichfork);
922 if (error) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100923 xfs_iext_destroy(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 ifp->if_flags &= ~XFS_IFEXTENTS;
925 return error;
926 }
Christoph Hellwiga6f64d42007-08-16 16:23:40 +1000927 xfs_validate_extents(ifp, nextents, XFS_EXTFMT_INODE(ip));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 return 0;
929}
930
931/*
932 * Allocate an inode on disk and return a copy of its in-core version.
933 * The in-core inode is locked exclusively. Set mode, nlink, and rdev
934 * appropriately within the inode. The uid and gid for the inode are
935 * set according to the contents of the given cred structure.
936 *
937 * Use xfs_dialloc() to allocate the on-disk inode. If xfs_dialloc()
938 * has a free inode available, call xfs_iget()
939 * to obtain the in-core version of the allocated inode. Finally,
940 * fill in the inode and log its initial contents. In this case,
941 * ialloc_context would be set to NULL and call_again set to false.
942 *
943 * If xfs_dialloc() does not have an available inode,
944 * it will replenish its supply by doing an allocation. Since we can
945 * only do one allocation within a transaction without deadlocks, we
946 * must commit the current transaction before returning the inode itself.
947 * In this case, therefore, we will set call_again to true and return.
948 * The caller should then commit the current transaction, start a new
949 * transaction, and call xfs_ialloc() again to actually get the inode.
950 *
951 * To ensure that some other process does not grab the inode that
952 * was allocated during the first call to xfs_ialloc(), this routine
953 * also returns the [locked] bp pointing to the head of the freelist
954 * as ialloc_context. The caller should hold this buffer across
955 * the commit and pass it back into this routine on the second call.
David Chinnerb11f94d2007-07-11 11:09:33 +1000956 *
957 * If we are allocating quota inodes, we do not have a parent inode
958 * to attach to or associate with (i.e. pip == NULL) because they
959 * are not linked into the directory structure - they are attached
960 * directly to the superblock - and so have no parent.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 */
962int
963xfs_ialloc(
964 xfs_trans_t *tp,
965 xfs_inode_t *pip,
Al Viro576b1d62011-07-26 02:50:15 -0400966 umode_t mode,
Nathan Scott31b084a2005-05-05 13:25:00 -0700967 xfs_nlink_t nlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 xfs_dev_t rdev,
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +0000969 prid_t prid,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 int okalloc,
971 xfs_buf_t **ialloc_context,
972 boolean_t *call_again,
973 xfs_inode_t **ipp)
974{
975 xfs_ino_t ino;
976 xfs_inode_t *ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 uint flags;
978 int error;
Christoph Hellwigdff35fd2008-08-13 16:44:15 +1000979 timespec_t tv;
David Chinnerbf904242008-10-30 17:36:14 +1100980 int filestreams = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
982 /*
983 * Call the space management code to pick
984 * the on-disk inode to be allocated.
985 */
David Chinnerb11f94d2007-07-11 11:09:33 +1000986 error = xfs_dialloc(tp, pip ? pip->i_ino : 0, mode, okalloc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 ialloc_context, call_again, &ino);
David Chinnerbf904242008-10-30 17:36:14 +1100988 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 if (*call_again || ino == NULLFSINO) {
991 *ipp = NULL;
992 return 0;
993 }
994 ASSERT(*ialloc_context == NULL);
995
996 /*
997 * Get the in-core inode with the lock held exclusively.
998 * This is because we're setting fields here we need
999 * to prevent others from looking at until we're done.
1000 */
Christoph Hellwigec3ba852011-02-13 13:26:42 +00001001 error = xfs_iget(tp->t_mountp, tp, ino, XFS_IGET_CREATE,
1002 XFS_ILOCK_EXCL, &ip);
David Chinnerbf904242008-10-30 17:36:14 +11001003 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 ASSERT(ip != NULL);
1006
Al Viro576b1d62011-07-26 02:50:15 -04001007 ip->i_d.di_mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 ip->i_d.di_onlink = 0;
1009 ip->i_d.di_nlink = nlink;
1010 ASSERT(ip->i_d.di_nlink == nlink);
David Howells9e2b2dc2008-08-13 16:20:04 +01001011 ip->i_d.di_uid = current_fsuid();
1012 ip->i_d.di_gid = current_fsgid();
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +00001013 xfs_set_projid(ip, prid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 memset(&(ip->i_d.di_pad[0]), 0, sizeof(ip->i_d.di_pad));
1015
1016 /*
1017 * If the superblock version is up to where we support new format
1018 * inodes and this is currently an old format inode, then change
1019 * the inode version number now. This way we only do the conversion
1020 * here rather than here and in the flush/logging code.
1021 */
Eric Sandeen62118702008-03-06 13:44:28 +11001022 if (xfs_sb_version_hasnlink(&tp->t_mountp->m_sb) &&
Christoph Hellwig51ce16d2008-11-28 14:23:39 +11001023 ip->i_d.di_version == 1) {
1024 ip->i_d.di_version = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 /*
1026 * We've already zeroed the old link count, the projid field,
1027 * and the pad field.
1028 */
1029 }
1030
1031 /*
1032 * Project ids won't be stored on disk if we are using a version 1 inode.
1033 */
Christoph Hellwig51ce16d2008-11-28 14:23:39 +11001034 if ((prid != 0) && (ip->i_d.di_version == 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 xfs_bump_ino_vers2(tp, ip);
1036
Christoph Hellwigbd186aa2007-08-30 17:21:12 +10001037 if (pip && XFS_INHERIT_GID(pip)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 ip->i_d.di_gid = pip->i_d.di_gid;
Al Viroabbede12011-07-26 02:31:30 -04001039 if ((pip->i_d.di_mode & S_ISGID) && S_ISDIR(mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 ip->i_d.di_mode |= S_ISGID;
1041 }
1042 }
1043
1044 /*
1045 * If the group ID of the new file does not match the effective group
1046 * ID or one of the supplementary group IDs, the S_ISGID bit is cleared
1047 * (and only if the irix_sgid_inherit compatibility variable is set).
1048 */
1049 if ((irix_sgid_inherit) &&
1050 (ip->i_d.di_mode & S_ISGID) &&
1051 (!in_group_p((gid_t)ip->i_d.di_gid))) {
1052 ip->i_d.di_mode &= ~S_ISGID;
1053 }
1054
1055 ip->i_d.di_size = 0;
1056 ip->i_d.di_nextents = 0;
1057 ASSERT(ip->i_d.di_nblocks == 0);
Christoph Hellwigdff35fd2008-08-13 16:44:15 +10001058
1059 nanotime(&tv);
1060 ip->i_d.di_mtime.t_sec = (__int32_t)tv.tv_sec;
1061 ip->i_d.di_mtime.t_nsec = (__int32_t)tv.tv_nsec;
1062 ip->i_d.di_atime = ip->i_d.di_mtime;
1063 ip->i_d.di_ctime = ip->i_d.di_mtime;
1064
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 /*
1066 * di_gen will have been taken care of in xfs_iread.
1067 */
1068 ip->i_d.di_extsize = 0;
1069 ip->i_d.di_dmevmask = 0;
1070 ip->i_d.di_dmstate = 0;
1071 ip->i_d.di_flags = 0;
1072 flags = XFS_ILOG_CORE;
1073 switch (mode & S_IFMT) {
1074 case S_IFIFO:
1075 case S_IFCHR:
1076 case S_IFBLK:
1077 case S_IFSOCK:
1078 ip->i_d.di_format = XFS_DINODE_FMT_DEV;
1079 ip->i_df.if_u2.if_rdev = rdev;
1080 ip->i_df.if_flags = 0;
1081 flags |= XFS_ILOG_DEV;
1082 break;
1083 case S_IFREG:
David Chinnerbf904242008-10-30 17:36:14 +11001084 /*
1085 * we can't set up filestreams until after the VFS inode
1086 * is set up properly.
1087 */
1088 if (pip && xfs_inode_is_filestream(pip))
1089 filestreams = 1;
David Chinner2a82b8b2007-07-11 11:09:12 +10001090 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 case S_IFDIR:
David Chinnerb11f94d2007-07-11 11:09:33 +10001092 if (pip && (pip->i_d.di_flags & XFS_DIFLAG_ANY)) {
Nathan Scott365ca832005-06-21 15:39:12 +10001093 uint di_flags = 0;
1094
Al Viroabbede12011-07-26 02:31:30 -04001095 if (S_ISDIR(mode)) {
Nathan Scott365ca832005-06-21 15:39:12 +10001096 if (pip->i_d.di_flags & XFS_DIFLAG_RTINHERIT)
1097 di_flags |= XFS_DIFLAG_RTINHERIT;
Nathan Scottdd9f4382006-01-11 15:28:28 +11001098 if (pip->i_d.di_flags & XFS_DIFLAG_EXTSZINHERIT) {
1099 di_flags |= XFS_DIFLAG_EXTSZINHERIT;
1100 ip->i_d.di_extsize = pip->i_d.di_extsize;
1101 }
Al Viroabbede12011-07-26 02:31:30 -04001102 } else if (S_ISREG(mode)) {
Christoph Hellwig613d7042007-10-11 17:44:08 +10001103 if (pip->i_d.di_flags & XFS_DIFLAG_RTINHERIT)
Nathan Scott365ca832005-06-21 15:39:12 +10001104 di_flags |= XFS_DIFLAG_REALTIME;
Nathan Scottdd9f4382006-01-11 15:28:28 +11001105 if (pip->i_d.di_flags & XFS_DIFLAG_EXTSZINHERIT) {
1106 di_flags |= XFS_DIFLAG_EXTSIZE;
1107 ip->i_d.di_extsize = pip->i_d.di_extsize;
1108 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 }
1110 if ((pip->i_d.di_flags & XFS_DIFLAG_NOATIME) &&
1111 xfs_inherit_noatime)
Nathan Scott365ca832005-06-21 15:39:12 +10001112 di_flags |= XFS_DIFLAG_NOATIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 if ((pip->i_d.di_flags & XFS_DIFLAG_NODUMP) &&
1114 xfs_inherit_nodump)
Nathan Scott365ca832005-06-21 15:39:12 +10001115 di_flags |= XFS_DIFLAG_NODUMP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 if ((pip->i_d.di_flags & XFS_DIFLAG_SYNC) &&
1117 xfs_inherit_sync)
Nathan Scott365ca832005-06-21 15:39:12 +10001118 di_flags |= XFS_DIFLAG_SYNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 if ((pip->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) &&
1120 xfs_inherit_nosymlinks)
Nathan Scott365ca832005-06-21 15:39:12 +10001121 di_flags |= XFS_DIFLAG_NOSYMLINKS;
1122 if (pip->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
1123 di_flags |= XFS_DIFLAG_PROJINHERIT;
Barry Naujokd3446ea2006-06-09 14:54:19 +10001124 if ((pip->i_d.di_flags & XFS_DIFLAG_NODEFRAG) &&
1125 xfs_inherit_nodefrag)
1126 di_flags |= XFS_DIFLAG_NODEFRAG;
David Chinner2a82b8b2007-07-11 11:09:12 +10001127 if (pip->i_d.di_flags & XFS_DIFLAG_FILESTREAM)
1128 di_flags |= XFS_DIFLAG_FILESTREAM;
Nathan Scott365ca832005-06-21 15:39:12 +10001129 ip->i_d.di_flags |= di_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 }
1131 /* FALLTHROUGH */
1132 case S_IFLNK:
1133 ip->i_d.di_format = XFS_DINODE_FMT_EXTENTS;
1134 ip->i_df.if_flags = XFS_IFEXTENTS;
1135 ip->i_df.if_bytes = ip->i_df.if_real_bytes = 0;
1136 ip->i_df.if_u1.if_extents = NULL;
1137 break;
1138 default:
1139 ASSERT(0);
1140 }
1141 /*
1142 * Attribute fork settings for new inode.
1143 */
1144 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
1145 ip->i_d.di_anextents = 0;
1146
1147 /*
1148 * Log the new values stuffed into the inode.
1149 */
Christoph Hellwigddc34152011-09-19 15:00:54 +00001150 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 xfs_trans_log_inode(tp, ip, flags);
1152
Nathan Scottb83bd132006-06-09 16:48:30 +10001153 /* now that we have an i_mode we can setup inode ops and unlock */
Christoph Hellwig41be8be2008-08-13 16:23:13 +10001154 xfs_setup_inode(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
David Chinnerbf904242008-10-30 17:36:14 +11001156 /* now we have set up the vfs inode we can associate the filestream */
1157 if (filestreams) {
1158 error = xfs_filestream_associate(pip, ip);
1159 if (error < 0)
1160 return -error;
1161 if (!error)
1162 xfs_iflags_set(ip, XFS_IFILESTREAM);
1163 }
1164
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 *ipp = ip;
1166 return 0;
1167}
1168
1169/*
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001170 * Free up the underlying blocks past new_size. The new size must be smaller
1171 * than the current size. This routine can be used both for the attribute and
1172 * data fork, and does not modify the inode size, which is left to the caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 *
David Chinnerf6485052008-04-17 16:50:04 +10001174 * The transaction passed to this routine must have made a permanent log
1175 * reservation of at least XFS_ITRUNCATE_LOG_RES. This routine may commit the
1176 * given transaction and start new ones, so make sure everything involved in
1177 * the transaction is tidy before calling here. Some transaction will be
1178 * returned to the caller to be committed. The incoming transaction must
1179 * already include the inode, and both inode locks must be held exclusively.
1180 * The inode must also be "held" within the transaction. On return the inode
1181 * will be "held" within the returned transaction. This routine does NOT
1182 * require any disk space to be reserved for it within the transaction.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 *
David Chinnerf6485052008-04-17 16:50:04 +10001184 * If we get an error, we must return with the inode locked and linked into the
1185 * current transaction. This keeps things simple for the higher level code,
1186 * because it always knows that the inode is locked and held in the transaction
1187 * that returns to it whether errors occur or not. We don't mark the inode
1188 * dirty on error so that transactions can be easily aborted if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 */
1190int
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001191xfs_itruncate_extents(
1192 struct xfs_trans **tpp,
1193 struct xfs_inode *ip,
1194 int whichfork,
1195 xfs_fsize_t new_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196{
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001197 struct xfs_mount *mp = ip->i_mount;
1198 struct xfs_trans *tp = *tpp;
1199 struct xfs_trans *ntp;
1200 xfs_bmap_free_t free_list;
1201 xfs_fsblock_t first_block;
1202 xfs_fileoff_t first_unmap_block;
1203 xfs_fileoff_t last_block;
1204 xfs_filblks_t unmap_len;
1205 int committed;
1206 int error = 0;
1207 int done = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
Christoph Hellwig579aa9c2008-04-22 17:34:00 +10001209 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_IOLOCK_EXCL));
Christoph Hellwigce7ae1512011-12-18 20:00:11 +00001210 ASSERT(new_size <= XFS_ISIZE(ip));
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001211 ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 ASSERT(ip->i_itemp != NULL);
Christoph Hellwig898621d2010-06-24 11:36:58 +10001213 ASSERT(ip->i_itemp->ili_lock_flags == 0);
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001214 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
Christoph Hellwig673e8e52011-12-18 20:00:04 +00001216 trace_xfs_itruncate_extents_start(ip, new_size);
1217
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 /*
1219 * Since it is possible for space to become allocated beyond
1220 * the end of the file (in a crash where the space is allocated
1221 * but the inode size is not yet updated), simply remove any
1222 * blocks which show up between the new EOF and the maximum
1223 * possible file size. If the first block to be removed is
1224 * beyond the maximum file size (ie it is the same as last_block),
1225 * then there is nothing to do.
1226 */
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001227 first_unmap_block = XFS_B_TO_FSB(mp, (xfs_ufsize_t)new_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 last_block = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp));
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001229 if (first_unmap_block == last_block)
1230 return 0;
1231
1232 ASSERT(first_unmap_block < last_block);
1233 unmap_len = last_block - first_unmap_block + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 while (!done) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06001235 xfs_bmap_init(&free_list, &first_block);
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001236 error = xfs_bunmapi(tp, ip,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001237 first_unmap_block, unmap_len,
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001238 xfs_bmapi_aflag(whichfork),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 XFS_ITRUNC_MAX_EXTENTS,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001240 &first_block, &free_list,
Christoph Hellwigb4e91812010-06-23 18:11:15 +10001241 &done);
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001242 if (error)
1243 goto out_bmap_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244
1245 /*
1246 * Duplicate the transaction that has the permanent
1247 * reservation and commit the old transaction.
1248 */
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001249 error = xfs_bmap_finish(&tp, &free_list, &committed);
Christoph Hellwig898621d2010-06-24 11:36:58 +10001250 if (committed)
Christoph Hellwigddc34152011-09-19 15:00:54 +00001251 xfs_trans_ijoin(tp, ip, 0);
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001252 if (error)
1253 goto out_bmap_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254
1255 if (committed) {
1256 /*
David Chinnerf6485052008-04-17 16:50:04 +10001257 * Mark the inode dirty so it will be logged and
David Chinnere5720ee2008-04-10 12:21:18 +10001258 * moved forward in the log as part of every commit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 */
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001260 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 }
David Chinnerf6485052008-04-17 16:50:04 +10001262
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001263 ntp = xfs_trans_dup(tp);
1264 error = xfs_trans_commit(tp, 0);
1265 tp = ntp;
David Chinnere5720ee2008-04-10 12:21:18 +10001266
Christoph Hellwigddc34152011-09-19 15:00:54 +00001267 xfs_trans_ijoin(tp, ip, 0);
David Chinnerf6485052008-04-17 16:50:04 +10001268
Dave Chinnercc09c0d2008-11-17 17:37:10 +11001269 if (error)
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001270 goto out;
1271
Dave Chinnercc09c0d2008-11-17 17:37:10 +11001272 /*
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001273 * Transaction commit worked ok so we can drop the extra ticket
Dave Chinnercc09c0d2008-11-17 17:37:10 +11001274 * reference that we gained in xfs_trans_dup()
1275 */
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001276 xfs_log_ticket_put(tp->t_ticket);
1277 error = xfs_trans_reserve(tp, 0,
David Chinnerf6485052008-04-17 16:50:04 +10001278 XFS_ITRUNCATE_LOG_RES(mp), 0,
1279 XFS_TRANS_PERM_LOG_RES,
1280 XFS_ITRUNCATE_LOG_COUNT);
1281 if (error)
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001282 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 }
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001284
Christoph Hellwig673e8e52011-12-18 20:00:04 +00001285 /*
1286 * Always re-log the inode so that our permanent transaction can keep
1287 * on rolling it forward in the log.
1288 */
1289 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1290
1291 trace_xfs_itruncate_extents_end(ip, new_size);
1292
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001293out:
1294 *tpp = tp;
1295 return error;
1296out_bmap_cancel:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 /*
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001298 * If the bunmapi call encounters an error, return to the caller where
1299 * the transaction can be properly aborted. We just need to make sure
1300 * we're not holding any resources that we were not when we came in.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 */
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001302 xfs_bmap_cancel(&free_list);
1303 goto out;
1304}
1305
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306/*
1307 * This is called when the inode's link count goes to 0.
1308 * We place the on-disk inode on a list in the AGI. It
1309 * will be pulled from this list when the inode is freed.
1310 */
1311int
1312xfs_iunlink(
1313 xfs_trans_t *tp,
1314 xfs_inode_t *ip)
1315{
1316 xfs_mount_t *mp;
1317 xfs_agi_t *agi;
1318 xfs_dinode_t *dip;
1319 xfs_buf_t *agibp;
1320 xfs_buf_t *ibp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 xfs_agino_t agino;
1322 short bucket_index;
1323 int offset;
1324 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325
1326 ASSERT(ip->i_d.di_nlink == 0);
1327 ASSERT(ip->i_d.di_mode != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
1329 mp = tp->t_mountp;
1330
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 /*
1332 * Get the agi buffer first. It ensures lock ordering
1333 * on the list.
1334 */
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11001335 error = xfs_read_agi(mp, tp, XFS_INO_TO_AGNO(mp, ip->i_ino), &agibp);
Vlad Apostolov859d7182007-10-11 17:44:18 +10001336 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 agi = XFS_BUF_TO_AGI(agibp);
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11001339
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 /*
1341 * Get the index into the agi hash table for the
1342 * list this inode will go on.
1343 */
1344 agino = XFS_INO_TO_AGINO(mp, ip->i_ino);
1345 ASSERT(agino != 0);
1346 bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS;
1347 ASSERT(agi->agi_unlinked[bucket_index]);
Christoph Hellwig16259e72005-11-02 15:11:25 +11001348 ASSERT(be32_to_cpu(agi->agi_unlinked[bucket_index]) != agino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001350 if (agi->agi_unlinked[bucket_index] != cpu_to_be32(NULLAGINO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 /*
1352 * There is already another inode in the bucket we need
1353 * to add ourselves to. Add us at the front of the list.
1354 * Here we put the head pointer into our next pointer,
1355 * and then we fall through to point the head at us.
1356 */
Dave Chinnera8acad72012-04-23 15:58:54 +10001357 error = xfs_itobp(mp, tp, ip, &dip, &ibp, 0);
Vlad Apostolovc319b582007-11-23 16:27:51 +11001358 if (error)
1359 return error;
1360
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001361 ASSERT(dip->di_next_unlinked == cpu_to_be32(NULLAGINO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 dip->di_next_unlinked = agi->agi_unlinked[bucket_index];
Christoph Hellwig92bfc6e2008-11-28 14:23:41 +11001363 offset = ip->i_imap.im_boffset +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 offsetof(xfs_dinode_t, di_next_unlinked);
1365 xfs_trans_inode_buf(tp, ibp);
1366 xfs_trans_log_buf(tp, ibp, offset,
1367 (offset + sizeof(xfs_agino_t) - 1));
1368 xfs_inobp_check(mp, ibp);
1369 }
1370
1371 /*
1372 * Point the bucket head pointer at the inode being inserted.
1373 */
1374 ASSERT(agino != 0);
Christoph Hellwig16259e72005-11-02 15:11:25 +11001375 agi->agi_unlinked[bucket_index] = cpu_to_be32(agino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 offset = offsetof(xfs_agi_t, agi_unlinked) +
1377 (sizeof(xfs_agino_t) * bucket_index);
1378 xfs_trans_log_buf(tp, agibp, offset,
1379 (offset + sizeof(xfs_agino_t) - 1));
1380 return 0;
1381}
1382
1383/*
1384 * Pull the on-disk inode from the AGI unlinked list.
1385 */
1386STATIC int
1387xfs_iunlink_remove(
1388 xfs_trans_t *tp,
1389 xfs_inode_t *ip)
1390{
1391 xfs_ino_t next_ino;
1392 xfs_mount_t *mp;
1393 xfs_agi_t *agi;
1394 xfs_dinode_t *dip;
1395 xfs_buf_t *agibp;
1396 xfs_buf_t *ibp;
1397 xfs_agnumber_t agno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 xfs_agino_t agino;
1399 xfs_agino_t next_agino;
1400 xfs_buf_t *last_ibp;
Nathan Scott6fdf8cc2006-06-28 10:13:52 +10001401 xfs_dinode_t *last_dip = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 short bucket_index;
Nathan Scott6fdf8cc2006-06-28 10:13:52 +10001403 int offset, last_offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 mp = tp->t_mountp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 agno = XFS_INO_TO_AGNO(mp, ip->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
1409 /*
1410 * Get the agi buffer first. It ensures lock ordering
1411 * on the list.
1412 */
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11001413 error = xfs_read_agi(mp, tp, agno, &agibp);
1414 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 return error;
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11001416
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 agi = XFS_BUF_TO_AGI(agibp);
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11001418
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 /*
1420 * Get the index into the agi hash table for the
1421 * list this inode will go on.
1422 */
1423 agino = XFS_INO_TO_AGINO(mp, ip->i_ino);
1424 ASSERT(agino != 0);
1425 bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001426 ASSERT(agi->agi_unlinked[bucket_index] != cpu_to_be32(NULLAGINO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 ASSERT(agi->agi_unlinked[bucket_index]);
1428
Christoph Hellwig16259e72005-11-02 15:11:25 +11001429 if (be32_to_cpu(agi->agi_unlinked[bucket_index]) == agino) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 /*
1431 * We're at the head of the list. Get the inode's
1432 * on-disk buffer to see if there is anyone after us
1433 * on the list. Only modify our next pointer if it
1434 * is not already NULLAGINO. This saves us the overhead
1435 * of dealing with the buffer when there is no need to
1436 * change it.
1437 */
Dave Chinnera8acad72012-04-23 15:58:54 +10001438 error = xfs_itobp(mp, tp, ip, &dip, &ibp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 if (error) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001440 xfs_warn(mp, "%s: xfs_itobp() returned error %d.",
1441 __func__, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 return error;
1443 }
Christoph Hellwig347d1c02007-08-28 13:57:51 +10001444 next_agino = be32_to_cpu(dip->di_next_unlinked);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 ASSERT(next_agino != 0);
1446 if (next_agino != NULLAGINO) {
Christoph Hellwig347d1c02007-08-28 13:57:51 +10001447 dip->di_next_unlinked = cpu_to_be32(NULLAGINO);
Christoph Hellwig92bfc6e2008-11-28 14:23:41 +11001448 offset = ip->i_imap.im_boffset +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 offsetof(xfs_dinode_t, di_next_unlinked);
1450 xfs_trans_inode_buf(tp, ibp);
1451 xfs_trans_log_buf(tp, ibp, offset,
1452 (offset + sizeof(xfs_agino_t) - 1));
1453 xfs_inobp_check(mp, ibp);
1454 } else {
1455 xfs_trans_brelse(tp, ibp);
1456 }
1457 /*
1458 * Point the bucket head pointer at the next inode.
1459 */
1460 ASSERT(next_agino != 0);
1461 ASSERT(next_agino != agino);
Christoph Hellwig16259e72005-11-02 15:11:25 +11001462 agi->agi_unlinked[bucket_index] = cpu_to_be32(next_agino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 offset = offsetof(xfs_agi_t, agi_unlinked) +
1464 (sizeof(xfs_agino_t) * bucket_index);
1465 xfs_trans_log_buf(tp, agibp, offset,
1466 (offset + sizeof(xfs_agino_t) - 1));
1467 } else {
1468 /*
1469 * We need to search the list for the inode being freed.
1470 */
Christoph Hellwig16259e72005-11-02 15:11:25 +11001471 next_agino = be32_to_cpu(agi->agi_unlinked[bucket_index]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 last_ibp = NULL;
1473 while (next_agino != agino) {
1474 /*
1475 * If the last inode wasn't the one pointing to
1476 * us, then release its buffer since we're not
1477 * going to do anything with it.
1478 */
1479 if (last_ibp != NULL) {
1480 xfs_trans_brelse(tp, last_ibp);
1481 }
1482 next_ino = XFS_AGINO_TO_INO(mp, agno, next_agino);
1483 error = xfs_inotobp(mp, tp, next_ino, &last_dip,
Christoph Hellwigc679eef2008-10-30 18:04:13 +11001484 &last_ibp, &last_offset, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 if (error) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001486 xfs_warn(mp,
1487 "%s: xfs_inotobp() returned error %d.",
1488 __func__, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 return error;
1490 }
Christoph Hellwig347d1c02007-08-28 13:57:51 +10001491 next_agino = be32_to_cpu(last_dip->di_next_unlinked);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 ASSERT(next_agino != NULLAGINO);
1493 ASSERT(next_agino != 0);
1494 }
1495 /*
1496 * Now last_ibp points to the buffer previous to us on
1497 * the unlinked list. Pull us from the list.
1498 */
Dave Chinnera8acad72012-04-23 15:58:54 +10001499 error = xfs_itobp(mp, tp, ip, &dip, &ibp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 if (error) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001501 xfs_warn(mp, "%s: xfs_itobp(2) returned error %d.",
1502 __func__, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 return error;
1504 }
Christoph Hellwig347d1c02007-08-28 13:57:51 +10001505 next_agino = be32_to_cpu(dip->di_next_unlinked);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 ASSERT(next_agino != 0);
1507 ASSERT(next_agino != agino);
1508 if (next_agino != NULLAGINO) {
Christoph Hellwig347d1c02007-08-28 13:57:51 +10001509 dip->di_next_unlinked = cpu_to_be32(NULLAGINO);
Christoph Hellwig92bfc6e2008-11-28 14:23:41 +11001510 offset = ip->i_imap.im_boffset +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 offsetof(xfs_dinode_t, di_next_unlinked);
1512 xfs_trans_inode_buf(tp, ibp);
1513 xfs_trans_log_buf(tp, ibp, offset,
1514 (offset + sizeof(xfs_agino_t) - 1));
1515 xfs_inobp_check(mp, ibp);
1516 } else {
1517 xfs_trans_brelse(tp, ibp);
1518 }
1519 /*
1520 * Point the previous inode on the list to the next inode.
1521 */
Christoph Hellwig347d1c02007-08-28 13:57:51 +10001522 last_dip->di_next_unlinked = cpu_to_be32(next_agino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 ASSERT(next_agino != 0);
1524 offset = last_offset + offsetof(xfs_dinode_t, di_next_unlinked);
1525 xfs_trans_inode_buf(tp, last_ibp);
1526 xfs_trans_log_buf(tp, last_ibp, offset,
1527 (offset + sizeof(xfs_agino_t) - 1));
1528 xfs_inobp_check(mp, last_ibp);
1529 }
1530 return 0;
1531}
1532
Dave Chinner5b3eed72010-08-24 11:42:41 +10001533/*
1534 * A big issue when freeing the inode cluster is is that we _cannot_ skip any
1535 * inodes that are in memory - they all must be marked stale and attached to
1536 * the cluster buffer.
1537 */
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +00001538STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539xfs_ifree_cluster(
1540 xfs_inode_t *free_ip,
1541 xfs_trans_t *tp,
1542 xfs_ino_t inum)
1543{
1544 xfs_mount_t *mp = free_ip->i_mount;
1545 int blks_per_cluster;
1546 int nbufs;
1547 int ninodes;
Dave Chinner5b257b42010-06-03 16:22:29 +10001548 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 xfs_daddr_t blkno;
1550 xfs_buf_t *bp;
Dave Chinner5b257b42010-06-03 16:22:29 +10001551 xfs_inode_t *ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 xfs_inode_log_item_t *iip;
1553 xfs_log_item_t *lip;
Dave Chinner5017e972010-01-11 11:47:40 +00001554 struct xfs_perag *pag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555
Dave Chinner5017e972010-01-11 11:47:40 +00001556 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, inum));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 if (mp->m_sb.sb_blocksize >= XFS_INODE_CLUSTER_SIZE(mp)) {
1558 blks_per_cluster = 1;
1559 ninodes = mp->m_sb.sb_inopblock;
1560 nbufs = XFS_IALLOC_BLOCKS(mp);
1561 } else {
1562 blks_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) /
1563 mp->m_sb.sb_blocksize;
1564 ninodes = blks_per_cluster * mp->m_sb.sb_inopblock;
1565 nbufs = XFS_IALLOC_BLOCKS(mp) / blks_per_cluster;
1566 }
1567
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 for (j = 0; j < nbufs; j++, inum += ninodes) {
1569 blkno = XFS_AGB_TO_DADDR(mp, XFS_INO_TO_AGNO(mp, inum),
1570 XFS_INO_TO_AGBNO(mp, inum));
1571
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 /*
Dave Chinner5b257b42010-06-03 16:22:29 +10001573 * We obtain and lock the backing buffer first in the process
1574 * here, as we have to ensure that any dirty inode that we
1575 * can't get the flush lock on is attached to the buffer.
1576 * If we scan the in-memory inodes first, then buffer IO can
1577 * complete before we get a lock on it, and hence we may fail
1578 * to mark all the active inodes on the buffer stale.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 */
Dave Chinner5b257b42010-06-03 16:22:29 +10001580 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, blkno,
Dave Chinnera8acad72012-04-23 15:58:54 +10001581 mp->m_bsize * blks_per_cluster, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +00001583 if (!bp)
1584 return ENOMEM;
Dave Chinner5b257b42010-06-03 16:22:29 +10001585 /*
1586 * Walk the inodes already attached to the buffer and mark them
1587 * stale. These will all have the flush locks held, so an
Dave Chinner5b3eed72010-08-24 11:42:41 +10001588 * in-memory inode walk can't lock them. By marking them all
1589 * stale first, we will not attempt to lock them in the loop
1590 * below as the XFS_ISTALE flag will be set.
Dave Chinner5b257b42010-06-03 16:22:29 +10001591 */
Christoph Hellwigadadbee2011-07-13 13:43:49 +02001592 lip = bp->b_fspriv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 while (lip) {
1594 if (lip->li_type == XFS_LI_INODE) {
1595 iip = (xfs_inode_log_item_t *)lip;
1596 ASSERT(iip->ili_logged == 1);
Christoph Hellwigca30b2a2010-06-23 18:11:15 +10001597 lip->li_cb = xfs_istale_done;
David Chinner7b2e2a32008-10-30 17:39:12 +11001598 xfs_trans_ail_copy_lsn(mp->m_ail,
1599 &iip->ili_flush_lsn,
1600 &iip->ili_item.li_lsn);
David Chinnere5ffd2b2006-11-21 18:55:33 +11001601 xfs_iflags_set(iip->ili_inode, XFS_ISTALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 }
1603 lip = lip->li_bio_list;
1604 }
1605
Dave Chinner5b3eed72010-08-24 11:42:41 +10001606
Dave Chinner5b257b42010-06-03 16:22:29 +10001607 /*
1608 * For each inode in memory attempt to add it to the inode
1609 * buffer and set it up for being staled on buffer IO
1610 * completion. This is safe as we've locked out tail pushing
1611 * and flushing by locking the buffer.
1612 *
1613 * We have already marked every inode that was part of a
1614 * transaction stale above, which means there is no point in
1615 * even trying to lock them.
1616 */
1617 for (i = 0; i < ninodes; i++) {
Dave Chinner5b3eed72010-08-24 11:42:41 +10001618retry:
Dave Chinner1a3e8f32010-12-17 17:29:43 +11001619 rcu_read_lock();
Dave Chinner5b257b42010-06-03 16:22:29 +10001620 ip = radix_tree_lookup(&pag->pag_ici_root,
1621 XFS_INO_TO_AGINO(mp, (inum + i)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622
Dave Chinner1a3e8f32010-12-17 17:29:43 +11001623 /* Inode not in memory, nothing to do */
1624 if (!ip) {
1625 rcu_read_unlock();
Dave Chinner5b257b42010-06-03 16:22:29 +10001626 continue;
1627 }
1628
Dave Chinner5b3eed72010-08-24 11:42:41 +10001629 /*
Dave Chinner1a3e8f32010-12-17 17:29:43 +11001630 * because this is an RCU protected lookup, we could
1631 * find a recently freed or even reallocated inode
1632 * during the lookup. We need to check under the
1633 * i_flags_lock for a valid inode here. Skip it if it
1634 * is not valid, the wrong inode or stale.
1635 */
1636 spin_lock(&ip->i_flags_lock);
1637 if (ip->i_ino != inum + i ||
1638 __xfs_iflags_test(ip, XFS_ISTALE)) {
1639 spin_unlock(&ip->i_flags_lock);
1640 rcu_read_unlock();
1641 continue;
1642 }
1643 spin_unlock(&ip->i_flags_lock);
1644
1645 /*
Dave Chinner5b3eed72010-08-24 11:42:41 +10001646 * Don't try to lock/unlock the current inode, but we
1647 * _cannot_ skip the other inodes that we did not find
1648 * in the list attached to the buffer and are not
1649 * already marked stale. If we can't lock it, back off
1650 * and retry.
1651 */
Dave Chinner5b257b42010-06-03 16:22:29 +10001652 if (ip != free_ip &&
1653 !xfs_ilock_nowait(ip, XFS_ILOCK_EXCL)) {
Dave Chinner1a3e8f32010-12-17 17:29:43 +11001654 rcu_read_unlock();
Dave Chinner5b3eed72010-08-24 11:42:41 +10001655 delay(1);
1656 goto retry;
Dave Chinner5b257b42010-06-03 16:22:29 +10001657 }
Dave Chinner1a3e8f32010-12-17 17:29:43 +11001658 rcu_read_unlock();
Dave Chinner5b257b42010-06-03 16:22:29 +10001659
Dave Chinner5b3eed72010-08-24 11:42:41 +10001660 xfs_iflock(ip);
Dave Chinner5b257b42010-06-03 16:22:29 +10001661 xfs_iflags_set(ip, XFS_ISTALE);
Dave Chinner5b257b42010-06-03 16:22:29 +10001662
Dave Chinner5b3eed72010-08-24 11:42:41 +10001663 /*
1664 * we don't need to attach clean inodes or those only
1665 * with unlogged changes (which we throw away, anyway).
1666 */
Dave Chinner5b257b42010-06-03 16:22:29 +10001667 iip = ip->i_itemp;
Dave Chinner5b3eed72010-08-24 11:42:41 +10001668 if (!iip || xfs_inode_clean(ip)) {
Dave Chinner5b257b42010-06-03 16:22:29 +10001669 ASSERT(ip != free_ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 xfs_ifunlock(ip);
1671 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1672 continue;
1673 }
1674
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +00001675 iip->ili_last_fields = iip->ili_fields;
1676 iip->ili_fields = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 iip->ili_logged = 1;
David Chinner7b2e2a32008-10-30 17:39:12 +11001678 xfs_trans_ail_copy_lsn(mp->m_ail, &iip->ili_flush_lsn,
1679 &iip->ili_item.li_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680
Christoph Hellwigca30b2a2010-06-23 18:11:15 +10001681 xfs_buf_attach_iodone(bp, xfs_istale_done,
1682 &iip->ili_item);
Dave Chinner5b257b42010-06-03 16:22:29 +10001683
1684 if (ip != free_ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 }
1687
Dave Chinner5b3eed72010-08-24 11:42:41 +10001688 xfs_trans_stale_inode_buf(tp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 xfs_trans_binval(tp, bp);
1690 }
1691
Dave Chinner5017e972010-01-11 11:47:40 +00001692 xfs_perag_put(pag);
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +00001693 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694}
1695
1696/*
1697 * This is called to return an inode to the inode free list.
1698 * The inode should already be truncated to 0 length and have
1699 * no pages associated with it. This routine also assumes that
1700 * the inode is already a part of the transaction.
1701 *
1702 * The on-disk copy of the inode will have been added to the list
1703 * of unlinked inodes in the AGI. We need to remove the inode from
1704 * that list atomically with respect to freeing it here.
1705 */
1706int
1707xfs_ifree(
1708 xfs_trans_t *tp,
1709 xfs_inode_t *ip,
1710 xfs_bmap_free_t *flist)
1711{
1712 int error;
1713 int delete;
1714 xfs_ino_t first_ino;
Vlad Apostolovc319b582007-11-23 16:27:51 +11001715 xfs_dinode_t *dip;
1716 xfs_buf_t *ibp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717
Christoph Hellwig579aa9c2008-04-22 17:34:00 +10001718 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 ASSERT(ip->i_d.di_nlink == 0);
1720 ASSERT(ip->i_d.di_nextents == 0);
1721 ASSERT(ip->i_d.di_anextents == 0);
Christoph Hellwigce7ae1512011-12-18 20:00:11 +00001722 ASSERT(ip->i_d.di_size == 0 || !S_ISREG(ip->i_d.di_mode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 ASSERT(ip->i_d.di_nblocks == 0);
1724
1725 /*
1726 * Pull the on-disk inode from the AGI unlinked list.
1727 */
1728 error = xfs_iunlink_remove(tp, ip);
1729 if (error != 0) {
1730 return error;
1731 }
1732
1733 error = xfs_difree(tp, ip->i_ino, flist, &delete, &first_ino);
1734 if (error != 0) {
1735 return error;
1736 }
1737 ip->i_d.di_mode = 0; /* mark incore inode as free */
1738 ip->i_d.di_flags = 0;
1739 ip->i_d.di_dmevmask = 0;
1740 ip->i_d.di_forkoff = 0; /* mark the attr fork not in use */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 ip->i_d.di_format = XFS_DINODE_FMT_EXTENTS;
1742 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
1743 /*
1744 * Bump the generation count so no one will be confused
1745 * by reincarnations of this inode.
1746 */
1747 ip->i_d.di_gen++;
Vlad Apostolovc319b582007-11-23 16:27:51 +11001748
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1750
Dave Chinnera8acad72012-04-23 15:58:54 +10001751 error = xfs_itobp(ip->i_mount, tp, ip, &dip, &ibp, 0);
Vlad Apostolovc319b582007-11-23 16:27:51 +11001752 if (error)
1753 return error;
1754
1755 /*
1756 * Clear the on-disk di_mode. This is to prevent xfs_bulkstat
1757 * from picking up this inode when it is reclaimed (its incore state
1758 * initialzed but not flushed to disk yet). The in-core di_mode is
1759 * already cleared and a corresponding transaction logged.
1760 * The hack here just synchronizes the in-core to on-disk
1761 * di_mode value in advance before the actual inode sync to disk.
1762 * This is OK because the inode is already unlinked and would never
1763 * change its di_mode again for this inode generation.
1764 * This is a temporary hack that would require a proper fix
1765 * in the future.
1766 */
Christoph Hellwig81591fe2008-11-28 14:23:39 +11001767 dip->di_mode = 0;
Vlad Apostolovc319b582007-11-23 16:27:51 +11001768
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 if (delete) {
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +00001770 error = xfs_ifree_cluster(ip, tp, first_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 }
1772
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +00001773 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774}
1775
1776/*
1777 * Reallocate the space for if_broot based on the number of records
1778 * being added or deleted as indicated in rec_diff. Move the records
1779 * and pointers in if_broot to fit the new size. When shrinking this
1780 * will eliminate holes between the records and pointers created by
1781 * the caller. When growing this will create holes to be filled in
1782 * by the caller.
1783 *
1784 * The caller must not request to add more records than would fit in
1785 * the on-disk inode root. If the if_broot is currently NULL, then
1786 * if we adding records one will be allocated. The caller must also
1787 * not request that the number of records go below zero, although
1788 * it can go to zero.
1789 *
1790 * ip -- the inode whose if_broot area is changing
1791 * ext_diff -- the change in the number of records, positive or negative,
1792 * requested for the if_broot array.
1793 */
1794void
1795xfs_iroot_realloc(
1796 xfs_inode_t *ip,
1797 int rec_diff,
1798 int whichfork)
1799{
Christoph Hellwig60197e82008-10-30 17:11:19 +11001800 struct xfs_mount *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 int cur_max;
1802 xfs_ifork_t *ifp;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11001803 struct xfs_btree_block *new_broot;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 int new_max;
1805 size_t new_size;
1806 char *np;
1807 char *op;
1808
1809 /*
1810 * Handle the degenerate case quietly.
1811 */
1812 if (rec_diff == 0) {
1813 return;
1814 }
1815
1816 ifp = XFS_IFORK_PTR(ip, whichfork);
1817 if (rec_diff > 0) {
1818 /*
1819 * If there wasn't any memory allocated before, just
1820 * allocate it now and get out.
1821 */
1822 if (ifp->if_broot_bytes == 0) {
1823 new_size = (size_t)XFS_BMAP_BROOT_SPACE_CALC(rec_diff);
Dave Chinner4a7eddd2010-07-20 17:53:59 +10001824 ifp->if_broot = kmem_alloc(new_size, KM_SLEEP | KM_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 ifp->if_broot_bytes = (int)new_size;
1826 return;
1827 }
1828
1829 /*
1830 * If there is already an existing if_broot, then we need
1831 * to realloc() it and shift the pointers to their new
1832 * location. The records don't change location because
1833 * they are kept butted up against the btree block header.
1834 */
Christoph Hellwig60197e82008-10-30 17:11:19 +11001835 cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 new_max = cur_max + rec_diff;
1837 new_size = (size_t)XFS_BMAP_BROOT_SPACE_CALC(new_max);
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11001838 ifp->if_broot = kmem_realloc(ifp->if_broot, new_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 (size_t)XFS_BMAP_BROOT_SPACE_CALC(cur_max), /* old size */
Dave Chinner4a7eddd2010-07-20 17:53:59 +10001840 KM_SLEEP | KM_NOFS);
Christoph Hellwig60197e82008-10-30 17:11:19 +11001841 op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
1842 ifp->if_broot_bytes);
1843 np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
1844 (int)new_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 ifp->if_broot_bytes = (int)new_size;
1846 ASSERT(ifp->if_broot_bytes <=
1847 XFS_IFORK_SIZE(ip, whichfork) + XFS_BROOT_SIZE_ADJ);
1848 memmove(np, op, cur_max * (uint)sizeof(xfs_dfsbno_t));
1849 return;
1850 }
1851
1852 /*
1853 * rec_diff is less than 0. In this case, we are shrinking the
1854 * if_broot buffer. It must already exist. If we go to zero
1855 * records, just get rid of the root and clear the status bit.
1856 */
1857 ASSERT((ifp->if_broot != NULL) && (ifp->if_broot_bytes > 0));
Christoph Hellwig60197e82008-10-30 17:11:19 +11001858 cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 new_max = cur_max + rec_diff;
1860 ASSERT(new_max >= 0);
1861 if (new_max > 0)
1862 new_size = (size_t)XFS_BMAP_BROOT_SPACE_CALC(new_max);
1863 else
1864 new_size = 0;
1865 if (new_size > 0) {
Dave Chinner4a7eddd2010-07-20 17:53:59 +10001866 new_broot = kmem_alloc(new_size, KM_SLEEP | KM_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 /*
1868 * First copy over the btree block header.
1869 */
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11001870 memcpy(new_broot, ifp->if_broot, XFS_BTREE_LBLOCK_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 } else {
1872 new_broot = NULL;
1873 ifp->if_flags &= ~XFS_IFBROOT;
1874 }
1875
1876 /*
1877 * Only copy the records and pointers if there are any.
1878 */
1879 if (new_max > 0) {
1880 /*
1881 * First copy the records.
1882 */
Christoph Hellwig136341b2008-10-30 17:11:40 +11001883 op = (char *)XFS_BMBT_REC_ADDR(mp, ifp->if_broot, 1);
1884 np = (char *)XFS_BMBT_REC_ADDR(mp, new_broot, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 memcpy(np, op, new_max * (uint)sizeof(xfs_bmbt_rec_t));
1886
1887 /*
1888 * Then copy the pointers.
1889 */
Christoph Hellwig60197e82008-10-30 17:11:19 +11001890 op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 ifp->if_broot_bytes);
Christoph Hellwig60197e82008-10-30 17:11:19 +11001892 np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, new_broot, 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 (int)new_size);
1894 memcpy(np, op, new_max * (uint)sizeof(xfs_dfsbno_t));
1895 }
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001896 kmem_free(ifp->if_broot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 ifp->if_broot = new_broot;
1898 ifp->if_broot_bytes = (int)new_size;
1899 ASSERT(ifp->if_broot_bytes <=
1900 XFS_IFORK_SIZE(ip, whichfork) + XFS_BROOT_SIZE_ADJ);
1901 return;
1902}
1903
1904
1905/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 * This is called when the amount of space needed for if_data
1907 * is increased or decreased. The change in size is indicated by
1908 * the number of bytes that need to be added or deleted in the
1909 * byte_diff parameter.
1910 *
1911 * If the amount of space needed has decreased below the size of the
1912 * inline buffer, then switch to using the inline buffer. Otherwise,
1913 * use kmem_realloc() or kmem_alloc() to adjust the size of the buffer
1914 * to what is needed.
1915 *
1916 * ip -- the inode whose if_data area is changing
1917 * byte_diff -- the change in the number of bytes, positive or negative,
1918 * requested for the if_data array.
1919 */
1920void
1921xfs_idata_realloc(
1922 xfs_inode_t *ip,
1923 int byte_diff,
1924 int whichfork)
1925{
1926 xfs_ifork_t *ifp;
1927 int new_size;
1928 int real_size;
1929
1930 if (byte_diff == 0) {
1931 return;
1932 }
1933
1934 ifp = XFS_IFORK_PTR(ip, whichfork);
1935 new_size = (int)ifp->if_bytes + byte_diff;
1936 ASSERT(new_size >= 0);
1937
1938 if (new_size == 0) {
1939 if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) {
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001940 kmem_free(ifp->if_u1.if_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 }
1942 ifp->if_u1.if_data = NULL;
1943 real_size = 0;
1944 } else if (new_size <= sizeof(ifp->if_u2.if_inline_data)) {
1945 /*
1946 * If the valid extents/data can fit in if_inline_ext/data,
1947 * copy them from the malloc'd vector and free it.
1948 */
1949 if (ifp->if_u1.if_data == NULL) {
1950 ifp->if_u1.if_data = ifp->if_u2.if_inline_data;
1951 } else if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) {
1952 ASSERT(ifp->if_real_bytes != 0);
1953 memcpy(ifp->if_u2.if_inline_data, ifp->if_u1.if_data,
1954 new_size);
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001955 kmem_free(ifp->if_u1.if_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 ifp->if_u1.if_data = ifp->if_u2.if_inline_data;
1957 }
1958 real_size = 0;
1959 } else {
1960 /*
1961 * Stuck with malloc/realloc.
1962 * For inline data, the underlying buffer must be
1963 * a multiple of 4 bytes in size so that it can be
1964 * logged and stay on word boundaries. We enforce
1965 * that here.
1966 */
1967 real_size = roundup(new_size, 4);
1968 if (ifp->if_u1.if_data == NULL) {
1969 ASSERT(ifp->if_real_bytes == 0);
Dave Chinner4a7eddd2010-07-20 17:53:59 +10001970 ifp->if_u1.if_data = kmem_alloc(real_size,
1971 KM_SLEEP | KM_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 } else if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) {
1973 /*
1974 * Only do the realloc if the underlying size
1975 * is really changing.
1976 */
1977 if (ifp->if_real_bytes != real_size) {
1978 ifp->if_u1.if_data =
1979 kmem_realloc(ifp->if_u1.if_data,
1980 real_size,
1981 ifp->if_real_bytes,
Dave Chinner4a7eddd2010-07-20 17:53:59 +10001982 KM_SLEEP | KM_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 }
1984 } else {
1985 ASSERT(ifp->if_real_bytes == 0);
Dave Chinner4a7eddd2010-07-20 17:53:59 +10001986 ifp->if_u1.if_data = kmem_alloc(real_size,
1987 KM_SLEEP | KM_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 memcpy(ifp->if_u1.if_data, ifp->if_u2.if_inline_data,
1989 ifp->if_bytes);
1990 }
1991 }
1992 ifp->if_real_bytes = real_size;
1993 ifp->if_bytes = new_size;
1994 ASSERT(ifp->if_bytes <= XFS_IFORK_SIZE(ip, whichfork));
1995}
1996
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997void
1998xfs_idestroy_fork(
1999 xfs_inode_t *ip,
2000 int whichfork)
2001{
2002 xfs_ifork_t *ifp;
2003
2004 ifp = XFS_IFORK_PTR(ip, whichfork);
2005 if (ifp->if_broot != NULL) {
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10002006 kmem_free(ifp->if_broot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 ifp->if_broot = NULL;
2008 }
2009
2010 /*
2011 * If the format is local, then we can't have an extents
2012 * array so just look for an inline data array. If we're
2013 * not local then we may or may not have an extents list,
2014 * so check and free it up if we do.
2015 */
2016 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
2017 if ((ifp->if_u1.if_data != ifp->if_u2.if_inline_data) &&
2018 (ifp->if_u1.if_data != NULL)) {
2019 ASSERT(ifp->if_real_bytes != 0);
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10002020 kmem_free(ifp->if_u1.if_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 ifp->if_u1.if_data = NULL;
2022 ifp->if_real_bytes = 0;
2023 }
2024 } else if ((ifp->if_flags & XFS_IFEXTENTS) &&
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11002025 ((ifp->if_flags & XFS_IFEXTIREC) ||
2026 ((ifp->if_u1.if_extents != NULL) &&
2027 (ifp->if_u1.if_extents != ifp->if_u2.if_inline_ext)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 ASSERT(ifp->if_real_bytes != 0);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002029 xfs_iext_destroy(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 }
2031 ASSERT(ifp->if_u1.if_extents == NULL ||
2032 ifp->if_u1.if_extents == ifp->if_u2.if_inline_ext);
2033 ASSERT(ifp->if_real_bytes == 0);
2034 if (whichfork == XFS_ATTR_FORK) {
2035 kmem_zone_free(xfs_ifork_zone, ip->i_afp);
2036 ip->i_afp = NULL;
2037 }
2038}
2039
2040/*
Christoph Hellwig60ec6782010-02-17 19:43:56 +00002041 * This is called to unpin an inode. The caller must have the inode locked
2042 * in at least shared mode so that the buffer cannot be subsequently pinned
2043 * once someone is waiting for it to be unpinned.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 */
Christoph Hellwig60ec6782010-02-17 19:43:56 +00002045static void
Christoph Hellwigf392e632011-12-18 20:00:10 +00002046xfs_iunpin(
Christoph Hellwig60ec6782010-02-17 19:43:56 +00002047 struct xfs_inode *ip)
David Chinnera3f74ff2008-03-06 13:43:42 +11002048{
Christoph Hellwig579aa9c2008-04-22 17:34:00 +10002049 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
David Chinnera3f74ff2008-03-06 13:43:42 +11002050
Dave Chinner4aaf15d2010-03-08 11:24:07 +11002051 trace_xfs_inode_unpin_nowait(ip, _RET_IP_);
2052
David Chinnera3f74ff2008-03-06 13:43:42 +11002053 /* Give the log a push to start the unpinning I/O */
Christoph Hellwig60ec6782010-02-17 19:43:56 +00002054 xfs_log_force_lsn(ip->i_mount, ip->i_itemp->ili_last_lsn, 0);
Christoph Hellwiga14a3482010-01-19 09:56:46 +00002055
David Chinnera3f74ff2008-03-06 13:43:42 +11002056}
2057
Christoph Hellwigf392e632011-12-18 20:00:10 +00002058static void
2059__xfs_iunpin_wait(
2060 struct xfs_inode *ip)
2061{
2062 wait_queue_head_t *wq = bit_waitqueue(&ip->i_flags, __XFS_IPINNED_BIT);
2063 DEFINE_WAIT_BIT(wait, &ip->i_flags, __XFS_IPINNED_BIT);
2064
2065 xfs_iunpin(ip);
2066
2067 do {
2068 prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
2069 if (xfs_ipincount(ip))
2070 io_schedule();
2071 } while (xfs_ipincount(ip));
2072 finish_wait(wq, &wait.wait);
2073}
2074
Dave Chinner777df5a2010-02-06 12:37:26 +11002075void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076xfs_iunpin_wait(
Christoph Hellwig60ec6782010-02-17 19:43:56 +00002077 struct xfs_inode *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078{
Christoph Hellwigf392e632011-12-18 20:00:10 +00002079 if (xfs_ipincount(ip))
2080 __xfs_iunpin_wait(ip);
David Chinnera3f74ff2008-03-06 13:43:42 +11002081}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083/*
2084 * xfs_iextents_copy()
2085 *
2086 * This is called to copy the REAL extents (as opposed to the delayed
2087 * allocation extents) from the inode into the given buffer. It
2088 * returns the number of bytes copied into the buffer.
2089 *
2090 * If there are no delayed allocation extents, then we can just
2091 * memcpy() the extents into the buffer. Otherwise, we need to
2092 * examine each extent in turn and skip those which are delayed.
2093 */
2094int
2095xfs_iextents_copy(
2096 xfs_inode_t *ip,
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10002097 xfs_bmbt_rec_t *dp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 int whichfork)
2099{
2100 int copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 int i;
2102 xfs_ifork_t *ifp;
2103 int nrecs;
2104 xfs_fsblock_t start_block;
2105
2106 ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwig579aa9c2008-04-22 17:34:00 +10002107 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 ASSERT(ifp->if_bytes > 0);
2109
2110 nrecs = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
Eric Sandeen3a59c942007-07-11 11:09:47 +10002111 XFS_BMAP_TRACE_EXLIST(ip, nrecs, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 ASSERT(nrecs > 0);
2113
2114 /*
2115 * There are some delayed allocation extents in the
2116 * inode, so copy the extents one at a time and skip
2117 * the delayed ones. There must be at least one
2118 * non-delayed extent.
2119 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 copied = 0;
2121 for (i = 0; i < nrecs; i++) {
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10002122 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 start_block = xfs_bmbt_get_startblock(ep);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002124 if (isnullstartblock(start_block)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 /*
2126 * It's a delayed allocation extent, so skip it.
2127 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 continue;
2129 }
2130
2131 /* Translate to on disk format */
Christoph Hellwigcd8b0a92007-08-16 16:24:15 +10002132 put_unaligned(cpu_to_be64(ep->l0), &dp->l0);
2133 put_unaligned(cpu_to_be64(ep->l1), &dp->l1);
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10002134 dp++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 copied++;
2136 }
2137 ASSERT(copied != 0);
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10002138 xfs_validate_extents(ifp, copied, XFS_EXTFMT_INODE(ip));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139
2140 return (copied * (uint)sizeof(xfs_bmbt_rec_t));
2141}
2142
2143/*
2144 * Each of the following cases stores data into the same region
2145 * of the on-disk inode, so only one of them can be valid at
2146 * any given time. While it is possible to have conflicting formats
2147 * and log flags, e.g. having XFS_ILOG_?DATA set when the fork is
2148 * in EXTENTS format, this can only happen when the fork has
2149 * changed formats after being modified but before being flushed.
2150 * In these cases, the format always takes precedence, because the
2151 * format indicates the current state of the fork.
2152 */
2153/*ARGSUSED*/
David Chinnere4ac9672008-04-10 12:23:58 +10002154STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155xfs_iflush_fork(
2156 xfs_inode_t *ip,
2157 xfs_dinode_t *dip,
2158 xfs_inode_log_item_t *iip,
2159 int whichfork,
2160 xfs_buf_t *bp)
2161{
2162 char *cp;
2163 xfs_ifork_t *ifp;
2164 xfs_mount_t *mp;
2165#ifdef XFS_TRANS_DEBUG
2166 int first;
2167#endif
2168 static const short brootflag[2] =
2169 { XFS_ILOG_DBROOT, XFS_ILOG_ABROOT };
2170 static const short dataflag[2] =
2171 { XFS_ILOG_DDATA, XFS_ILOG_ADATA };
2172 static const short extflag[2] =
2173 { XFS_ILOG_DEXT, XFS_ILOG_AEXT };
2174
David Chinnere4ac9672008-04-10 12:23:58 +10002175 if (!iip)
2176 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 ifp = XFS_IFORK_PTR(ip, whichfork);
2178 /*
2179 * This can happen if we gave up in iformat in an error path,
2180 * for the attribute fork.
2181 */
David Chinnere4ac9672008-04-10 12:23:58 +10002182 if (!ifp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 ASSERT(whichfork == XFS_ATTR_FORK);
David Chinnere4ac9672008-04-10 12:23:58 +10002184 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 }
2186 cp = XFS_DFORK_PTR(dip, whichfork);
2187 mp = ip->i_mount;
2188 switch (XFS_IFORK_FORMAT(ip, whichfork)) {
2189 case XFS_DINODE_FMT_LOCAL:
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +00002190 if ((iip->ili_fields & dataflag[whichfork]) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 (ifp->if_bytes > 0)) {
2192 ASSERT(ifp->if_u1.if_data != NULL);
2193 ASSERT(ifp->if_bytes <= XFS_IFORK_SIZE(ip, whichfork));
2194 memcpy(cp, ifp->if_u1.if_data, ifp->if_bytes);
2195 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 break;
2197
2198 case XFS_DINODE_FMT_EXTENTS:
2199 ASSERT((ifp->if_flags & XFS_IFEXTENTS) ||
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +00002200 !(iip->ili_fields & extflag[whichfork]));
2201 if ((iip->ili_fields & extflag[whichfork]) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 (ifp->if_bytes > 0)) {
Christoph Hellwigab1908a2011-05-11 15:04:10 +00002203 ASSERT(xfs_iext_get_ext(ifp, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) > 0);
2205 (void)xfs_iextents_copy(ip, (xfs_bmbt_rec_t *)cp,
2206 whichfork);
2207 }
2208 break;
2209
2210 case XFS_DINODE_FMT_BTREE:
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +00002211 if ((iip->ili_fields & brootflag[whichfork]) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 (ifp->if_broot_bytes > 0)) {
2213 ASSERT(ifp->if_broot != NULL);
2214 ASSERT(ifp->if_broot_bytes <=
2215 (XFS_IFORK_SIZE(ip, whichfork) +
2216 XFS_BROOT_SIZE_ADJ));
Christoph Hellwig60197e82008-10-30 17:11:19 +11002217 xfs_bmbt_to_bmdr(mp, ifp->if_broot, ifp->if_broot_bytes,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 (xfs_bmdr_block_t *)cp,
2219 XFS_DFORK_SIZE(dip, mp, whichfork));
2220 }
2221 break;
2222
2223 case XFS_DINODE_FMT_DEV:
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +00002224 if (iip->ili_fields & XFS_ILOG_DEV) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 ASSERT(whichfork == XFS_DATA_FORK);
Christoph Hellwig81591fe2008-11-28 14:23:39 +11002226 xfs_dinode_put_rdev(dip, ip->i_df.if_u2.if_rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 }
2228 break;
2229
2230 case XFS_DINODE_FMT_UUID:
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +00002231 if (iip->ili_fields & XFS_ILOG_UUID) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 ASSERT(whichfork == XFS_DATA_FORK);
Christoph Hellwig81591fe2008-11-28 14:23:39 +11002233 memcpy(XFS_DFORK_DPTR(dip),
2234 &ip->i_df.if_u2.if_uuid,
2235 sizeof(uuid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 }
2237 break;
2238
2239 default:
2240 ASSERT(0);
2241 break;
2242 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243}
2244
David Chinnerbad55842008-03-06 13:43:49 +11002245STATIC int
2246xfs_iflush_cluster(
2247 xfs_inode_t *ip,
2248 xfs_buf_t *bp)
2249{
2250 xfs_mount_t *mp = ip->i_mount;
Dave Chinner5017e972010-01-11 11:47:40 +00002251 struct xfs_perag *pag;
David Chinnerbad55842008-03-06 13:43:49 +11002252 unsigned long first_index, mask;
David Chinnerc8f5f122008-05-20 11:30:15 +10002253 unsigned long inodes_per_cluster;
David Chinnerbad55842008-03-06 13:43:49 +11002254 int ilist_size;
2255 xfs_inode_t **ilist;
2256 xfs_inode_t *iq;
David Chinnerbad55842008-03-06 13:43:49 +11002257 int nr_found;
2258 int clcount = 0;
2259 int bufwasdelwri;
2260 int i;
2261
Dave Chinner5017e972010-01-11 11:47:40 +00002262 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
David Chinnerbad55842008-03-06 13:43:49 +11002263
David Chinnerc8f5f122008-05-20 11:30:15 +10002264 inodes_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_inodelog;
2265 ilist_size = inodes_per_cluster * sizeof(xfs_inode_t *);
David Chinner49383b02008-05-19 16:29:34 +10002266 ilist = kmem_alloc(ilist_size, KM_MAYFAIL|KM_NOFS);
David Chinnerbad55842008-03-06 13:43:49 +11002267 if (!ilist)
Dave Chinner44b56e02010-01-11 11:47:43 +00002268 goto out_put;
David Chinnerbad55842008-03-06 13:43:49 +11002269
2270 mask = ~(((XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_inodelog)) - 1);
2271 first_index = XFS_INO_TO_AGINO(mp, ip->i_ino) & mask;
Dave Chinner1a3e8f32010-12-17 17:29:43 +11002272 rcu_read_lock();
David Chinnerbad55842008-03-06 13:43:49 +11002273 /* really need a gang lookup range call here */
2274 nr_found = radix_tree_gang_lookup(&pag->pag_ici_root, (void**)ilist,
David Chinnerc8f5f122008-05-20 11:30:15 +10002275 first_index, inodes_per_cluster);
David Chinnerbad55842008-03-06 13:43:49 +11002276 if (nr_found == 0)
2277 goto out_free;
2278
2279 for (i = 0; i < nr_found; i++) {
2280 iq = ilist[i];
2281 if (iq == ip)
2282 continue;
Dave Chinner1a3e8f32010-12-17 17:29:43 +11002283
2284 /*
2285 * because this is an RCU protected lookup, we could find a
2286 * recently freed or even reallocated inode during the lookup.
2287 * We need to check under the i_flags_lock for a valid inode
2288 * here. Skip it if it is not valid or the wrong inode.
2289 */
2290 spin_lock(&ip->i_flags_lock);
2291 if (!ip->i_ino ||
2292 (XFS_INO_TO_AGINO(mp, iq->i_ino) & mask) != first_index) {
2293 spin_unlock(&ip->i_flags_lock);
2294 continue;
2295 }
2296 spin_unlock(&ip->i_flags_lock);
2297
David Chinnerbad55842008-03-06 13:43:49 +11002298 /*
2299 * Do an un-protected check to see if the inode is dirty and
2300 * is a candidate for flushing. These checks will be repeated
2301 * later after the appropriate locks are acquired.
2302 */
David Chinner33540402008-03-06 13:43:59 +11002303 if (xfs_inode_clean(iq) && xfs_ipincount(iq) == 0)
David Chinnerbad55842008-03-06 13:43:49 +11002304 continue;
David Chinnerbad55842008-03-06 13:43:49 +11002305
2306 /*
2307 * Try to get locks. If any are unavailable or it is pinned,
2308 * then this inode cannot be flushed and is skipped.
2309 */
2310
2311 if (!xfs_ilock_nowait(iq, XFS_ILOCK_SHARED))
2312 continue;
2313 if (!xfs_iflock_nowait(iq)) {
2314 xfs_iunlock(iq, XFS_ILOCK_SHARED);
2315 continue;
2316 }
2317 if (xfs_ipincount(iq)) {
2318 xfs_ifunlock(iq);
2319 xfs_iunlock(iq, XFS_ILOCK_SHARED);
2320 continue;
2321 }
2322
2323 /*
2324 * arriving here means that this inode can be flushed. First
2325 * re-check that it's dirty before flushing.
2326 */
David Chinner33540402008-03-06 13:43:59 +11002327 if (!xfs_inode_clean(iq)) {
2328 int error;
David Chinnerbad55842008-03-06 13:43:49 +11002329 error = xfs_iflush_int(iq, bp);
2330 if (error) {
2331 xfs_iunlock(iq, XFS_ILOCK_SHARED);
2332 goto cluster_corrupt_out;
2333 }
2334 clcount++;
2335 } else {
2336 xfs_ifunlock(iq);
2337 }
2338 xfs_iunlock(iq, XFS_ILOCK_SHARED);
2339 }
2340
2341 if (clcount) {
2342 XFS_STATS_INC(xs_icluster_flushcnt);
2343 XFS_STATS_ADD(xs_icluster_flushinode, clcount);
2344 }
2345
2346out_free:
Dave Chinner1a3e8f32010-12-17 17:29:43 +11002347 rcu_read_unlock();
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10002348 kmem_free(ilist);
Dave Chinner44b56e02010-01-11 11:47:43 +00002349out_put:
2350 xfs_perag_put(pag);
David Chinnerbad55842008-03-06 13:43:49 +11002351 return 0;
2352
2353
2354cluster_corrupt_out:
2355 /*
2356 * Corruption detected in the clustering loop. Invalidate the
2357 * inode buffer and shut down the filesystem.
2358 */
Dave Chinner1a3e8f32010-12-17 17:29:43 +11002359 rcu_read_unlock();
David Chinnerbad55842008-03-06 13:43:49 +11002360 /*
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002361 * Clean up the buffer. If it was delwri, just release it --
David Chinnerbad55842008-03-06 13:43:49 +11002362 * brelse can handle it with no problems. If not, shut down the
2363 * filesystem before releasing the buffer.
2364 */
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002365 bufwasdelwri = (bp->b_flags & _XBF_DELWRI_Q);
David Chinnerbad55842008-03-06 13:43:49 +11002366 if (bufwasdelwri)
2367 xfs_buf_relse(bp);
2368
2369 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
2370
2371 if (!bufwasdelwri) {
2372 /*
2373 * Just like incore_relse: if we have b_iodone functions,
2374 * mark the buffer as an error and call them. Otherwise
2375 * mark it as stale and brelse.
2376 */
Christoph Hellwigcb669ca2011-07-13 13:43:49 +02002377 if (bp->b_iodone) {
David Chinnerbad55842008-03-06 13:43:49 +11002378 XFS_BUF_UNDONE(bp);
Christoph Hellwigc867cb62011-10-10 16:52:46 +00002379 xfs_buf_stale(bp);
Chandra Seetharaman5a52c2a582011-07-22 23:39:51 +00002380 xfs_buf_ioerror(bp, EIO);
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00002381 xfs_buf_ioend(bp, 0);
David Chinnerbad55842008-03-06 13:43:49 +11002382 } else {
Christoph Hellwigc867cb62011-10-10 16:52:46 +00002383 xfs_buf_stale(bp);
David Chinnerbad55842008-03-06 13:43:49 +11002384 xfs_buf_relse(bp);
2385 }
2386 }
2387
2388 /*
2389 * Unlocks the flush lock
2390 */
Dave Chinner04913fd2012-04-23 15:58:41 +10002391 xfs_iflush_abort(iq, false);
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10002392 kmem_free(ilist);
Dave Chinner44b56e02010-01-11 11:47:43 +00002393 xfs_perag_put(pag);
David Chinnerbad55842008-03-06 13:43:49 +11002394 return XFS_ERROR(EFSCORRUPTED);
2395}
2396
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397/*
Christoph Hellwig4c468192012-04-23 15:58:36 +10002398 * Flush dirty inode metadata into the backing buffer.
2399 *
2400 * The caller must have the inode lock and the inode flush lock held. The
2401 * inode lock will still be held upon return to the caller, and the inode
2402 * flush lock will be released after the inode has reached the disk.
2403 *
2404 * The caller must write out the buffer returned in *bpp and release it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 */
2406int
2407xfs_iflush(
Christoph Hellwig4c468192012-04-23 15:58:36 +10002408 struct xfs_inode *ip,
2409 struct xfs_buf **bpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410{
Christoph Hellwig4c468192012-04-23 15:58:36 +10002411 struct xfs_mount *mp = ip->i_mount;
2412 struct xfs_buf *bp;
2413 struct xfs_dinode *dip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415
2416 XFS_STATS_INC(xs_iflush_count);
2417
Christoph Hellwig579aa9c2008-04-22 17:34:00 +10002418 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
Christoph Hellwig474fce02011-12-18 20:00:09 +00002419 ASSERT(xfs_isiflocked(ip));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE ||
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00002421 ip->i_d.di_nextents > XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422
Christoph Hellwig4c468192012-04-23 15:58:36 +10002423 *bpp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 xfs_iunpin_wait(ip);
2426
2427 /*
Dave Chinner4b6a4682010-01-11 11:45:21 +00002428 * For stale inodes we cannot rely on the backing buffer remaining
2429 * stale in cache for the remaining life of the stale inode and so
2430 * xfs_itobp() below may give us a buffer that no longer contains
2431 * inodes below. We have to check this after ensuring the inode is
2432 * unpinned so that it is safe to reclaim the stale inode after the
2433 * flush call.
2434 */
2435 if (xfs_iflags_test(ip, XFS_ISTALE)) {
2436 xfs_ifunlock(ip);
2437 return 0;
2438 }
2439
2440 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 * This may have been unpinned because the filesystem is shutting
2442 * down forcibly. If that's the case we must not write this inode
Christoph Hellwig32ce90a2012-04-23 15:58:32 +10002443 * to disk, because the log record didn't make it to disk.
2444 *
2445 * We also have to remove the log item from the AIL in this case,
2446 * as we wait for an empty AIL as part of the unmount process.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 */
2448 if (XFS_FORCED_SHUTDOWN(mp)) {
Christoph Hellwig32ce90a2012-04-23 15:58:32 +10002449 error = XFS_ERROR(EIO);
2450 goto abort_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 }
2452
2453 /*
David Chinnera3f74ff2008-03-06 13:43:42 +11002454 * Get the buffer containing the on-disk inode.
2455 */
Christoph Hellwig4c468192012-04-23 15:58:36 +10002456 error = xfs_itobp(mp, NULL, ip, &dip, &bp, XBF_TRYLOCK);
David Chinnera3f74ff2008-03-06 13:43:42 +11002457 if (error || !bp) {
2458 xfs_ifunlock(ip);
2459 return error;
2460 }
2461
2462 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 * First flush out the inode that xfs_iflush was called with.
2464 */
2465 error = xfs_iflush_int(ip, bp);
David Chinnerbad55842008-03-06 13:43:49 +11002466 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 goto corrupt_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468
2469 /*
David Chinnera3f74ff2008-03-06 13:43:42 +11002470 * If the buffer is pinned then push on the log now so we won't
2471 * get stuck waiting in the write for too long.
2472 */
Chandra Seetharaman811e64c2011-07-22 23:40:27 +00002473 if (xfs_buf_ispinned(bp))
Christoph Hellwiga14a3482010-01-19 09:56:46 +00002474 xfs_log_force(mp, 0);
David Chinnera3f74ff2008-03-06 13:43:42 +11002475
2476 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 * inode clustering:
2478 * see if other inodes can be gathered into this write
2479 */
David Chinnerbad55842008-03-06 13:43:49 +11002480 error = xfs_iflush_cluster(ip, bp);
2481 if (error)
2482 goto cluster_corrupt_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483
Christoph Hellwig4c468192012-04-23 15:58:36 +10002484 *bpp = bp;
2485 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486
2487corrupt_out:
2488 xfs_buf_relse(bp);
Nathan Scott7d04a332006-06-09 14:58:38 +10002489 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490cluster_corrupt_out:
Christoph Hellwig32ce90a2012-04-23 15:58:32 +10002491 error = XFS_ERROR(EFSCORRUPTED);
2492abort_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 /*
2494 * Unlocks the flush lock
2495 */
Dave Chinner04913fd2012-04-23 15:58:41 +10002496 xfs_iflush_abort(ip, false);
Christoph Hellwig32ce90a2012-04-23 15:58:32 +10002497 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498}
2499
2500
2501STATIC int
2502xfs_iflush_int(
2503 xfs_inode_t *ip,
2504 xfs_buf_t *bp)
2505{
2506 xfs_inode_log_item_t *iip;
2507 xfs_dinode_t *dip;
2508 xfs_mount_t *mp;
2509#ifdef XFS_TRANS_DEBUG
2510 int first;
2511#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512
Christoph Hellwig579aa9c2008-04-22 17:34:00 +10002513 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
Christoph Hellwig474fce02011-12-18 20:00:09 +00002514 ASSERT(xfs_isiflocked(ip));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE ||
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00002516 ip->i_d.di_nextents > XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517
2518 iip = ip->i_itemp;
2519 mp = ip->i_mount;
2520
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 /* set *dip = inode's place in the buffer */
Christoph Hellwig92bfc6e2008-11-28 14:23:41 +11002522 dip = (xfs_dinode_t *)xfs_buf_offset(bp, ip->i_imap.im_boffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523
Christoph Hellwig69ef9212011-07-08 14:36:05 +02002524 if (XFS_TEST_ERROR(dip->di_magic != cpu_to_be16(XFS_DINODE_MAGIC),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 mp, XFS_ERRTAG_IFLUSH_1, XFS_RANDOM_IFLUSH_1)) {
Dave Chinner6a19d932011-03-07 10:02:35 +11002526 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
2527 "%s: Bad inode %Lu magic number 0x%x, ptr 0x%p",
2528 __func__, ip->i_ino, be16_to_cpu(dip->di_magic), dip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 goto corrupt_out;
2530 }
2531 if (XFS_TEST_ERROR(ip->i_d.di_magic != XFS_DINODE_MAGIC,
2532 mp, XFS_ERRTAG_IFLUSH_2, XFS_RANDOM_IFLUSH_2)) {
Dave Chinner6a19d932011-03-07 10:02:35 +11002533 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
2534 "%s: Bad inode %Lu, ptr 0x%p, magic number 0x%x",
2535 __func__, ip->i_ino, ip, ip->i_d.di_magic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 goto corrupt_out;
2537 }
Al Viroabbede12011-07-26 02:31:30 -04002538 if (S_ISREG(ip->i_d.di_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 if (XFS_TEST_ERROR(
2540 (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS) &&
2541 (ip->i_d.di_format != XFS_DINODE_FMT_BTREE),
2542 mp, XFS_ERRTAG_IFLUSH_3, XFS_RANDOM_IFLUSH_3)) {
Dave Chinner6a19d932011-03-07 10:02:35 +11002543 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
2544 "%s: Bad regular inode %Lu, ptr 0x%p",
2545 __func__, ip->i_ino, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 goto corrupt_out;
2547 }
Al Viroabbede12011-07-26 02:31:30 -04002548 } else if (S_ISDIR(ip->i_d.di_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 if (XFS_TEST_ERROR(
2550 (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS) &&
2551 (ip->i_d.di_format != XFS_DINODE_FMT_BTREE) &&
2552 (ip->i_d.di_format != XFS_DINODE_FMT_LOCAL),
2553 mp, XFS_ERRTAG_IFLUSH_4, XFS_RANDOM_IFLUSH_4)) {
Dave Chinner6a19d932011-03-07 10:02:35 +11002554 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
2555 "%s: Bad directory inode %Lu, ptr 0x%p",
2556 __func__, ip->i_ino, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 goto corrupt_out;
2558 }
2559 }
2560 if (XFS_TEST_ERROR(ip->i_d.di_nextents + ip->i_d.di_anextents >
2561 ip->i_d.di_nblocks, mp, XFS_ERRTAG_IFLUSH_5,
2562 XFS_RANDOM_IFLUSH_5)) {
Dave Chinner6a19d932011-03-07 10:02:35 +11002563 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
2564 "%s: detected corrupt incore inode %Lu, "
2565 "total extents = %d, nblocks = %Ld, ptr 0x%p",
2566 __func__, ip->i_ino,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 ip->i_d.di_nextents + ip->i_d.di_anextents,
Dave Chinner6a19d932011-03-07 10:02:35 +11002568 ip->i_d.di_nblocks, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 goto corrupt_out;
2570 }
2571 if (XFS_TEST_ERROR(ip->i_d.di_forkoff > mp->m_sb.sb_inodesize,
2572 mp, XFS_ERRTAG_IFLUSH_6, XFS_RANDOM_IFLUSH_6)) {
Dave Chinner6a19d932011-03-07 10:02:35 +11002573 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
2574 "%s: bad inode %Lu, forkoff 0x%x, ptr 0x%p",
2575 __func__, ip->i_ino, ip->i_d.di_forkoff, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 goto corrupt_out;
2577 }
2578 /*
2579 * bump the flush iteration count, used to detect flushes which
2580 * postdate a log record during recovery.
2581 */
2582
2583 ip->i_d.di_flushiter++;
2584
2585 /*
2586 * Copy the dirty parts of the inode into the on-disk
2587 * inode. We always copy out the core of the inode,
2588 * because if the inode is dirty at all the core must
2589 * be.
2590 */
Christoph Hellwig81591fe2008-11-28 14:23:39 +11002591 xfs_dinode_to_disk(dip, &ip->i_d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592
2593 /* Wrap, we never let the log put out DI_MAX_FLUSH */
2594 if (ip->i_d.di_flushiter == DI_MAX_FLUSH)
2595 ip->i_d.di_flushiter = 0;
2596
2597 /*
2598 * If this is really an old format inode and the superblock version
2599 * has not been updated to support only new format inodes, then
2600 * convert back to the old inode format. If the superblock version
2601 * has been updated, then make the conversion permanent.
2602 */
Christoph Hellwig51ce16d2008-11-28 14:23:39 +11002603 ASSERT(ip->i_d.di_version == 1 || xfs_sb_version_hasnlink(&mp->m_sb));
2604 if (ip->i_d.di_version == 1) {
Eric Sandeen62118702008-03-06 13:44:28 +11002605 if (!xfs_sb_version_hasnlink(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 /*
2607 * Convert it back.
2608 */
2609 ASSERT(ip->i_d.di_nlink <= XFS_MAXLINK_1);
Christoph Hellwig81591fe2008-11-28 14:23:39 +11002610 dip->di_onlink = cpu_to_be16(ip->i_d.di_nlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 } else {
2612 /*
2613 * The superblock version has already been bumped,
2614 * so just make the conversion to the new inode
2615 * format permanent.
2616 */
Christoph Hellwig51ce16d2008-11-28 14:23:39 +11002617 ip->i_d.di_version = 2;
2618 dip->di_version = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 ip->i_d.di_onlink = 0;
Christoph Hellwig81591fe2008-11-28 14:23:39 +11002620 dip->di_onlink = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 memset(&(ip->i_d.di_pad[0]), 0, sizeof(ip->i_d.di_pad));
Christoph Hellwig81591fe2008-11-28 14:23:39 +11002622 memset(&(dip->di_pad[0]), 0,
2623 sizeof(dip->di_pad));
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +00002624 ASSERT(xfs_get_projid(ip) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 }
2626 }
2627
David Chinnere4ac9672008-04-10 12:23:58 +10002628 xfs_iflush_fork(ip, dip, iip, XFS_DATA_FORK, bp);
2629 if (XFS_IFORK_Q(ip))
2630 xfs_iflush_fork(ip, dip, iip, XFS_ATTR_FORK, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 xfs_inobp_check(mp, bp);
2632
2633 /*
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +00002634 * We've recorded everything logged in the inode, so we'd like to clear
2635 * the ili_fields bits so we don't log and flush things unnecessarily.
2636 * However, we can't stop logging all this information until the data
2637 * we've copied into the disk buffer is written to disk. If we did we
2638 * might overwrite the copy of the inode in the log with all the data
2639 * after re-logging only part of it, and in the face of a crash we
2640 * wouldn't have all the data we need to recover.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 *
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +00002642 * What we do is move the bits to the ili_last_fields field. When
2643 * logging the inode, these bits are moved back to the ili_fields field.
2644 * In the xfs_iflush_done() routine we clear ili_last_fields, since we
2645 * know that the information those bits represent is permanently on
2646 * disk. As long as the flush completes before the inode is logged
2647 * again, then both ili_fields and ili_last_fields will be cleared.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 *
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +00002649 * We can play with the ili_fields bits here, because the inode lock
2650 * must be held exclusively in order to set bits there and the flush
2651 * lock protects the ili_last_fields bits. Set ili_logged so the flush
2652 * done routine can tell whether or not to look in the AIL. Also, store
2653 * the current LSN of the inode so that we can tell whether the item has
2654 * moved in the AIL from xfs_iflush_done(). In order to read the lsn we
2655 * need the AIL lock, because it is a 64 bit value that cannot be read
2656 * atomically.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 */
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +00002658 if (iip != NULL && iip->ili_fields != 0) {
2659 iip->ili_last_fields = iip->ili_fields;
2660 iip->ili_fields = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 iip->ili_logged = 1;
2662
David Chinner7b2e2a32008-10-30 17:39:12 +11002663 xfs_trans_ail_copy_lsn(mp->m_ail, &iip->ili_flush_lsn,
2664 &iip->ili_item.li_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665
2666 /*
2667 * Attach the function xfs_iflush_done to the inode's
2668 * buffer. This will remove the inode from the AIL
2669 * and unlock the inode's flush lock when the inode is
2670 * completely written to disk.
2671 */
Christoph Hellwigca30b2a2010-06-23 18:11:15 +10002672 xfs_buf_attach_iodone(bp, xfs_iflush_done, &iip->ili_item);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673
Christoph Hellwigadadbee2011-07-13 13:43:49 +02002674 ASSERT(bp->b_fspriv != NULL);
Christoph Hellwigcb669ca2011-07-13 13:43:49 +02002675 ASSERT(bp->b_iodone != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 } else {
2677 /*
2678 * We're flushing an inode which is not in the AIL and has
Christoph Hellwig8a9c9982012-02-29 09:53:52 +00002679 * not been logged. For this case we can immediately drop
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 * the inode flush lock because we can avoid the whole
2681 * AIL state thing. It's OK to drop the flush lock now,
2682 * because we've already locked the buffer and to do anything
2683 * you really need both.
2684 */
2685 if (iip != NULL) {
2686 ASSERT(iip->ili_logged == 0);
2687 ASSERT(iip->ili_last_fields == 0);
2688 ASSERT((iip->ili_item.li_flags & XFS_LI_IN_AIL) == 0);
2689 }
2690 xfs_ifunlock(ip);
2691 }
2692
2693 return 0;
2694
2695corrupt_out:
2696 return XFS_ERROR(EFSCORRUPTED);
2697}
2698
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002699/*
2700 * Return a pointer to the extent record at file index idx.
2701 */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10002702xfs_bmbt_rec_host_t *
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002703xfs_iext_get_ext(
2704 xfs_ifork_t *ifp, /* inode fork pointer */
2705 xfs_extnum_t idx) /* index of target extent */
2706{
2707 ASSERT(idx >= 0);
Christoph Hellwig87bef182011-05-11 15:04:11 +00002708 ASSERT(idx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t));
2709
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11002710 if ((ifp->if_flags & XFS_IFEXTIREC) && (idx == 0)) {
2711 return ifp->if_u1.if_ext_irec->er_extbuf;
2712 } else if (ifp->if_flags & XFS_IFEXTIREC) {
2713 xfs_ext_irec_t *erp; /* irec pointer */
2714 int erp_idx = 0; /* irec index */
2715 xfs_extnum_t page_idx = idx; /* ext index in target list */
2716
2717 erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 0);
2718 return &erp->er_extbuf[page_idx];
2719 } else if (ifp->if_bytes) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002720 return &ifp->if_u1.if_extents[idx];
2721 } else {
2722 return NULL;
2723 }
2724}
2725
2726/*
2727 * Insert new item(s) into the extent records for incore inode
2728 * fork 'ifp'. 'count' new items are inserted at index 'idx'.
2729 */
2730void
2731xfs_iext_insert(
Christoph Hellwig6ef35542009-11-25 00:00:21 +00002732 xfs_inode_t *ip, /* incore inode pointer */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002733 xfs_extnum_t idx, /* starting index of new items */
2734 xfs_extnum_t count, /* number of inserted items */
Christoph Hellwig6ef35542009-11-25 00:00:21 +00002735 xfs_bmbt_irec_t *new, /* items to insert */
2736 int state) /* type of extent conversion */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002737{
Christoph Hellwig6ef35542009-11-25 00:00:21 +00002738 xfs_ifork_t *ifp = (state & BMAP_ATTRFORK) ? ip->i_afp : &ip->i_df;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002739 xfs_extnum_t i; /* extent record index */
2740
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002741 trace_xfs_iext_insert(ip, idx, new, state, _RET_IP_);
2742
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002743 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
2744 xfs_iext_add(ifp, idx, count);
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10002745 for (i = idx; i < idx + count; i++, new++)
2746 xfs_bmbt_set_all(xfs_iext_get_ext(ifp, i), new);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002747}
2748
2749/*
2750 * This is called when the amount of space required for incore file
2751 * extents needs to be increased. The ext_diff parameter stores the
2752 * number of new extents being added and the idx parameter contains
2753 * the extent index where the new extents will be added. If the new
2754 * extents are being appended, then we just need to (re)allocate and
2755 * initialize the space. Otherwise, if the new extents are being
2756 * inserted into the middle of the existing entries, a bit more work
2757 * is required to make room for the new extents to be inserted. The
2758 * caller is responsible for filling in the new extent entries upon
2759 * return.
2760 */
2761void
2762xfs_iext_add(
2763 xfs_ifork_t *ifp, /* inode fork pointer */
2764 xfs_extnum_t idx, /* index to begin adding exts */
Nathan Scottc41564b2006-03-29 08:55:14 +10002765 int ext_diff) /* number of extents to add */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002766{
2767 int byte_diff; /* new bytes being added */
2768 int new_size; /* size of extents after adding */
2769 xfs_extnum_t nextents; /* number of extents in file */
2770
2771 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
2772 ASSERT((idx >= 0) && (idx <= nextents));
2773 byte_diff = ext_diff * sizeof(xfs_bmbt_rec_t);
2774 new_size = ifp->if_bytes + byte_diff;
2775 /*
2776 * If the new number of extents (nextents + ext_diff)
2777 * fits inside the inode, then continue to use the inline
2778 * extent buffer.
2779 */
2780 if (nextents + ext_diff <= XFS_INLINE_EXTS) {
2781 if (idx < nextents) {
2782 memmove(&ifp->if_u2.if_inline_ext[idx + ext_diff],
2783 &ifp->if_u2.if_inline_ext[idx],
2784 (nextents - idx) * sizeof(xfs_bmbt_rec_t));
2785 memset(&ifp->if_u2.if_inline_ext[idx], 0, byte_diff);
2786 }
2787 ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext;
2788 ifp->if_real_bytes = 0;
2789 }
2790 /*
2791 * Otherwise use a linear (direct) extent list.
2792 * If the extents are currently inside the inode,
2793 * xfs_iext_realloc_direct will switch us from
2794 * inline to direct extent allocation mode.
2795 */
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11002796 else if (nextents + ext_diff <= XFS_LINEAR_EXTS) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002797 xfs_iext_realloc_direct(ifp, new_size);
2798 if (idx < nextents) {
2799 memmove(&ifp->if_u1.if_extents[idx + ext_diff],
2800 &ifp->if_u1.if_extents[idx],
2801 (nextents - idx) * sizeof(xfs_bmbt_rec_t));
2802 memset(&ifp->if_u1.if_extents[idx], 0, byte_diff);
2803 }
2804 }
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11002805 /* Indirection array */
2806 else {
2807 xfs_ext_irec_t *erp;
2808 int erp_idx = 0;
2809 int page_idx = idx;
2810
2811 ASSERT(nextents + ext_diff > XFS_LINEAR_EXTS);
2812 if (ifp->if_flags & XFS_IFEXTIREC) {
2813 erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 1);
2814 } else {
2815 xfs_iext_irec_init(ifp);
2816 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
2817 erp = ifp->if_u1.if_ext_irec;
2818 }
2819 /* Extents fit in target extent page */
2820 if (erp && erp->er_extcount + ext_diff <= XFS_LINEAR_EXTS) {
2821 if (page_idx < erp->er_extcount) {
2822 memmove(&erp->er_extbuf[page_idx + ext_diff],
2823 &erp->er_extbuf[page_idx],
2824 (erp->er_extcount - page_idx) *
2825 sizeof(xfs_bmbt_rec_t));
2826 memset(&erp->er_extbuf[page_idx], 0, byte_diff);
2827 }
2828 erp->er_extcount += ext_diff;
2829 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff);
2830 }
2831 /* Insert a new extent page */
2832 else if (erp) {
2833 xfs_iext_add_indirect_multi(ifp,
2834 erp_idx, page_idx, ext_diff);
2835 }
2836 /*
2837 * If extent(s) are being appended to the last page in
2838 * the indirection array and the new extent(s) don't fit
2839 * in the page, then erp is NULL and erp_idx is set to
2840 * the next index needed in the indirection array.
2841 */
2842 else {
2843 int count = ext_diff;
2844
2845 while (count) {
2846 erp = xfs_iext_irec_new(ifp, erp_idx);
2847 erp->er_extcount = count;
2848 count -= MIN(count, (int)XFS_LINEAR_EXTS);
2849 if (count) {
2850 erp_idx++;
2851 }
2852 }
2853 }
2854 }
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002855 ifp->if_bytes = new_size;
2856}
2857
2858/*
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11002859 * This is called when incore extents are being added to the indirection
2860 * array and the new extents do not fit in the target extent list. The
2861 * erp_idx parameter contains the irec index for the target extent list
2862 * in the indirection array, and the idx parameter contains the extent
2863 * index within the list. The number of extents being added is stored
2864 * in the count parameter.
2865 *
2866 * |-------| |-------|
2867 * | | | | idx - number of extents before idx
2868 * | idx | | count |
2869 * | | | | count - number of extents being inserted at idx
2870 * |-------| |-------|
2871 * | count | | nex2 | nex2 - number of extents after idx + count
2872 * |-------| |-------|
2873 */
2874void
2875xfs_iext_add_indirect_multi(
2876 xfs_ifork_t *ifp, /* inode fork pointer */
2877 int erp_idx, /* target extent irec index */
2878 xfs_extnum_t idx, /* index within target list */
2879 int count) /* new extents being added */
2880{
2881 int byte_diff; /* new bytes being added */
2882 xfs_ext_irec_t *erp; /* pointer to irec entry */
2883 xfs_extnum_t ext_diff; /* number of extents to add */
2884 xfs_extnum_t ext_cnt; /* new extents still needed */
2885 xfs_extnum_t nex2; /* extents after idx + count */
2886 xfs_bmbt_rec_t *nex2_ep = NULL; /* temp list for nex2 extents */
2887 int nlists; /* number of irec's (lists) */
2888
2889 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
2890 erp = &ifp->if_u1.if_ext_irec[erp_idx];
2891 nex2 = erp->er_extcount - idx;
2892 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
2893
2894 /*
2895 * Save second part of target extent list
2896 * (all extents past */
2897 if (nex2) {
2898 byte_diff = nex2 * sizeof(xfs_bmbt_rec_t);
David Chinner67850732008-08-13 16:02:51 +10002899 nex2_ep = (xfs_bmbt_rec_t *) kmem_alloc(byte_diff, KM_NOFS);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11002900 memmove(nex2_ep, &erp->er_extbuf[idx], byte_diff);
2901 erp->er_extcount -= nex2;
2902 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, -nex2);
2903 memset(&erp->er_extbuf[idx], 0, byte_diff);
2904 }
2905
2906 /*
2907 * Add the new extents to the end of the target
2908 * list, then allocate new irec record(s) and
2909 * extent buffer(s) as needed to store the rest
2910 * of the new extents.
2911 */
2912 ext_cnt = count;
2913 ext_diff = MIN(ext_cnt, (int)XFS_LINEAR_EXTS - erp->er_extcount);
2914 if (ext_diff) {
2915 erp->er_extcount += ext_diff;
2916 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff);
2917 ext_cnt -= ext_diff;
2918 }
2919 while (ext_cnt) {
2920 erp_idx++;
2921 erp = xfs_iext_irec_new(ifp, erp_idx);
2922 ext_diff = MIN(ext_cnt, (int)XFS_LINEAR_EXTS);
2923 erp->er_extcount = ext_diff;
2924 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff);
2925 ext_cnt -= ext_diff;
2926 }
2927
2928 /* Add nex2 extents back to indirection array */
2929 if (nex2) {
2930 xfs_extnum_t ext_avail;
2931 int i;
2932
2933 byte_diff = nex2 * sizeof(xfs_bmbt_rec_t);
2934 ext_avail = XFS_LINEAR_EXTS - erp->er_extcount;
2935 i = 0;
2936 /*
2937 * If nex2 extents fit in the current page, append
2938 * nex2_ep after the new extents.
2939 */
2940 if (nex2 <= ext_avail) {
2941 i = erp->er_extcount;
2942 }
2943 /*
2944 * Otherwise, check if space is available in the
2945 * next page.
2946 */
2947 else if ((erp_idx < nlists - 1) &&
2948 (nex2 <= (ext_avail = XFS_LINEAR_EXTS -
2949 ifp->if_u1.if_ext_irec[erp_idx+1].er_extcount))) {
2950 erp_idx++;
2951 erp++;
2952 /* Create a hole for nex2 extents */
2953 memmove(&erp->er_extbuf[nex2], erp->er_extbuf,
2954 erp->er_extcount * sizeof(xfs_bmbt_rec_t));
2955 }
2956 /*
2957 * Final choice, create a new extent page for
2958 * nex2 extents.
2959 */
2960 else {
2961 erp_idx++;
2962 erp = xfs_iext_irec_new(ifp, erp_idx);
2963 }
2964 memmove(&erp->er_extbuf[i], nex2_ep, byte_diff);
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10002965 kmem_free(nex2_ep);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11002966 erp->er_extcount += nex2;
2967 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, nex2);
2968 }
2969}
2970
2971/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002972 * This is called when the amount of space required for incore file
2973 * extents needs to be decreased. The ext_diff parameter stores the
2974 * number of extents to be removed and the idx parameter contains
2975 * the extent index where the extents will be removed from.
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11002976 *
2977 * If the amount of space needed has decreased below the linear
2978 * limit, XFS_IEXT_BUFSZ, then switch to using the contiguous
2979 * extent array. Otherwise, use kmem_realloc() to adjust the
2980 * size to what is needed.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002981 */
2982void
2983xfs_iext_remove(
Christoph Hellwig6ef35542009-11-25 00:00:21 +00002984 xfs_inode_t *ip, /* incore inode pointer */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002985 xfs_extnum_t idx, /* index to begin removing exts */
Christoph Hellwig6ef35542009-11-25 00:00:21 +00002986 int ext_diff, /* number of extents to remove */
2987 int state) /* type of extent conversion */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002988{
Christoph Hellwig6ef35542009-11-25 00:00:21 +00002989 xfs_ifork_t *ifp = (state & BMAP_ATTRFORK) ? ip->i_afp : &ip->i_df;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002990 xfs_extnum_t nextents; /* number of extents in file */
2991 int new_size; /* size of extents after removal */
2992
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002993 trace_xfs_iext_remove(ip, idx, state, _RET_IP_);
2994
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002995 ASSERT(ext_diff > 0);
2996 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
2997 new_size = (nextents - ext_diff) * sizeof(xfs_bmbt_rec_t);
2998
2999 if (new_size == 0) {
3000 xfs_iext_destroy(ifp);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003001 } else if (ifp->if_flags & XFS_IFEXTIREC) {
3002 xfs_iext_remove_indirect(ifp, idx, ext_diff);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003003 } else if (ifp->if_real_bytes) {
3004 xfs_iext_remove_direct(ifp, idx, ext_diff);
3005 } else {
3006 xfs_iext_remove_inline(ifp, idx, ext_diff);
3007 }
3008 ifp->if_bytes = new_size;
3009}
3010
3011/*
3012 * This removes ext_diff extents from the inline buffer, beginning
3013 * at extent index idx.
3014 */
3015void
3016xfs_iext_remove_inline(
3017 xfs_ifork_t *ifp, /* inode fork pointer */
3018 xfs_extnum_t idx, /* index to begin removing exts */
3019 int ext_diff) /* number of extents to remove */
3020{
3021 int nextents; /* number of extents in file */
3022
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003023 ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003024 ASSERT(idx < XFS_INLINE_EXTS);
3025 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3026 ASSERT(((nextents - ext_diff) > 0) &&
3027 (nextents - ext_diff) < XFS_INLINE_EXTS);
3028
3029 if (idx + ext_diff < nextents) {
3030 memmove(&ifp->if_u2.if_inline_ext[idx],
3031 &ifp->if_u2.if_inline_ext[idx + ext_diff],
3032 (nextents - (idx + ext_diff)) *
3033 sizeof(xfs_bmbt_rec_t));
3034 memset(&ifp->if_u2.if_inline_ext[nextents - ext_diff],
3035 0, ext_diff * sizeof(xfs_bmbt_rec_t));
3036 } else {
3037 memset(&ifp->if_u2.if_inline_ext[idx], 0,
3038 ext_diff * sizeof(xfs_bmbt_rec_t));
3039 }
3040}
3041
3042/*
3043 * This removes ext_diff extents from a linear (direct) extent list,
3044 * beginning at extent index idx. If the extents are being removed
3045 * from the end of the list (ie. truncate) then we just need to re-
3046 * allocate the list to remove the extra space. Otherwise, if the
3047 * extents are being removed from the middle of the existing extent
3048 * entries, then we first need to move the extent records beginning
3049 * at idx + ext_diff up in the list to overwrite the records being
3050 * removed, then remove the extra space via kmem_realloc.
3051 */
3052void
3053xfs_iext_remove_direct(
3054 xfs_ifork_t *ifp, /* inode fork pointer */
3055 xfs_extnum_t idx, /* index to begin removing exts */
3056 int ext_diff) /* number of extents to remove */
3057{
3058 xfs_extnum_t nextents; /* number of extents in file */
3059 int new_size; /* size of extents after removal */
3060
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003061 ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003062 new_size = ifp->if_bytes -
3063 (ext_diff * sizeof(xfs_bmbt_rec_t));
3064 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3065
3066 if (new_size == 0) {
3067 xfs_iext_destroy(ifp);
3068 return;
3069 }
3070 /* Move extents up in the list (if needed) */
3071 if (idx + ext_diff < nextents) {
3072 memmove(&ifp->if_u1.if_extents[idx],
3073 &ifp->if_u1.if_extents[idx + ext_diff],
3074 (nextents - (idx + ext_diff)) *
3075 sizeof(xfs_bmbt_rec_t));
3076 }
3077 memset(&ifp->if_u1.if_extents[nextents - ext_diff],
3078 0, ext_diff * sizeof(xfs_bmbt_rec_t));
3079 /*
3080 * Reallocate the direct extent list. If the extents
3081 * will fit inside the inode then xfs_iext_realloc_direct
3082 * will switch from direct to inline extent allocation
3083 * mode for us.
3084 */
3085 xfs_iext_realloc_direct(ifp, new_size);
3086 ifp->if_bytes = new_size;
3087}
3088
3089/*
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003090 * This is called when incore extents are being removed from the
3091 * indirection array and the extents being removed span multiple extent
3092 * buffers. The idx parameter contains the file extent index where we
3093 * want to begin removing extents, and the count parameter contains
3094 * how many extents need to be removed.
3095 *
3096 * |-------| |-------|
3097 * | nex1 | | | nex1 - number of extents before idx
3098 * |-------| | count |
3099 * | | | | count - number of extents being removed at idx
3100 * | count | |-------|
3101 * | | | nex2 | nex2 - number of extents after idx + count
3102 * |-------| |-------|
3103 */
3104void
3105xfs_iext_remove_indirect(
3106 xfs_ifork_t *ifp, /* inode fork pointer */
3107 xfs_extnum_t idx, /* index to begin removing extents */
3108 int count) /* number of extents to remove */
3109{
3110 xfs_ext_irec_t *erp; /* indirection array pointer */
3111 int erp_idx = 0; /* indirection array index */
3112 xfs_extnum_t ext_cnt; /* extents left to remove */
3113 xfs_extnum_t ext_diff; /* extents to remove in current list */
3114 xfs_extnum_t nex1; /* number of extents before idx */
3115 xfs_extnum_t nex2; /* extents after idx + count */
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003116 int page_idx = idx; /* index in target extent list */
3117
3118 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3119 erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 0);
3120 ASSERT(erp != NULL);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003121 nex1 = page_idx;
3122 ext_cnt = count;
3123 while (ext_cnt) {
3124 nex2 = MAX((erp->er_extcount - (nex1 + ext_cnt)), 0);
3125 ext_diff = MIN(ext_cnt, (erp->er_extcount - nex1));
3126 /*
3127 * Check for deletion of entire list;
3128 * xfs_iext_irec_remove() updates extent offsets.
3129 */
3130 if (ext_diff == erp->er_extcount) {
3131 xfs_iext_irec_remove(ifp, erp_idx);
3132 ext_cnt -= ext_diff;
3133 nex1 = 0;
3134 if (ext_cnt) {
3135 ASSERT(erp_idx < ifp->if_real_bytes /
3136 XFS_IEXT_BUFSZ);
3137 erp = &ifp->if_u1.if_ext_irec[erp_idx];
3138 nex1 = 0;
3139 continue;
3140 } else {
3141 break;
3142 }
3143 }
3144 /* Move extents up (if needed) */
3145 if (nex2) {
3146 memmove(&erp->er_extbuf[nex1],
3147 &erp->er_extbuf[nex1 + ext_diff],
3148 nex2 * sizeof(xfs_bmbt_rec_t));
3149 }
3150 /* Zero out rest of page */
3151 memset(&erp->er_extbuf[nex1 + nex2], 0, (XFS_IEXT_BUFSZ -
3152 ((nex1 + nex2) * sizeof(xfs_bmbt_rec_t))));
3153 /* Update remaining counters */
3154 erp->er_extcount -= ext_diff;
3155 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, -ext_diff);
3156 ext_cnt -= ext_diff;
3157 nex1 = 0;
3158 erp_idx++;
3159 erp++;
3160 }
3161 ifp->if_bytes -= count * sizeof(xfs_bmbt_rec_t);
3162 xfs_iext_irec_compact(ifp);
3163}
3164
3165/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003166 * Create, destroy, or resize a linear (direct) block of extents.
3167 */
3168void
3169xfs_iext_realloc_direct(
3170 xfs_ifork_t *ifp, /* inode fork pointer */
3171 int new_size) /* new size of extents */
3172{
3173 int rnew_size; /* real new size of extents */
3174
3175 rnew_size = new_size;
3176
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003177 ASSERT(!(ifp->if_flags & XFS_IFEXTIREC) ||
3178 ((new_size >= 0) && (new_size <= XFS_IEXT_BUFSZ) &&
3179 (new_size != ifp->if_real_bytes)));
3180
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003181 /* Free extent records */
3182 if (new_size == 0) {
3183 xfs_iext_destroy(ifp);
3184 }
3185 /* Resize direct extent list and zero any new bytes */
3186 else if (ifp->if_real_bytes) {
3187 /* Check if extents will fit inside the inode */
3188 if (new_size <= XFS_INLINE_EXTS * sizeof(xfs_bmbt_rec_t)) {
3189 xfs_iext_direct_to_inline(ifp, new_size /
3190 (uint)sizeof(xfs_bmbt_rec_t));
3191 ifp->if_bytes = new_size;
3192 return;
3193 }
Vignesh Babu16a087d2007-06-28 16:46:37 +10003194 if (!is_power_of_2(new_size)){
Robert P. J. Day40ebd812007-11-23 16:30:51 +11003195 rnew_size = roundup_pow_of_two(new_size);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003196 }
3197 if (rnew_size != ifp->if_real_bytes) {
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003198 ifp->if_u1.if_extents =
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003199 kmem_realloc(ifp->if_u1.if_extents,
3200 rnew_size,
David Chinner67850732008-08-13 16:02:51 +10003201 ifp->if_real_bytes, KM_NOFS);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003202 }
3203 if (rnew_size > ifp->if_real_bytes) {
3204 memset(&ifp->if_u1.if_extents[ifp->if_bytes /
3205 (uint)sizeof(xfs_bmbt_rec_t)], 0,
3206 rnew_size - ifp->if_real_bytes);
3207 }
3208 }
3209 /*
3210 * Switch from the inline extent buffer to a direct
3211 * extent list. Be sure to include the inline extent
3212 * bytes in new_size.
3213 */
3214 else {
3215 new_size += ifp->if_bytes;
Vignesh Babu16a087d2007-06-28 16:46:37 +10003216 if (!is_power_of_2(new_size)) {
Robert P. J. Day40ebd812007-11-23 16:30:51 +11003217 rnew_size = roundup_pow_of_two(new_size);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003218 }
3219 xfs_iext_inline_to_direct(ifp, rnew_size);
3220 }
3221 ifp->if_real_bytes = rnew_size;
3222 ifp->if_bytes = new_size;
3223}
3224
3225/*
3226 * Switch from linear (direct) extent records to inline buffer.
3227 */
3228void
3229xfs_iext_direct_to_inline(
3230 xfs_ifork_t *ifp, /* inode fork pointer */
3231 xfs_extnum_t nextents) /* number of extents in file */
3232{
3233 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
3234 ASSERT(nextents <= XFS_INLINE_EXTS);
3235 /*
3236 * The inline buffer was zeroed when we switched
3237 * from inline to direct extent allocation mode,
3238 * so we don't need to clear it here.
3239 */
3240 memcpy(ifp->if_u2.if_inline_ext, ifp->if_u1.if_extents,
3241 nextents * sizeof(xfs_bmbt_rec_t));
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10003242 kmem_free(ifp->if_u1.if_extents);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003243 ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext;
3244 ifp->if_real_bytes = 0;
3245}
3246
3247/*
3248 * Switch from inline buffer to linear (direct) extent records.
3249 * new_size should already be rounded up to the next power of 2
3250 * by the caller (when appropriate), so use new_size as it is.
3251 * However, since new_size may be rounded up, we can't update
3252 * if_bytes here. It is the caller's responsibility to update
3253 * if_bytes upon return.
3254 */
3255void
3256xfs_iext_inline_to_direct(
3257 xfs_ifork_t *ifp, /* inode fork pointer */
3258 int new_size) /* number of extents in file */
3259{
David Chinner67850732008-08-13 16:02:51 +10003260 ifp->if_u1.if_extents = kmem_alloc(new_size, KM_NOFS);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003261 memset(ifp->if_u1.if_extents, 0, new_size);
3262 if (ifp->if_bytes) {
3263 memcpy(ifp->if_u1.if_extents, ifp->if_u2.if_inline_ext,
3264 ifp->if_bytes);
3265 memset(ifp->if_u2.if_inline_ext, 0, XFS_INLINE_EXTS *
3266 sizeof(xfs_bmbt_rec_t));
3267 }
3268 ifp->if_real_bytes = new_size;
3269}
3270
3271/*
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003272 * Resize an extent indirection array to new_size bytes.
3273 */
Eric Sandeend96f8f82009-07-02 00:09:33 -05003274STATIC void
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003275xfs_iext_realloc_indirect(
3276 xfs_ifork_t *ifp, /* inode fork pointer */
3277 int new_size) /* new indirection array size */
3278{
3279 int nlists; /* number of irec's (ex lists) */
3280 int size; /* current indirection array size */
3281
3282 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3283 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3284 size = nlists * sizeof(xfs_ext_irec_t);
3285 ASSERT(ifp->if_real_bytes);
3286 ASSERT((new_size >= 0) && (new_size != size));
3287 if (new_size == 0) {
3288 xfs_iext_destroy(ifp);
3289 } else {
3290 ifp->if_u1.if_ext_irec = (xfs_ext_irec_t *)
3291 kmem_realloc(ifp->if_u1.if_ext_irec,
David Chinner67850732008-08-13 16:02:51 +10003292 new_size, size, KM_NOFS);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003293 }
3294}
3295
3296/*
3297 * Switch from indirection array to linear (direct) extent allocations.
3298 */
Eric Sandeend96f8f82009-07-02 00:09:33 -05003299STATIC void
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003300xfs_iext_indirect_to_direct(
3301 xfs_ifork_t *ifp) /* inode fork pointer */
3302{
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003303 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003304 xfs_extnum_t nextents; /* number of extents in file */
3305 int size; /* size of file extents */
3306
3307 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3308 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3309 ASSERT(nextents <= XFS_LINEAR_EXTS);
3310 size = nextents * sizeof(xfs_bmbt_rec_t);
3311
Lachlan McIlroy71a8c872008-09-26 12:17:57 +10003312 xfs_iext_irec_compact_pages(ifp);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003313 ASSERT(ifp->if_real_bytes == XFS_IEXT_BUFSZ);
3314
3315 ep = ifp->if_u1.if_ext_irec->er_extbuf;
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10003316 kmem_free(ifp->if_u1.if_ext_irec);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003317 ifp->if_flags &= ~XFS_IFEXTIREC;
3318 ifp->if_u1.if_extents = ep;
3319 ifp->if_bytes = size;
3320 if (nextents < XFS_LINEAR_EXTS) {
3321 xfs_iext_realloc_direct(ifp, size);
3322 }
3323}
3324
3325/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003326 * Free incore file extents.
3327 */
3328void
3329xfs_iext_destroy(
3330 xfs_ifork_t *ifp) /* inode fork pointer */
3331{
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003332 if (ifp->if_flags & XFS_IFEXTIREC) {
3333 int erp_idx;
3334 int nlists;
3335
3336 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3337 for (erp_idx = nlists - 1; erp_idx >= 0 ; erp_idx--) {
3338 xfs_iext_irec_remove(ifp, erp_idx);
3339 }
3340 ifp->if_flags &= ~XFS_IFEXTIREC;
3341 } else if (ifp->if_real_bytes) {
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10003342 kmem_free(ifp->if_u1.if_extents);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003343 } else if (ifp->if_bytes) {
3344 memset(ifp->if_u2.if_inline_ext, 0, XFS_INLINE_EXTS *
3345 sizeof(xfs_bmbt_rec_t));
3346 }
3347 ifp->if_u1.if_extents = NULL;
3348 ifp->if_real_bytes = 0;
3349 ifp->if_bytes = 0;
3350}
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003351
3352/*
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003353 * Return a pointer to the extent record for file system block bno.
3354 */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003355xfs_bmbt_rec_host_t * /* pointer to found extent record */
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003356xfs_iext_bno_to_ext(
3357 xfs_ifork_t *ifp, /* inode fork pointer */
3358 xfs_fileoff_t bno, /* block number to search for */
3359 xfs_extnum_t *idxp) /* index of target extent */
3360{
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003361 xfs_bmbt_rec_host_t *base; /* pointer to first extent */
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003362 xfs_filblks_t blockcount = 0; /* number of blocks in extent */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003363 xfs_bmbt_rec_host_t *ep = NULL; /* pointer to target extent */
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003364 xfs_ext_irec_t *erp = NULL; /* indirection array pointer */
Nathan Scottc41564b2006-03-29 08:55:14 +10003365 int high; /* upper boundary in search */
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003366 xfs_extnum_t idx = 0; /* index of target extent */
Nathan Scottc41564b2006-03-29 08:55:14 +10003367 int low; /* lower boundary in search */
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003368 xfs_extnum_t nextents; /* number of file extents */
3369 xfs_fileoff_t startoff = 0; /* start offset of extent */
3370
3371 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3372 if (nextents == 0) {
3373 *idxp = 0;
3374 return NULL;
3375 }
3376 low = 0;
3377 if (ifp->if_flags & XFS_IFEXTIREC) {
3378 /* Find target extent list */
3379 int erp_idx = 0;
3380 erp = xfs_iext_bno_to_irec(ifp, bno, &erp_idx);
3381 base = erp->er_extbuf;
3382 high = erp->er_extcount - 1;
3383 } else {
3384 base = ifp->if_u1.if_extents;
3385 high = nextents - 1;
3386 }
3387 /* Binary search extent records */
3388 while (low <= high) {
3389 idx = (low + high) >> 1;
3390 ep = base + idx;
3391 startoff = xfs_bmbt_get_startoff(ep);
3392 blockcount = xfs_bmbt_get_blockcount(ep);
3393 if (bno < startoff) {
3394 high = idx - 1;
3395 } else if (bno >= startoff + blockcount) {
3396 low = idx + 1;
3397 } else {
3398 /* Convert back to file-based extent index */
3399 if (ifp->if_flags & XFS_IFEXTIREC) {
3400 idx += erp->er_extoff;
3401 }
3402 *idxp = idx;
3403 return ep;
3404 }
3405 }
3406 /* Convert back to file-based extent index */
3407 if (ifp->if_flags & XFS_IFEXTIREC) {
3408 idx += erp->er_extoff;
3409 }
3410 if (bno >= startoff + blockcount) {
3411 if (++idx == nextents) {
3412 ep = NULL;
3413 } else {
3414 ep = xfs_iext_get_ext(ifp, idx);
3415 }
3416 }
3417 *idxp = idx;
3418 return ep;
3419}
3420
3421/*
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003422 * Return a pointer to the indirection array entry containing the
3423 * extent record for filesystem block bno. Store the index of the
3424 * target irec in *erp_idxp.
3425 */
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003426xfs_ext_irec_t * /* pointer to found extent record */
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003427xfs_iext_bno_to_irec(
3428 xfs_ifork_t *ifp, /* inode fork pointer */
3429 xfs_fileoff_t bno, /* block number to search for */
3430 int *erp_idxp) /* irec index of target ext list */
3431{
3432 xfs_ext_irec_t *erp = NULL; /* indirection array pointer */
3433 xfs_ext_irec_t *erp_next; /* next indirection array entry */
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003434 int erp_idx; /* indirection array index */
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003435 int nlists; /* number of extent irec's (lists) */
3436 int high; /* binary search upper limit */
3437 int low; /* binary search lower limit */
3438
3439 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3440 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3441 erp_idx = 0;
3442 low = 0;
3443 high = nlists - 1;
3444 while (low <= high) {
3445 erp_idx = (low + high) >> 1;
3446 erp = &ifp->if_u1.if_ext_irec[erp_idx];
3447 erp_next = erp_idx < nlists - 1 ? erp + 1 : NULL;
3448 if (bno < xfs_bmbt_get_startoff(erp->er_extbuf)) {
3449 high = erp_idx - 1;
3450 } else if (erp_next && bno >=
3451 xfs_bmbt_get_startoff(erp_next->er_extbuf)) {
3452 low = erp_idx + 1;
3453 } else {
3454 break;
3455 }
3456 }
3457 *erp_idxp = erp_idx;
3458 return erp;
3459}
3460
3461/*
3462 * Return a pointer to the indirection array entry containing the
3463 * extent record at file extent index *idxp. Store the index of the
3464 * target irec in *erp_idxp and store the page index of the target
3465 * extent record in *idxp.
3466 */
3467xfs_ext_irec_t *
3468xfs_iext_idx_to_irec(
3469 xfs_ifork_t *ifp, /* inode fork pointer */
3470 xfs_extnum_t *idxp, /* extent index (file -> page) */
3471 int *erp_idxp, /* pointer to target irec */
3472 int realloc) /* new bytes were just added */
3473{
3474 xfs_ext_irec_t *prev; /* pointer to previous irec */
3475 xfs_ext_irec_t *erp = NULL; /* pointer to current irec */
3476 int erp_idx; /* indirection array index */
3477 int nlists; /* number of irec's (ex lists) */
3478 int high; /* binary search upper limit */
3479 int low; /* binary search lower limit */
3480 xfs_extnum_t page_idx = *idxp; /* extent index in target list */
3481
3482 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
Christoph Hellwig87bef182011-05-11 15:04:11 +00003483 ASSERT(page_idx >= 0);
3484 ASSERT(page_idx <= ifp->if_bytes / sizeof(xfs_bmbt_rec_t));
3485 ASSERT(page_idx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t) || realloc);
3486
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003487 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3488 erp_idx = 0;
3489 low = 0;
3490 high = nlists - 1;
3491
3492 /* Binary search extent irec's */
3493 while (low <= high) {
3494 erp_idx = (low + high) >> 1;
3495 erp = &ifp->if_u1.if_ext_irec[erp_idx];
3496 prev = erp_idx > 0 ? erp - 1 : NULL;
3497 if (page_idx < erp->er_extoff || (page_idx == erp->er_extoff &&
3498 realloc && prev && prev->er_extcount < XFS_LINEAR_EXTS)) {
3499 high = erp_idx - 1;
3500 } else if (page_idx > erp->er_extoff + erp->er_extcount ||
3501 (page_idx == erp->er_extoff + erp->er_extcount &&
3502 !realloc)) {
3503 low = erp_idx + 1;
3504 } else if (page_idx == erp->er_extoff + erp->er_extcount &&
3505 erp->er_extcount == XFS_LINEAR_EXTS) {
3506 ASSERT(realloc);
3507 page_idx = 0;
3508 erp_idx++;
3509 erp = erp_idx < nlists ? erp + 1 : NULL;
3510 break;
3511 } else {
3512 page_idx -= erp->er_extoff;
3513 break;
3514 }
3515 }
3516 *idxp = page_idx;
3517 *erp_idxp = erp_idx;
3518 return(erp);
3519}
3520
3521/*
3522 * Allocate and initialize an indirection array once the space needed
3523 * for incore extents increases above XFS_IEXT_BUFSZ.
3524 */
3525void
3526xfs_iext_irec_init(
3527 xfs_ifork_t *ifp) /* inode fork pointer */
3528{
3529 xfs_ext_irec_t *erp; /* indirection array pointer */
3530 xfs_extnum_t nextents; /* number of extents in file */
3531
3532 ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
3533 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3534 ASSERT(nextents <= XFS_LINEAR_EXTS);
3535
David Chinner67850732008-08-13 16:02:51 +10003536 erp = kmem_alloc(sizeof(xfs_ext_irec_t), KM_NOFS);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003537
3538 if (nextents == 0) {
David Chinner67850732008-08-13 16:02:51 +10003539 ifp->if_u1.if_extents = kmem_alloc(XFS_IEXT_BUFSZ, KM_NOFS);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003540 } else if (!ifp->if_real_bytes) {
3541 xfs_iext_inline_to_direct(ifp, XFS_IEXT_BUFSZ);
3542 } else if (ifp->if_real_bytes < XFS_IEXT_BUFSZ) {
3543 xfs_iext_realloc_direct(ifp, XFS_IEXT_BUFSZ);
3544 }
3545 erp->er_extbuf = ifp->if_u1.if_extents;
3546 erp->er_extcount = nextents;
3547 erp->er_extoff = 0;
3548
3549 ifp->if_flags |= XFS_IFEXTIREC;
3550 ifp->if_real_bytes = XFS_IEXT_BUFSZ;
3551 ifp->if_bytes = nextents * sizeof(xfs_bmbt_rec_t);
3552 ifp->if_u1.if_ext_irec = erp;
3553
3554 return;
3555}
3556
3557/*
3558 * Allocate and initialize a new entry in the indirection array.
3559 */
3560xfs_ext_irec_t *
3561xfs_iext_irec_new(
3562 xfs_ifork_t *ifp, /* inode fork pointer */
3563 int erp_idx) /* index for new irec */
3564{
3565 xfs_ext_irec_t *erp; /* indirection array pointer */
3566 int i; /* loop counter */
3567 int nlists; /* number of irec's (ex lists) */
3568
3569 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3570 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3571
3572 /* Resize indirection array */
3573 xfs_iext_realloc_indirect(ifp, ++nlists *
3574 sizeof(xfs_ext_irec_t));
3575 /*
3576 * Move records down in the array so the
3577 * new page can use erp_idx.
3578 */
3579 erp = ifp->if_u1.if_ext_irec;
3580 for (i = nlists - 1; i > erp_idx; i--) {
3581 memmove(&erp[i], &erp[i-1], sizeof(xfs_ext_irec_t));
3582 }
3583 ASSERT(i == erp_idx);
3584
3585 /* Initialize new extent record */
3586 erp = ifp->if_u1.if_ext_irec;
David Chinner67850732008-08-13 16:02:51 +10003587 erp[erp_idx].er_extbuf = kmem_alloc(XFS_IEXT_BUFSZ, KM_NOFS);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003588 ifp->if_real_bytes = nlists * XFS_IEXT_BUFSZ;
3589 memset(erp[erp_idx].er_extbuf, 0, XFS_IEXT_BUFSZ);
3590 erp[erp_idx].er_extcount = 0;
3591 erp[erp_idx].er_extoff = erp_idx > 0 ?
3592 erp[erp_idx-1].er_extoff + erp[erp_idx-1].er_extcount : 0;
3593 return (&erp[erp_idx]);
3594}
3595
3596/*
3597 * Remove a record from the indirection array.
3598 */
3599void
3600xfs_iext_irec_remove(
3601 xfs_ifork_t *ifp, /* inode fork pointer */
3602 int erp_idx) /* irec index to remove */
3603{
3604 xfs_ext_irec_t *erp; /* indirection array pointer */
3605 int i; /* loop counter */
3606 int nlists; /* number of irec's (ex lists) */
3607
3608 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3609 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3610 erp = &ifp->if_u1.if_ext_irec[erp_idx];
3611 if (erp->er_extbuf) {
3612 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1,
3613 -erp->er_extcount);
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10003614 kmem_free(erp->er_extbuf);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003615 }
3616 /* Compact extent records */
3617 erp = ifp->if_u1.if_ext_irec;
3618 for (i = erp_idx; i < nlists - 1; i++) {
3619 memmove(&erp[i], &erp[i+1], sizeof(xfs_ext_irec_t));
3620 }
3621 /*
3622 * Manually free the last extent record from the indirection
3623 * array. A call to xfs_iext_realloc_indirect() with a size
3624 * of zero would result in a call to xfs_iext_destroy() which
3625 * would in turn call this function again, creating a nasty
3626 * infinite loop.
3627 */
3628 if (--nlists) {
3629 xfs_iext_realloc_indirect(ifp,
3630 nlists * sizeof(xfs_ext_irec_t));
3631 } else {
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10003632 kmem_free(ifp->if_u1.if_ext_irec);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003633 }
3634 ifp->if_real_bytes = nlists * XFS_IEXT_BUFSZ;
3635}
3636
3637/*
3638 * This is called to clean up large amounts of unused memory allocated
3639 * by the indirection array. Before compacting anything though, verify
3640 * that the indirection array is still needed and switch back to the
3641 * linear extent list (or even the inline buffer) if possible. The
3642 * compaction policy is as follows:
3643 *
3644 * Full Compaction: Extents fit into a single page (or inline buffer)
Lachlan McIlroy71a8c872008-09-26 12:17:57 +10003645 * Partial Compaction: Extents occupy less than 50% of allocated space
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003646 * No Compaction: Extents occupy at least 50% of allocated space
3647 */
3648void
3649xfs_iext_irec_compact(
3650 xfs_ifork_t *ifp) /* inode fork pointer */
3651{
3652 xfs_extnum_t nextents; /* number of extents in file */
3653 int nlists; /* number of irec's (ex lists) */
3654
3655 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3656 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3657 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3658
3659 if (nextents == 0) {
3660 xfs_iext_destroy(ifp);
3661 } else if (nextents <= XFS_INLINE_EXTS) {
3662 xfs_iext_indirect_to_direct(ifp);
3663 xfs_iext_direct_to_inline(ifp, nextents);
3664 } else if (nextents <= XFS_LINEAR_EXTS) {
3665 xfs_iext_indirect_to_direct(ifp);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003666 } else if (nextents < (nlists * XFS_LINEAR_EXTS) >> 1) {
3667 xfs_iext_irec_compact_pages(ifp);
3668 }
3669}
3670
3671/*
3672 * Combine extents from neighboring extent pages.
3673 */
3674void
3675xfs_iext_irec_compact_pages(
3676 xfs_ifork_t *ifp) /* inode fork pointer */
3677{
3678 xfs_ext_irec_t *erp, *erp_next;/* pointers to irec entries */
3679 int erp_idx = 0; /* indirection array index */
3680 int nlists; /* number of irec's (ex lists) */
3681
3682 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3683 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3684 while (erp_idx < nlists - 1) {
3685 erp = &ifp->if_u1.if_ext_irec[erp_idx];
3686 erp_next = erp + 1;
3687 if (erp_next->er_extcount <=
3688 (XFS_LINEAR_EXTS - erp->er_extcount)) {
Lachlan McIlroy71a8c872008-09-26 12:17:57 +10003689 memcpy(&erp->er_extbuf[erp->er_extcount],
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003690 erp_next->er_extbuf, erp_next->er_extcount *
3691 sizeof(xfs_bmbt_rec_t));
3692 erp->er_extcount += erp_next->er_extcount;
3693 /*
3694 * Free page before removing extent record
3695 * so er_extoffs don't get modified in
3696 * xfs_iext_irec_remove.
3697 */
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10003698 kmem_free(erp_next->er_extbuf);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003699 erp_next->er_extbuf = NULL;
3700 xfs_iext_irec_remove(ifp, erp_idx + 1);
3701 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3702 } else {
3703 erp_idx++;
3704 }
3705 }
3706}
3707
3708/*
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003709 * This is called to update the er_extoff field in the indirection
3710 * array when extents have been added or removed from one of the
3711 * extent lists. erp_idx contains the irec index to begin updating
3712 * at and ext_diff contains the number of extents that were added
3713 * or removed.
3714 */
3715void
3716xfs_iext_irec_update_extoffs(
3717 xfs_ifork_t *ifp, /* inode fork pointer */
3718 int erp_idx, /* irec index to update */
3719 int ext_diff) /* number of new extents */
3720{
3721 int i; /* loop counter */
3722 int nlists; /* number of irec's (ex lists */
3723
3724 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3725 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3726 for (i = erp_idx; i < nlists; i++) {
3727 ifp->if_u1.if_ext_irec[i].er_extoff += ext_diff;
3728 }
3729}