blob: 34c985de5fa0c3ced0f9dfb9e567dd612d1e20e5 [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/*
Christoph Hellwig475ee412012-07-03 12:21:22 -0400135 * This routine is called to map an inode to the buffer containing the on-disk
136 * version of the inode. It returns a pointer to the buffer containing the
137 * on-disk inode in the bpp parameter, and in the dipp parameter it returns a
138 * pointer to the on-disk inode within that buffer.
139 *
140 * If a non-zero error is returned, then the contents of bpp and dipp are
141 * undefined.
David Chinner4ae29b42008-03-06 13:43:34 +1100142 */
Christoph Hellwig475ee412012-07-03 12:21:22 -0400143int
David Chinner4ae29b42008-03-06 13:43:34 +1100144xfs_imap_to_bp(
Christoph Hellwig475ee412012-07-03 12:21:22 -0400145 struct xfs_mount *mp,
146 struct xfs_trans *tp,
147 struct xfs_imap *imap,
148 struct xfs_dinode **dipp,
149 struct xfs_buf **bpp,
150 uint buf_flags,
151 uint iget_flags)
David Chinner4ae29b42008-03-06 13:43:34 +1100152{
Christoph Hellwig475ee412012-07-03 12:21:22 -0400153 struct xfs_buf *bp;
154 int error;
155 int i;
156 int ni;
David Chinner4ae29b42008-03-06 13:43:34 +1100157
Dave Chinner611c9942012-04-23 15:59:07 +1000158 buf_flags |= XBF_UNMAPPED;
David Chinner4ae29b42008-03-06 13:43:34 +1100159 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, imap->im_blkno,
David Chinnera3f74ff2008-03-06 13:43:42 +1100160 (int)imap->im_len, buf_flags, &bp);
David Chinner4ae29b42008-03-06 13:43:34 +1100161 if (error) {
David Chinnera3f74ff2008-03-06 13:43:42 +1100162 if (error != EAGAIN) {
Dave Chinner0b932cc2011-03-07 10:08:35 +1100163 xfs_warn(mp,
164 "%s: xfs_trans_read_buf() returned error %d.",
165 __func__, error);
David Chinnera3f74ff2008-03-06 13:43:42 +1100166 } else {
Christoph Hellwig0cadda12010-01-19 09:56:44 +0000167 ASSERT(buf_flags & XBF_TRYLOCK);
David Chinnera3f74ff2008-03-06 13:43:42 +1100168 }
David Chinner4ae29b42008-03-06 13:43:34 +1100169 return error;
170 }
171
172 /*
173 * Validate the magic number and version of every inode in the buffer
174 * (if DEBUG kernel) or the first inode in the buffer, otherwise.
175 */
176#ifdef DEBUG
177 ni = BBTOB(imap->im_len) >> mp->m_sb.sb_inodelog;
178#else /* usual case */
179 ni = 1;
180#endif
181
182 for (i = 0; i < ni; i++) {
183 int di_ok;
184 xfs_dinode_t *dip;
185
186 dip = (xfs_dinode_t *)xfs_buf_offset(bp,
187 (i << mp->m_sb.sb_inodelog));
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200188 di_ok = dip->di_magic == cpu_to_be16(XFS_DINODE_MAGIC) &&
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100189 XFS_DINODE_GOOD_VERSION(dip->di_version);
David Chinner4ae29b42008-03-06 13:43:34 +1100190 if (unlikely(XFS_TEST_ERROR(!di_ok, mp,
191 XFS_ERRTAG_ITOBP_INOTOBP,
192 XFS_RANDOM_ITOBP_INOTOBP))) {
Dave Chinner19207792010-06-24 11:15:47 +1000193 if (iget_flags & XFS_IGET_UNTRUSTED) {
David Chinner4ae29b42008-03-06 13:43:34 +1100194 xfs_trans_brelse(tp, bp);
195 return XFS_ERROR(EINVAL);
196 }
Christoph Hellwig475ee412012-07-03 12:21:22 -0400197 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_HIGH,
198 mp, dip);
David Chinner4ae29b42008-03-06 13:43:34 +1100199#ifdef DEBUG
Dave Chinner0b932cc2011-03-07 10:08:35 +1100200 xfs_emerg(mp,
201 "bad inode magic/vsn daddr %lld #%d (magic=%x)",
David Chinner4ae29b42008-03-06 13:43:34 +1100202 (unsigned long long)imap->im_blkno, i,
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100203 be16_to_cpu(dip->di_magic));
Dave Chinner0b932cc2011-03-07 10:08:35 +1100204 ASSERT(0);
David Chinner4ae29b42008-03-06 13:43:34 +1100205#endif
206 xfs_trans_brelse(tp, bp);
207 return XFS_ERROR(EFSCORRUPTED);
208 }
209 }
210
211 xfs_inobp_check(mp, bp);
Christoph Hellwig475ee412012-07-03 12:21:22 -0400212
David Chinner4ae29b42008-03-06 13:43:34 +1100213 *bpp = bp;
Christoph Hellwig475ee412012-07-03 12:21:22 -0400214 *dipp = (struct xfs_dinode *)xfs_buf_offset(bp, imap->im_boffset);
David Chinner4ae29b42008-03-06 13:43:34 +1100215 return 0;
216}
217
218/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 * This routine is called to map an inode number within a file
220 * system to the buffer containing the on-disk version of the
221 * inode. It returns a pointer to the buffer containing the
222 * on-disk inode in the bpp parameter, and in the dip parameter
223 * it returns a pointer to the on-disk inode within that buffer.
224 *
225 * If a non-zero error is returned, then the contents of bpp and
226 * dipp are undefined.
227 *
228 * Use xfs_imap() to determine the size and location of the
229 * buffer to read from disk.
230 */
Christoph Hellwigc679eef2008-10-30 18:04:13 +1100231int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232xfs_inotobp(
233 xfs_mount_t *mp,
234 xfs_trans_t *tp,
235 xfs_ino_t ino,
236 xfs_dinode_t **dipp,
237 xfs_buf_t **bpp,
Christoph Hellwigc679eef2008-10-30 18:04:13 +1100238 int *offset,
239 uint imap_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240{
Christoph Hellwig92bfc6e2008-11-28 14:23:41 +1100241 struct xfs_imap imap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 xfs_buf_t *bp;
243 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 imap.im_blkno = 0;
Christoph Hellwiga1941892008-11-28 14:23:40 +1100246 error = xfs_imap(mp, tp, ino, &imap, imap_flags);
David Chinner4ae29b42008-03-06 13:43:34 +1100247 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
Christoph Hellwig475ee412012-07-03 12:21:22 -0400250 error = xfs_imap_to_bp(mp, tp, &imap, dipp, &bp, 0, imap_flags);
David Chinner4ae29b42008-03-06 13:43:34 +1100251 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 *bpp = bp;
255 *offset = imap.im_boffset;
256 return 0;
257}
258
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259/*
260 * Move inode type and inode format specific information from the
261 * on-disk inode to the in-core inode. For fifos, devs, and sockets
262 * this means set if_rdev to the proper value. For files, directories,
263 * and symlinks this means to bring in the in-line data or extent
264 * pointers. For a file in B-tree format, only the root is immediately
265 * brought in-core. The rest will be in-lined in if_extents when it
266 * is first referenced (see xfs_iread_extents()).
267 */
268STATIC int
269xfs_iformat(
270 xfs_inode_t *ip,
271 xfs_dinode_t *dip)
272{
273 xfs_attr_shortform_t *atp;
274 int size;
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000275 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 xfs_fsize_t di_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100278 if (unlikely(be32_to_cpu(dip->di_nextents) +
279 be16_to_cpu(dip->di_anextents) >
280 be64_to_cpu(dip->di_nblocks))) {
Dave Chinner65333b42011-03-07 10:03:35 +1100281 xfs_warn(ip->i_mount,
Nathan Scott3762ec62006-01-12 10:29:53 +1100282 "corrupt dinode %Lu, extent total = %d, nblocks = %Lu.",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 (unsigned long long)ip->i_ino,
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100284 (int)(be32_to_cpu(dip->di_nextents) +
285 be16_to_cpu(dip->di_anextents)),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 (unsigned long long)
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100287 be64_to_cpu(dip->di_nblocks));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 XFS_CORRUPTION_ERROR("xfs_iformat(1)", XFS_ERRLEVEL_LOW,
289 ip->i_mount, dip);
290 return XFS_ERROR(EFSCORRUPTED);
291 }
292
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100293 if (unlikely(dip->di_forkoff > ip->i_mount->m_sb.sb_inodesize)) {
Dave Chinner65333b42011-03-07 10:03:35 +1100294 xfs_warn(ip->i_mount, "corrupt dinode %Lu, forkoff = 0x%x.",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 (unsigned long long)ip->i_ino,
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100296 dip->di_forkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 XFS_CORRUPTION_ERROR("xfs_iformat(2)", XFS_ERRLEVEL_LOW,
298 ip->i_mount, dip);
299 return XFS_ERROR(EFSCORRUPTED);
300 }
301
Christoph Hellwigb89d4202009-08-10 11:32:18 -0300302 if (unlikely((ip->i_d.di_flags & XFS_DIFLAG_REALTIME) &&
303 !ip->i_mount->m_rtdev_targp)) {
Dave Chinner65333b42011-03-07 10:03:35 +1100304 xfs_warn(ip->i_mount,
Christoph Hellwigb89d4202009-08-10 11:32:18 -0300305 "corrupt dinode %Lu, has realtime flag set.",
306 ip->i_ino);
307 XFS_CORRUPTION_ERROR("xfs_iformat(realtime)",
308 XFS_ERRLEVEL_LOW, ip->i_mount, dip);
309 return XFS_ERROR(EFSCORRUPTED);
310 }
311
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 switch (ip->i_d.di_mode & S_IFMT) {
313 case S_IFIFO:
314 case S_IFCHR:
315 case S_IFBLK:
316 case S_IFSOCK:
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100317 if (unlikely(dip->di_format != XFS_DINODE_FMT_DEV)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 XFS_CORRUPTION_ERROR("xfs_iformat(3)", XFS_ERRLEVEL_LOW,
319 ip->i_mount, dip);
320 return XFS_ERROR(EFSCORRUPTED);
321 }
322 ip->i_d.di_size = 0;
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100323 ip->i_df.if_u2.if_rdev = xfs_dinode_get_rdev(dip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 break;
325
326 case S_IFREG:
327 case S_IFLNK:
328 case S_IFDIR:
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100329 switch (dip->di_format) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 case XFS_DINODE_FMT_LOCAL:
331 /*
332 * no local regular files yet
333 */
Al Viroabbede12011-07-26 02:31:30 -0400334 if (unlikely(S_ISREG(be16_to_cpu(dip->di_mode)))) {
Dave Chinner65333b42011-03-07 10:03:35 +1100335 xfs_warn(ip->i_mount,
336 "corrupt inode %Lu (local format for regular file).",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 (unsigned long long) ip->i_ino);
338 XFS_CORRUPTION_ERROR("xfs_iformat(4)",
339 XFS_ERRLEVEL_LOW,
340 ip->i_mount, dip);
341 return XFS_ERROR(EFSCORRUPTED);
342 }
343
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100344 di_size = be64_to_cpu(dip->di_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 if (unlikely(di_size > XFS_DFORK_DSIZE(dip, ip->i_mount))) {
Dave Chinner65333b42011-03-07 10:03:35 +1100346 xfs_warn(ip->i_mount,
347 "corrupt inode %Lu (bad size %Ld for local inode).",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 (unsigned long long) ip->i_ino,
349 (long long) di_size);
350 XFS_CORRUPTION_ERROR("xfs_iformat(5)",
351 XFS_ERRLEVEL_LOW,
352 ip->i_mount, dip);
353 return XFS_ERROR(EFSCORRUPTED);
354 }
355
356 size = (int)di_size;
357 error = xfs_iformat_local(ip, dip, XFS_DATA_FORK, size);
358 break;
359 case XFS_DINODE_FMT_EXTENTS:
360 error = xfs_iformat_extents(ip, dip, XFS_DATA_FORK);
361 break;
362 case XFS_DINODE_FMT_BTREE:
363 error = xfs_iformat_btree(ip, dip, XFS_DATA_FORK);
364 break;
365 default:
366 XFS_ERROR_REPORT("xfs_iformat(6)", XFS_ERRLEVEL_LOW,
367 ip->i_mount);
368 return XFS_ERROR(EFSCORRUPTED);
369 }
370 break;
371
372 default:
373 XFS_ERROR_REPORT("xfs_iformat(7)", XFS_ERRLEVEL_LOW, ip->i_mount);
374 return XFS_ERROR(EFSCORRUPTED);
375 }
376 if (error) {
377 return error;
378 }
379 if (!XFS_DFORK_Q(dip))
380 return 0;
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000381
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 ASSERT(ip->i_afp == NULL);
Dave Chinner4a7eddd2010-07-20 17:53:59 +1000383 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP | KM_NOFS);
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000384
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100385 switch (dip->di_aformat) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 case XFS_DINODE_FMT_LOCAL:
387 atp = (xfs_attr_shortform_t *)XFS_DFORK_APTR(dip);
Nathan Scott3b244aa2006-03-17 17:29:25 +1100388 size = be16_to_cpu(atp->hdr.totsize);
Christoph Hellwig2809f762009-01-19 02:04:16 +0100389
390 if (unlikely(size < sizeof(struct xfs_attr_sf_hdr))) {
Dave Chinner65333b42011-03-07 10:03:35 +1100391 xfs_warn(ip->i_mount,
392 "corrupt inode %Lu (bad attr fork size %Ld).",
Christoph Hellwig2809f762009-01-19 02:04:16 +0100393 (unsigned long long) ip->i_ino,
394 (long long) size);
395 XFS_CORRUPTION_ERROR("xfs_iformat(8)",
396 XFS_ERRLEVEL_LOW,
397 ip->i_mount, dip);
398 return XFS_ERROR(EFSCORRUPTED);
399 }
400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 error = xfs_iformat_local(ip, dip, XFS_ATTR_FORK, size);
402 break;
403 case XFS_DINODE_FMT_EXTENTS:
404 error = xfs_iformat_extents(ip, dip, XFS_ATTR_FORK);
405 break;
406 case XFS_DINODE_FMT_BTREE:
407 error = xfs_iformat_btree(ip, dip, XFS_ATTR_FORK);
408 break;
409 default:
410 error = XFS_ERROR(EFSCORRUPTED);
411 break;
412 }
413 if (error) {
414 kmem_zone_free(xfs_ifork_zone, ip->i_afp);
415 ip->i_afp = NULL;
416 xfs_idestroy_fork(ip, XFS_DATA_FORK);
417 }
418 return error;
419}
420
421/*
422 * The file is in-lined in the on-disk inode.
423 * If it fits into if_inline_data, then copy
424 * it there, otherwise allocate a buffer for it
425 * and copy the data there. Either way, set
426 * if_data to point at the data.
427 * If we allocate a buffer for the data, make
428 * sure that its size is a multiple of 4 and
429 * record the real size in i_real_bytes.
430 */
431STATIC int
432xfs_iformat_local(
433 xfs_inode_t *ip,
434 xfs_dinode_t *dip,
435 int whichfork,
436 int size)
437{
438 xfs_ifork_t *ifp;
439 int real_size;
440
441 /*
442 * If the size is unreasonable, then something
443 * is wrong and we just bail out rather than crash in
444 * kmem_alloc() or memcpy() below.
445 */
446 if (unlikely(size > XFS_DFORK_SIZE(dip, ip->i_mount, whichfork))) {
Dave Chinner65333b42011-03-07 10:03:35 +1100447 xfs_warn(ip->i_mount,
448 "corrupt inode %Lu (bad size %d for local fork, size = %d).",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 (unsigned long long) ip->i_ino, size,
450 XFS_DFORK_SIZE(dip, ip->i_mount, whichfork));
451 XFS_CORRUPTION_ERROR("xfs_iformat_local", XFS_ERRLEVEL_LOW,
452 ip->i_mount, dip);
453 return XFS_ERROR(EFSCORRUPTED);
454 }
455 ifp = XFS_IFORK_PTR(ip, whichfork);
456 real_size = 0;
457 if (size == 0)
458 ifp->if_u1.if_data = NULL;
459 else if (size <= sizeof(ifp->if_u2.if_inline_data))
460 ifp->if_u1.if_data = ifp->if_u2.if_inline_data;
461 else {
462 real_size = roundup(size, 4);
Dave Chinner4a7eddd2010-07-20 17:53:59 +1000463 ifp->if_u1.if_data = kmem_alloc(real_size, KM_SLEEP | KM_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 }
465 ifp->if_bytes = size;
466 ifp->if_real_bytes = real_size;
467 if (size)
468 memcpy(ifp->if_u1.if_data, XFS_DFORK_PTR(dip, whichfork), size);
469 ifp->if_flags &= ~XFS_IFEXTENTS;
470 ifp->if_flags |= XFS_IFINLINE;
471 return 0;
472}
473
474/*
475 * The file consists of a set of extents all
476 * of which fit into the on-disk inode.
477 * If there are few enough extents to fit into
478 * the if_inline_ext, then copy them there.
479 * Otherwise allocate a buffer for them and copy
480 * them into it. Either way, set if_extents
481 * to point at the extents.
482 */
483STATIC int
484xfs_iformat_extents(
485 xfs_inode_t *ip,
486 xfs_dinode_t *dip,
487 int whichfork)
488{
Christoph Hellwiga6f64d42007-08-16 16:23:40 +1000489 xfs_bmbt_rec_t *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 xfs_ifork_t *ifp;
491 int nex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 int size;
493 int i;
494
495 ifp = XFS_IFORK_PTR(ip, whichfork);
496 nex = XFS_DFORK_NEXTENTS(dip, whichfork);
497 size = nex * (uint)sizeof(xfs_bmbt_rec_t);
498
499 /*
500 * If the number of extents is unreasonable, then something
501 * is wrong and we just bail out rather than crash in
502 * kmem_alloc() or memcpy() below.
503 */
504 if (unlikely(size < 0 || size > XFS_DFORK_SIZE(dip, ip->i_mount, whichfork))) {
Dave Chinner65333b42011-03-07 10:03:35 +1100505 xfs_warn(ip->i_mount, "corrupt inode %Lu ((a)extents = %d).",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 (unsigned long long) ip->i_ino, nex);
507 XFS_CORRUPTION_ERROR("xfs_iformat_extents(1)", XFS_ERRLEVEL_LOW,
508 ip->i_mount, dip);
509 return XFS_ERROR(EFSCORRUPTED);
510 }
511
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100512 ifp->if_real_bytes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 if (nex == 0)
514 ifp->if_u1.if_extents = NULL;
515 else if (nex <= XFS_INLINE_EXTS)
516 ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100517 else
518 xfs_iext_add(ifp, 0, nex);
519
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 ifp->if_bytes = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 if (size) {
522 dp = (xfs_bmbt_rec_t *) XFS_DFORK_PTR(dip, whichfork);
Christoph Hellwiga6f64d42007-08-16 16:23:40 +1000523 xfs_validate_extents(ifp, nex, XFS_EXTFMT_INODE(ip));
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100524 for (i = 0; i < nex; i++, dp++) {
Christoph Hellwiga6f64d42007-08-16 16:23:40 +1000525 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, i);
Harvey Harrison597bca62008-08-13 16:29:21 +1000526 ep->l0 = get_unaligned_be64(&dp->l0);
527 ep->l1 = get_unaligned_be64(&dp->l1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 }
Eric Sandeen3a59c942007-07-11 11:09:47 +1000529 XFS_BMAP_TRACE_EXLIST(ip, nex, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 if (whichfork != XFS_DATA_FORK ||
531 XFS_EXTFMT_INODE(ip) == XFS_EXTFMT_NOSTATE)
532 if (unlikely(xfs_check_nostate_extents(
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100533 ifp, 0, nex))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 XFS_ERROR_REPORT("xfs_iformat_extents(2)",
535 XFS_ERRLEVEL_LOW,
536 ip->i_mount);
537 return XFS_ERROR(EFSCORRUPTED);
538 }
539 }
540 ifp->if_flags |= XFS_IFEXTENTS;
541 return 0;
542}
543
544/*
545 * The file has too many extents to fit into
546 * the inode, so they are in B-tree format.
547 * Allocate a buffer for the root of the B-tree
548 * and copy the root into it. The i_extents
549 * field will remain NULL until all of the
550 * extents are read in (when they are needed).
551 */
552STATIC int
553xfs_iformat_btree(
554 xfs_inode_t *ip,
555 xfs_dinode_t *dip,
556 int whichfork)
557{
558 xfs_bmdr_block_t *dfp;
559 xfs_ifork_t *ifp;
560 /* REFERENCED */
561 int nrecs;
562 int size;
563
564 ifp = XFS_IFORK_PTR(ip, whichfork);
565 dfp = (xfs_bmdr_block_t *)XFS_DFORK_PTR(dip, whichfork);
566 size = XFS_BMAP_BROOT_SPACE(dfp);
Christoph Hellwig60197e82008-10-30 17:11:19 +1100567 nrecs = be16_to_cpu(dfp->bb_numrecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
569 /*
570 * blow out if -- fork has less extents than can fit in
571 * fork (fork shouldn't be a btree format), root btree
572 * block has more records than can fit into the fork,
573 * or the number of extents is greater than the number of
574 * blocks.
575 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000576 if (unlikely(XFS_IFORK_NEXTENTS(ip, whichfork) <=
577 XFS_IFORK_MAXEXT(ip, whichfork) ||
578 XFS_BMDR_SPACE_CALC(nrecs) >
579 XFS_DFORK_SIZE(dip, ip->i_mount, whichfork) ||
580 XFS_IFORK_NEXTENTS(ip, whichfork) > ip->i_d.di_nblocks)) {
Dave Chinner65333b42011-03-07 10:03:35 +1100581 xfs_warn(ip->i_mount, "corrupt inode %Lu (btree).",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 (unsigned long long) ip->i_ino);
Dave Chinner65333b42011-03-07 10:03:35 +1100583 XFS_CORRUPTION_ERROR("xfs_iformat_btree", XFS_ERRLEVEL_LOW,
584 ip->i_mount, dip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 return XFS_ERROR(EFSCORRUPTED);
586 }
587
588 ifp->if_broot_bytes = size;
Dave Chinner4a7eddd2010-07-20 17:53:59 +1000589 ifp->if_broot = kmem_alloc(size, KM_SLEEP | KM_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 ASSERT(ifp->if_broot != NULL);
591 /*
592 * Copy and convert from the on-disk structure
593 * to the in-memory structure.
594 */
Christoph Hellwig60197e82008-10-30 17:11:19 +1100595 xfs_bmdr_to_bmbt(ip->i_mount, dfp,
596 XFS_DFORK_SIZE(dip, ip->i_mount, whichfork),
597 ifp->if_broot, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 ifp->if_flags &= ~XFS_IFEXTENTS;
599 ifp->if_flags |= XFS_IFBROOT;
600
601 return 0;
602}
603
Eric Sandeend96f8f82009-07-02 00:09:33 -0500604STATIC void
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000605xfs_dinode_from_disk(
606 xfs_icdinode_t *to,
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100607 xfs_dinode_t *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608{
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000609 to->di_magic = be16_to_cpu(from->di_magic);
610 to->di_mode = be16_to_cpu(from->di_mode);
611 to->di_version = from ->di_version;
612 to->di_format = from->di_format;
613 to->di_onlink = be16_to_cpu(from->di_onlink);
614 to->di_uid = be32_to_cpu(from->di_uid);
615 to->di_gid = be32_to_cpu(from->di_gid);
616 to->di_nlink = be32_to_cpu(from->di_nlink);
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +0000617 to->di_projid_lo = be16_to_cpu(from->di_projid_lo);
618 to->di_projid_hi = be16_to_cpu(from->di_projid_hi);
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000619 memcpy(to->di_pad, from->di_pad, sizeof(to->di_pad));
620 to->di_flushiter = be16_to_cpu(from->di_flushiter);
621 to->di_atime.t_sec = be32_to_cpu(from->di_atime.t_sec);
622 to->di_atime.t_nsec = be32_to_cpu(from->di_atime.t_nsec);
623 to->di_mtime.t_sec = be32_to_cpu(from->di_mtime.t_sec);
624 to->di_mtime.t_nsec = be32_to_cpu(from->di_mtime.t_nsec);
625 to->di_ctime.t_sec = be32_to_cpu(from->di_ctime.t_sec);
626 to->di_ctime.t_nsec = be32_to_cpu(from->di_ctime.t_nsec);
627 to->di_size = be64_to_cpu(from->di_size);
628 to->di_nblocks = be64_to_cpu(from->di_nblocks);
629 to->di_extsize = be32_to_cpu(from->di_extsize);
630 to->di_nextents = be32_to_cpu(from->di_nextents);
631 to->di_anextents = be16_to_cpu(from->di_anextents);
632 to->di_forkoff = from->di_forkoff;
633 to->di_aformat = from->di_aformat;
634 to->di_dmevmask = be32_to_cpu(from->di_dmevmask);
635 to->di_dmstate = be16_to_cpu(from->di_dmstate);
636 to->di_flags = be16_to_cpu(from->di_flags);
637 to->di_gen = be32_to_cpu(from->di_gen);
638}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000640void
641xfs_dinode_to_disk(
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100642 xfs_dinode_t *to,
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000643 xfs_icdinode_t *from)
644{
645 to->di_magic = cpu_to_be16(from->di_magic);
646 to->di_mode = cpu_to_be16(from->di_mode);
647 to->di_version = from ->di_version;
648 to->di_format = from->di_format;
649 to->di_onlink = cpu_to_be16(from->di_onlink);
650 to->di_uid = cpu_to_be32(from->di_uid);
651 to->di_gid = cpu_to_be32(from->di_gid);
652 to->di_nlink = cpu_to_be32(from->di_nlink);
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +0000653 to->di_projid_lo = cpu_to_be16(from->di_projid_lo);
654 to->di_projid_hi = cpu_to_be16(from->di_projid_hi);
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000655 memcpy(to->di_pad, from->di_pad, sizeof(to->di_pad));
656 to->di_flushiter = cpu_to_be16(from->di_flushiter);
657 to->di_atime.t_sec = cpu_to_be32(from->di_atime.t_sec);
658 to->di_atime.t_nsec = cpu_to_be32(from->di_atime.t_nsec);
659 to->di_mtime.t_sec = cpu_to_be32(from->di_mtime.t_sec);
660 to->di_mtime.t_nsec = cpu_to_be32(from->di_mtime.t_nsec);
661 to->di_ctime.t_sec = cpu_to_be32(from->di_ctime.t_sec);
662 to->di_ctime.t_nsec = cpu_to_be32(from->di_ctime.t_nsec);
663 to->di_size = cpu_to_be64(from->di_size);
664 to->di_nblocks = cpu_to_be64(from->di_nblocks);
665 to->di_extsize = cpu_to_be32(from->di_extsize);
666 to->di_nextents = cpu_to_be32(from->di_nextents);
667 to->di_anextents = cpu_to_be16(from->di_anextents);
668 to->di_forkoff = from->di_forkoff;
669 to->di_aformat = from->di_aformat;
670 to->di_dmevmask = cpu_to_be32(from->di_dmevmask);
671 to->di_dmstate = cpu_to_be16(from->di_dmstate);
672 to->di_flags = cpu_to_be16(from->di_flags);
673 to->di_gen = cpu_to_be32(from->di_gen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674}
675
676STATIC uint
677_xfs_dic2xflags(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 __uint16_t di_flags)
679{
680 uint flags = 0;
681
682 if (di_flags & XFS_DIFLAG_ANY) {
683 if (di_flags & XFS_DIFLAG_REALTIME)
684 flags |= XFS_XFLAG_REALTIME;
685 if (di_flags & XFS_DIFLAG_PREALLOC)
686 flags |= XFS_XFLAG_PREALLOC;
687 if (di_flags & XFS_DIFLAG_IMMUTABLE)
688 flags |= XFS_XFLAG_IMMUTABLE;
689 if (di_flags & XFS_DIFLAG_APPEND)
690 flags |= XFS_XFLAG_APPEND;
691 if (di_flags & XFS_DIFLAG_SYNC)
692 flags |= XFS_XFLAG_SYNC;
693 if (di_flags & XFS_DIFLAG_NOATIME)
694 flags |= XFS_XFLAG_NOATIME;
695 if (di_flags & XFS_DIFLAG_NODUMP)
696 flags |= XFS_XFLAG_NODUMP;
697 if (di_flags & XFS_DIFLAG_RTINHERIT)
698 flags |= XFS_XFLAG_RTINHERIT;
699 if (di_flags & XFS_DIFLAG_PROJINHERIT)
700 flags |= XFS_XFLAG_PROJINHERIT;
701 if (di_flags & XFS_DIFLAG_NOSYMLINKS)
702 flags |= XFS_XFLAG_NOSYMLINKS;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100703 if (di_flags & XFS_DIFLAG_EXTSIZE)
704 flags |= XFS_XFLAG_EXTSIZE;
705 if (di_flags & XFS_DIFLAG_EXTSZINHERIT)
706 flags |= XFS_XFLAG_EXTSZINHERIT;
Barry Naujokd3446ea2006-06-09 14:54:19 +1000707 if (di_flags & XFS_DIFLAG_NODEFRAG)
708 flags |= XFS_XFLAG_NODEFRAG;
David Chinner2a82b8b2007-07-11 11:09:12 +1000709 if (di_flags & XFS_DIFLAG_FILESTREAM)
710 flags |= XFS_XFLAG_FILESTREAM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 }
712
713 return flags;
714}
715
716uint
717xfs_ip2xflags(
718 xfs_inode_t *ip)
719{
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000720 xfs_icdinode_t *dic = &ip->i_d;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
Nathan Scotta916e2b2006-06-09 17:12:17 +1000722 return _xfs_dic2xflags(dic->di_flags) |
Christoph Hellwig45ba5982007-12-07 14:07:20 +1100723 (XFS_IFORK_Q(ip) ? XFS_XFLAG_HASATTR : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724}
725
726uint
727xfs_dic2xflags(
Christoph Hellwig45ba5982007-12-07 14:07:20 +1100728 xfs_dinode_t *dip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729{
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100730 return _xfs_dic2xflags(be16_to_cpu(dip->di_flags)) |
Christoph Hellwig45ba5982007-12-07 14:07:20 +1100731 (XFS_DFORK_Q(dip) ? XFS_XFLAG_HASATTR : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732}
733
734/*
Christoph Hellwig24f211b2008-11-28 14:23:42 +1100735 * Read the disk inode attributes into the in-core inode structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 */
737int
738xfs_iread(
739 xfs_mount_t *mp,
740 xfs_trans_t *tp,
Christoph Hellwig24f211b2008-11-28 14:23:42 +1100741 xfs_inode_t *ip,
Christoph Hellwig24f211b2008-11-28 14:23:42 +1100742 uint iget_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743{
744 xfs_buf_t *bp;
745 xfs_dinode_t *dip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 int error;
747
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 /*
Christoph Hellwig92bfc6e2008-11-28 14:23:41 +1100749 * Fill in the location information in the in-core inode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 */
Christoph Hellwig24f211b2008-11-28 14:23:42 +1100751 error = xfs_imap(mp, tp, ip->i_ino, &ip->i_imap, iget_flags);
Christoph Hellwig9ed04512008-10-30 18:26:04 +1100752 if (error)
Christoph Hellwig24f211b2008-11-28 14:23:42 +1100753 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
755 /*
Christoph Hellwig92bfc6e2008-11-28 14:23:41 +1100756 * Get pointers to the on-disk inode and the buffer containing it.
Christoph Hellwig76d8b272008-11-28 14:23:40 +1100757 */
Christoph Hellwig475ee412012-07-03 12:21:22 -0400758 error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &bp, 0, iget_flags);
Christoph Hellwig76d8b272008-11-28 14:23:40 +1100759 if (error)
Christoph Hellwig24f211b2008-11-28 14:23:42 +1100760 return error;
Christoph Hellwig76d8b272008-11-28 14:23:40 +1100761
762 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 * If we got something that isn't an inode it means someone
764 * (nfs or dmi) has a stale handle.
765 */
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200766 if (dip->di_magic != cpu_to_be16(XFS_DINODE_MAGIC)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767#ifdef DEBUG
Dave Chinner53487782011-03-07 10:05:35 +1100768 xfs_alert(mp,
769 "%s: dip->di_magic (0x%x) != XFS_DINODE_MAGIC (0x%x)",
770 __func__, be16_to_cpu(dip->di_magic), XFS_DINODE_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771#endif /* DEBUG */
Christoph Hellwig9ed04512008-10-30 18:26:04 +1100772 error = XFS_ERROR(EINVAL);
773 goto out_brelse;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 }
775
776 /*
777 * If the on-disk inode is already linked to a directory
778 * entry, copy all of the inode into the in-core inode.
779 * xfs_iformat() handles copying in the inode format
780 * specific information.
781 * Otherwise, just get the truly permanent information.
782 */
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100783 if (dip->di_mode) {
784 xfs_dinode_from_disk(&ip->i_d, dip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 error = xfs_iformat(ip, dip);
786 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787#ifdef DEBUG
Dave Chinner53487782011-03-07 10:05:35 +1100788 xfs_alert(mp, "%s: xfs_iformat() returned error %d",
789 __func__, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790#endif /* DEBUG */
Christoph Hellwig9ed04512008-10-30 18:26:04 +1100791 goto out_brelse;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 }
793 } else {
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100794 ip->i_d.di_magic = be16_to_cpu(dip->di_magic);
795 ip->i_d.di_version = dip->di_version;
796 ip->i_d.di_gen = be32_to_cpu(dip->di_gen);
797 ip->i_d.di_flushiter = be16_to_cpu(dip->di_flushiter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 /*
799 * Make sure to pull in the mode here as well in
800 * case the inode is released without being used.
801 * This ensures that xfs_inactive() will see that
802 * the inode is already free and not try to mess
803 * with the uninitialized part of it.
804 */
805 ip->i_d.di_mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 }
807
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 /*
809 * The inode format changed when we moved the link count and
810 * made it 32 bits long. If this is an old format inode,
811 * convert it in memory to look like a new one. If it gets
812 * flushed to disk we will convert back before flushing or
813 * logging it. We zero out the new projid field and the old link
814 * count field. We'll handle clearing the pad field (the remains
815 * of the old uuid field) when we actually convert the inode to
816 * the new format. We don't change the version number so that we
817 * can distinguish this from a real new format inode.
818 */
Christoph Hellwig51ce16d2008-11-28 14:23:39 +1100819 if (ip->i_d.di_version == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 ip->i_d.di_nlink = ip->i_d.di_onlink;
821 ip->i_d.di_onlink = 0;
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +0000822 xfs_set_projid(ip, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 }
824
825 ip->i_delayed_blks = 0;
826
827 /*
828 * Mark the buffer containing the inode as something to keep
829 * around for a while. This helps to keep recently accessed
830 * meta-data in-core longer.
831 */
Dave Chinner821eb212010-12-02 16:31:13 +1100832 xfs_buf_set_ref(bp, XFS_INO_REF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
834 /*
835 * Use xfs_trans_brelse() to release the buffer containing the
836 * on-disk inode, because it was acquired with xfs_trans_read_buf()
Christoph Hellwig475ee412012-07-03 12:21:22 -0400837 * in xfs_imap_to_bp() above. If tp is NULL, this is just a normal
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 * brelse(). If we're within a transaction, then xfs_trans_brelse()
839 * will only release the buffer if it is not dirty within the
840 * transaction. It will be OK to release the buffer in this case,
841 * because inodes on disk are never destroyed and we will be
842 * locking the new in-core inode before putting it in the hash
843 * table where other processes can find it. Thus we don't have
844 * to worry about the inode being changed just because we released
845 * the buffer.
846 */
Christoph Hellwig9ed04512008-10-30 18:26:04 +1100847 out_brelse:
848 xfs_trans_brelse(tp, bp);
Christoph Hellwig9ed04512008-10-30 18:26:04 +1100849 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850}
851
852/*
853 * Read in extents from a btree-format inode.
854 * Allocate and fill in if_extents. Real work is done in xfs_bmap.c.
855 */
856int
857xfs_iread_extents(
858 xfs_trans_t *tp,
859 xfs_inode_t *ip,
860 int whichfork)
861{
862 int error;
863 xfs_ifork_t *ifp;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100864 xfs_extnum_t nextents;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
866 if (unlikely(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
867 XFS_ERROR_REPORT("xfs_iread_extents", XFS_ERRLEVEL_LOW,
868 ip->i_mount);
869 return XFS_ERROR(EFSCORRUPTED);
870 }
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100871 nextents = XFS_IFORK_NEXTENTS(ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 ifp = XFS_IFORK_PTR(ip, whichfork);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100873
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 /*
875 * We know that the size is valid (it's checked in iformat_btree)
876 */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100877 ifp->if_bytes = ifp->if_real_bytes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 ifp->if_flags |= XFS_IFEXTENTS;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100879 xfs_iext_add(ifp, 0, nextents);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 error = xfs_bmap_read_extents(tp, ip, whichfork);
881 if (error) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100882 xfs_iext_destroy(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 ifp->if_flags &= ~XFS_IFEXTENTS;
884 return error;
885 }
Christoph Hellwiga6f64d42007-08-16 16:23:40 +1000886 xfs_validate_extents(ifp, nextents, XFS_EXTFMT_INODE(ip));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 return 0;
888}
889
890/*
891 * Allocate an inode on disk and return a copy of its in-core version.
892 * The in-core inode is locked exclusively. Set mode, nlink, and rdev
893 * appropriately within the inode. The uid and gid for the inode are
894 * set according to the contents of the given cred structure.
895 *
896 * Use xfs_dialloc() to allocate the on-disk inode. If xfs_dialloc()
897 * has a free inode available, call xfs_iget()
898 * to obtain the in-core version of the allocated inode. Finally,
899 * fill in the inode and log its initial contents. In this case,
900 * ialloc_context would be set to NULL and call_again set to false.
901 *
902 * If xfs_dialloc() does not have an available inode,
903 * it will replenish its supply by doing an allocation. Since we can
904 * only do one allocation within a transaction without deadlocks, we
905 * must commit the current transaction before returning the inode itself.
906 * In this case, therefore, we will set call_again to true and return.
907 * The caller should then commit the current transaction, start a new
908 * transaction, and call xfs_ialloc() again to actually get the inode.
909 *
910 * To ensure that some other process does not grab the inode that
911 * was allocated during the first call to xfs_ialloc(), this routine
912 * also returns the [locked] bp pointing to the head of the freelist
913 * as ialloc_context. The caller should hold this buffer across
914 * the commit and pass it back into this routine on the second call.
David Chinnerb11f94d2007-07-11 11:09:33 +1000915 *
916 * If we are allocating quota inodes, we do not have a parent inode
917 * to attach to or associate with (i.e. pip == NULL) because they
918 * are not linked into the directory structure - they are attached
919 * directly to the superblock - and so have no parent.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 */
921int
922xfs_ialloc(
923 xfs_trans_t *tp,
924 xfs_inode_t *pip,
Al Viro576b1d62011-07-26 02:50:15 -0400925 umode_t mode,
Nathan Scott31b084a2005-05-05 13:25:00 -0700926 xfs_nlink_t nlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 xfs_dev_t rdev,
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +0000928 prid_t prid,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 int okalloc,
930 xfs_buf_t **ialloc_context,
931 boolean_t *call_again,
932 xfs_inode_t **ipp)
933{
934 xfs_ino_t ino;
935 xfs_inode_t *ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 uint flags;
937 int error;
Christoph Hellwigdff35fd2008-08-13 16:44:15 +1000938 timespec_t tv;
David Chinnerbf904242008-10-30 17:36:14 +1100939 int filestreams = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
941 /*
942 * Call the space management code to pick
943 * the on-disk inode to be allocated.
944 */
David Chinnerb11f94d2007-07-11 11:09:33 +1000945 error = xfs_dialloc(tp, pip ? pip->i_ino : 0, mode, okalloc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 ialloc_context, call_again, &ino);
David Chinnerbf904242008-10-30 17:36:14 +1100947 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 if (*call_again || ino == NULLFSINO) {
950 *ipp = NULL;
951 return 0;
952 }
953 ASSERT(*ialloc_context == NULL);
954
955 /*
956 * Get the in-core inode with the lock held exclusively.
957 * This is because we're setting fields here we need
958 * to prevent others from looking at until we're done.
959 */
Christoph Hellwigec3ba852011-02-13 13:26:42 +0000960 error = xfs_iget(tp->t_mountp, tp, ino, XFS_IGET_CREATE,
961 XFS_ILOCK_EXCL, &ip);
David Chinnerbf904242008-10-30 17:36:14 +1100962 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 ASSERT(ip != NULL);
965
Al Viro576b1d62011-07-26 02:50:15 -0400966 ip->i_d.di_mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 ip->i_d.di_onlink = 0;
968 ip->i_d.di_nlink = nlink;
969 ASSERT(ip->i_d.di_nlink == nlink);
David Howells9e2b2dc2008-08-13 16:20:04 +0100970 ip->i_d.di_uid = current_fsuid();
971 ip->i_d.di_gid = current_fsgid();
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +0000972 xfs_set_projid(ip, prid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 memset(&(ip->i_d.di_pad[0]), 0, sizeof(ip->i_d.di_pad));
974
975 /*
976 * If the superblock version is up to where we support new format
977 * inodes and this is currently an old format inode, then change
978 * the inode version number now. This way we only do the conversion
979 * here rather than here and in the flush/logging code.
980 */
Eric Sandeen62118702008-03-06 13:44:28 +1100981 if (xfs_sb_version_hasnlink(&tp->t_mountp->m_sb) &&
Christoph Hellwig51ce16d2008-11-28 14:23:39 +1100982 ip->i_d.di_version == 1) {
983 ip->i_d.di_version = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 /*
985 * We've already zeroed the old link count, the projid field,
986 * and the pad field.
987 */
988 }
989
990 /*
991 * Project ids won't be stored on disk if we are using a version 1 inode.
992 */
Christoph Hellwig51ce16d2008-11-28 14:23:39 +1100993 if ((prid != 0) && (ip->i_d.di_version == 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 xfs_bump_ino_vers2(tp, ip);
995
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000996 if (pip && XFS_INHERIT_GID(pip)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 ip->i_d.di_gid = pip->i_d.di_gid;
Al Viroabbede12011-07-26 02:31:30 -0400998 if ((pip->i_d.di_mode & S_ISGID) && S_ISDIR(mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 ip->i_d.di_mode |= S_ISGID;
1000 }
1001 }
1002
1003 /*
1004 * If the group ID of the new file does not match the effective group
1005 * ID or one of the supplementary group IDs, the S_ISGID bit is cleared
1006 * (and only if the irix_sgid_inherit compatibility variable is set).
1007 */
1008 if ((irix_sgid_inherit) &&
1009 (ip->i_d.di_mode & S_ISGID) &&
1010 (!in_group_p((gid_t)ip->i_d.di_gid))) {
1011 ip->i_d.di_mode &= ~S_ISGID;
1012 }
1013
1014 ip->i_d.di_size = 0;
1015 ip->i_d.di_nextents = 0;
1016 ASSERT(ip->i_d.di_nblocks == 0);
Christoph Hellwigdff35fd2008-08-13 16:44:15 +10001017
1018 nanotime(&tv);
1019 ip->i_d.di_mtime.t_sec = (__int32_t)tv.tv_sec;
1020 ip->i_d.di_mtime.t_nsec = (__int32_t)tv.tv_nsec;
1021 ip->i_d.di_atime = ip->i_d.di_mtime;
1022 ip->i_d.di_ctime = ip->i_d.di_mtime;
1023
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 /*
1025 * di_gen will have been taken care of in xfs_iread.
1026 */
1027 ip->i_d.di_extsize = 0;
1028 ip->i_d.di_dmevmask = 0;
1029 ip->i_d.di_dmstate = 0;
1030 ip->i_d.di_flags = 0;
1031 flags = XFS_ILOG_CORE;
1032 switch (mode & S_IFMT) {
1033 case S_IFIFO:
1034 case S_IFCHR:
1035 case S_IFBLK:
1036 case S_IFSOCK:
1037 ip->i_d.di_format = XFS_DINODE_FMT_DEV;
1038 ip->i_df.if_u2.if_rdev = rdev;
1039 ip->i_df.if_flags = 0;
1040 flags |= XFS_ILOG_DEV;
1041 break;
1042 case S_IFREG:
David Chinnerbf904242008-10-30 17:36:14 +11001043 /*
1044 * we can't set up filestreams until after the VFS inode
1045 * is set up properly.
1046 */
1047 if (pip && xfs_inode_is_filestream(pip))
1048 filestreams = 1;
David Chinner2a82b8b2007-07-11 11:09:12 +10001049 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 case S_IFDIR:
David Chinnerb11f94d2007-07-11 11:09:33 +10001051 if (pip && (pip->i_d.di_flags & XFS_DIFLAG_ANY)) {
Nathan Scott365ca832005-06-21 15:39:12 +10001052 uint di_flags = 0;
1053
Al Viroabbede12011-07-26 02:31:30 -04001054 if (S_ISDIR(mode)) {
Nathan Scott365ca832005-06-21 15:39:12 +10001055 if (pip->i_d.di_flags & XFS_DIFLAG_RTINHERIT)
1056 di_flags |= XFS_DIFLAG_RTINHERIT;
Nathan Scottdd9f4382006-01-11 15:28:28 +11001057 if (pip->i_d.di_flags & XFS_DIFLAG_EXTSZINHERIT) {
1058 di_flags |= XFS_DIFLAG_EXTSZINHERIT;
1059 ip->i_d.di_extsize = pip->i_d.di_extsize;
1060 }
Al Viroabbede12011-07-26 02:31:30 -04001061 } else if (S_ISREG(mode)) {
Christoph Hellwig613d7042007-10-11 17:44:08 +10001062 if (pip->i_d.di_flags & XFS_DIFLAG_RTINHERIT)
Nathan Scott365ca832005-06-21 15:39:12 +10001063 di_flags |= XFS_DIFLAG_REALTIME;
Nathan Scottdd9f4382006-01-11 15:28:28 +11001064 if (pip->i_d.di_flags & XFS_DIFLAG_EXTSZINHERIT) {
1065 di_flags |= XFS_DIFLAG_EXTSIZE;
1066 ip->i_d.di_extsize = pip->i_d.di_extsize;
1067 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 }
1069 if ((pip->i_d.di_flags & XFS_DIFLAG_NOATIME) &&
1070 xfs_inherit_noatime)
Nathan Scott365ca832005-06-21 15:39:12 +10001071 di_flags |= XFS_DIFLAG_NOATIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 if ((pip->i_d.di_flags & XFS_DIFLAG_NODUMP) &&
1073 xfs_inherit_nodump)
Nathan Scott365ca832005-06-21 15:39:12 +10001074 di_flags |= XFS_DIFLAG_NODUMP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 if ((pip->i_d.di_flags & XFS_DIFLAG_SYNC) &&
1076 xfs_inherit_sync)
Nathan Scott365ca832005-06-21 15:39:12 +10001077 di_flags |= XFS_DIFLAG_SYNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 if ((pip->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) &&
1079 xfs_inherit_nosymlinks)
Nathan Scott365ca832005-06-21 15:39:12 +10001080 di_flags |= XFS_DIFLAG_NOSYMLINKS;
1081 if (pip->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
1082 di_flags |= XFS_DIFLAG_PROJINHERIT;
Barry Naujokd3446ea2006-06-09 14:54:19 +10001083 if ((pip->i_d.di_flags & XFS_DIFLAG_NODEFRAG) &&
1084 xfs_inherit_nodefrag)
1085 di_flags |= XFS_DIFLAG_NODEFRAG;
David Chinner2a82b8b2007-07-11 11:09:12 +10001086 if (pip->i_d.di_flags & XFS_DIFLAG_FILESTREAM)
1087 di_flags |= XFS_DIFLAG_FILESTREAM;
Nathan Scott365ca832005-06-21 15:39:12 +10001088 ip->i_d.di_flags |= di_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 }
1090 /* FALLTHROUGH */
1091 case S_IFLNK:
1092 ip->i_d.di_format = XFS_DINODE_FMT_EXTENTS;
1093 ip->i_df.if_flags = XFS_IFEXTENTS;
1094 ip->i_df.if_bytes = ip->i_df.if_real_bytes = 0;
1095 ip->i_df.if_u1.if_extents = NULL;
1096 break;
1097 default:
1098 ASSERT(0);
1099 }
1100 /*
1101 * Attribute fork settings for new inode.
1102 */
1103 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
1104 ip->i_d.di_anextents = 0;
1105
1106 /*
1107 * Log the new values stuffed into the inode.
1108 */
Christoph Hellwigddc34152011-09-19 15:00:54 +00001109 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 xfs_trans_log_inode(tp, ip, flags);
1111
Nathan Scottb83bd132006-06-09 16:48:30 +10001112 /* now that we have an i_mode we can setup inode ops and unlock */
Christoph Hellwig41be8be2008-08-13 16:23:13 +10001113 xfs_setup_inode(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
David Chinnerbf904242008-10-30 17:36:14 +11001115 /* now we have set up the vfs inode we can associate the filestream */
1116 if (filestreams) {
1117 error = xfs_filestream_associate(pip, ip);
1118 if (error < 0)
1119 return -error;
1120 if (!error)
1121 xfs_iflags_set(ip, XFS_IFILESTREAM);
1122 }
1123
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 *ipp = ip;
1125 return 0;
1126}
1127
1128/*
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001129 * Free up the underlying blocks past new_size. The new size must be smaller
1130 * than the current size. This routine can be used both for the attribute and
1131 * data fork, and does not modify the inode size, which is left to the caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 *
David Chinnerf6485052008-04-17 16:50:04 +10001133 * The transaction passed to this routine must have made a permanent log
1134 * reservation of at least XFS_ITRUNCATE_LOG_RES. This routine may commit the
1135 * given transaction and start new ones, so make sure everything involved in
1136 * the transaction is tidy before calling here. Some transaction will be
1137 * returned to the caller to be committed. The incoming transaction must
1138 * already include the inode, and both inode locks must be held exclusively.
1139 * The inode must also be "held" within the transaction. On return the inode
1140 * will be "held" within the returned transaction. This routine does NOT
1141 * require any disk space to be reserved for it within the transaction.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 *
David Chinnerf6485052008-04-17 16:50:04 +10001143 * If we get an error, we must return with the inode locked and linked into the
1144 * current transaction. This keeps things simple for the higher level code,
1145 * because it always knows that the inode is locked and held in the transaction
1146 * that returns to it whether errors occur or not. We don't mark the inode
1147 * dirty on error so that transactions can be easily aborted if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 */
1149int
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001150xfs_itruncate_extents(
1151 struct xfs_trans **tpp,
1152 struct xfs_inode *ip,
1153 int whichfork,
1154 xfs_fsize_t new_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155{
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001156 struct xfs_mount *mp = ip->i_mount;
1157 struct xfs_trans *tp = *tpp;
1158 struct xfs_trans *ntp;
1159 xfs_bmap_free_t free_list;
1160 xfs_fsblock_t first_block;
1161 xfs_fileoff_t first_unmap_block;
1162 xfs_fileoff_t last_block;
1163 xfs_filblks_t unmap_len;
1164 int committed;
1165 int error = 0;
1166 int done = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
Christoph Hellwig579aa9c2008-04-22 17:34:00 +10001168 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_IOLOCK_EXCL));
Christoph Hellwigce7ae1512011-12-18 20:00:11 +00001169 ASSERT(new_size <= XFS_ISIZE(ip));
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001170 ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 ASSERT(ip->i_itemp != NULL);
Christoph Hellwig898621d2010-06-24 11:36:58 +10001172 ASSERT(ip->i_itemp->ili_lock_flags == 0);
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001173 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
Christoph Hellwig673e8e52011-12-18 20:00:04 +00001175 trace_xfs_itruncate_extents_start(ip, new_size);
1176
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 /*
1178 * Since it is possible for space to become allocated beyond
1179 * the end of the file (in a crash where the space is allocated
1180 * but the inode size is not yet updated), simply remove any
1181 * blocks which show up between the new EOF and the maximum
1182 * possible file size. If the first block to be removed is
1183 * beyond the maximum file size (ie it is the same as last_block),
1184 * then there is nothing to do.
1185 */
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001186 first_unmap_block = XFS_B_TO_FSB(mp, (xfs_ufsize_t)new_size);
Dave Chinner32972382012-06-08 15:44:54 +10001187 last_block = XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes);
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001188 if (first_unmap_block == last_block)
1189 return 0;
1190
1191 ASSERT(first_unmap_block < last_block);
1192 unmap_len = last_block - first_unmap_block + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 while (!done) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06001194 xfs_bmap_init(&free_list, &first_block);
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001195 error = xfs_bunmapi(tp, ip,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001196 first_unmap_block, unmap_len,
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001197 xfs_bmapi_aflag(whichfork),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 XFS_ITRUNC_MAX_EXTENTS,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001199 &first_block, &free_list,
Christoph Hellwigb4e91812010-06-23 18:11:15 +10001200 &done);
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001201 if (error)
1202 goto out_bmap_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
1204 /*
1205 * Duplicate the transaction that has the permanent
1206 * reservation and commit the old transaction.
1207 */
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001208 error = xfs_bmap_finish(&tp, &free_list, &committed);
Christoph Hellwig898621d2010-06-24 11:36:58 +10001209 if (committed)
Christoph Hellwigddc34152011-09-19 15:00:54 +00001210 xfs_trans_ijoin(tp, ip, 0);
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001211 if (error)
1212 goto out_bmap_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
1214 if (committed) {
1215 /*
David Chinnerf6485052008-04-17 16:50:04 +10001216 * Mark the inode dirty so it will be logged and
David Chinnere5720ee2008-04-10 12:21:18 +10001217 * moved forward in the log as part of every commit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 */
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001219 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 }
David Chinnerf6485052008-04-17 16:50:04 +10001221
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001222 ntp = xfs_trans_dup(tp);
1223 error = xfs_trans_commit(tp, 0);
1224 tp = ntp;
David Chinnere5720ee2008-04-10 12:21:18 +10001225
Christoph Hellwigddc34152011-09-19 15:00:54 +00001226 xfs_trans_ijoin(tp, ip, 0);
David Chinnerf6485052008-04-17 16:50:04 +10001227
Dave Chinnercc09c0d2008-11-17 17:37:10 +11001228 if (error)
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001229 goto out;
1230
Dave Chinnercc09c0d2008-11-17 17:37:10 +11001231 /*
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001232 * Transaction commit worked ok so we can drop the extra ticket
Dave Chinnercc09c0d2008-11-17 17:37:10 +11001233 * reference that we gained in xfs_trans_dup()
1234 */
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001235 xfs_log_ticket_put(tp->t_ticket);
1236 error = xfs_trans_reserve(tp, 0,
David Chinnerf6485052008-04-17 16:50:04 +10001237 XFS_ITRUNCATE_LOG_RES(mp), 0,
1238 XFS_TRANS_PERM_LOG_RES,
1239 XFS_ITRUNCATE_LOG_COUNT);
1240 if (error)
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001241 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 }
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001243
Christoph Hellwig673e8e52011-12-18 20:00:04 +00001244 /*
1245 * Always re-log the inode so that our permanent transaction can keep
1246 * on rolling it forward in the log.
1247 */
1248 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1249
1250 trace_xfs_itruncate_extents_end(ip, new_size);
1251
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001252out:
1253 *tpp = tp;
1254 return error;
1255out_bmap_cancel:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 /*
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001257 * If the bunmapi call encounters an error, return to the caller where
1258 * the transaction can be properly aborted. We just need to make sure
1259 * we're not holding any resources that we were not when we came in.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 */
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001261 xfs_bmap_cancel(&free_list);
1262 goto out;
1263}
1264
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265/*
1266 * This is called when the inode's link count goes to 0.
1267 * We place the on-disk inode on a list in the AGI. It
1268 * will be pulled from this list when the inode is freed.
1269 */
1270int
1271xfs_iunlink(
1272 xfs_trans_t *tp,
1273 xfs_inode_t *ip)
1274{
1275 xfs_mount_t *mp;
1276 xfs_agi_t *agi;
1277 xfs_dinode_t *dip;
1278 xfs_buf_t *agibp;
1279 xfs_buf_t *ibp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 xfs_agino_t agino;
1281 short bucket_index;
1282 int offset;
1283 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
1285 ASSERT(ip->i_d.di_nlink == 0);
1286 ASSERT(ip->i_d.di_mode != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
1288 mp = tp->t_mountp;
1289
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 /*
1291 * Get the agi buffer first. It ensures lock ordering
1292 * on the list.
1293 */
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11001294 error = xfs_read_agi(mp, tp, XFS_INO_TO_AGNO(mp, ip->i_ino), &agibp);
Vlad Apostolov859d7182007-10-11 17:44:18 +10001295 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 agi = XFS_BUF_TO_AGI(agibp);
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11001298
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 /*
1300 * Get the index into the agi hash table for the
1301 * list this inode will go on.
1302 */
1303 agino = XFS_INO_TO_AGINO(mp, ip->i_ino);
1304 ASSERT(agino != 0);
1305 bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS;
1306 ASSERT(agi->agi_unlinked[bucket_index]);
Christoph Hellwig16259e72005-11-02 15:11:25 +11001307 ASSERT(be32_to_cpu(agi->agi_unlinked[bucket_index]) != agino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001309 if (agi->agi_unlinked[bucket_index] != cpu_to_be32(NULLAGINO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 /*
1311 * There is already another inode in the bucket we need
1312 * to add ourselves to. Add us at the front of the list.
1313 * Here we put the head pointer into our next pointer,
1314 * and then we fall through to point the head at us.
1315 */
Christoph Hellwig475ee412012-07-03 12:21:22 -04001316 error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp,
1317 0, 0);
Vlad Apostolovc319b582007-11-23 16:27:51 +11001318 if (error)
1319 return error;
1320
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001321 ASSERT(dip->di_next_unlinked == cpu_to_be32(NULLAGINO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 dip->di_next_unlinked = agi->agi_unlinked[bucket_index];
Christoph Hellwig92bfc6e2008-11-28 14:23:41 +11001323 offset = ip->i_imap.im_boffset +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 offsetof(xfs_dinode_t, di_next_unlinked);
1325 xfs_trans_inode_buf(tp, ibp);
1326 xfs_trans_log_buf(tp, ibp, offset,
1327 (offset + sizeof(xfs_agino_t) - 1));
1328 xfs_inobp_check(mp, ibp);
1329 }
1330
1331 /*
1332 * Point the bucket head pointer at the inode being inserted.
1333 */
1334 ASSERT(agino != 0);
Christoph Hellwig16259e72005-11-02 15:11:25 +11001335 agi->agi_unlinked[bucket_index] = cpu_to_be32(agino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 offset = offsetof(xfs_agi_t, agi_unlinked) +
1337 (sizeof(xfs_agino_t) * bucket_index);
1338 xfs_trans_log_buf(tp, agibp, offset,
1339 (offset + sizeof(xfs_agino_t) - 1));
1340 return 0;
1341}
1342
1343/*
1344 * Pull the on-disk inode from the AGI unlinked list.
1345 */
1346STATIC int
1347xfs_iunlink_remove(
1348 xfs_trans_t *tp,
1349 xfs_inode_t *ip)
1350{
1351 xfs_ino_t next_ino;
1352 xfs_mount_t *mp;
1353 xfs_agi_t *agi;
1354 xfs_dinode_t *dip;
1355 xfs_buf_t *agibp;
1356 xfs_buf_t *ibp;
1357 xfs_agnumber_t agno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 xfs_agino_t agino;
1359 xfs_agino_t next_agino;
1360 xfs_buf_t *last_ibp;
Nathan Scott6fdf8cc2006-06-28 10:13:52 +10001361 xfs_dinode_t *last_dip = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 short bucket_index;
Nathan Scott6fdf8cc2006-06-28 10:13:52 +10001363 int offset, last_offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 mp = tp->t_mountp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 agno = XFS_INO_TO_AGNO(mp, ip->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
1369 /*
1370 * Get the agi buffer first. It ensures lock ordering
1371 * on the list.
1372 */
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11001373 error = xfs_read_agi(mp, tp, agno, &agibp);
1374 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 return error;
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11001376
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 agi = XFS_BUF_TO_AGI(agibp);
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11001378
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 /*
1380 * Get the index into the agi hash table for the
1381 * list this inode will go on.
1382 */
1383 agino = XFS_INO_TO_AGINO(mp, ip->i_ino);
1384 ASSERT(agino != 0);
1385 bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001386 ASSERT(agi->agi_unlinked[bucket_index] != cpu_to_be32(NULLAGINO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 ASSERT(agi->agi_unlinked[bucket_index]);
1388
Christoph Hellwig16259e72005-11-02 15:11:25 +11001389 if (be32_to_cpu(agi->agi_unlinked[bucket_index]) == agino) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 /*
Christoph Hellwig475ee412012-07-03 12:21:22 -04001391 * We're at the head of the list. Get the inode's on-disk
1392 * buffer to see if there is anyone after us on the list.
1393 * Only modify our next pointer if it is not already NULLAGINO.
1394 * This saves us the overhead of dealing with the buffer when
1395 * there is no need to change it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 */
Christoph Hellwig475ee412012-07-03 12:21:22 -04001397 error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp,
1398 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 if (error) {
Christoph Hellwig475ee412012-07-03 12:21:22 -04001400 xfs_warn(mp, "%s: xfs_imap_to_bp returned error %d.",
Dave Chinner0b932cc2011-03-07 10:08:35 +11001401 __func__, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 return error;
1403 }
Christoph Hellwig347d1c02007-08-28 13:57:51 +10001404 next_agino = be32_to_cpu(dip->di_next_unlinked);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 ASSERT(next_agino != 0);
1406 if (next_agino != NULLAGINO) {
Christoph Hellwig347d1c02007-08-28 13:57:51 +10001407 dip->di_next_unlinked = cpu_to_be32(NULLAGINO);
Christoph Hellwig92bfc6e2008-11-28 14:23:41 +11001408 offset = ip->i_imap.im_boffset +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 offsetof(xfs_dinode_t, di_next_unlinked);
1410 xfs_trans_inode_buf(tp, ibp);
1411 xfs_trans_log_buf(tp, ibp, offset,
1412 (offset + sizeof(xfs_agino_t) - 1));
1413 xfs_inobp_check(mp, ibp);
1414 } else {
1415 xfs_trans_brelse(tp, ibp);
1416 }
1417 /*
1418 * Point the bucket head pointer at the next inode.
1419 */
1420 ASSERT(next_agino != 0);
1421 ASSERT(next_agino != agino);
Christoph Hellwig16259e72005-11-02 15:11:25 +11001422 agi->agi_unlinked[bucket_index] = cpu_to_be32(next_agino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 offset = offsetof(xfs_agi_t, agi_unlinked) +
1424 (sizeof(xfs_agino_t) * bucket_index);
1425 xfs_trans_log_buf(tp, agibp, offset,
1426 (offset + sizeof(xfs_agino_t) - 1));
1427 } else {
1428 /*
1429 * We need to search the list for the inode being freed.
1430 */
Christoph Hellwig16259e72005-11-02 15:11:25 +11001431 next_agino = be32_to_cpu(agi->agi_unlinked[bucket_index]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 last_ibp = NULL;
1433 while (next_agino != agino) {
1434 /*
1435 * If the last inode wasn't the one pointing to
1436 * us, then release its buffer since we're not
1437 * going to do anything with it.
1438 */
1439 if (last_ibp != NULL) {
1440 xfs_trans_brelse(tp, last_ibp);
1441 }
1442 next_ino = XFS_AGINO_TO_INO(mp, agno, next_agino);
1443 error = xfs_inotobp(mp, tp, next_ino, &last_dip,
Christoph Hellwigc679eef2008-10-30 18:04:13 +11001444 &last_ibp, &last_offset, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 if (error) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001446 xfs_warn(mp,
1447 "%s: xfs_inotobp() returned error %d.",
1448 __func__, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 return error;
1450 }
Christoph Hellwig347d1c02007-08-28 13:57:51 +10001451 next_agino = be32_to_cpu(last_dip->di_next_unlinked);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 ASSERT(next_agino != NULLAGINO);
1453 ASSERT(next_agino != 0);
1454 }
Christoph Hellwig475ee412012-07-03 12:21:22 -04001455
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 /*
Christoph Hellwig475ee412012-07-03 12:21:22 -04001457 * Now last_ibp points to the buffer previous to us on the
1458 * unlinked list. Pull us from the list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 */
Christoph Hellwig475ee412012-07-03 12:21:22 -04001460 error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp,
1461 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 if (error) {
Christoph Hellwig475ee412012-07-03 12:21:22 -04001463 xfs_warn(mp, "%s: xfs_imap_to_bp(2) returned error %d.",
Dave Chinner0b932cc2011-03-07 10:08:35 +11001464 __func__, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 return error;
1466 }
Christoph Hellwig347d1c02007-08-28 13:57:51 +10001467 next_agino = be32_to_cpu(dip->di_next_unlinked);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 ASSERT(next_agino != 0);
1469 ASSERT(next_agino != agino);
1470 if (next_agino != NULLAGINO) {
Christoph Hellwig347d1c02007-08-28 13:57:51 +10001471 dip->di_next_unlinked = cpu_to_be32(NULLAGINO);
Christoph Hellwig92bfc6e2008-11-28 14:23:41 +11001472 offset = ip->i_imap.im_boffset +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 offsetof(xfs_dinode_t, di_next_unlinked);
1474 xfs_trans_inode_buf(tp, ibp);
1475 xfs_trans_log_buf(tp, ibp, offset,
1476 (offset + sizeof(xfs_agino_t) - 1));
1477 xfs_inobp_check(mp, ibp);
1478 } else {
1479 xfs_trans_brelse(tp, ibp);
1480 }
1481 /*
1482 * Point the previous inode on the list to the next inode.
1483 */
Christoph Hellwig347d1c02007-08-28 13:57:51 +10001484 last_dip->di_next_unlinked = cpu_to_be32(next_agino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 ASSERT(next_agino != 0);
1486 offset = last_offset + offsetof(xfs_dinode_t, di_next_unlinked);
1487 xfs_trans_inode_buf(tp, last_ibp);
1488 xfs_trans_log_buf(tp, last_ibp, offset,
1489 (offset + sizeof(xfs_agino_t) - 1));
1490 xfs_inobp_check(mp, last_ibp);
1491 }
1492 return 0;
1493}
1494
Dave Chinner5b3eed72010-08-24 11:42:41 +10001495/*
1496 * A big issue when freeing the inode cluster is is that we _cannot_ skip any
1497 * inodes that are in memory - they all must be marked stale and attached to
1498 * the cluster buffer.
1499 */
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +00001500STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501xfs_ifree_cluster(
1502 xfs_inode_t *free_ip,
1503 xfs_trans_t *tp,
1504 xfs_ino_t inum)
1505{
1506 xfs_mount_t *mp = free_ip->i_mount;
1507 int blks_per_cluster;
1508 int nbufs;
1509 int ninodes;
Dave Chinner5b257b42010-06-03 16:22:29 +10001510 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 xfs_daddr_t blkno;
1512 xfs_buf_t *bp;
Dave Chinner5b257b42010-06-03 16:22:29 +10001513 xfs_inode_t *ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 xfs_inode_log_item_t *iip;
1515 xfs_log_item_t *lip;
Dave Chinner5017e972010-01-11 11:47:40 +00001516 struct xfs_perag *pag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517
Dave Chinner5017e972010-01-11 11:47:40 +00001518 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, inum));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 if (mp->m_sb.sb_blocksize >= XFS_INODE_CLUSTER_SIZE(mp)) {
1520 blks_per_cluster = 1;
1521 ninodes = mp->m_sb.sb_inopblock;
1522 nbufs = XFS_IALLOC_BLOCKS(mp);
1523 } else {
1524 blks_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) /
1525 mp->m_sb.sb_blocksize;
1526 ninodes = blks_per_cluster * mp->m_sb.sb_inopblock;
1527 nbufs = XFS_IALLOC_BLOCKS(mp) / blks_per_cluster;
1528 }
1529
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 for (j = 0; j < nbufs; j++, inum += ninodes) {
1531 blkno = XFS_AGB_TO_DADDR(mp, XFS_INO_TO_AGNO(mp, inum),
1532 XFS_INO_TO_AGBNO(mp, inum));
1533
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 /*
Dave Chinner5b257b42010-06-03 16:22:29 +10001535 * We obtain and lock the backing buffer first in the process
1536 * here, as we have to ensure that any dirty inode that we
1537 * can't get the flush lock on is attached to the buffer.
1538 * If we scan the in-memory inodes first, then buffer IO can
1539 * complete before we get a lock on it, and hence we may fail
1540 * to mark all the active inodes on the buffer stale.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 */
Dave Chinner5b257b42010-06-03 16:22:29 +10001542 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, blkno,
Dave Chinnera8acad72012-04-23 15:58:54 +10001543 mp->m_bsize * blks_per_cluster, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +00001545 if (!bp)
1546 return ENOMEM;
Dave Chinner5b257b42010-06-03 16:22:29 +10001547 /*
1548 * Walk the inodes already attached to the buffer and mark them
1549 * stale. These will all have the flush locks held, so an
Dave Chinner5b3eed72010-08-24 11:42:41 +10001550 * in-memory inode walk can't lock them. By marking them all
1551 * stale first, we will not attempt to lock them in the loop
1552 * below as the XFS_ISTALE flag will be set.
Dave Chinner5b257b42010-06-03 16:22:29 +10001553 */
Christoph Hellwigadadbee2011-07-13 13:43:49 +02001554 lip = bp->b_fspriv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 while (lip) {
1556 if (lip->li_type == XFS_LI_INODE) {
1557 iip = (xfs_inode_log_item_t *)lip;
1558 ASSERT(iip->ili_logged == 1);
Christoph Hellwigca30b2a2010-06-23 18:11:15 +10001559 lip->li_cb = xfs_istale_done;
David Chinner7b2e2a32008-10-30 17:39:12 +11001560 xfs_trans_ail_copy_lsn(mp->m_ail,
1561 &iip->ili_flush_lsn,
1562 &iip->ili_item.li_lsn);
David Chinnere5ffd2b2006-11-21 18:55:33 +11001563 xfs_iflags_set(iip->ili_inode, XFS_ISTALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 }
1565 lip = lip->li_bio_list;
1566 }
1567
Dave Chinner5b3eed72010-08-24 11:42:41 +10001568
Dave Chinner5b257b42010-06-03 16:22:29 +10001569 /*
1570 * For each inode in memory attempt to add it to the inode
1571 * buffer and set it up for being staled on buffer IO
1572 * completion. This is safe as we've locked out tail pushing
1573 * and flushing by locking the buffer.
1574 *
1575 * We have already marked every inode that was part of a
1576 * transaction stale above, which means there is no point in
1577 * even trying to lock them.
1578 */
1579 for (i = 0; i < ninodes; i++) {
Dave Chinner5b3eed72010-08-24 11:42:41 +10001580retry:
Dave Chinner1a3e8f32010-12-17 17:29:43 +11001581 rcu_read_lock();
Dave Chinner5b257b42010-06-03 16:22:29 +10001582 ip = radix_tree_lookup(&pag->pag_ici_root,
1583 XFS_INO_TO_AGINO(mp, (inum + i)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584
Dave Chinner1a3e8f32010-12-17 17:29:43 +11001585 /* Inode not in memory, nothing to do */
1586 if (!ip) {
1587 rcu_read_unlock();
Dave Chinner5b257b42010-06-03 16:22:29 +10001588 continue;
1589 }
1590
Dave Chinner5b3eed72010-08-24 11:42:41 +10001591 /*
Dave Chinner1a3e8f32010-12-17 17:29:43 +11001592 * because this is an RCU protected lookup, we could
1593 * find a recently freed or even reallocated inode
1594 * during the lookup. We need to check under the
1595 * i_flags_lock for a valid inode here. Skip it if it
1596 * is not valid, the wrong inode or stale.
1597 */
1598 spin_lock(&ip->i_flags_lock);
1599 if (ip->i_ino != inum + i ||
1600 __xfs_iflags_test(ip, XFS_ISTALE)) {
1601 spin_unlock(&ip->i_flags_lock);
1602 rcu_read_unlock();
1603 continue;
1604 }
1605 spin_unlock(&ip->i_flags_lock);
1606
1607 /*
Dave Chinner5b3eed72010-08-24 11:42:41 +10001608 * Don't try to lock/unlock the current inode, but we
1609 * _cannot_ skip the other inodes that we did not find
1610 * in the list attached to the buffer and are not
1611 * already marked stale. If we can't lock it, back off
1612 * and retry.
1613 */
Dave Chinner5b257b42010-06-03 16:22:29 +10001614 if (ip != free_ip &&
1615 !xfs_ilock_nowait(ip, XFS_ILOCK_EXCL)) {
Dave Chinner1a3e8f32010-12-17 17:29:43 +11001616 rcu_read_unlock();
Dave Chinner5b3eed72010-08-24 11:42:41 +10001617 delay(1);
1618 goto retry;
Dave Chinner5b257b42010-06-03 16:22:29 +10001619 }
Dave Chinner1a3e8f32010-12-17 17:29:43 +11001620 rcu_read_unlock();
Dave Chinner5b257b42010-06-03 16:22:29 +10001621
Dave Chinner5b3eed72010-08-24 11:42:41 +10001622 xfs_iflock(ip);
Dave Chinner5b257b42010-06-03 16:22:29 +10001623 xfs_iflags_set(ip, XFS_ISTALE);
Dave Chinner5b257b42010-06-03 16:22:29 +10001624
Dave Chinner5b3eed72010-08-24 11:42:41 +10001625 /*
1626 * we don't need to attach clean inodes or those only
1627 * with unlogged changes (which we throw away, anyway).
1628 */
Dave Chinner5b257b42010-06-03 16:22:29 +10001629 iip = ip->i_itemp;
Dave Chinner5b3eed72010-08-24 11:42:41 +10001630 if (!iip || xfs_inode_clean(ip)) {
Dave Chinner5b257b42010-06-03 16:22:29 +10001631 ASSERT(ip != free_ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 xfs_ifunlock(ip);
1633 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1634 continue;
1635 }
1636
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +00001637 iip->ili_last_fields = iip->ili_fields;
1638 iip->ili_fields = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 iip->ili_logged = 1;
David Chinner7b2e2a32008-10-30 17:39:12 +11001640 xfs_trans_ail_copy_lsn(mp->m_ail, &iip->ili_flush_lsn,
1641 &iip->ili_item.li_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642
Christoph Hellwigca30b2a2010-06-23 18:11:15 +10001643 xfs_buf_attach_iodone(bp, xfs_istale_done,
1644 &iip->ili_item);
Dave Chinner5b257b42010-06-03 16:22:29 +10001645
1646 if (ip != free_ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 }
1649
Dave Chinner5b3eed72010-08-24 11:42:41 +10001650 xfs_trans_stale_inode_buf(tp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 xfs_trans_binval(tp, bp);
1652 }
1653
Dave Chinner5017e972010-01-11 11:47:40 +00001654 xfs_perag_put(pag);
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +00001655 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656}
1657
1658/*
1659 * This is called to return an inode to the inode free list.
1660 * The inode should already be truncated to 0 length and have
1661 * no pages associated with it. This routine also assumes that
1662 * the inode is already a part of the transaction.
1663 *
1664 * The on-disk copy of the inode will have been added to the list
1665 * of unlinked inodes in the AGI. We need to remove the inode from
1666 * that list atomically with respect to freeing it here.
1667 */
1668int
1669xfs_ifree(
1670 xfs_trans_t *tp,
1671 xfs_inode_t *ip,
1672 xfs_bmap_free_t *flist)
1673{
1674 int error;
1675 int delete;
1676 xfs_ino_t first_ino;
Vlad Apostolovc319b582007-11-23 16:27:51 +11001677 xfs_dinode_t *dip;
1678 xfs_buf_t *ibp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679
Christoph Hellwig579aa9c2008-04-22 17:34:00 +10001680 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 ASSERT(ip->i_d.di_nlink == 0);
1682 ASSERT(ip->i_d.di_nextents == 0);
1683 ASSERT(ip->i_d.di_anextents == 0);
Christoph Hellwigce7ae1512011-12-18 20:00:11 +00001684 ASSERT(ip->i_d.di_size == 0 || !S_ISREG(ip->i_d.di_mode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 ASSERT(ip->i_d.di_nblocks == 0);
1686
1687 /*
1688 * Pull the on-disk inode from the AGI unlinked list.
1689 */
1690 error = xfs_iunlink_remove(tp, ip);
1691 if (error != 0) {
1692 return error;
1693 }
1694
1695 error = xfs_difree(tp, ip->i_ino, flist, &delete, &first_ino);
1696 if (error != 0) {
1697 return error;
1698 }
1699 ip->i_d.di_mode = 0; /* mark incore inode as free */
1700 ip->i_d.di_flags = 0;
1701 ip->i_d.di_dmevmask = 0;
1702 ip->i_d.di_forkoff = 0; /* mark the attr fork not in use */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 ip->i_d.di_format = XFS_DINODE_FMT_EXTENTS;
1704 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
1705 /*
1706 * Bump the generation count so no one will be confused
1707 * by reincarnations of this inode.
1708 */
1709 ip->i_d.di_gen++;
Vlad Apostolovc319b582007-11-23 16:27:51 +11001710
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1712
Christoph Hellwig475ee412012-07-03 12:21:22 -04001713 error = xfs_imap_to_bp(ip->i_mount, tp, &ip->i_imap, &dip, &ibp,
1714 0, 0);
Vlad Apostolovc319b582007-11-23 16:27:51 +11001715 if (error)
1716 return error;
1717
1718 /*
1719 * Clear the on-disk di_mode. This is to prevent xfs_bulkstat
1720 * from picking up this inode when it is reclaimed (its incore state
1721 * initialzed but not flushed to disk yet). The in-core di_mode is
1722 * already cleared and a corresponding transaction logged.
1723 * The hack here just synchronizes the in-core to on-disk
1724 * di_mode value in advance before the actual inode sync to disk.
1725 * This is OK because the inode is already unlinked and would never
1726 * change its di_mode again for this inode generation.
1727 * This is a temporary hack that would require a proper fix
1728 * in the future.
1729 */
Christoph Hellwig81591fe2008-11-28 14:23:39 +11001730 dip->di_mode = 0;
Vlad Apostolovc319b582007-11-23 16:27:51 +11001731
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 if (delete) {
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +00001733 error = xfs_ifree_cluster(ip, tp, first_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 }
1735
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +00001736 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737}
1738
1739/*
1740 * Reallocate the space for if_broot based on the number of records
1741 * being added or deleted as indicated in rec_diff. Move the records
1742 * and pointers in if_broot to fit the new size. When shrinking this
1743 * will eliminate holes between the records and pointers created by
1744 * the caller. When growing this will create holes to be filled in
1745 * by the caller.
1746 *
1747 * The caller must not request to add more records than would fit in
1748 * the on-disk inode root. If the if_broot is currently NULL, then
1749 * if we adding records one will be allocated. The caller must also
1750 * not request that the number of records go below zero, although
1751 * it can go to zero.
1752 *
1753 * ip -- the inode whose if_broot area is changing
1754 * ext_diff -- the change in the number of records, positive or negative,
1755 * requested for the if_broot array.
1756 */
1757void
1758xfs_iroot_realloc(
1759 xfs_inode_t *ip,
1760 int rec_diff,
1761 int whichfork)
1762{
Christoph Hellwig60197e82008-10-30 17:11:19 +11001763 struct xfs_mount *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 int cur_max;
1765 xfs_ifork_t *ifp;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11001766 struct xfs_btree_block *new_broot;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 int new_max;
1768 size_t new_size;
1769 char *np;
1770 char *op;
1771
1772 /*
1773 * Handle the degenerate case quietly.
1774 */
1775 if (rec_diff == 0) {
1776 return;
1777 }
1778
1779 ifp = XFS_IFORK_PTR(ip, whichfork);
1780 if (rec_diff > 0) {
1781 /*
1782 * If there wasn't any memory allocated before, just
1783 * allocate it now and get out.
1784 */
1785 if (ifp->if_broot_bytes == 0) {
1786 new_size = (size_t)XFS_BMAP_BROOT_SPACE_CALC(rec_diff);
Dave Chinner4a7eddd2010-07-20 17:53:59 +10001787 ifp->if_broot = kmem_alloc(new_size, KM_SLEEP | KM_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 ifp->if_broot_bytes = (int)new_size;
1789 return;
1790 }
1791
1792 /*
1793 * If there is already an existing if_broot, then we need
1794 * to realloc() it and shift the pointers to their new
1795 * location. The records don't change location because
1796 * they are kept butted up against the btree block header.
1797 */
Christoph Hellwig60197e82008-10-30 17:11:19 +11001798 cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 new_max = cur_max + rec_diff;
1800 new_size = (size_t)XFS_BMAP_BROOT_SPACE_CALC(new_max);
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11001801 ifp->if_broot = kmem_realloc(ifp->if_broot, new_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 (size_t)XFS_BMAP_BROOT_SPACE_CALC(cur_max), /* old size */
Dave Chinner4a7eddd2010-07-20 17:53:59 +10001803 KM_SLEEP | KM_NOFS);
Christoph Hellwig60197e82008-10-30 17:11:19 +11001804 op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
1805 ifp->if_broot_bytes);
1806 np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
1807 (int)new_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 ifp->if_broot_bytes = (int)new_size;
1809 ASSERT(ifp->if_broot_bytes <=
1810 XFS_IFORK_SIZE(ip, whichfork) + XFS_BROOT_SIZE_ADJ);
1811 memmove(np, op, cur_max * (uint)sizeof(xfs_dfsbno_t));
1812 return;
1813 }
1814
1815 /*
1816 * rec_diff is less than 0. In this case, we are shrinking the
1817 * if_broot buffer. It must already exist. If we go to zero
1818 * records, just get rid of the root and clear the status bit.
1819 */
1820 ASSERT((ifp->if_broot != NULL) && (ifp->if_broot_bytes > 0));
Christoph Hellwig60197e82008-10-30 17:11:19 +11001821 cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 new_max = cur_max + rec_diff;
1823 ASSERT(new_max >= 0);
1824 if (new_max > 0)
1825 new_size = (size_t)XFS_BMAP_BROOT_SPACE_CALC(new_max);
1826 else
1827 new_size = 0;
1828 if (new_size > 0) {
Dave Chinner4a7eddd2010-07-20 17:53:59 +10001829 new_broot = kmem_alloc(new_size, KM_SLEEP | KM_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 /*
1831 * First copy over the btree block header.
1832 */
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11001833 memcpy(new_broot, ifp->if_broot, XFS_BTREE_LBLOCK_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 } else {
1835 new_broot = NULL;
1836 ifp->if_flags &= ~XFS_IFBROOT;
1837 }
1838
1839 /*
1840 * Only copy the records and pointers if there are any.
1841 */
1842 if (new_max > 0) {
1843 /*
1844 * First copy the records.
1845 */
Christoph Hellwig136341b2008-10-30 17:11:40 +11001846 op = (char *)XFS_BMBT_REC_ADDR(mp, ifp->if_broot, 1);
1847 np = (char *)XFS_BMBT_REC_ADDR(mp, new_broot, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 memcpy(np, op, new_max * (uint)sizeof(xfs_bmbt_rec_t));
1849
1850 /*
1851 * Then copy the pointers.
1852 */
Christoph Hellwig60197e82008-10-30 17:11:19 +11001853 op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 ifp->if_broot_bytes);
Christoph Hellwig60197e82008-10-30 17:11:19 +11001855 np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, new_broot, 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 (int)new_size);
1857 memcpy(np, op, new_max * (uint)sizeof(xfs_dfsbno_t));
1858 }
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001859 kmem_free(ifp->if_broot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 ifp->if_broot = new_broot;
1861 ifp->if_broot_bytes = (int)new_size;
1862 ASSERT(ifp->if_broot_bytes <=
1863 XFS_IFORK_SIZE(ip, whichfork) + XFS_BROOT_SIZE_ADJ);
1864 return;
1865}
1866
1867
1868/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 * This is called when the amount of space needed for if_data
1870 * is increased or decreased. The change in size is indicated by
1871 * the number of bytes that need to be added or deleted in the
1872 * byte_diff parameter.
1873 *
1874 * If the amount of space needed has decreased below the size of the
1875 * inline buffer, then switch to using the inline buffer. Otherwise,
1876 * use kmem_realloc() or kmem_alloc() to adjust the size of the buffer
1877 * to what is needed.
1878 *
1879 * ip -- the inode whose if_data area is changing
1880 * byte_diff -- the change in the number of bytes, positive or negative,
1881 * requested for the if_data array.
1882 */
1883void
1884xfs_idata_realloc(
1885 xfs_inode_t *ip,
1886 int byte_diff,
1887 int whichfork)
1888{
1889 xfs_ifork_t *ifp;
1890 int new_size;
1891 int real_size;
1892
1893 if (byte_diff == 0) {
1894 return;
1895 }
1896
1897 ifp = XFS_IFORK_PTR(ip, whichfork);
1898 new_size = (int)ifp->if_bytes + byte_diff;
1899 ASSERT(new_size >= 0);
1900
1901 if (new_size == 0) {
1902 if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) {
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001903 kmem_free(ifp->if_u1.if_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 }
1905 ifp->if_u1.if_data = NULL;
1906 real_size = 0;
1907 } else if (new_size <= sizeof(ifp->if_u2.if_inline_data)) {
1908 /*
1909 * If the valid extents/data can fit in if_inline_ext/data,
1910 * copy them from the malloc'd vector and free it.
1911 */
1912 if (ifp->if_u1.if_data == NULL) {
1913 ifp->if_u1.if_data = ifp->if_u2.if_inline_data;
1914 } else if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) {
1915 ASSERT(ifp->if_real_bytes != 0);
1916 memcpy(ifp->if_u2.if_inline_data, ifp->if_u1.if_data,
1917 new_size);
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001918 kmem_free(ifp->if_u1.if_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 ifp->if_u1.if_data = ifp->if_u2.if_inline_data;
1920 }
1921 real_size = 0;
1922 } else {
1923 /*
1924 * Stuck with malloc/realloc.
1925 * For inline data, the underlying buffer must be
1926 * a multiple of 4 bytes in size so that it can be
1927 * logged and stay on word boundaries. We enforce
1928 * that here.
1929 */
1930 real_size = roundup(new_size, 4);
1931 if (ifp->if_u1.if_data == NULL) {
1932 ASSERT(ifp->if_real_bytes == 0);
Dave Chinner4a7eddd2010-07-20 17:53:59 +10001933 ifp->if_u1.if_data = kmem_alloc(real_size,
1934 KM_SLEEP | KM_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 } else if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) {
1936 /*
1937 * Only do the realloc if the underlying size
1938 * is really changing.
1939 */
1940 if (ifp->if_real_bytes != real_size) {
1941 ifp->if_u1.if_data =
1942 kmem_realloc(ifp->if_u1.if_data,
1943 real_size,
1944 ifp->if_real_bytes,
Dave Chinner4a7eddd2010-07-20 17:53:59 +10001945 KM_SLEEP | KM_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 }
1947 } else {
1948 ASSERT(ifp->if_real_bytes == 0);
Dave Chinner4a7eddd2010-07-20 17:53:59 +10001949 ifp->if_u1.if_data = kmem_alloc(real_size,
1950 KM_SLEEP | KM_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 memcpy(ifp->if_u1.if_data, ifp->if_u2.if_inline_data,
1952 ifp->if_bytes);
1953 }
1954 }
1955 ifp->if_real_bytes = real_size;
1956 ifp->if_bytes = new_size;
1957 ASSERT(ifp->if_bytes <= XFS_IFORK_SIZE(ip, whichfork));
1958}
1959
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960void
1961xfs_idestroy_fork(
1962 xfs_inode_t *ip,
1963 int whichfork)
1964{
1965 xfs_ifork_t *ifp;
1966
1967 ifp = XFS_IFORK_PTR(ip, whichfork);
1968 if (ifp->if_broot != NULL) {
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001969 kmem_free(ifp->if_broot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 ifp->if_broot = NULL;
1971 }
1972
1973 /*
1974 * If the format is local, then we can't have an extents
1975 * array so just look for an inline data array. If we're
1976 * not local then we may or may not have an extents list,
1977 * so check and free it up if we do.
1978 */
1979 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
1980 if ((ifp->if_u1.if_data != ifp->if_u2.if_inline_data) &&
1981 (ifp->if_u1.if_data != NULL)) {
1982 ASSERT(ifp->if_real_bytes != 0);
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001983 kmem_free(ifp->if_u1.if_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 ifp->if_u1.if_data = NULL;
1985 ifp->if_real_bytes = 0;
1986 }
1987 } else if ((ifp->if_flags & XFS_IFEXTENTS) &&
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11001988 ((ifp->if_flags & XFS_IFEXTIREC) ||
1989 ((ifp->if_u1.if_extents != NULL) &&
1990 (ifp->if_u1.if_extents != ifp->if_u2.if_inline_ext)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 ASSERT(ifp->if_real_bytes != 0);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001992 xfs_iext_destroy(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 }
1994 ASSERT(ifp->if_u1.if_extents == NULL ||
1995 ifp->if_u1.if_extents == ifp->if_u2.if_inline_ext);
1996 ASSERT(ifp->if_real_bytes == 0);
1997 if (whichfork == XFS_ATTR_FORK) {
1998 kmem_zone_free(xfs_ifork_zone, ip->i_afp);
1999 ip->i_afp = NULL;
2000 }
2001}
2002
2003/*
Christoph Hellwig60ec6782010-02-17 19:43:56 +00002004 * This is called to unpin an inode. The caller must have the inode locked
2005 * in at least shared mode so that the buffer cannot be subsequently pinned
2006 * once someone is waiting for it to be unpinned.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 */
Christoph Hellwig60ec6782010-02-17 19:43:56 +00002008static void
Christoph Hellwigf392e632011-12-18 20:00:10 +00002009xfs_iunpin(
Christoph Hellwig60ec6782010-02-17 19:43:56 +00002010 struct xfs_inode *ip)
David Chinnera3f74ff2008-03-06 13:43:42 +11002011{
Christoph Hellwig579aa9c2008-04-22 17:34:00 +10002012 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
David Chinnera3f74ff2008-03-06 13:43:42 +11002013
Dave Chinner4aaf15d2010-03-08 11:24:07 +11002014 trace_xfs_inode_unpin_nowait(ip, _RET_IP_);
2015
David Chinnera3f74ff2008-03-06 13:43:42 +11002016 /* Give the log a push to start the unpinning I/O */
Christoph Hellwig60ec6782010-02-17 19:43:56 +00002017 xfs_log_force_lsn(ip->i_mount, ip->i_itemp->ili_last_lsn, 0);
Christoph Hellwiga14a3482010-01-19 09:56:46 +00002018
David Chinnera3f74ff2008-03-06 13:43:42 +11002019}
2020
Christoph Hellwigf392e632011-12-18 20:00:10 +00002021static void
2022__xfs_iunpin_wait(
2023 struct xfs_inode *ip)
2024{
2025 wait_queue_head_t *wq = bit_waitqueue(&ip->i_flags, __XFS_IPINNED_BIT);
2026 DEFINE_WAIT_BIT(wait, &ip->i_flags, __XFS_IPINNED_BIT);
2027
2028 xfs_iunpin(ip);
2029
2030 do {
2031 prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
2032 if (xfs_ipincount(ip))
2033 io_schedule();
2034 } while (xfs_ipincount(ip));
2035 finish_wait(wq, &wait.wait);
2036}
2037
Dave Chinner777df5a2010-02-06 12:37:26 +11002038void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039xfs_iunpin_wait(
Christoph Hellwig60ec6782010-02-17 19:43:56 +00002040 struct xfs_inode *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041{
Christoph Hellwigf392e632011-12-18 20:00:10 +00002042 if (xfs_ipincount(ip))
2043 __xfs_iunpin_wait(ip);
David Chinnera3f74ff2008-03-06 13:43:42 +11002044}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046/*
2047 * xfs_iextents_copy()
2048 *
2049 * This is called to copy the REAL extents (as opposed to the delayed
2050 * allocation extents) from the inode into the given buffer. It
2051 * returns the number of bytes copied into the buffer.
2052 *
2053 * If there are no delayed allocation extents, then we can just
2054 * memcpy() the extents into the buffer. Otherwise, we need to
2055 * examine each extent in turn and skip those which are delayed.
2056 */
2057int
2058xfs_iextents_copy(
2059 xfs_inode_t *ip,
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10002060 xfs_bmbt_rec_t *dp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 int whichfork)
2062{
2063 int copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 int i;
2065 xfs_ifork_t *ifp;
2066 int nrecs;
2067 xfs_fsblock_t start_block;
2068
2069 ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwig579aa9c2008-04-22 17:34:00 +10002070 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 ASSERT(ifp->if_bytes > 0);
2072
2073 nrecs = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
Eric Sandeen3a59c942007-07-11 11:09:47 +10002074 XFS_BMAP_TRACE_EXLIST(ip, nrecs, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 ASSERT(nrecs > 0);
2076
2077 /*
2078 * There are some delayed allocation extents in the
2079 * inode, so copy the extents one at a time and skip
2080 * the delayed ones. There must be at least one
2081 * non-delayed extent.
2082 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 copied = 0;
2084 for (i = 0; i < nrecs; i++) {
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10002085 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 start_block = xfs_bmbt_get_startblock(ep);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002087 if (isnullstartblock(start_block)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 /*
2089 * It's a delayed allocation extent, so skip it.
2090 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 continue;
2092 }
2093
2094 /* Translate to on disk format */
Christoph Hellwigcd8b0a92007-08-16 16:24:15 +10002095 put_unaligned(cpu_to_be64(ep->l0), &dp->l0);
2096 put_unaligned(cpu_to_be64(ep->l1), &dp->l1);
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10002097 dp++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 copied++;
2099 }
2100 ASSERT(copied != 0);
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10002101 xfs_validate_extents(ifp, copied, XFS_EXTFMT_INODE(ip));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102
2103 return (copied * (uint)sizeof(xfs_bmbt_rec_t));
2104}
2105
2106/*
2107 * Each of the following cases stores data into the same region
2108 * of the on-disk inode, so only one of them can be valid at
2109 * any given time. While it is possible to have conflicting formats
2110 * and log flags, e.g. having XFS_ILOG_?DATA set when the fork is
2111 * in EXTENTS format, this can only happen when the fork has
2112 * changed formats after being modified but before being flushed.
2113 * In these cases, the format always takes precedence, because the
2114 * format indicates the current state of the fork.
2115 */
2116/*ARGSUSED*/
David Chinnere4ac9672008-04-10 12:23:58 +10002117STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118xfs_iflush_fork(
2119 xfs_inode_t *ip,
2120 xfs_dinode_t *dip,
2121 xfs_inode_log_item_t *iip,
2122 int whichfork,
2123 xfs_buf_t *bp)
2124{
2125 char *cp;
2126 xfs_ifork_t *ifp;
2127 xfs_mount_t *mp;
2128#ifdef XFS_TRANS_DEBUG
2129 int first;
2130#endif
2131 static const short brootflag[2] =
2132 { XFS_ILOG_DBROOT, XFS_ILOG_ABROOT };
2133 static const short dataflag[2] =
2134 { XFS_ILOG_DDATA, XFS_ILOG_ADATA };
2135 static const short extflag[2] =
2136 { XFS_ILOG_DEXT, XFS_ILOG_AEXT };
2137
David Chinnere4ac9672008-04-10 12:23:58 +10002138 if (!iip)
2139 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 ifp = XFS_IFORK_PTR(ip, whichfork);
2141 /*
2142 * This can happen if we gave up in iformat in an error path,
2143 * for the attribute fork.
2144 */
David Chinnere4ac9672008-04-10 12:23:58 +10002145 if (!ifp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 ASSERT(whichfork == XFS_ATTR_FORK);
David Chinnere4ac9672008-04-10 12:23:58 +10002147 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 }
2149 cp = XFS_DFORK_PTR(dip, whichfork);
2150 mp = ip->i_mount;
2151 switch (XFS_IFORK_FORMAT(ip, whichfork)) {
2152 case XFS_DINODE_FMT_LOCAL:
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +00002153 if ((iip->ili_fields & dataflag[whichfork]) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 (ifp->if_bytes > 0)) {
2155 ASSERT(ifp->if_u1.if_data != NULL);
2156 ASSERT(ifp->if_bytes <= XFS_IFORK_SIZE(ip, whichfork));
2157 memcpy(cp, ifp->if_u1.if_data, ifp->if_bytes);
2158 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 break;
2160
2161 case XFS_DINODE_FMT_EXTENTS:
2162 ASSERT((ifp->if_flags & XFS_IFEXTENTS) ||
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +00002163 !(iip->ili_fields & extflag[whichfork]));
2164 if ((iip->ili_fields & extflag[whichfork]) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 (ifp->if_bytes > 0)) {
Christoph Hellwigab1908a2011-05-11 15:04:10 +00002166 ASSERT(xfs_iext_get_ext(ifp, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) > 0);
2168 (void)xfs_iextents_copy(ip, (xfs_bmbt_rec_t *)cp,
2169 whichfork);
2170 }
2171 break;
2172
2173 case XFS_DINODE_FMT_BTREE:
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +00002174 if ((iip->ili_fields & brootflag[whichfork]) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 (ifp->if_broot_bytes > 0)) {
2176 ASSERT(ifp->if_broot != NULL);
2177 ASSERT(ifp->if_broot_bytes <=
2178 (XFS_IFORK_SIZE(ip, whichfork) +
2179 XFS_BROOT_SIZE_ADJ));
Christoph Hellwig60197e82008-10-30 17:11:19 +11002180 xfs_bmbt_to_bmdr(mp, ifp->if_broot, ifp->if_broot_bytes,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 (xfs_bmdr_block_t *)cp,
2182 XFS_DFORK_SIZE(dip, mp, whichfork));
2183 }
2184 break;
2185
2186 case XFS_DINODE_FMT_DEV:
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +00002187 if (iip->ili_fields & XFS_ILOG_DEV) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 ASSERT(whichfork == XFS_DATA_FORK);
Christoph Hellwig81591fe2008-11-28 14:23:39 +11002189 xfs_dinode_put_rdev(dip, ip->i_df.if_u2.if_rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 }
2191 break;
2192
2193 case XFS_DINODE_FMT_UUID:
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +00002194 if (iip->ili_fields & XFS_ILOG_UUID) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 ASSERT(whichfork == XFS_DATA_FORK);
Christoph Hellwig81591fe2008-11-28 14:23:39 +11002196 memcpy(XFS_DFORK_DPTR(dip),
2197 &ip->i_df.if_u2.if_uuid,
2198 sizeof(uuid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 }
2200 break;
2201
2202 default:
2203 ASSERT(0);
2204 break;
2205 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206}
2207
David Chinnerbad55842008-03-06 13:43:49 +11002208STATIC int
2209xfs_iflush_cluster(
2210 xfs_inode_t *ip,
2211 xfs_buf_t *bp)
2212{
2213 xfs_mount_t *mp = ip->i_mount;
Dave Chinner5017e972010-01-11 11:47:40 +00002214 struct xfs_perag *pag;
David Chinnerbad55842008-03-06 13:43:49 +11002215 unsigned long first_index, mask;
David Chinnerc8f5f122008-05-20 11:30:15 +10002216 unsigned long inodes_per_cluster;
David Chinnerbad55842008-03-06 13:43:49 +11002217 int ilist_size;
2218 xfs_inode_t **ilist;
2219 xfs_inode_t *iq;
David Chinnerbad55842008-03-06 13:43:49 +11002220 int nr_found;
2221 int clcount = 0;
2222 int bufwasdelwri;
2223 int i;
2224
Dave Chinner5017e972010-01-11 11:47:40 +00002225 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
David Chinnerbad55842008-03-06 13:43:49 +11002226
David Chinnerc8f5f122008-05-20 11:30:15 +10002227 inodes_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_inodelog;
2228 ilist_size = inodes_per_cluster * sizeof(xfs_inode_t *);
David Chinner49383b02008-05-19 16:29:34 +10002229 ilist = kmem_alloc(ilist_size, KM_MAYFAIL|KM_NOFS);
David Chinnerbad55842008-03-06 13:43:49 +11002230 if (!ilist)
Dave Chinner44b56e02010-01-11 11:47:43 +00002231 goto out_put;
David Chinnerbad55842008-03-06 13:43:49 +11002232
2233 mask = ~(((XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_inodelog)) - 1);
2234 first_index = XFS_INO_TO_AGINO(mp, ip->i_ino) & mask;
Dave Chinner1a3e8f32010-12-17 17:29:43 +11002235 rcu_read_lock();
David Chinnerbad55842008-03-06 13:43:49 +11002236 /* really need a gang lookup range call here */
2237 nr_found = radix_tree_gang_lookup(&pag->pag_ici_root, (void**)ilist,
David Chinnerc8f5f122008-05-20 11:30:15 +10002238 first_index, inodes_per_cluster);
David Chinnerbad55842008-03-06 13:43:49 +11002239 if (nr_found == 0)
2240 goto out_free;
2241
2242 for (i = 0; i < nr_found; i++) {
2243 iq = ilist[i];
2244 if (iq == ip)
2245 continue;
Dave Chinner1a3e8f32010-12-17 17:29:43 +11002246
2247 /*
2248 * because this is an RCU protected lookup, we could find a
2249 * recently freed or even reallocated inode during the lookup.
2250 * We need to check under the i_flags_lock for a valid inode
2251 * here. Skip it if it is not valid or the wrong inode.
2252 */
2253 spin_lock(&ip->i_flags_lock);
2254 if (!ip->i_ino ||
2255 (XFS_INO_TO_AGINO(mp, iq->i_ino) & mask) != first_index) {
2256 spin_unlock(&ip->i_flags_lock);
2257 continue;
2258 }
2259 spin_unlock(&ip->i_flags_lock);
2260
David Chinnerbad55842008-03-06 13:43:49 +11002261 /*
2262 * Do an un-protected check to see if the inode is dirty and
2263 * is a candidate for flushing. These checks will be repeated
2264 * later after the appropriate locks are acquired.
2265 */
David Chinner33540402008-03-06 13:43:59 +11002266 if (xfs_inode_clean(iq) && xfs_ipincount(iq) == 0)
David Chinnerbad55842008-03-06 13:43:49 +11002267 continue;
David Chinnerbad55842008-03-06 13:43:49 +11002268
2269 /*
2270 * Try to get locks. If any are unavailable or it is pinned,
2271 * then this inode cannot be flushed and is skipped.
2272 */
2273
2274 if (!xfs_ilock_nowait(iq, XFS_ILOCK_SHARED))
2275 continue;
2276 if (!xfs_iflock_nowait(iq)) {
2277 xfs_iunlock(iq, XFS_ILOCK_SHARED);
2278 continue;
2279 }
2280 if (xfs_ipincount(iq)) {
2281 xfs_ifunlock(iq);
2282 xfs_iunlock(iq, XFS_ILOCK_SHARED);
2283 continue;
2284 }
2285
2286 /*
2287 * arriving here means that this inode can be flushed. First
2288 * re-check that it's dirty before flushing.
2289 */
David Chinner33540402008-03-06 13:43:59 +11002290 if (!xfs_inode_clean(iq)) {
2291 int error;
David Chinnerbad55842008-03-06 13:43:49 +11002292 error = xfs_iflush_int(iq, bp);
2293 if (error) {
2294 xfs_iunlock(iq, XFS_ILOCK_SHARED);
2295 goto cluster_corrupt_out;
2296 }
2297 clcount++;
2298 } else {
2299 xfs_ifunlock(iq);
2300 }
2301 xfs_iunlock(iq, XFS_ILOCK_SHARED);
2302 }
2303
2304 if (clcount) {
2305 XFS_STATS_INC(xs_icluster_flushcnt);
2306 XFS_STATS_ADD(xs_icluster_flushinode, clcount);
2307 }
2308
2309out_free:
Dave Chinner1a3e8f32010-12-17 17:29:43 +11002310 rcu_read_unlock();
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10002311 kmem_free(ilist);
Dave Chinner44b56e02010-01-11 11:47:43 +00002312out_put:
2313 xfs_perag_put(pag);
David Chinnerbad55842008-03-06 13:43:49 +11002314 return 0;
2315
2316
2317cluster_corrupt_out:
2318 /*
2319 * Corruption detected in the clustering loop. Invalidate the
2320 * inode buffer and shut down the filesystem.
2321 */
Dave Chinner1a3e8f32010-12-17 17:29:43 +11002322 rcu_read_unlock();
David Chinnerbad55842008-03-06 13:43:49 +11002323 /*
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002324 * Clean up the buffer. If it was delwri, just release it --
David Chinnerbad55842008-03-06 13:43:49 +11002325 * brelse can handle it with no problems. If not, shut down the
2326 * filesystem before releasing the buffer.
2327 */
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002328 bufwasdelwri = (bp->b_flags & _XBF_DELWRI_Q);
David Chinnerbad55842008-03-06 13:43:49 +11002329 if (bufwasdelwri)
2330 xfs_buf_relse(bp);
2331
2332 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
2333
2334 if (!bufwasdelwri) {
2335 /*
2336 * Just like incore_relse: if we have b_iodone functions,
2337 * mark the buffer as an error and call them. Otherwise
2338 * mark it as stale and brelse.
2339 */
Christoph Hellwigcb669ca2011-07-13 13:43:49 +02002340 if (bp->b_iodone) {
David Chinnerbad55842008-03-06 13:43:49 +11002341 XFS_BUF_UNDONE(bp);
Christoph Hellwigc867cb62011-10-10 16:52:46 +00002342 xfs_buf_stale(bp);
Chandra Seetharaman5a52c2a582011-07-22 23:39:51 +00002343 xfs_buf_ioerror(bp, EIO);
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00002344 xfs_buf_ioend(bp, 0);
David Chinnerbad55842008-03-06 13:43:49 +11002345 } else {
Christoph Hellwigc867cb62011-10-10 16:52:46 +00002346 xfs_buf_stale(bp);
David Chinnerbad55842008-03-06 13:43:49 +11002347 xfs_buf_relse(bp);
2348 }
2349 }
2350
2351 /*
2352 * Unlocks the flush lock
2353 */
Dave Chinner04913fd2012-04-23 15:58:41 +10002354 xfs_iflush_abort(iq, false);
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10002355 kmem_free(ilist);
Dave Chinner44b56e02010-01-11 11:47:43 +00002356 xfs_perag_put(pag);
David Chinnerbad55842008-03-06 13:43:49 +11002357 return XFS_ERROR(EFSCORRUPTED);
2358}
2359
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360/*
Christoph Hellwig4c468192012-04-23 15:58:36 +10002361 * Flush dirty inode metadata into the backing buffer.
2362 *
2363 * The caller must have the inode lock and the inode flush lock held. The
2364 * inode lock will still be held upon return to the caller, and the inode
2365 * flush lock will be released after the inode has reached the disk.
2366 *
2367 * The caller must write out the buffer returned in *bpp and release it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 */
2369int
2370xfs_iflush(
Christoph Hellwig4c468192012-04-23 15:58:36 +10002371 struct xfs_inode *ip,
2372 struct xfs_buf **bpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373{
Christoph Hellwig4c468192012-04-23 15:58:36 +10002374 struct xfs_mount *mp = ip->i_mount;
2375 struct xfs_buf *bp;
2376 struct xfs_dinode *dip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378
2379 XFS_STATS_INC(xs_iflush_count);
2380
Christoph Hellwig579aa9c2008-04-22 17:34:00 +10002381 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
Christoph Hellwig474fce02011-12-18 20:00:09 +00002382 ASSERT(xfs_isiflocked(ip));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE ||
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00002384 ip->i_d.di_nextents > XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385
Christoph Hellwig4c468192012-04-23 15:58:36 +10002386 *bpp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 xfs_iunpin_wait(ip);
2389
2390 /*
Dave Chinner4b6a4682010-01-11 11:45:21 +00002391 * For stale inodes we cannot rely on the backing buffer remaining
2392 * stale in cache for the remaining life of the stale inode and so
Christoph Hellwig475ee412012-07-03 12:21:22 -04002393 * xfs_imap_to_bp() below may give us a buffer that no longer contains
Dave Chinner4b6a4682010-01-11 11:45:21 +00002394 * inodes below. We have to check this after ensuring the inode is
2395 * unpinned so that it is safe to reclaim the stale inode after the
2396 * flush call.
2397 */
2398 if (xfs_iflags_test(ip, XFS_ISTALE)) {
2399 xfs_ifunlock(ip);
2400 return 0;
2401 }
2402
2403 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 * This may have been unpinned because the filesystem is shutting
2405 * down forcibly. If that's the case we must not write this inode
Christoph Hellwig32ce90a2012-04-23 15:58:32 +10002406 * to disk, because the log record didn't make it to disk.
2407 *
2408 * We also have to remove the log item from the AIL in this case,
2409 * as we wait for an empty AIL as part of the unmount process.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 */
2411 if (XFS_FORCED_SHUTDOWN(mp)) {
Christoph Hellwig32ce90a2012-04-23 15:58:32 +10002412 error = XFS_ERROR(EIO);
2413 goto abort_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 }
2415
2416 /*
David Chinnera3f74ff2008-03-06 13:43:42 +11002417 * Get the buffer containing the on-disk inode.
2418 */
Christoph Hellwig475ee412012-07-03 12:21:22 -04002419 error = xfs_imap_to_bp(mp, NULL, &ip->i_imap, &dip, &bp, XBF_TRYLOCK,
2420 0);
David Chinnera3f74ff2008-03-06 13:43:42 +11002421 if (error || !bp) {
2422 xfs_ifunlock(ip);
2423 return error;
2424 }
2425
2426 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 * First flush out the inode that xfs_iflush was called with.
2428 */
2429 error = xfs_iflush_int(ip, bp);
David Chinnerbad55842008-03-06 13:43:49 +11002430 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 goto corrupt_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432
2433 /*
David Chinnera3f74ff2008-03-06 13:43:42 +11002434 * If the buffer is pinned then push on the log now so we won't
2435 * get stuck waiting in the write for too long.
2436 */
Chandra Seetharaman811e64c2011-07-22 23:40:27 +00002437 if (xfs_buf_ispinned(bp))
Christoph Hellwiga14a3482010-01-19 09:56:46 +00002438 xfs_log_force(mp, 0);
David Chinnera3f74ff2008-03-06 13:43:42 +11002439
2440 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 * inode clustering:
2442 * see if other inodes can be gathered into this write
2443 */
David Chinnerbad55842008-03-06 13:43:49 +11002444 error = xfs_iflush_cluster(ip, bp);
2445 if (error)
2446 goto cluster_corrupt_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447
Christoph Hellwig4c468192012-04-23 15:58:36 +10002448 *bpp = bp;
2449 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450
2451corrupt_out:
2452 xfs_buf_relse(bp);
Nathan Scott7d04a332006-06-09 14:58:38 +10002453 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454cluster_corrupt_out:
Christoph Hellwig32ce90a2012-04-23 15:58:32 +10002455 error = XFS_ERROR(EFSCORRUPTED);
2456abort_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 /*
2458 * Unlocks the flush lock
2459 */
Dave Chinner04913fd2012-04-23 15:58:41 +10002460 xfs_iflush_abort(ip, false);
Christoph Hellwig32ce90a2012-04-23 15:58:32 +10002461 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462}
2463
2464
2465STATIC int
2466xfs_iflush_int(
2467 xfs_inode_t *ip,
2468 xfs_buf_t *bp)
2469{
2470 xfs_inode_log_item_t *iip;
2471 xfs_dinode_t *dip;
2472 xfs_mount_t *mp;
2473#ifdef XFS_TRANS_DEBUG
2474 int first;
2475#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476
Christoph Hellwig579aa9c2008-04-22 17:34:00 +10002477 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
Christoph Hellwig474fce02011-12-18 20:00:09 +00002478 ASSERT(xfs_isiflocked(ip));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE ||
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00002480 ip->i_d.di_nextents > XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481
2482 iip = ip->i_itemp;
2483 mp = ip->i_mount;
2484
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 /* set *dip = inode's place in the buffer */
Christoph Hellwig92bfc6e2008-11-28 14:23:41 +11002486 dip = (xfs_dinode_t *)xfs_buf_offset(bp, ip->i_imap.im_boffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487
Christoph Hellwig69ef9212011-07-08 14:36:05 +02002488 if (XFS_TEST_ERROR(dip->di_magic != cpu_to_be16(XFS_DINODE_MAGIC),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 mp, XFS_ERRTAG_IFLUSH_1, XFS_RANDOM_IFLUSH_1)) {
Dave Chinner6a19d932011-03-07 10:02:35 +11002490 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
2491 "%s: Bad inode %Lu magic number 0x%x, ptr 0x%p",
2492 __func__, ip->i_ino, be16_to_cpu(dip->di_magic), dip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 goto corrupt_out;
2494 }
2495 if (XFS_TEST_ERROR(ip->i_d.di_magic != XFS_DINODE_MAGIC,
2496 mp, XFS_ERRTAG_IFLUSH_2, XFS_RANDOM_IFLUSH_2)) {
Dave Chinner6a19d932011-03-07 10:02:35 +11002497 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
2498 "%s: Bad inode %Lu, ptr 0x%p, magic number 0x%x",
2499 __func__, ip->i_ino, ip, ip->i_d.di_magic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 goto corrupt_out;
2501 }
Al Viroabbede12011-07-26 02:31:30 -04002502 if (S_ISREG(ip->i_d.di_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 if (XFS_TEST_ERROR(
2504 (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS) &&
2505 (ip->i_d.di_format != XFS_DINODE_FMT_BTREE),
2506 mp, XFS_ERRTAG_IFLUSH_3, XFS_RANDOM_IFLUSH_3)) {
Dave Chinner6a19d932011-03-07 10:02:35 +11002507 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
2508 "%s: Bad regular inode %Lu, ptr 0x%p",
2509 __func__, ip->i_ino, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 goto corrupt_out;
2511 }
Al Viroabbede12011-07-26 02:31:30 -04002512 } else if (S_ISDIR(ip->i_d.di_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 if (XFS_TEST_ERROR(
2514 (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS) &&
2515 (ip->i_d.di_format != XFS_DINODE_FMT_BTREE) &&
2516 (ip->i_d.di_format != XFS_DINODE_FMT_LOCAL),
2517 mp, XFS_ERRTAG_IFLUSH_4, XFS_RANDOM_IFLUSH_4)) {
Dave Chinner6a19d932011-03-07 10:02:35 +11002518 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
2519 "%s: Bad directory inode %Lu, ptr 0x%p",
2520 __func__, ip->i_ino, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 goto corrupt_out;
2522 }
2523 }
2524 if (XFS_TEST_ERROR(ip->i_d.di_nextents + ip->i_d.di_anextents >
2525 ip->i_d.di_nblocks, mp, XFS_ERRTAG_IFLUSH_5,
2526 XFS_RANDOM_IFLUSH_5)) {
Dave Chinner6a19d932011-03-07 10:02:35 +11002527 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
2528 "%s: detected corrupt incore inode %Lu, "
2529 "total extents = %d, nblocks = %Ld, ptr 0x%p",
2530 __func__, ip->i_ino,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 ip->i_d.di_nextents + ip->i_d.di_anextents,
Dave Chinner6a19d932011-03-07 10:02:35 +11002532 ip->i_d.di_nblocks, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 goto corrupt_out;
2534 }
2535 if (XFS_TEST_ERROR(ip->i_d.di_forkoff > mp->m_sb.sb_inodesize,
2536 mp, XFS_ERRTAG_IFLUSH_6, XFS_RANDOM_IFLUSH_6)) {
Dave Chinner6a19d932011-03-07 10:02:35 +11002537 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
2538 "%s: bad inode %Lu, forkoff 0x%x, ptr 0x%p",
2539 __func__, ip->i_ino, ip->i_d.di_forkoff, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 goto corrupt_out;
2541 }
2542 /*
2543 * bump the flush iteration count, used to detect flushes which
2544 * postdate a log record during recovery.
2545 */
2546
2547 ip->i_d.di_flushiter++;
2548
2549 /*
2550 * Copy the dirty parts of the inode into the on-disk
2551 * inode. We always copy out the core of the inode,
2552 * because if the inode is dirty at all the core must
2553 * be.
2554 */
Christoph Hellwig81591fe2008-11-28 14:23:39 +11002555 xfs_dinode_to_disk(dip, &ip->i_d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556
2557 /* Wrap, we never let the log put out DI_MAX_FLUSH */
2558 if (ip->i_d.di_flushiter == DI_MAX_FLUSH)
2559 ip->i_d.di_flushiter = 0;
2560
2561 /*
2562 * If this is really an old format inode and the superblock version
2563 * has not been updated to support only new format inodes, then
2564 * convert back to the old inode format. If the superblock version
2565 * has been updated, then make the conversion permanent.
2566 */
Christoph Hellwig51ce16d2008-11-28 14:23:39 +11002567 ASSERT(ip->i_d.di_version == 1 || xfs_sb_version_hasnlink(&mp->m_sb));
2568 if (ip->i_d.di_version == 1) {
Eric Sandeen62118702008-03-06 13:44:28 +11002569 if (!xfs_sb_version_hasnlink(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 /*
2571 * Convert it back.
2572 */
2573 ASSERT(ip->i_d.di_nlink <= XFS_MAXLINK_1);
Christoph Hellwig81591fe2008-11-28 14:23:39 +11002574 dip->di_onlink = cpu_to_be16(ip->i_d.di_nlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 } else {
2576 /*
2577 * The superblock version has already been bumped,
2578 * so just make the conversion to the new inode
2579 * format permanent.
2580 */
Christoph Hellwig51ce16d2008-11-28 14:23:39 +11002581 ip->i_d.di_version = 2;
2582 dip->di_version = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 ip->i_d.di_onlink = 0;
Christoph Hellwig81591fe2008-11-28 14:23:39 +11002584 dip->di_onlink = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 memset(&(ip->i_d.di_pad[0]), 0, sizeof(ip->i_d.di_pad));
Christoph Hellwig81591fe2008-11-28 14:23:39 +11002586 memset(&(dip->di_pad[0]), 0,
2587 sizeof(dip->di_pad));
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +00002588 ASSERT(xfs_get_projid(ip) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 }
2590 }
2591
David Chinnere4ac9672008-04-10 12:23:58 +10002592 xfs_iflush_fork(ip, dip, iip, XFS_DATA_FORK, bp);
2593 if (XFS_IFORK_Q(ip))
2594 xfs_iflush_fork(ip, dip, iip, XFS_ATTR_FORK, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 xfs_inobp_check(mp, bp);
2596
2597 /*
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +00002598 * We've recorded everything logged in the inode, so we'd like to clear
2599 * the ili_fields bits so we don't log and flush things unnecessarily.
2600 * However, we can't stop logging all this information until the data
2601 * we've copied into the disk buffer is written to disk. If we did we
2602 * might overwrite the copy of the inode in the log with all the data
2603 * after re-logging only part of it, and in the face of a crash we
2604 * wouldn't have all the data we need to recover.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 *
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +00002606 * What we do is move the bits to the ili_last_fields field. When
2607 * logging the inode, these bits are moved back to the ili_fields field.
2608 * In the xfs_iflush_done() routine we clear ili_last_fields, since we
2609 * know that the information those bits represent is permanently on
2610 * disk. As long as the flush completes before the inode is logged
2611 * again, then both ili_fields and ili_last_fields will be cleared.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 *
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +00002613 * We can play with the ili_fields bits here, because the inode lock
2614 * must be held exclusively in order to set bits there and the flush
2615 * lock protects the ili_last_fields bits. Set ili_logged so the flush
2616 * done routine can tell whether or not to look in the AIL. Also, store
2617 * the current LSN of the inode so that we can tell whether the item has
2618 * moved in the AIL from xfs_iflush_done(). In order to read the lsn we
2619 * need the AIL lock, because it is a 64 bit value that cannot be read
2620 * atomically.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 */
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +00002622 if (iip != NULL && iip->ili_fields != 0) {
2623 iip->ili_last_fields = iip->ili_fields;
2624 iip->ili_fields = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 iip->ili_logged = 1;
2626
David Chinner7b2e2a32008-10-30 17:39:12 +11002627 xfs_trans_ail_copy_lsn(mp->m_ail, &iip->ili_flush_lsn,
2628 &iip->ili_item.li_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629
2630 /*
2631 * Attach the function xfs_iflush_done to the inode's
2632 * buffer. This will remove the inode from the AIL
2633 * and unlock the inode's flush lock when the inode is
2634 * completely written to disk.
2635 */
Christoph Hellwigca30b2a2010-06-23 18:11:15 +10002636 xfs_buf_attach_iodone(bp, xfs_iflush_done, &iip->ili_item);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637
Christoph Hellwigadadbee2011-07-13 13:43:49 +02002638 ASSERT(bp->b_fspriv != NULL);
Christoph Hellwigcb669ca2011-07-13 13:43:49 +02002639 ASSERT(bp->b_iodone != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 } else {
2641 /*
2642 * We're flushing an inode which is not in the AIL and has
Christoph Hellwig8a9c9982012-02-29 09:53:52 +00002643 * not been logged. For this case we can immediately drop
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 * the inode flush lock because we can avoid the whole
2645 * AIL state thing. It's OK to drop the flush lock now,
2646 * because we've already locked the buffer and to do anything
2647 * you really need both.
2648 */
2649 if (iip != NULL) {
2650 ASSERT(iip->ili_logged == 0);
2651 ASSERT(iip->ili_last_fields == 0);
2652 ASSERT((iip->ili_item.li_flags & XFS_LI_IN_AIL) == 0);
2653 }
2654 xfs_ifunlock(ip);
2655 }
2656
2657 return 0;
2658
2659corrupt_out:
2660 return XFS_ERROR(EFSCORRUPTED);
2661}
2662
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002663/*
2664 * Return a pointer to the extent record at file index idx.
2665 */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10002666xfs_bmbt_rec_host_t *
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002667xfs_iext_get_ext(
2668 xfs_ifork_t *ifp, /* inode fork pointer */
2669 xfs_extnum_t idx) /* index of target extent */
2670{
2671 ASSERT(idx >= 0);
Christoph Hellwig87bef182011-05-11 15:04:11 +00002672 ASSERT(idx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t));
2673
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11002674 if ((ifp->if_flags & XFS_IFEXTIREC) && (idx == 0)) {
2675 return ifp->if_u1.if_ext_irec->er_extbuf;
2676 } else if (ifp->if_flags & XFS_IFEXTIREC) {
2677 xfs_ext_irec_t *erp; /* irec pointer */
2678 int erp_idx = 0; /* irec index */
2679 xfs_extnum_t page_idx = idx; /* ext index in target list */
2680
2681 erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 0);
2682 return &erp->er_extbuf[page_idx];
2683 } else if (ifp->if_bytes) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002684 return &ifp->if_u1.if_extents[idx];
2685 } else {
2686 return NULL;
2687 }
2688}
2689
2690/*
2691 * Insert new item(s) into the extent records for incore inode
2692 * fork 'ifp'. 'count' new items are inserted at index 'idx'.
2693 */
2694void
2695xfs_iext_insert(
Christoph Hellwig6ef35542009-11-25 00:00:21 +00002696 xfs_inode_t *ip, /* incore inode pointer */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002697 xfs_extnum_t idx, /* starting index of new items */
2698 xfs_extnum_t count, /* number of inserted items */
Christoph Hellwig6ef35542009-11-25 00:00:21 +00002699 xfs_bmbt_irec_t *new, /* items to insert */
2700 int state) /* type of extent conversion */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002701{
Christoph Hellwig6ef35542009-11-25 00:00:21 +00002702 xfs_ifork_t *ifp = (state & BMAP_ATTRFORK) ? ip->i_afp : &ip->i_df;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002703 xfs_extnum_t i; /* extent record index */
2704
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002705 trace_xfs_iext_insert(ip, idx, new, state, _RET_IP_);
2706
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002707 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
2708 xfs_iext_add(ifp, idx, count);
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10002709 for (i = idx; i < idx + count; i++, new++)
2710 xfs_bmbt_set_all(xfs_iext_get_ext(ifp, i), new);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002711}
2712
2713/*
2714 * This is called when the amount of space required for incore file
2715 * extents needs to be increased. The ext_diff parameter stores the
2716 * number of new extents being added and the idx parameter contains
2717 * the extent index where the new extents will be added. If the new
2718 * extents are being appended, then we just need to (re)allocate and
2719 * initialize the space. Otherwise, if the new extents are being
2720 * inserted into the middle of the existing entries, a bit more work
2721 * is required to make room for the new extents to be inserted. The
2722 * caller is responsible for filling in the new extent entries upon
2723 * return.
2724 */
2725void
2726xfs_iext_add(
2727 xfs_ifork_t *ifp, /* inode fork pointer */
2728 xfs_extnum_t idx, /* index to begin adding exts */
Nathan Scottc41564b2006-03-29 08:55:14 +10002729 int ext_diff) /* number of extents to add */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002730{
2731 int byte_diff; /* new bytes being added */
2732 int new_size; /* size of extents after adding */
2733 xfs_extnum_t nextents; /* number of extents in file */
2734
2735 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
2736 ASSERT((idx >= 0) && (idx <= nextents));
2737 byte_diff = ext_diff * sizeof(xfs_bmbt_rec_t);
2738 new_size = ifp->if_bytes + byte_diff;
2739 /*
2740 * If the new number of extents (nextents + ext_diff)
2741 * fits inside the inode, then continue to use the inline
2742 * extent buffer.
2743 */
2744 if (nextents + ext_diff <= XFS_INLINE_EXTS) {
2745 if (idx < nextents) {
2746 memmove(&ifp->if_u2.if_inline_ext[idx + ext_diff],
2747 &ifp->if_u2.if_inline_ext[idx],
2748 (nextents - idx) * sizeof(xfs_bmbt_rec_t));
2749 memset(&ifp->if_u2.if_inline_ext[idx], 0, byte_diff);
2750 }
2751 ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext;
2752 ifp->if_real_bytes = 0;
2753 }
2754 /*
2755 * Otherwise use a linear (direct) extent list.
2756 * If the extents are currently inside the inode,
2757 * xfs_iext_realloc_direct will switch us from
2758 * inline to direct extent allocation mode.
2759 */
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11002760 else if (nextents + ext_diff <= XFS_LINEAR_EXTS) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002761 xfs_iext_realloc_direct(ifp, new_size);
2762 if (idx < nextents) {
2763 memmove(&ifp->if_u1.if_extents[idx + ext_diff],
2764 &ifp->if_u1.if_extents[idx],
2765 (nextents - idx) * sizeof(xfs_bmbt_rec_t));
2766 memset(&ifp->if_u1.if_extents[idx], 0, byte_diff);
2767 }
2768 }
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11002769 /* Indirection array */
2770 else {
2771 xfs_ext_irec_t *erp;
2772 int erp_idx = 0;
2773 int page_idx = idx;
2774
2775 ASSERT(nextents + ext_diff > XFS_LINEAR_EXTS);
2776 if (ifp->if_flags & XFS_IFEXTIREC) {
2777 erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 1);
2778 } else {
2779 xfs_iext_irec_init(ifp);
2780 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
2781 erp = ifp->if_u1.if_ext_irec;
2782 }
2783 /* Extents fit in target extent page */
2784 if (erp && erp->er_extcount + ext_diff <= XFS_LINEAR_EXTS) {
2785 if (page_idx < erp->er_extcount) {
2786 memmove(&erp->er_extbuf[page_idx + ext_diff],
2787 &erp->er_extbuf[page_idx],
2788 (erp->er_extcount - page_idx) *
2789 sizeof(xfs_bmbt_rec_t));
2790 memset(&erp->er_extbuf[page_idx], 0, byte_diff);
2791 }
2792 erp->er_extcount += ext_diff;
2793 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff);
2794 }
2795 /* Insert a new extent page */
2796 else if (erp) {
2797 xfs_iext_add_indirect_multi(ifp,
2798 erp_idx, page_idx, ext_diff);
2799 }
2800 /*
2801 * If extent(s) are being appended to the last page in
2802 * the indirection array and the new extent(s) don't fit
2803 * in the page, then erp is NULL and erp_idx is set to
2804 * the next index needed in the indirection array.
2805 */
2806 else {
2807 int count = ext_diff;
2808
2809 while (count) {
2810 erp = xfs_iext_irec_new(ifp, erp_idx);
2811 erp->er_extcount = count;
2812 count -= MIN(count, (int)XFS_LINEAR_EXTS);
2813 if (count) {
2814 erp_idx++;
2815 }
2816 }
2817 }
2818 }
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002819 ifp->if_bytes = new_size;
2820}
2821
2822/*
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11002823 * This is called when incore extents are being added to the indirection
2824 * array and the new extents do not fit in the target extent list. The
2825 * erp_idx parameter contains the irec index for the target extent list
2826 * in the indirection array, and the idx parameter contains the extent
2827 * index within the list. The number of extents being added is stored
2828 * in the count parameter.
2829 *
2830 * |-------| |-------|
2831 * | | | | idx - number of extents before idx
2832 * | idx | | count |
2833 * | | | | count - number of extents being inserted at idx
2834 * |-------| |-------|
2835 * | count | | nex2 | nex2 - number of extents after idx + count
2836 * |-------| |-------|
2837 */
2838void
2839xfs_iext_add_indirect_multi(
2840 xfs_ifork_t *ifp, /* inode fork pointer */
2841 int erp_idx, /* target extent irec index */
2842 xfs_extnum_t idx, /* index within target list */
2843 int count) /* new extents being added */
2844{
2845 int byte_diff; /* new bytes being added */
2846 xfs_ext_irec_t *erp; /* pointer to irec entry */
2847 xfs_extnum_t ext_diff; /* number of extents to add */
2848 xfs_extnum_t ext_cnt; /* new extents still needed */
2849 xfs_extnum_t nex2; /* extents after idx + count */
2850 xfs_bmbt_rec_t *nex2_ep = NULL; /* temp list for nex2 extents */
2851 int nlists; /* number of irec's (lists) */
2852
2853 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
2854 erp = &ifp->if_u1.if_ext_irec[erp_idx];
2855 nex2 = erp->er_extcount - idx;
2856 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
2857
2858 /*
2859 * Save second part of target extent list
2860 * (all extents past */
2861 if (nex2) {
2862 byte_diff = nex2 * sizeof(xfs_bmbt_rec_t);
David Chinner67850732008-08-13 16:02:51 +10002863 nex2_ep = (xfs_bmbt_rec_t *) kmem_alloc(byte_diff, KM_NOFS);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11002864 memmove(nex2_ep, &erp->er_extbuf[idx], byte_diff);
2865 erp->er_extcount -= nex2;
2866 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, -nex2);
2867 memset(&erp->er_extbuf[idx], 0, byte_diff);
2868 }
2869
2870 /*
2871 * Add the new extents to the end of the target
2872 * list, then allocate new irec record(s) and
2873 * extent buffer(s) as needed to store the rest
2874 * of the new extents.
2875 */
2876 ext_cnt = count;
2877 ext_diff = MIN(ext_cnt, (int)XFS_LINEAR_EXTS - erp->er_extcount);
2878 if (ext_diff) {
2879 erp->er_extcount += ext_diff;
2880 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff);
2881 ext_cnt -= ext_diff;
2882 }
2883 while (ext_cnt) {
2884 erp_idx++;
2885 erp = xfs_iext_irec_new(ifp, erp_idx);
2886 ext_diff = MIN(ext_cnt, (int)XFS_LINEAR_EXTS);
2887 erp->er_extcount = ext_diff;
2888 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff);
2889 ext_cnt -= ext_diff;
2890 }
2891
2892 /* Add nex2 extents back to indirection array */
2893 if (nex2) {
2894 xfs_extnum_t ext_avail;
2895 int i;
2896
2897 byte_diff = nex2 * sizeof(xfs_bmbt_rec_t);
2898 ext_avail = XFS_LINEAR_EXTS - erp->er_extcount;
2899 i = 0;
2900 /*
2901 * If nex2 extents fit in the current page, append
2902 * nex2_ep after the new extents.
2903 */
2904 if (nex2 <= ext_avail) {
2905 i = erp->er_extcount;
2906 }
2907 /*
2908 * Otherwise, check if space is available in the
2909 * next page.
2910 */
2911 else if ((erp_idx < nlists - 1) &&
2912 (nex2 <= (ext_avail = XFS_LINEAR_EXTS -
2913 ifp->if_u1.if_ext_irec[erp_idx+1].er_extcount))) {
2914 erp_idx++;
2915 erp++;
2916 /* Create a hole for nex2 extents */
2917 memmove(&erp->er_extbuf[nex2], erp->er_extbuf,
2918 erp->er_extcount * sizeof(xfs_bmbt_rec_t));
2919 }
2920 /*
2921 * Final choice, create a new extent page for
2922 * nex2 extents.
2923 */
2924 else {
2925 erp_idx++;
2926 erp = xfs_iext_irec_new(ifp, erp_idx);
2927 }
2928 memmove(&erp->er_extbuf[i], nex2_ep, byte_diff);
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10002929 kmem_free(nex2_ep);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11002930 erp->er_extcount += nex2;
2931 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, nex2);
2932 }
2933}
2934
2935/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002936 * This is called when the amount of space required for incore file
2937 * extents needs to be decreased. The ext_diff parameter stores the
2938 * number of extents to be removed and the idx parameter contains
2939 * the extent index where the extents will be removed from.
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11002940 *
2941 * If the amount of space needed has decreased below the linear
2942 * limit, XFS_IEXT_BUFSZ, then switch to using the contiguous
2943 * extent array. Otherwise, use kmem_realloc() to adjust the
2944 * size to what is needed.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002945 */
2946void
2947xfs_iext_remove(
Christoph Hellwig6ef35542009-11-25 00:00:21 +00002948 xfs_inode_t *ip, /* incore inode pointer */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002949 xfs_extnum_t idx, /* index to begin removing exts */
Christoph Hellwig6ef35542009-11-25 00:00:21 +00002950 int ext_diff, /* number of extents to remove */
2951 int state) /* type of extent conversion */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002952{
Christoph Hellwig6ef35542009-11-25 00:00:21 +00002953 xfs_ifork_t *ifp = (state & BMAP_ATTRFORK) ? ip->i_afp : &ip->i_df;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002954 xfs_extnum_t nextents; /* number of extents in file */
2955 int new_size; /* size of extents after removal */
2956
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002957 trace_xfs_iext_remove(ip, idx, state, _RET_IP_);
2958
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002959 ASSERT(ext_diff > 0);
2960 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
2961 new_size = (nextents - ext_diff) * sizeof(xfs_bmbt_rec_t);
2962
2963 if (new_size == 0) {
2964 xfs_iext_destroy(ifp);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11002965 } else if (ifp->if_flags & XFS_IFEXTIREC) {
2966 xfs_iext_remove_indirect(ifp, idx, ext_diff);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002967 } else if (ifp->if_real_bytes) {
2968 xfs_iext_remove_direct(ifp, idx, ext_diff);
2969 } else {
2970 xfs_iext_remove_inline(ifp, idx, ext_diff);
2971 }
2972 ifp->if_bytes = new_size;
2973}
2974
2975/*
2976 * This removes ext_diff extents from the inline buffer, beginning
2977 * at extent index idx.
2978 */
2979void
2980xfs_iext_remove_inline(
2981 xfs_ifork_t *ifp, /* inode fork pointer */
2982 xfs_extnum_t idx, /* index to begin removing exts */
2983 int ext_diff) /* number of extents to remove */
2984{
2985 int nextents; /* number of extents in file */
2986
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11002987 ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002988 ASSERT(idx < XFS_INLINE_EXTS);
2989 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
2990 ASSERT(((nextents - ext_diff) > 0) &&
2991 (nextents - ext_diff) < XFS_INLINE_EXTS);
2992
2993 if (idx + ext_diff < nextents) {
2994 memmove(&ifp->if_u2.if_inline_ext[idx],
2995 &ifp->if_u2.if_inline_ext[idx + ext_diff],
2996 (nextents - (idx + ext_diff)) *
2997 sizeof(xfs_bmbt_rec_t));
2998 memset(&ifp->if_u2.if_inline_ext[nextents - ext_diff],
2999 0, ext_diff * sizeof(xfs_bmbt_rec_t));
3000 } else {
3001 memset(&ifp->if_u2.if_inline_ext[idx], 0,
3002 ext_diff * sizeof(xfs_bmbt_rec_t));
3003 }
3004}
3005
3006/*
3007 * This removes ext_diff extents from a linear (direct) extent list,
3008 * beginning at extent index idx. If the extents are being removed
3009 * from the end of the list (ie. truncate) then we just need to re-
3010 * allocate the list to remove the extra space. Otherwise, if the
3011 * extents are being removed from the middle of the existing extent
3012 * entries, then we first need to move the extent records beginning
3013 * at idx + ext_diff up in the list to overwrite the records being
3014 * removed, then remove the extra space via kmem_realloc.
3015 */
3016void
3017xfs_iext_remove_direct(
3018 xfs_ifork_t *ifp, /* inode fork pointer */
3019 xfs_extnum_t idx, /* index to begin removing exts */
3020 int ext_diff) /* number of extents to remove */
3021{
3022 xfs_extnum_t nextents; /* number of extents in file */
3023 int new_size; /* size of extents after removal */
3024
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003025 ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003026 new_size = ifp->if_bytes -
3027 (ext_diff * sizeof(xfs_bmbt_rec_t));
3028 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3029
3030 if (new_size == 0) {
3031 xfs_iext_destroy(ifp);
3032 return;
3033 }
3034 /* Move extents up in the list (if needed) */
3035 if (idx + ext_diff < nextents) {
3036 memmove(&ifp->if_u1.if_extents[idx],
3037 &ifp->if_u1.if_extents[idx + ext_diff],
3038 (nextents - (idx + ext_diff)) *
3039 sizeof(xfs_bmbt_rec_t));
3040 }
3041 memset(&ifp->if_u1.if_extents[nextents - ext_diff],
3042 0, ext_diff * sizeof(xfs_bmbt_rec_t));
3043 /*
3044 * Reallocate the direct extent list. If the extents
3045 * will fit inside the inode then xfs_iext_realloc_direct
3046 * will switch from direct to inline extent allocation
3047 * mode for us.
3048 */
3049 xfs_iext_realloc_direct(ifp, new_size);
3050 ifp->if_bytes = new_size;
3051}
3052
3053/*
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003054 * This is called when incore extents are being removed from the
3055 * indirection array and the extents being removed span multiple extent
3056 * buffers. The idx parameter contains the file extent index where we
3057 * want to begin removing extents, and the count parameter contains
3058 * how many extents need to be removed.
3059 *
3060 * |-------| |-------|
3061 * | nex1 | | | nex1 - number of extents before idx
3062 * |-------| | count |
3063 * | | | | count - number of extents being removed at idx
3064 * | count | |-------|
3065 * | | | nex2 | nex2 - number of extents after idx + count
3066 * |-------| |-------|
3067 */
3068void
3069xfs_iext_remove_indirect(
3070 xfs_ifork_t *ifp, /* inode fork pointer */
3071 xfs_extnum_t idx, /* index to begin removing extents */
3072 int count) /* number of extents to remove */
3073{
3074 xfs_ext_irec_t *erp; /* indirection array pointer */
3075 int erp_idx = 0; /* indirection array index */
3076 xfs_extnum_t ext_cnt; /* extents left to remove */
3077 xfs_extnum_t ext_diff; /* extents to remove in current list */
3078 xfs_extnum_t nex1; /* number of extents before idx */
3079 xfs_extnum_t nex2; /* extents after idx + count */
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003080 int page_idx = idx; /* index in target extent list */
3081
3082 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3083 erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 0);
3084 ASSERT(erp != NULL);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003085 nex1 = page_idx;
3086 ext_cnt = count;
3087 while (ext_cnt) {
3088 nex2 = MAX((erp->er_extcount - (nex1 + ext_cnt)), 0);
3089 ext_diff = MIN(ext_cnt, (erp->er_extcount - nex1));
3090 /*
3091 * Check for deletion of entire list;
3092 * xfs_iext_irec_remove() updates extent offsets.
3093 */
3094 if (ext_diff == erp->er_extcount) {
3095 xfs_iext_irec_remove(ifp, erp_idx);
3096 ext_cnt -= ext_diff;
3097 nex1 = 0;
3098 if (ext_cnt) {
3099 ASSERT(erp_idx < ifp->if_real_bytes /
3100 XFS_IEXT_BUFSZ);
3101 erp = &ifp->if_u1.if_ext_irec[erp_idx];
3102 nex1 = 0;
3103 continue;
3104 } else {
3105 break;
3106 }
3107 }
3108 /* Move extents up (if needed) */
3109 if (nex2) {
3110 memmove(&erp->er_extbuf[nex1],
3111 &erp->er_extbuf[nex1 + ext_diff],
3112 nex2 * sizeof(xfs_bmbt_rec_t));
3113 }
3114 /* Zero out rest of page */
3115 memset(&erp->er_extbuf[nex1 + nex2], 0, (XFS_IEXT_BUFSZ -
3116 ((nex1 + nex2) * sizeof(xfs_bmbt_rec_t))));
3117 /* Update remaining counters */
3118 erp->er_extcount -= ext_diff;
3119 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, -ext_diff);
3120 ext_cnt -= ext_diff;
3121 nex1 = 0;
3122 erp_idx++;
3123 erp++;
3124 }
3125 ifp->if_bytes -= count * sizeof(xfs_bmbt_rec_t);
3126 xfs_iext_irec_compact(ifp);
3127}
3128
3129/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003130 * Create, destroy, or resize a linear (direct) block of extents.
3131 */
3132void
3133xfs_iext_realloc_direct(
3134 xfs_ifork_t *ifp, /* inode fork pointer */
3135 int new_size) /* new size of extents */
3136{
3137 int rnew_size; /* real new size of extents */
3138
3139 rnew_size = new_size;
3140
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003141 ASSERT(!(ifp->if_flags & XFS_IFEXTIREC) ||
3142 ((new_size >= 0) && (new_size <= XFS_IEXT_BUFSZ) &&
3143 (new_size != ifp->if_real_bytes)));
3144
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003145 /* Free extent records */
3146 if (new_size == 0) {
3147 xfs_iext_destroy(ifp);
3148 }
3149 /* Resize direct extent list and zero any new bytes */
3150 else if (ifp->if_real_bytes) {
3151 /* Check if extents will fit inside the inode */
3152 if (new_size <= XFS_INLINE_EXTS * sizeof(xfs_bmbt_rec_t)) {
3153 xfs_iext_direct_to_inline(ifp, new_size /
3154 (uint)sizeof(xfs_bmbt_rec_t));
3155 ifp->if_bytes = new_size;
3156 return;
3157 }
Vignesh Babu16a087d2007-06-28 16:46:37 +10003158 if (!is_power_of_2(new_size)){
Robert P. J. Day40ebd812007-11-23 16:30:51 +11003159 rnew_size = roundup_pow_of_two(new_size);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003160 }
3161 if (rnew_size != ifp->if_real_bytes) {
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003162 ifp->if_u1.if_extents =
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003163 kmem_realloc(ifp->if_u1.if_extents,
3164 rnew_size,
David Chinner67850732008-08-13 16:02:51 +10003165 ifp->if_real_bytes, KM_NOFS);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003166 }
3167 if (rnew_size > ifp->if_real_bytes) {
3168 memset(&ifp->if_u1.if_extents[ifp->if_bytes /
3169 (uint)sizeof(xfs_bmbt_rec_t)], 0,
3170 rnew_size - ifp->if_real_bytes);
3171 }
3172 }
3173 /*
3174 * Switch from the inline extent buffer to a direct
3175 * extent list. Be sure to include the inline extent
3176 * bytes in new_size.
3177 */
3178 else {
3179 new_size += ifp->if_bytes;
Vignesh Babu16a087d2007-06-28 16:46:37 +10003180 if (!is_power_of_2(new_size)) {
Robert P. J. Day40ebd812007-11-23 16:30:51 +11003181 rnew_size = roundup_pow_of_two(new_size);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003182 }
3183 xfs_iext_inline_to_direct(ifp, rnew_size);
3184 }
3185 ifp->if_real_bytes = rnew_size;
3186 ifp->if_bytes = new_size;
3187}
3188
3189/*
3190 * Switch from linear (direct) extent records to inline buffer.
3191 */
3192void
3193xfs_iext_direct_to_inline(
3194 xfs_ifork_t *ifp, /* inode fork pointer */
3195 xfs_extnum_t nextents) /* number of extents in file */
3196{
3197 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
3198 ASSERT(nextents <= XFS_INLINE_EXTS);
3199 /*
3200 * The inline buffer was zeroed when we switched
3201 * from inline to direct extent allocation mode,
3202 * so we don't need to clear it here.
3203 */
3204 memcpy(ifp->if_u2.if_inline_ext, ifp->if_u1.if_extents,
3205 nextents * sizeof(xfs_bmbt_rec_t));
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10003206 kmem_free(ifp->if_u1.if_extents);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003207 ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext;
3208 ifp->if_real_bytes = 0;
3209}
3210
3211/*
3212 * Switch from inline buffer to linear (direct) extent records.
3213 * new_size should already be rounded up to the next power of 2
3214 * by the caller (when appropriate), so use new_size as it is.
3215 * However, since new_size may be rounded up, we can't update
3216 * if_bytes here. It is the caller's responsibility to update
3217 * if_bytes upon return.
3218 */
3219void
3220xfs_iext_inline_to_direct(
3221 xfs_ifork_t *ifp, /* inode fork pointer */
3222 int new_size) /* number of extents in file */
3223{
David Chinner67850732008-08-13 16:02:51 +10003224 ifp->if_u1.if_extents = kmem_alloc(new_size, KM_NOFS);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003225 memset(ifp->if_u1.if_extents, 0, new_size);
3226 if (ifp->if_bytes) {
3227 memcpy(ifp->if_u1.if_extents, ifp->if_u2.if_inline_ext,
3228 ifp->if_bytes);
3229 memset(ifp->if_u2.if_inline_ext, 0, XFS_INLINE_EXTS *
3230 sizeof(xfs_bmbt_rec_t));
3231 }
3232 ifp->if_real_bytes = new_size;
3233}
3234
3235/*
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003236 * Resize an extent indirection array to new_size bytes.
3237 */
Eric Sandeend96f8f82009-07-02 00:09:33 -05003238STATIC void
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003239xfs_iext_realloc_indirect(
3240 xfs_ifork_t *ifp, /* inode fork pointer */
3241 int new_size) /* new indirection array size */
3242{
3243 int nlists; /* number of irec's (ex lists) */
3244 int size; /* current indirection array size */
3245
3246 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3247 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3248 size = nlists * sizeof(xfs_ext_irec_t);
3249 ASSERT(ifp->if_real_bytes);
3250 ASSERT((new_size >= 0) && (new_size != size));
3251 if (new_size == 0) {
3252 xfs_iext_destroy(ifp);
3253 } else {
3254 ifp->if_u1.if_ext_irec = (xfs_ext_irec_t *)
3255 kmem_realloc(ifp->if_u1.if_ext_irec,
David Chinner67850732008-08-13 16:02:51 +10003256 new_size, size, KM_NOFS);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003257 }
3258}
3259
3260/*
3261 * Switch from indirection array to linear (direct) extent allocations.
3262 */
Eric Sandeend96f8f82009-07-02 00:09:33 -05003263STATIC void
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003264xfs_iext_indirect_to_direct(
3265 xfs_ifork_t *ifp) /* inode fork pointer */
3266{
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003267 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003268 xfs_extnum_t nextents; /* number of extents in file */
3269 int size; /* size of file extents */
3270
3271 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3272 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3273 ASSERT(nextents <= XFS_LINEAR_EXTS);
3274 size = nextents * sizeof(xfs_bmbt_rec_t);
3275
Lachlan McIlroy71a8c872008-09-26 12:17:57 +10003276 xfs_iext_irec_compact_pages(ifp);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003277 ASSERT(ifp->if_real_bytes == XFS_IEXT_BUFSZ);
3278
3279 ep = ifp->if_u1.if_ext_irec->er_extbuf;
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10003280 kmem_free(ifp->if_u1.if_ext_irec);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003281 ifp->if_flags &= ~XFS_IFEXTIREC;
3282 ifp->if_u1.if_extents = ep;
3283 ifp->if_bytes = size;
3284 if (nextents < XFS_LINEAR_EXTS) {
3285 xfs_iext_realloc_direct(ifp, size);
3286 }
3287}
3288
3289/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003290 * Free incore file extents.
3291 */
3292void
3293xfs_iext_destroy(
3294 xfs_ifork_t *ifp) /* inode fork pointer */
3295{
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003296 if (ifp->if_flags & XFS_IFEXTIREC) {
3297 int erp_idx;
3298 int nlists;
3299
3300 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3301 for (erp_idx = nlists - 1; erp_idx >= 0 ; erp_idx--) {
3302 xfs_iext_irec_remove(ifp, erp_idx);
3303 }
3304 ifp->if_flags &= ~XFS_IFEXTIREC;
3305 } else if (ifp->if_real_bytes) {
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10003306 kmem_free(ifp->if_u1.if_extents);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003307 } else if (ifp->if_bytes) {
3308 memset(ifp->if_u2.if_inline_ext, 0, XFS_INLINE_EXTS *
3309 sizeof(xfs_bmbt_rec_t));
3310 }
3311 ifp->if_u1.if_extents = NULL;
3312 ifp->if_real_bytes = 0;
3313 ifp->if_bytes = 0;
3314}
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003315
3316/*
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003317 * Return a pointer to the extent record for file system block bno.
3318 */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003319xfs_bmbt_rec_host_t * /* pointer to found extent record */
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003320xfs_iext_bno_to_ext(
3321 xfs_ifork_t *ifp, /* inode fork pointer */
3322 xfs_fileoff_t bno, /* block number to search for */
3323 xfs_extnum_t *idxp) /* index of target extent */
3324{
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003325 xfs_bmbt_rec_host_t *base; /* pointer to first extent */
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003326 xfs_filblks_t blockcount = 0; /* number of blocks in extent */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003327 xfs_bmbt_rec_host_t *ep = NULL; /* pointer to target extent */
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003328 xfs_ext_irec_t *erp = NULL; /* indirection array pointer */
Nathan Scottc41564b2006-03-29 08:55:14 +10003329 int high; /* upper boundary in search */
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003330 xfs_extnum_t idx = 0; /* index of target extent */
Nathan Scottc41564b2006-03-29 08:55:14 +10003331 int low; /* lower boundary in search */
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003332 xfs_extnum_t nextents; /* number of file extents */
3333 xfs_fileoff_t startoff = 0; /* start offset of extent */
3334
3335 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3336 if (nextents == 0) {
3337 *idxp = 0;
3338 return NULL;
3339 }
3340 low = 0;
3341 if (ifp->if_flags & XFS_IFEXTIREC) {
3342 /* Find target extent list */
3343 int erp_idx = 0;
3344 erp = xfs_iext_bno_to_irec(ifp, bno, &erp_idx);
3345 base = erp->er_extbuf;
3346 high = erp->er_extcount - 1;
3347 } else {
3348 base = ifp->if_u1.if_extents;
3349 high = nextents - 1;
3350 }
3351 /* Binary search extent records */
3352 while (low <= high) {
3353 idx = (low + high) >> 1;
3354 ep = base + idx;
3355 startoff = xfs_bmbt_get_startoff(ep);
3356 blockcount = xfs_bmbt_get_blockcount(ep);
3357 if (bno < startoff) {
3358 high = idx - 1;
3359 } else if (bno >= startoff + blockcount) {
3360 low = idx + 1;
3361 } else {
3362 /* Convert back to file-based extent index */
3363 if (ifp->if_flags & XFS_IFEXTIREC) {
3364 idx += erp->er_extoff;
3365 }
3366 *idxp = idx;
3367 return ep;
3368 }
3369 }
3370 /* Convert back to file-based extent index */
3371 if (ifp->if_flags & XFS_IFEXTIREC) {
3372 idx += erp->er_extoff;
3373 }
3374 if (bno >= startoff + blockcount) {
3375 if (++idx == nextents) {
3376 ep = NULL;
3377 } else {
3378 ep = xfs_iext_get_ext(ifp, idx);
3379 }
3380 }
3381 *idxp = idx;
3382 return ep;
3383}
3384
3385/*
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003386 * Return a pointer to the indirection array entry containing the
3387 * extent record for filesystem block bno. Store the index of the
3388 * target irec in *erp_idxp.
3389 */
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003390xfs_ext_irec_t * /* pointer to found extent record */
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003391xfs_iext_bno_to_irec(
3392 xfs_ifork_t *ifp, /* inode fork pointer */
3393 xfs_fileoff_t bno, /* block number to search for */
3394 int *erp_idxp) /* irec index of target ext list */
3395{
3396 xfs_ext_irec_t *erp = NULL; /* indirection array pointer */
3397 xfs_ext_irec_t *erp_next; /* next indirection array entry */
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003398 int erp_idx; /* indirection array index */
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003399 int nlists; /* number of extent irec's (lists) */
3400 int high; /* binary search upper limit */
3401 int low; /* binary search lower limit */
3402
3403 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3404 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3405 erp_idx = 0;
3406 low = 0;
3407 high = nlists - 1;
3408 while (low <= high) {
3409 erp_idx = (low + high) >> 1;
3410 erp = &ifp->if_u1.if_ext_irec[erp_idx];
3411 erp_next = erp_idx < nlists - 1 ? erp + 1 : NULL;
3412 if (bno < xfs_bmbt_get_startoff(erp->er_extbuf)) {
3413 high = erp_idx - 1;
3414 } else if (erp_next && bno >=
3415 xfs_bmbt_get_startoff(erp_next->er_extbuf)) {
3416 low = erp_idx + 1;
3417 } else {
3418 break;
3419 }
3420 }
3421 *erp_idxp = erp_idx;
3422 return erp;
3423}
3424
3425/*
3426 * Return a pointer to the indirection array entry containing the
3427 * extent record at file extent index *idxp. Store the index of the
3428 * target irec in *erp_idxp and store the page index of the target
3429 * extent record in *idxp.
3430 */
3431xfs_ext_irec_t *
3432xfs_iext_idx_to_irec(
3433 xfs_ifork_t *ifp, /* inode fork pointer */
3434 xfs_extnum_t *idxp, /* extent index (file -> page) */
3435 int *erp_idxp, /* pointer to target irec */
3436 int realloc) /* new bytes were just added */
3437{
3438 xfs_ext_irec_t *prev; /* pointer to previous irec */
3439 xfs_ext_irec_t *erp = NULL; /* pointer to current irec */
3440 int erp_idx; /* indirection array index */
3441 int nlists; /* number of irec's (ex lists) */
3442 int high; /* binary search upper limit */
3443 int low; /* binary search lower limit */
3444 xfs_extnum_t page_idx = *idxp; /* extent index in target list */
3445
3446 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
Christoph Hellwig87bef182011-05-11 15:04:11 +00003447 ASSERT(page_idx >= 0);
3448 ASSERT(page_idx <= ifp->if_bytes / sizeof(xfs_bmbt_rec_t));
3449 ASSERT(page_idx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t) || realloc);
3450
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003451 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3452 erp_idx = 0;
3453 low = 0;
3454 high = nlists - 1;
3455
3456 /* Binary search extent irec's */
3457 while (low <= high) {
3458 erp_idx = (low + high) >> 1;
3459 erp = &ifp->if_u1.if_ext_irec[erp_idx];
3460 prev = erp_idx > 0 ? erp - 1 : NULL;
3461 if (page_idx < erp->er_extoff || (page_idx == erp->er_extoff &&
3462 realloc && prev && prev->er_extcount < XFS_LINEAR_EXTS)) {
3463 high = erp_idx - 1;
3464 } else if (page_idx > erp->er_extoff + erp->er_extcount ||
3465 (page_idx == erp->er_extoff + erp->er_extcount &&
3466 !realloc)) {
3467 low = erp_idx + 1;
3468 } else if (page_idx == erp->er_extoff + erp->er_extcount &&
3469 erp->er_extcount == XFS_LINEAR_EXTS) {
3470 ASSERT(realloc);
3471 page_idx = 0;
3472 erp_idx++;
3473 erp = erp_idx < nlists ? erp + 1 : NULL;
3474 break;
3475 } else {
3476 page_idx -= erp->er_extoff;
3477 break;
3478 }
3479 }
3480 *idxp = page_idx;
3481 *erp_idxp = erp_idx;
3482 return(erp);
3483}
3484
3485/*
3486 * Allocate and initialize an indirection array once the space needed
3487 * for incore extents increases above XFS_IEXT_BUFSZ.
3488 */
3489void
3490xfs_iext_irec_init(
3491 xfs_ifork_t *ifp) /* inode fork pointer */
3492{
3493 xfs_ext_irec_t *erp; /* indirection array pointer */
3494 xfs_extnum_t nextents; /* number of extents in file */
3495
3496 ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
3497 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3498 ASSERT(nextents <= XFS_LINEAR_EXTS);
3499
David Chinner67850732008-08-13 16:02:51 +10003500 erp = kmem_alloc(sizeof(xfs_ext_irec_t), KM_NOFS);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003501
3502 if (nextents == 0) {
David Chinner67850732008-08-13 16:02:51 +10003503 ifp->if_u1.if_extents = kmem_alloc(XFS_IEXT_BUFSZ, KM_NOFS);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003504 } else if (!ifp->if_real_bytes) {
3505 xfs_iext_inline_to_direct(ifp, XFS_IEXT_BUFSZ);
3506 } else if (ifp->if_real_bytes < XFS_IEXT_BUFSZ) {
3507 xfs_iext_realloc_direct(ifp, XFS_IEXT_BUFSZ);
3508 }
3509 erp->er_extbuf = ifp->if_u1.if_extents;
3510 erp->er_extcount = nextents;
3511 erp->er_extoff = 0;
3512
3513 ifp->if_flags |= XFS_IFEXTIREC;
3514 ifp->if_real_bytes = XFS_IEXT_BUFSZ;
3515 ifp->if_bytes = nextents * sizeof(xfs_bmbt_rec_t);
3516 ifp->if_u1.if_ext_irec = erp;
3517
3518 return;
3519}
3520
3521/*
3522 * Allocate and initialize a new entry in the indirection array.
3523 */
3524xfs_ext_irec_t *
3525xfs_iext_irec_new(
3526 xfs_ifork_t *ifp, /* inode fork pointer */
3527 int erp_idx) /* index for new irec */
3528{
3529 xfs_ext_irec_t *erp; /* indirection array pointer */
3530 int i; /* loop counter */
3531 int nlists; /* number of irec's (ex lists) */
3532
3533 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3534 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3535
3536 /* Resize indirection array */
3537 xfs_iext_realloc_indirect(ifp, ++nlists *
3538 sizeof(xfs_ext_irec_t));
3539 /*
3540 * Move records down in the array so the
3541 * new page can use erp_idx.
3542 */
3543 erp = ifp->if_u1.if_ext_irec;
3544 for (i = nlists - 1; i > erp_idx; i--) {
3545 memmove(&erp[i], &erp[i-1], sizeof(xfs_ext_irec_t));
3546 }
3547 ASSERT(i == erp_idx);
3548
3549 /* Initialize new extent record */
3550 erp = ifp->if_u1.if_ext_irec;
David Chinner67850732008-08-13 16:02:51 +10003551 erp[erp_idx].er_extbuf = kmem_alloc(XFS_IEXT_BUFSZ, KM_NOFS);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003552 ifp->if_real_bytes = nlists * XFS_IEXT_BUFSZ;
3553 memset(erp[erp_idx].er_extbuf, 0, XFS_IEXT_BUFSZ);
3554 erp[erp_idx].er_extcount = 0;
3555 erp[erp_idx].er_extoff = erp_idx > 0 ?
3556 erp[erp_idx-1].er_extoff + erp[erp_idx-1].er_extcount : 0;
3557 return (&erp[erp_idx]);
3558}
3559
3560/*
3561 * Remove a record from the indirection array.
3562 */
3563void
3564xfs_iext_irec_remove(
3565 xfs_ifork_t *ifp, /* inode fork pointer */
3566 int erp_idx) /* irec index to remove */
3567{
3568 xfs_ext_irec_t *erp; /* indirection array pointer */
3569 int i; /* loop counter */
3570 int nlists; /* number of irec's (ex lists) */
3571
3572 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3573 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3574 erp = &ifp->if_u1.if_ext_irec[erp_idx];
3575 if (erp->er_extbuf) {
3576 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1,
3577 -erp->er_extcount);
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10003578 kmem_free(erp->er_extbuf);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003579 }
3580 /* Compact extent records */
3581 erp = ifp->if_u1.if_ext_irec;
3582 for (i = erp_idx; i < nlists - 1; i++) {
3583 memmove(&erp[i], &erp[i+1], sizeof(xfs_ext_irec_t));
3584 }
3585 /*
3586 * Manually free the last extent record from the indirection
3587 * array. A call to xfs_iext_realloc_indirect() with a size
3588 * of zero would result in a call to xfs_iext_destroy() which
3589 * would in turn call this function again, creating a nasty
3590 * infinite loop.
3591 */
3592 if (--nlists) {
3593 xfs_iext_realloc_indirect(ifp,
3594 nlists * sizeof(xfs_ext_irec_t));
3595 } else {
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10003596 kmem_free(ifp->if_u1.if_ext_irec);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003597 }
3598 ifp->if_real_bytes = nlists * XFS_IEXT_BUFSZ;
3599}
3600
3601/*
3602 * This is called to clean up large amounts of unused memory allocated
3603 * by the indirection array. Before compacting anything though, verify
3604 * that the indirection array is still needed and switch back to the
3605 * linear extent list (or even the inline buffer) if possible. The
3606 * compaction policy is as follows:
3607 *
3608 * Full Compaction: Extents fit into a single page (or inline buffer)
Lachlan McIlroy71a8c872008-09-26 12:17:57 +10003609 * Partial Compaction: Extents occupy less than 50% of allocated space
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003610 * No Compaction: Extents occupy at least 50% of allocated space
3611 */
3612void
3613xfs_iext_irec_compact(
3614 xfs_ifork_t *ifp) /* inode fork pointer */
3615{
3616 xfs_extnum_t nextents; /* number of extents in file */
3617 int nlists; /* number of irec's (ex lists) */
3618
3619 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3620 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3621 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3622
3623 if (nextents == 0) {
3624 xfs_iext_destroy(ifp);
3625 } else if (nextents <= XFS_INLINE_EXTS) {
3626 xfs_iext_indirect_to_direct(ifp);
3627 xfs_iext_direct_to_inline(ifp, nextents);
3628 } else if (nextents <= XFS_LINEAR_EXTS) {
3629 xfs_iext_indirect_to_direct(ifp);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003630 } else if (nextents < (nlists * XFS_LINEAR_EXTS) >> 1) {
3631 xfs_iext_irec_compact_pages(ifp);
3632 }
3633}
3634
3635/*
3636 * Combine extents from neighboring extent pages.
3637 */
3638void
3639xfs_iext_irec_compact_pages(
3640 xfs_ifork_t *ifp) /* inode fork pointer */
3641{
3642 xfs_ext_irec_t *erp, *erp_next;/* pointers to irec entries */
3643 int erp_idx = 0; /* indirection array index */
3644 int nlists; /* number of irec's (ex lists) */
3645
3646 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3647 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3648 while (erp_idx < nlists - 1) {
3649 erp = &ifp->if_u1.if_ext_irec[erp_idx];
3650 erp_next = erp + 1;
3651 if (erp_next->er_extcount <=
3652 (XFS_LINEAR_EXTS - erp->er_extcount)) {
Lachlan McIlroy71a8c872008-09-26 12:17:57 +10003653 memcpy(&erp->er_extbuf[erp->er_extcount],
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003654 erp_next->er_extbuf, erp_next->er_extcount *
3655 sizeof(xfs_bmbt_rec_t));
3656 erp->er_extcount += erp_next->er_extcount;
3657 /*
3658 * Free page before removing extent record
3659 * so er_extoffs don't get modified in
3660 * xfs_iext_irec_remove.
3661 */
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10003662 kmem_free(erp_next->er_extbuf);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003663 erp_next->er_extbuf = NULL;
3664 xfs_iext_irec_remove(ifp, erp_idx + 1);
3665 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3666 } else {
3667 erp_idx++;
3668 }
3669 }
3670}
3671
3672/*
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003673 * This is called to update the er_extoff field in the indirection
3674 * array when extents have been added or removed from one of the
3675 * extent lists. erp_idx contains the irec index to begin updating
3676 * at and ext_diff contains the number of extents that were added
3677 * or removed.
3678 */
3679void
3680xfs_iext_irec_update_extoffs(
3681 xfs_ifork_t *ifp, /* inode fork pointer */
3682 int erp_idx, /* irec index to update */
3683 int ext_diff) /* number of new extents */
3684{
3685 int i; /* loop counter */
3686 int nlists; /* number of irec's (ex lists */
3687
3688 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3689 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3690 for (i = erp_idx; i < nlists; i++) {
3691 ifp->if_u1.if_ext_irec[i].er_extoff += ext_diff;
3692 }
3693}