blob: 5c10825f2f80f8d0508b7056def9034977c0c2ce [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 * Move inode type and inode format specific information from the
220 * on-disk inode to the in-core inode. For fifos, devs, and sockets
221 * this means set if_rdev to the proper value. For files, directories,
222 * and symlinks this means to bring in the in-line data or extent
223 * pointers. For a file in B-tree format, only the root is immediately
224 * brought in-core. The rest will be in-lined in if_extents when it
225 * is first referenced (see xfs_iread_extents()).
226 */
227STATIC int
228xfs_iformat(
229 xfs_inode_t *ip,
230 xfs_dinode_t *dip)
231{
232 xfs_attr_shortform_t *atp;
233 int size;
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000234 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 xfs_fsize_t di_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100237 if (unlikely(be32_to_cpu(dip->di_nextents) +
238 be16_to_cpu(dip->di_anextents) >
239 be64_to_cpu(dip->di_nblocks))) {
Dave Chinner65333b42011-03-07 10:03:35 +1100240 xfs_warn(ip->i_mount,
Nathan Scott3762ec62006-01-12 10:29:53 +1100241 "corrupt dinode %Lu, extent total = %d, nblocks = %Lu.",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 (unsigned long long)ip->i_ino,
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100243 (int)(be32_to_cpu(dip->di_nextents) +
244 be16_to_cpu(dip->di_anextents)),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 (unsigned long long)
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100246 be64_to_cpu(dip->di_nblocks));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 XFS_CORRUPTION_ERROR("xfs_iformat(1)", XFS_ERRLEVEL_LOW,
248 ip->i_mount, dip);
249 return XFS_ERROR(EFSCORRUPTED);
250 }
251
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100252 if (unlikely(dip->di_forkoff > ip->i_mount->m_sb.sb_inodesize)) {
Dave Chinner65333b42011-03-07 10:03:35 +1100253 xfs_warn(ip->i_mount, "corrupt dinode %Lu, forkoff = 0x%x.",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 (unsigned long long)ip->i_ino,
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100255 dip->di_forkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 XFS_CORRUPTION_ERROR("xfs_iformat(2)", XFS_ERRLEVEL_LOW,
257 ip->i_mount, dip);
258 return XFS_ERROR(EFSCORRUPTED);
259 }
260
Christoph Hellwigb89d4202009-08-10 11:32:18 -0300261 if (unlikely((ip->i_d.di_flags & XFS_DIFLAG_REALTIME) &&
262 !ip->i_mount->m_rtdev_targp)) {
Dave Chinner65333b42011-03-07 10:03:35 +1100263 xfs_warn(ip->i_mount,
Christoph Hellwigb89d4202009-08-10 11:32:18 -0300264 "corrupt dinode %Lu, has realtime flag set.",
265 ip->i_ino);
266 XFS_CORRUPTION_ERROR("xfs_iformat(realtime)",
267 XFS_ERRLEVEL_LOW, ip->i_mount, dip);
268 return XFS_ERROR(EFSCORRUPTED);
269 }
270
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 switch (ip->i_d.di_mode & S_IFMT) {
272 case S_IFIFO:
273 case S_IFCHR:
274 case S_IFBLK:
275 case S_IFSOCK:
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100276 if (unlikely(dip->di_format != XFS_DINODE_FMT_DEV)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 XFS_CORRUPTION_ERROR("xfs_iformat(3)", XFS_ERRLEVEL_LOW,
278 ip->i_mount, dip);
279 return XFS_ERROR(EFSCORRUPTED);
280 }
281 ip->i_d.di_size = 0;
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100282 ip->i_df.if_u2.if_rdev = xfs_dinode_get_rdev(dip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 break;
284
285 case S_IFREG:
286 case S_IFLNK:
287 case S_IFDIR:
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100288 switch (dip->di_format) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 case XFS_DINODE_FMT_LOCAL:
290 /*
291 * no local regular files yet
292 */
Al Viroabbede12011-07-26 02:31:30 -0400293 if (unlikely(S_ISREG(be16_to_cpu(dip->di_mode)))) {
Dave Chinner65333b42011-03-07 10:03:35 +1100294 xfs_warn(ip->i_mount,
295 "corrupt inode %Lu (local format for regular file).",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 (unsigned long long) ip->i_ino);
297 XFS_CORRUPTION_ERROR("xfs_iformat(4)",
298 XFS_ERRLEVEL_LOW,
299 ip->i_mount, dip);
300 return XFS_ERROR(EFSCORRUPTED);
301 }
302
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100303 di_size = be64_to_cpu(dip->di_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 if (unlikely(di_size > XFS_DFORK_DSIZE(dip, ip->i_mount))) {
Dave Chinner65333b42011-03-07 10:03:35 +1100305 xfs_warn(ip->i_mount,
306 "corrupt inode %Lu (bad size %Ld for local inode).",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 (unsigned long long) ip->i_ino,
308 (long long) di_size);
309 XFS_CORRUPTION_ERROR("xfs_iformat(5)",
310 XFS_ERRLEVEL_LOW,
311 ip->i_mount, dip);
312 return XFS_ERROR(EFSCORRUPTED);
313 }
314
315 size = (int)di_size;
316 error = xfs_iformat_local(ip, dip, XFS_DATA_FORK, size);
317 break;
318 case XFS_DINODE_FMT_EXTENTS:
319 error = xfs_iformat_extents(ip, dip, XFS_DATA_FORK);
320 break;
321 case XFS_DINODE_FMT_BTREE:
322 error = xfs_iformat_btree(ip, dip, XFS_DATA_FORK);
323 break;
324 default:
325 XFS_ERROR_REPORT("xfs_iformat(6)", XFS_ERRLEVEL_LOW,
326 ip->i_mount);
327 return XFS_ERROR(EFSCORRUPTED);
328 }
329 break;
330
331 default:
332 XFS_ERROR_REPORT("xfs_iformat(7)", XFS_ERRLEVEL_LOW, ip->i_mount);
333 return XFS_ERROR(EFSCORRUPTED);
334 }
335 if (error) {
336 return error;
337 }
338 if (!XFS_DFORK_Q(dip))
339 return 0;
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000340
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 ASSERT(ip->i_afp == NULL);
Dave Chinner4a7eddd2010-07-20 17:53:59 +1000342 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP | KM_NOFS);
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000343
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100344 switch (dip->di_aformat) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 case XFS_DINODE_FMT_LOCAL:
346 atp = (xfs_attr_shortform_t *)XFS_DFORK_APTR(dip);
Nathan Scott3b244aa2006-03-17 17:29:25 +1100347 size = be16_to_cpu(atp->hdr.totsize);
Christoph Hellwig2809f762009-01-19 02:04:16 +0100348
349 if (unlikely(size < sizeof(struct xfs_attr_sf_hdr))) {
Dave Chinner65333b42011-03-07 10:03:35 +1100350 xfs_warn(ip->i_mount,
351 "corrupt inode %Lu (bad attr fork size %Ld).",
Christoph Hellwig2809f762009-01-19 02:04:16 +0100352 (unsigned long long) ip->i_ino,
353 (long long) size);
354 XFS_CORRUPTION_ERROR("xfs_iformat(8)",
355 XFS_ERRLEVEL_LOW,
356 ip->i_mount, dip);
357 return XFS_ERROR(EFSCORRUPTED);
358 }
359
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 error = xfs_iformat_local(ip, dip, XFS_ATTR_FORK, size);
361 break;
362 case XFS_DINODE_FMT_EXTENTS:
363 error = xfs_iformat_extents(ip, dip, XFS_ATTR_FORK);
364 break;
365 case XFS_DINODE_FMT_BTREE:
366 error = xfs_iformat_btree(ip, dip, XFS_ATTR_FORK);
367 break;
368 default:
369 error = XFS_ERROR(EFSCORRUPTED);
370 break;
371 }
372 if (error) {
373 kmem_zone_free(xfs_ifork_zone, ip->i_afp);
374 ip->i_afp = NULL;
375 xfs_idestroy_fork(ip, XFS_DATA_FORK);
376 }
377 return error;
378}
379
380/*
381 * The file is in-lined in the on-disk inode.
382 * If it fits into if_inline_data, then copy
383 * it there, otherwise allocate a buffer for it
384 * and copy the data there. Either way, set
385 * if_data to point at the data.
386 * If we allocate a buffer for the data, make
387 * sure that its size is a multiple of 4 and
388 * record the real size in i_real_bytes.
389 */
390STATIC int
391xfs_iformat_local(
392 xfs_inode_t *ip,
393 xfs_dinode_t *dip,
394 int whichfork,
395 int size)
396{
397 xfs_ifork_t *ifp;
398 int real_size;
399
400 /*
401 * If the size is unreasonable, then something
402 * is wrong and we just bail out rather than crash in
403 * kmem_alloc() or memcpy() below.
404 */
405 if (unlikely(size > XFS_DFORK_SIZE(dip, ip->i_mount, whichfork))) {
Dave Chinner65333b42011-03-07 10:03:35 +1100406 xfs_warn(ip->i_mount,
407 "corrupt inode %Lu (bad size %d for local fork, size = %d).",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 (unsigned long long) ip->i_ino, size,
409 XFS_DFORK_SIZE(dip, ip->i_mount, whichfork));
410 XFS_CORRUPTION_ERROR("xfs_iformat_local", XFS_ERRLEVEL_LOW,
411 ip->i_mount, dip);
412 return XFS_ERROR(EFSCORRUPTED);
413 }
414 ifp = XFS_IFORK_PTR(ip, whichfork);
415 real_size = 0;
416 if (size == 0)
417 ifp->if_u1.if_data = NULL;
418 else if (size <= sizeof(ifp->if_u2.if_inline_data))
419 ifp->if_u1.if_data = ifp->if_u2.if_inline_data;
420 else {
421 real_size = roundup(size, 4);
Dave Chinner4a7eddd2010-07-20 17:53:59 +1000422 ifp->if_u1.if_data = kmem_alloc(real_size, KM_SLEEP | KM_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 }
424 ifp->if_bytes = size;
425 ifp->if_real_bytes = real_size;
426 if (size)
427 memcpy(ifp->if_u1.if_data, XFS_DFORK_PTR(dip, whichfork), size);
428 ifp->if_flags &= ~XFS_IFEXTENTS;
429 ifp->if_flags |= XFS_IFINLINE;
430 return 0;
431}
432
433/*
434 * The file consists of a set of extents all
435 * of which fit into the on-disk inode.
436 * If there are few enough extents to fit into
437 * the if_inline_ext, then copy them there.
438 * Otherwise allocate a buffer for them and copy
439 * them into it. Either way, set if_extents
440 * to point at the extents.
441 */
442STATIC int
443xfs_iformat_extents(
444 xfs_inode_t *ip,
445 xfs_dinode_t *dip,
446 int whichfork)
447{
Christoph Hellwiga6f64d42007-08-16 16:23:40 +1000448 xfs_bmbt_rec_t *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 xfs_ifork_t *ifp;
450 int nex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 int size;
452 int i;
453
454 ifp = XFS_IFORK_PTR(ip, whichfork);
455 nex = XFS_DFORK_NEXTENTS(dip, whichfork);
456 size = nex * (uint)sizeof(xfs_bmbt_rec_t);
457
458 /*
459 * If the number of extents is unreasonable, then something
460 * is wrong and we just bail out rather than crash in
461 * kmem_alloc() or memcpy() below.
462 */
463 if (unlikely(size < 0 || size > XFS_DFORK_SIZE(dip, ip->i_mount, whichfork))) {
Dave Chinner65333b42011-03-07 10:03:35 +1100464 xfs_warn(ip->i_mount, "corrupt inode %Lu ((a)extents = %d).",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 (unsigned long long) ip->i_ino, nex);
466 XFS_CORRUPTION_ERROR("xfs_iformat_extents(1)", XFS_ERRLEVEL_LOW,
467 ip->i_mount, dip);
468 return XFS_ERROR(EFSCORRUPTED);
469 }
470
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100471 ifp->if_real_bytes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 if (nex == 0)
473 ifp->if_u1.if_extents = NULL;
474 else if (nex <= XFS_INLINE_EXTS)
475 ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100476 else
477 xfs_iext_add(ifp, 0, nex);
478
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 ifp->if_bytes = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 if (size) {
481 dp = (xfs_bmbt_rec_t *) XFS_DFORK_PTR(dip, whichfork);
Christoph Hellwiga6f64d42007-08-16 16:23:40 +1000482 xfs_validate_extents(ifp, nex, XFS_EXTFMT_INODE(ip));
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100483 for (i = 0; i < nex; i++, dp++) {
Christoph Hellwiga6f64d42007-08-16 16:23:40 +1000484 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, i);
Harvey Harrison597bca62008-08-13 16:29:21 +1000485 ep->l0 = get_unaligned_be64(&dp->l0);
486 ep->l1 = get_unaligned_be64(&dp->l1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 }
Eric Sandeen3a59c942007-07-11 11:09:47 +1000488 XFS_BMAP_TRACE_EXLIST(ip, nex, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 if (whichfork != XFS_DATA_FORK ||
490 XFS_EXTFMT_INODE(ip) == XFS_EXTFMT_NOSTATE)
491 if (unlikely(xfs_check_nostate_extents(
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100492 ifp, 0, nex))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 XFS_ERROR_REPORT("xfs_iformat_extents(2)",
494 XFS_ERRLEVEL_LOW,
495 ip->i_mount);
496 return XFS_ERROR(EFSCORRUPTED);
497 }
498 }
499 ifp->if_flags |= XFS_IFEXTENTS;
500 return 0;
501}
502
503/*
504 * The file has too many extents to fit into
505 * the inode, so they are in B-tree format.
506 * Allocate a buffer for the root of the B-tree
507 * and copy the root into it. The i_extents
508 * field will remain NULL until all of the
509 * extents are read in (when they are needed).
510 */
511STATIC int
512xfs_iformat_btree(
513 xfs_inode_t *ip,
514 xfs_dinode_t *dip,
515 int whichfork)
516{
517 xfs_bmdr_block_t *dfp;
518 xfs_ifork_t *ifp;
519 /* REFERENCED */
520 int nrecs;
521 int size;
522
523 ifp = XFS_IFORK_PTR(ip, whichfork);
524 dfp = (xfs_bmdr_block_t *)XFS_DFORK_PTR(dip, whichfork);
525 size = XFS_BMAP_BROOT_SPACE(dfp);
Christoph Hellwig60197e82008-10-30 17:11:19 +1100526 nrecs = be16_to_cpu(dfp->bb_numrecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
528 /*
529 * blow out if -- fork has less extents than can fit in
530 * fork (fork shouldn't be a btree format), root btree
531 * block has more records than can fit into the fork,
532 * or the number of extents is greater than the number of
533 * blocks.
534 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000535 if (unlikely(XFS_IFORK_NEXTENTS(ip, whichfork) <=
536 XFS_IFORK_MAXEXT(ip, whichfork) ||
537 XFS_BMDR_SPACE_CALC(nrecs) >
538 XFS_DFORK_SIZE(dip, ip->i_mount, whichfork) ||
539 XFS_IFORK_NEXTENTS(ip, whichfork) > ip->i_d.di_nblocks)) {
Dave Chinner65333b42011-03-07 10:03:35 +1100540 xfs_warn(ip->i_mount, "corrupt inode %Lu (btree).",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 (unsigned long long) ip->i_ino);
Dave Chinner65333b42011-03-07 10:03:35 +1100542 XFS_CORRUPTION_ERROR("xfs_iformat_btree", XFS_ERRLEVEL_LOW,
543 ip->i_mount, dip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 return XFS_ERROR(EFSCORRUPTED);
545 }
546
547 ifp->if_broot_bytes = size;
Dave Chinner4a7eddd2010-07-20 17:53:59 +1000548 ifp->if_broot = kmem_alloc(size, KM_SLEEP | KM_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 ASSERT(ifp->if_broot != NULL);
550 /*
551 * Copy and convert from the on-disk structure
552 * to the in-memory structure.
553 */
Christoph Hellwig60197e82008-10-30 17:11:19 +1100554 xfs_bmdr_to_bmbt(ip->i_mount, dfp,
555 XFS_DFORK_SIZE(dip, ip->i_mount, whichfork),
556 ifp->if_broot, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 ifp->if_flags &= ~XFS_IFEXTENTS;
558 ifp->if_flags |= XFS_IFBROOT;
559
560 return 0;
561}
562
Eric Sandeend96f8f82009-07-02 00:09:33 -0500563STATIC void
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000564xfs_dinode_from_disk(
565 xfs_icdinode_t *to,
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100566 xfs_dinode_t *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567{
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000568 to->di_magic = be16_to_cpu(from->di_magic);
569 to->di_mode = be16_to_cpu(from->di_mode);
570 to->di_version = from ->di_version;
571 to->di_format = from->di_format;
572 to->di_onlink = be16_to_cpu(from->di_onlink);
573 to->di_uid = be32_to_cpu(from->di_uid);
574 to->di_gid = be32_to_cpu(from->di_gid);
575 to->di_nlink = be32_to_cpu(from->di_nlink);
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +0000576 to->di_projid_lo = be16_to_cpu(from->di_projid_lo);
577 to->di_projid_hi = be16_to_cpu(from->di_projid_hi);
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000578 memcpy(to->di_pad, from->di_pad, sizeof(to->di_pad));
579 to->di_flushiter = be16_to_cpu(from->di_flushiter);
580 to->di_atime.t_sec = be32_to_cpu(from->di_atime.t_sec);
581 to->di_atime.t_nsec = be32_to_cpu(from->di_atime.t_nsec);
582 to->di_mtime.t_sec = be32_to_cpu(from->di_mtime.t_sec);
583 to->di_mtime.t_nsec = be32_to_cpu(from->di_mtime.t_nsec);
584 to->di_ctime.t_sec = be32_to_cpu(from->di_ctime.t_sec);
585 to->di_ctime.t_nsec = be32_to_cpu(from->di_ctime.t_nsec);
586 to->di_size = be64_to_cpu(from->di_size);
587 to->di_nblocks = be64_to_cpu(from->di_nblocks);
588 to->di_extsize = be32_to_cpu(from->di_extsize);
589 to->di_nextents = be32_to_cpu(from->di_nextents);
590 to->di_anextents = be16_to_cpu(from->di_anextents);
591 to->di_forkoff = from->di_forkoff;
592 to->di_aformat = from->di_aformat;
593 to->di_dmevmask = be32_to_cpu(from->di_dmevmask);
594 to->di_dmstate = be16_to_cpu(from->di_dmstate);
595 to->di_flags = be16_to_cpu(from->di_flags);
596 to->di_gen = be32_to_cpu(from->di_gen);
597}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000599void
600xfs_dinode_to_disk(
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100601 xfs_dinode_t *to,
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000602 xfs_icdinode_t *from)
603{
604 to->di_magic = cpu_to_be16(from->di_magic);
605 to->di_mode = cpu_to_be16(from->di_mode);
606 to->di_version = from ->di_version;
607 to->di_format = from->di_format;
608 to->di_onlink = cpu_to_be16(from->di_onlink);
609 to->di_uid = cpu_to_be32(from->di_uid);
610 to->di_gid = cpu_to_be32(from->di_gid);
611 to->di_nlink = cpu_to_be32(from->di_nlink);
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +0000612 to->di_projid_lo = cpu_to_be16(from->di_projid_lo);
613 to->di_projid_hi = cpu_to_be16(from->di_projid_hi);
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000614 memcpy(to->di_pad, from->di_pad, sizeof(to->di_pad));
615 to->di_flushiter = cpu_to_be16(from->di_flushiter);
616 to->di_atime.t_sec = cpu_to_be32(from->di_atime.t_sec);
617 to->di_atime.t_nsec = cpu_to_be32(from->di_atime.t_nsec);
618 to->di_mtime.t_sec = cpu_to_be32(from->di_mtime.t_sec);
619 to->di_mtime.t_nsec = cpu_to_be32(from->di_mtime.t_nsec);
620 to->di_ctime.t_sec = cpu_to_be32(from->di_ctime.t_sec);
621 to->di_ctime.t_nsec = cpu_to_be32(from->di_ctime.t_nsec);
622 to->di_size = cpu_to_be64(from->di_size);
623 to->di_nblocks = cpu_to_be64(from->di_nblocks);
624 to->di_extsize = cpu_to_be32(from->di_extsize);
625 to->di_nextents = cpu_to_be32(from->di_nextents);
626 to->di_anextents = cpu_to_be16(from->di_anextents);
627 to->di_forkoff = from->di_forkoff;
628 to->di_aformat = from->di_aformat;
629 to->di_dmevmask = cpu_to_be32(from->di_dmevmask);
630 to->di_dmstate = cpu_to_be16(from->di_dmstate);
631 to->di_flags = cpu_to_be16(from->di_flags);
632 to->di_gen = cpu_to_be32(from->di_gen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633}
634
635STATIC uint
636_xfs_dic2xflags(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 __uint16_t di_flags)
638{
639 uint flags = 0;
640
641 if (di_flags & XFS_DIFLAG_ANY) {
642 if (di_flags & XFS_DIFLAG_REALTIME)
643 flags |= XFS_XFLAG_REALTIME;
644 if (di_flags & XFS_DIFLAG_PREALLOC)
645 flags |= XFS_XFLAG_PREALLOC;
646 if (di_flags & XFS_DIFLAG_IMMUTABLE)
647 flags |= XFS_XFLAG_IMMUTABLE;
648 if (di_flags & XFS_DIFLAG_APPEND)
649 flags |= XFS_XFLAG_APPEND;
650 if (di_flags & XFS_DIFLAG_SYNC)
651 flags |= XFS_XFLAG_SYNC;
652 if (di_flags & XFS_DIFLAG_NOATIME)
653 flags |= XFS_XFLAG_NOATIME;
654 if (di_flags & XFS_DIFLAG_NODUMP)
655 flags |= XFS_XFLAG_NODUMP;
656 if (di_flags & XFS_DIFLAG_RTINHERIT)
657 flags |= XFS_XFLAG_RTINHERIT;
658 if (di_flags & XFS_DIFLAG_PROJINHERIT)
659 flags |= XFS_XFLAG_PROJINHERIT;
660 if (di_flags & XFS_DIFLAG_NOSYMLINKS)
661 flags |= XFS_XFLAG_NOSYMLINKS;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100662 if (di_flags & XFS_DIFLAG_EXTSIZE)
663 flags |= XFS_XFLAG_EXTSIZE;
664 if (di_flags & XFS_DIFLAG_EXTSZINHERIT)
665 flags |= XFS_XFLAG_EXTSZINHERIT;
Barry Naujokd3446ea2006-06-09 14:54:19 +1000666 if (di_flags & XFS_DIFLAG_NODEFRAG)
667 flags |= XFS_XFLAG_NODEFRAG;
David Chinner2a82b8b2007-07-11 11:09:12 +1000668 if (di_flags & XFS_DIFLAG_FILESTREAM)
669 flags |= XFS_XFLAG_FILESTREAM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 }
671
672 return flags;
673}
674
675uint
676xfs_ip2xflags(
677 xfs_inode_t *ip)
678{
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000679 xfs_icdinode_t *dic = &ip->i_d;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
Nathan Scotta916e2b2006-06-09 17:12:17 +1000681 return _xfs_dic2xflags(dic->di_flags) |
Christoph Hellwig45ba5982007-12-07 14:07:20 +1100682 (XFS_IFORK_Q(ip) ? XFS_XFLAG_HASATTR : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683}
684
685uint
686xfs_dic2xflags(
Christoph Hellwig45ba5982007-12-07 14:07:20 +1100687 xfs_dinode_t *dip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688{
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100689 return _xfs_dic2xflags(be16_to_cpu(dip->di_flags)) |
Christoph Hellwig45ba5982007-12-07 14:07:20 +1100690 (XFS_DFORK_Q(dip) ? XFS_XFLAG_HASATTR : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691}
692
693/*
Christoph Hellwig24f211b2008-11-28 14:23:42 +1100694 * Read the disk inode attributes into the in-core inode structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 */
696int
697xfs_iread(
698 xfs_mount_t *mp,
699 xfs_trans_t *tp,
Christoph Hellwig24f211b2008-11-28 14:23:42 +1100700 xfs_inode_t *ip,
Christoph Hellwig24f211b2008-11-28 14:23:42 +1100701 uint iget_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702{
703 xfs_buf_t *bp;
704 xfs_dinode_t *dip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 int error;
706
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 /*
Christoph Hellwig92bfc6e2008-11-28 14:23:41 +1100708 * Fill in the location information in the in-core inode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 */
Christoph Hellwig24f211b2008-11-28 14:23:42 +1100710 error = xfs_imap(mp, tp, ip->i_ino, &ip->i_imap, iget_flags);
Christoph Hellwig9ed04512008-10-30 18:26:04 +1100711 if (error)
Christoph Hellwig24f211b2008-11-28 14:23:42 +1100712 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
714 /*
Christoph Hellwig92bfc6e2008-11-28 14:23:41 +1100715 * Get pointers to the on-disk inode and the buffer containing it.
Christoph Hellwig76d8b272008-11-28 14:23:40 +1100716 */
Christoph Hellwig475ee412012-07-03 12:21:22 -0400717 error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &bp, 0, iget_flags);
Christoph Hellwig76d8b272008-11-28 14:23:40 +1100718 if (error)
Christoph Hellwig24f211b2008-11-28 14:23:42 +1100719 return error;
Christoph Hellwig76d8b272008-11-28 14:23:40 +1100720
721 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 * If we got something that isn't an inode it means someone
723 * (nfs or dmi) has a stale handle.
724 */
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200725 if (dip->di_magic != cpu_to_be16(XFS_DINODE_MAGIC)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726#ifdef DEBUG
Dave Chinner53487782011-03-07 10:05:35 +1100727 xfs_alert(mp,
728 "%s: dip->di_magic (0x%x) != XFS_DINODE_MAGIC (0x%x)",
729 __func__, be16_to_cpu(dip->di_magic), XFS_DINODE_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730#endif /* DEBUG */
Christoph Hellwig9ed04512008-10-30 18:26:04 +1100731 error = XFS_ERROR(EINVAL);
732 goto out_brelse;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 }
734
735 /*
736 * If the on-disk inode is already linked to a directory
737 * entry, copy all of the inode into the in-core inode.
738 * xfs_iformat() handles copying in the inode format
739 * specific information.
740 * Otherwise, just get the truly permanent information.
741 */
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100742 if (dip->di_mode) {
743 xfs_dinode_from_disk(&ip->i_d, dip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 error = xfs_iformat(ip, dip);
745 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746#ifdef DEBUG
Dave Chinner53487782011-03-07 10:05:35 +1100747 xfs_alert(mp, "%s: xfs_iformat() returned error %d",
748 __func__, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749#endif /* DEBUG */
Christoph Hellwig9ed04512008-10-30 18:26:04 +1100750 goto out_brelse;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 }
752 } else {
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100753 ip->i_d.di_magic = be16_to_cpu(dip->di_magic);
754 ip->i_d.di_version = dip->di_version;
755 ip->i_d.di_gen = be32_to_cpu(dip->di_gen);
756 ip->i_d.di_flushiter = be16_to_cpu(dip->di_flushiter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 /*
758 * Make sure to pull in the mode here as well in
759 * case the inode is released without being used.
760 * This ensures that xfs_inactive() will see that
761 * the inode is already free and not try to mess
762 * with the uninitialized part of it.
763 */
764 ip->i_d.di_mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 }
766
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 /*
768 * The inode format changed when we moved the link count and
769 * made it 32 bits long. If this is an old format inode,
770 * convert it in memory to look like a new one. If it gets
771 * flushed to disk we will convert back before flushing or
772 * logging it. We zero out the new projid field and the old link
773 * count field. We'll handle clearing the pad field (the remains
774 * of the old uuid field) when we actually convert the inode to
775 * the new format. We don't change the version number so that we
776 * can distinguish this from a real new format inode.
777 */
Christoph Hellwig51ce16d2008-11-28 14:23:39 +1100778 if (ip->i_d.di_version == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 ip->i_d.di_nlink = ip->i_d.di_onlink;
780 ip->i_d.di_onlink = 0;
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +0000781 xfs_set_projid(ip, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 }
783
784 ip->i_delayed_blks = 0;
785
786 /*
787 * Mark the buffer containing the inode as something to keep
788 * around for a while. This helps to keep recently accessed
789 * meta-data in-core longer.
790 */
Dave Chinner821eb212010-12-02 16:31:13 +1100791 xfs_buf_set_ref(bp, XFS_INO_REF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
793 /*
794 * Use xfs_trans_brelse() to release the buffer containing the
795 * on-disk inode, because it was acquired with xfs_trans_read_buf()
Christoph Hellwig475ee412012-07-03 12:21:22 -0400796 * in xfs_imap_to_bp() above. If tp is NULL, this is just a normal
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 * brelse(). If we're within a transaction, then xfs_trans_brelse()
798 * will only release the buffer if it is not dirty within the
799 * transaction. It will be OK to release the buffer in this case,
800 * because inodes on disk are never destroyed and we will be
801 * locking the new in-core inode before putting it in the hash
802 * table where other processes can find it. Thus we don't have
803 * to worry about the inode being changed just because we released
804 * the buffer.
805 */
Christoph Hellwig9ed04512008-10-30 18:26:04 +1100806 out_brelse:
807 xfs_trans_brelse(tp, bp);
Christoph Hellwig9ed04512008-10-30 18:26:04 +1100808 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809}
810
811/*
812 * Read in extents from a btree-format inode.
813 * Allocate and fill in if_extents. Real work is done in xfs_bmap.c.
814 */
815int
816xfs_iread_extents(
817 xfs_trans_t *tp,
818 xfs_inode_t *ip,
819 int whichfork)
820{
821 int error;
822 xfs_ifork_t *ifp;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100823 xfs_extnum_t nextents;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
825 if (unlikely(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
826 XFS_ERROR_REPORT("xfs_iread_extents", XFS_ERRLEVEL_LOW,
827 ip->i_mount);
828 return XFS_ERROR(EFSCORRUPTED);
829 }
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100830 nextents = XFS_IFORK_NEXTENTS(ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 ifp = XFS_IFORK_PTR(ip, whichfork);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100832
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 /*
834 * We know that the size is valid (it's checked in iformat_btree)
835 */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100836 ifp->if_bytes = ifp->if_real_bytes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 ifp->if_flags |= XFS_IFEXTENTS;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100838 xfs_iext_add(ifp, 0, nextents);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 error = xfs_bmap_read_extents(tp, ip, whichfork);
840 if (error) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100841 xfs_iext_destroy(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 ifp->if_flags &= ~XFS_IFEXTENTS;
843 return error;
844 }
Christoph Hellwiga6f64d42007-08-16 16:23:40 +1000845 xfs_validate_extents(ifp, nextents, XFS_EXTFMT_INODE(ip));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 return 0;
847}
848
849/*
850 * Allocate an inode on disk and return a copy of its in-core version.
851 * The in-core inode is locked exclusively. Set mode, nlink, and rdev
852 * appropriately within the inode. The uid and gid for the inode are
853 * set according to the contents of the given cred structure.
854 *
855 * Use xfs_dialloc() to allocate the on-disk inode. If xfs_dialloc()
856 * has a free inode available, call xfs_iget()
857 * to obtain the in-core version of the allocated inode. Finally,
858 * fill in the inode and log its initial contents. In this case,
859 * ialloc_context would be set to NULL and call_again set to false.
860 *
861 * If xfs_dialloc() does not have an available inode,
862 * it will replenish its supply by doing an allocation. Since we can
863 * only do one allocation within a transaction without deadlocks, we
864 * must commit the current transaction before returning the inode itself.
865 * In this case, therefore, we will set call_again to true and return.
866 * The caller should then commit the current transaction, start a new
867 * transaction, and call xfs_ialloc() again to actually get the inode.
868 *
869 * To ensure that some other process does not grab the inode that
870 * was allocated during the first call to xfs_ialloc(), this routine
871 * also returns the [locked] bp pointing to the head of the freelist
872 * as ialloc_context. The caller should hold this buffer across
873 * the commit and pass it back into this routine on the second call.
David Chinnerb11f94d2007-07-11 11:09:33 +1000874 *
875 * If we are allocating quota inodes, we do not have a parent inode
876 * to attach to or associate with (i.e. pip == NULL) because they
877 * are not linked into the directory structure - they are attached
878 * directly to the superblock - and so have no parent.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 */
880int
881xfs_ialloc(
882 xfs_trans_t *tp,
883 xfs_inode_t *pip,
Al Viro576b1d62011-07-26 02:50:15 -0400884 umode_t mode,
Nathan Scott31b084a2005-05-05 13:25:00 -0700885 xfs_nlink_t nlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 xfs_dev_t rdev,
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +0000887 prid_t prid,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 int okalloc,
889 xfs_buf_t **ialloc_context,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 xfs_inode_t **ipp)
891{
892 xfs_ino_t ino;
893 xfs_inode_t *ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 uint flags;
895 int error;
Christoph Hellwigdff35fd2008-08-13 16:44:15 +1000896 timespec_t tv;
David Chinnerbf904242008-10-30 17:36:14 +1100897 int filestreams = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
899 /*
900 * Call the space management code to pick
901 * the on-disk inode to be allocated.
902 */
David Chinnerb11f94d2007-07-11 11:09:33 +1000903 error = xfs_dialloc(tp, pip ? pip->i_ino : 0, mode, okalloc,
Christoph Hellwig08358902012-07-04 10:54:47 -0400904 ialloc_context, &ino);
David Chinnerbf904242008-10-30 17:36:14 +1100905 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 return error;
Christoph Hellwig08358902012-07-04 10:54:47 -0400907 if (*ialloc_context || ino == NULLFSINO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 *ipp = NULL;
909 return 0;
910 }
911 ASSERT(*ialloc_context == NULL);
912
913 /*
914 * Get the in-core inode with the lock held exclusively.
915 * This is because we're setting fields here we need
916 * to prevent others from looking at until we're done.
917 */
Christoph Hellwigec3ba852011-02-13 13:26:42 +0000918 error = xfs_iget(tp->t_mountp, tp, ino, XFS_IGET_CREATE,
919 XFS_ILOCK_EXCL, &ip);
David Chinnerbf904242008-10-30 17:36:14 +1100920 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 ASSERT(ip != NULL);
923
Al Viro576b1d62011-07-26 02:50:15 -0400924 ip->i_d.di_mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 ip->i_d.di_onlink = 0;
926 ip->i_d.di_nlink = nlink;
927 ASSERT(ip->i_d.di_nlink == nlink);
David Howells9e2b2dc2008-08-13 16:20:04 +0100928 ip->i_d.di_uid = current_fsuid();
929 ip->i_d.di_gid = current_fsgid();
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +0000930 xfs_set_projid(ip, prid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 memset(&(ip->i_d.di_pad[0]), 0, sizeof(ip->i_d.di_pad));
932
933 /*
934 * If the superblock version is up to where we support new format
935 * inodes and this is currently an old format inode, then change
936 * the inode version number now. This way we only do the conversion
937 * here rather than here and in the flush/logging code.
938 */
Eric Sandeen62118702008-03-06 13:44:28 +1100939 if (xfs_sb_version_hasnlink(&tp->t_mountp->m_sb) &&
Christoph Hellwig51ce16d2008-11-28 14:23:39 +1100940 ip->i_d.di_version == 1) {
941 ip->i_d.di_version = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 /*
943 * We've already zeroed the old link count, the projid field,
944 * and the pad field.
945 */
946 }
947
948 /*
949 * Project ids won't be stored on disk if we are using a version 1 inode.
950 */
Christoph Hellwig51ce16d2008-11-28 14:23:39 +1100951 if ((prid != 0) && (ip->i_d.di_version == 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 xfs_bump_ino_vers2(tp, ip);
953
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000954 if (pip && XFS_INHERIT_GID(pip)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 ip->i_d.di_gid = pip->i_d.di_gid;
Al Viroabbede12011-07-26 02:31:30 -0400956 if ((pip->i_d.di_mode & S_ISGID) && S_ISDIR(mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 ip->i_d.di_mode |= S_ISGID;
958 }
959 }
960
961 /*
962 * If the group ID of the new file does not match the effective group
963 * ID or one of the supplementary group IDs, the S_ISGID bit is cleared
964 * (and only if the irix_sgid_inherit compatibility variable is set).
965 */
966 if ((irix_sgid_inherit) &&
967 (ip->i_d.di_mode & S_ISGID) &&
968 (!in_group_p((gid_t)ip->i_d.di_gid))) {
969 ip->i_d.di_mode &= ~S_ISGID;
970 }
971
972 ip->i_d.di_size = 0;
973 ip->i_d.di_nextents = 0;
974 ASSERT(ip->i_d.di_nblocks == 0);
Christoph Hellwigdff35fd2008-08-13 16:44:15 +1000975
976 nanotime(&tv);
977 ip->i_d.di_mtime.t_sec = (__int32_t)tv.tv_sec;
978 ip->i_d.di_mtime.t_nsec = (__int32_t)tv.tv_nsec;
979 ip->i_d.di_atime = ip->i_d.di_mtime;
980 ip->i_d.di_ctime = ip->i_d.di_mtime;
981
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 /*
983 * di_gen will have been taken care of in xfs_iread.
984 */
985 ip->i_d.di_extsize = 0;
986 ip->i_d.di_dmevmask = 0;
987 ip->i_d.di_dmstate = 0;
988 ip->i_d.di_flags = 0;
989 flags = XFS_ILOG_CORE;
990 switch (mode & S_IFMT) {
991 case S_IFIFO:
992 case S_IFCHR:
993 case S_IFBLK:
994 case S_IFSOCK:
995 ip->i_d.di_format = XFS_DINODE_FMT_DEV;
996 ip->i_df.if_u2.if_rdev = rdev;
997 ip->i_df.if_flags = 0;
998 flags |= XFS_ILOG_DEV;
999 break;
1000 case S_IFREG:
David Chinnerbf904242008-10-30 17:36:14 +11001001 /*
1002 * we can't set up filestreams until after the VFS inode
1003 * is set up properly.
1004 */
1005 if (pip && xfs_inode_is_filestream(pip))
1006 filestreams = 1;
David Chinner2a82b8b2007-07-11 11:09:12 +10001007 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 case S_IFDIR:
David Chinnerb11f94d2007-07-11 11:09:33 +10001009 if (pip && (pip->i_d.di_flags & XFS_DIFLAG_ANY)) {
Nathan Scott365ca832005-06-21 15:39:12 +10001010 uint di_flags = 0;
1011
Al Viroabbede12011-07-26 02:31:30 -04001012 if (S_ISDIR(mode)) {
Nathan Scott365ca832005-06-21 15:39:12 +10001013 if (pip->i_d.di_flags & XFS_DIFLAG_RTINHERIT)
1014 di_flags |= XFS_DIFLAG_RTINHERIT;
Nathan Scottdd9f4382006-01-11 15:28:28 +11001015 if (pip->i_d.di_flags & XFS_DIFLAG_EXTSZINHERIT) {
1016 di_flags |= XFS_DIFLAG_EXTSZINHERIT;
1017 ip->i_d.di_extsize = pip->i_d.di_extsize;
1018 }
Al Viroabbede12011-07-26 02:31:30 -04001019 } else if (S_ISREG(mode)) {
Christoph Hellwig613d7042007-10-11 17:44:08 +10001020 if (pip->i_d.di_flags & XFS_DIFLAG_RTINHERIT)
Nathan Scott365ca832005-06-21 15:39:12 +10001021 di_flags |= XFS_DIFLAG_REALTIME;
Nathan Scottdd9f4382006-01-11 15:28:28 +11001022 if (pip->i_d.di_flags & XFS_DIFLAG_EXTSZINHERIT) {
1023 di_flags |= XFS_DIFLAG_EXTSIZE;
1024 ip->i_d.di_extsize = pip->i_d.di_extsize;
1025 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 }
1027 if ((pip->i_d.di_flags & XFS_DIFLAG_NOATIME) &&
1028 xfs_inherit_noatime)
Nathan Scott365ca832005-06-21 15:39:12 +10001029 di_flags |= XFS_DIFLAG_NOATIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 if ((pip->i_d.di_flags & XFS_DIFLAG_NODUMP) &&
1031 xfs_inherit_nodump)
Nathan Scott365ca832005-06-21 15:39:12 +10001032 di_flags |= XFS_DIFLAG_NODUMP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 if ((pip->i_d.di_flags & XFS_DIFLAG_SYNC) &&
1034 xfs_inherit_sync)
Nathan Scott365ca832005-06-21 15:39:12 +10001035 di_flags |= XFS_DIFLAG_SYNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 if ((pip->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) &&
1037 xfs_inherit_nosymlinks)
Nathan Scott365ca832005-06-21 15:39:12 +10001038 di_flags |= XFS_DIFLAG_NOSYMLINKS;
1039 if (pip->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
1040 di_flags |= XFS_DIFLAG_PROJINHERIT;
Barry Naujokd3446ea2006-06-09 14:54:19 +10001041 if ((pip->i_d.di_flags & XFS_DIFLAG_NODEFRAG) &&
1042 xfs_inherit_nodefrag)
1043 di_flags |= XFS_DIFLAG_NODEFRAG;
David Chinner2a82b8b2007-07-11 11:09:12 +10001044 if (pip->i_d.di_flags & XFS_DIFLAG_FILESTREAM)
1045 di_flags |= XFS_DIFLAG_FILESTREAM;
Nathan Scott365ca832005-06-21 15:39:12 +10001046 ip->i_d.di_flags |= di_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 }
1048 /* FALLTHROUGH */
1049 case S_IFLNK:
1050 ip->i_d.di_format = XFS_DINODE_FMT_EXTENTS;
1051 ip->i_df.if_flags = XFS_IFEXTENTS;
1052 ip->i_df.if_bytes = ip->i_df.if_real_bytes = 0;
1053 ip->i_df.if_u1.if_extents = NULL;
1054 break;
1055 default:
1056 ASSERT(0);
1057 }
1058 /*
1059 * Attribute fork settings for new inode.
1060 */
1061 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
1062 ip->i_d.di_anextents = 0;
1063
1064 /*
1065 * Log the new values stuffed into the inode.
1066 */
Christoph Hellwigddc34152011-09-19 15:00:54 +00001067 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 xfs_trans_log_inode(tp, ip, flags);
1069
Nathan Scottb83bd132006-06-09 16:48:30 +10001070 /* now that we have an i_mode we can setup inode ops and unlock */
Christoph Hellwig41be8be2008-08-13 16:23:13 +10001071 xfs_setup_inode(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
David Chinnerbf904242008-10-30 17:36:14 +11001073 /* now we have set up the vfs inode we can associate the filestream */
1074 if (filestreams) {
1075 error = xfs_filestream_associate(pip, ip);
1076 if (error < 0)
1077 return -error;
1078 if (!error)
1079 xfs_iflags_set(ip, XFS_IFILESTREAM);
1080 }
1081
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 *ipp = ip;
1083 return 0;
1084}
1085
1086/*
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001087 * Free up the underlying blocks past new_size. The new size must be smaller
1088 * than the current size. This routine can be used both for the attribute and
1089 * data fork, and does not modify the inode size, which is left to the caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 *
David Chinnerf6485052008-04-17 16:50:04 +10001091 * The transaction passed to this routine must have made a permanent log
1092 * reservation of at least XFS_ITRUNCATE_LOG_RES. This routine may commit the
1093 * given transaction and start new ones, so make sure everything involved in
1094 * the transaction is tidy before calling here. Some transaction will be
1095 * returned to the caller to be committed. The incoming transaction must
1096 * already include the inode, and both inode locks must be held exclusively.
1097 * The inode must also be "held" within the transaction. On return the inode
1098 * will be "held" within the returned transaction. This routine does NOT
1099 * require any disk space to be reserved for it within the transaction.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 *
David Chinnerf6485052008-04-17 16:50:04 +10001101 * If we get an error, we must return with the inode locked and linked into the
1102 * current transaction. This keeps things simple for the higher level code,
1103 * because it always knows that the inode is locked and held in the transaction
1104 * that returns to it whether errors occur or not. We don't mark the inode
1105 * dirty on error so that transactions can be easily aborted if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 */
1107int
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001108xfs_itruncate_extents(
1109 struct xfs_trans **tpp,
1110 struct xfs_inode *ip,
1111 int whichfork,
1112 xfs_fsize_t new_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113{
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001114 struct xfs_mount *mp = ip->i_mount;
1115 struct xfs_trans *tp = *tpp;
1116 struct xfs_trans *ntp;
1117 xfs_bmap_free_t free_list;
1118 xfs_fsblock_t first_block;
1119 xfs_fileoff_t first_unmap_block;
1120 xfs_fileoff_t last_block;
1121 xfs_filblks_t unmap_len;
1122 int committed;
1123 int error = 0;
1124 int done = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
Christoph Hellwig579aa9c2008-04-22 17:34:00 +10001126 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_IOLOCK_EXCL));
Christoph Hellwigce7ae1512011-12-18 20:00:11 +00001127 ASSERT(new_size <= XFS_ISIZE(ip));
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001128 ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 ASSERT(ip->i_itemp != NULL);
Christoph Hellwig898621d2010-06-24 11:36:58 +10001130 ASSERT(ip->i_itemp->ili_lock_flags == 0);
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001131 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
Christoph Hellwig673e8e52011-12-18 20:00:04 +00001133 trace_xfs_itruncate_extents_start(ip, new_size);
1134
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 /*
1136 * Since it is possible for space to become allocated beyond
1137 * the end of the file (in a crash where the space is allocated
1138 * but the inode size is not yet updated), simply remove any
1139 * blocks which show up between the new EOF and the maximum
1140 * possible file size. If the first block to be removed is
1141 * beyond the maximum file size (ie it is the same as last_block),
1142 * then there is nothing to do.
1143 */
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001144 first_unmap_block = XFS_B_TO_FSB(mp, (xfs_ufsize_t)new_size);
Dave Chinner32972382012-06-08 15:44:54 +10001145 last_block = XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes);
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001146 if (first_unmap_block == last_block)
1147 return 0;
1148
1149 ASSERT(first_unmap_block < last_block);
1150 unmap_len = last_block - first_unmap_block + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 while (!done) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06001152 xfs_bmap_init(&free_list, &first_block);
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001153 error = xfs_bunmapi(tp, ip,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001154 first_unmap_block, unmap_len,
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001155 xfs_bmapi_aflag(whichfork),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 XFS_ITRUNC_MAX_EXTENTS,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001157 &first_block, &free_list,
Christoph Hellwigb4e91812010-06-23 18:11:15 +10001158 &done);
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001159 if (error)
1160 goto out_bmap_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
1162 /*
1163 * Duplicate the transaction that has the permanent
1164 * reservation and commit the old transaction.
1165 */
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001166 error = xfs_bmap_finish(&tp, &free_list, &committed);
Christoph Hellwig898621d2010-06-24 11:36:58 +10001167 if (committed)
Christoph Hellwigddc34152011-09-19 15:00:54 +00001168 xfs_trans_ijoin(tp, ip, 0);
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001169 if (error)
1170 goto out_bmap_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171
1172 if (committed) {
1173 /*
David Chinnerf6485052008-04-17 16:50:04 +10001174 * Mark the inode dirty so it will be logged and
David Chinnere5720ee2008-04-10 12:21:18 +10001175 * moved forward in the log as part of every commit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 */
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001177 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 }
David Chinnerf6485052008-04-17 16:50:04 +10001179
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001180 ntp = xfs_trans_dup(tp);
1181 error = xfs_trans_commit(tp, 0);
1182 tp = ntp;
David Chinnere5720ee2008-04-10 12:21:18 +10001183
Christoph Hellwigddc34152011-09-19 15:00:54 +00001184 xfs_trans_ijoin(tp, ip, 0);
David Chinnerf6485052008-04-17 16:50:04 +10001185
Dave Chinnercc09c0d2008-11-17 17:37:10 +11001186 if (error)
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001187 goto out;
1188
Dave Chinnercc09c0d2008-11-17 17:37:10 +11001189 /*
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001190 * Transaction commit worked ok so we can drop the extra ticket
Dave Chinnercc09c0d2008-11-17 17:37:10 +11001191 * reference that we gained in xfs_trans_dup()
1192 */
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001193 xfs_log_ticket_put(tp->t_ticket);
1194 error = xfs_trans_reserve(tp, 0,
David Chinnerf6485052008-04-17 16:50:04 +10001195 XFS_ITRUNCATE_LOG_RES(mp), 0,
1196 XFS_TRANS_PERM_LOG_RES,
1197 XFS_ITRUNCATE_LOG_COUNT);
1198 if (error)
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001199 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 }
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001201
Christoph Hellwig673e8e52011-12-18 20:00:04 +00001202 /*
1203 * Always re-log the inode so that our permanent transaction can keep
1204 * on rolling it forward in the log.
1205 */
1206 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1207
1208 trace_xfs_itruncate_extents_end(ip, new_size);
1209
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001210out:
1211 *tpp = tp;
1212 return error;
1213out_bmap_cancel:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 /*
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001215 * If the bunmapi call encounters an error, return to the caller where
1216 * the transaction can be properly aborted. We just need to make sure
1217 * we're not holding any resources that we were not when we came in.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 */
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001219 xfs_bmap_cancel(&free_list);
1220 goto out;
1221}
1222
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223/*
1224 * This is called when the inode's link count goes to 0.
1225 * We place the on-disk inode on a list in the AGI. It
1226 * will be pulled from this list when the inode is freed.
1227 */
1228int
1229xfs_iunlink(
1230 xfs_trans_t *tp,
1231 xfs_inode_t *ip)
1232{
1233 xfs_mount_t *mp;
1234 xfs_agi_t *agi;
1235 xfs_dinode_t *dip;
1236 xfs_buf_t *agibp;
1237 xfs_buf_t *ibp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 xfs_agino_t agino;
1239 short bucket_index;
1240 int offset;
1241 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
1243 ASSERT(ip->i_d.di_nlink == 0);
1244 ASSERT(ip->i_d.di_mode != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245
1246 mp = tp->t_mountp;
1247
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 /*
1249 * Get the agi buffer first. It ensures lock ordering
1250 * on the list.
1251 */
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11001252 error = xfs_read_agi(mp, tp, XFS_INO_TO_AGNO(mp, ip->i_ino), &agibp);
Vlad Apostolov859d7182007-10-11 17:44:18 +10001253 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 agi = XFS_BUF_TO_AGI(agibp);
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11001256
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 /*
1258 * Get the index into the agi hash table for the
1259 * list this inode will go on.
1260 */
1261 agino = XFS_INO_TO_AGINO(mp, ip->i_ino);
1262 ASSERT(agino != 0);
1263 bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS;
1264 ASSERT(agi->agi_unlinked[bucket_index]);
Christoph Hellwig16259e72005-11-02 15:11:25 +11001265 ASSERT(be32_to_cpu(agi->agi_unlinked[bucket_index]) != agino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001267 if (agi->agi_unlinked[bucket_index] != cpu_to_be32(NULLAGINO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 /*
1269 * There is already another inode in the bucket we need
1270 * to add ourselves to. Add us at the front of the list.
1271 * Here we put the head pointer into our next pointer,
1272 * and then we fall through to point the head at us.
1273 */
Christoph Hellwig475ee412012-07-03 12:21:22 -04001274 error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp,
1275 0, 0);
Vlad Apostolovc319b582007-11-23 16:27:51 +11001276 if (error)
1277 return error;
1278
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001279 ASSERT(dip->di_next_unlinked == cpu_to_be32(NULLAGINO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 dip->di_next_unlinked = agi->agi_unlinked[bucket_index];
Christoph Hellwig92bfc6e2008-11-28 14:23:41 +11001281 offset = ip->i_imap.im_boffset +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 offsetof(xfs_dinode_t, di_next_unlinked);
1283 xfs_trans_inode_buf(tp, ibp);
1284 xfs_trans_log_buf(tp, ibp, offset,
1285 (offset + sizeof(xfs_agino_t) - 1));
1286 xfs_inobp_check(mp, ibp);
1287 }
1288
1289 /*
1290 * Point the bucket head pointer at the inode being inserted.
1291 */
1292 ASSERT(agino != 0);
Christoph Hellwig16259e72005-11-02 15:11:25 +11001293 agi->agi_unlinked[bucket_index] = cpu_to_be32(agino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 offset = offsetof(xfs_agi_t, agi_unlinked) +
1295 (sizeof(xfs_agino_t) * bucket_index);
1296 xfs_trans_log_buf(tp, agibp, offset,
1297 (offset + sizeof(xfs_agino_t) - 1));
1298 return 0;
1299}
1300
1301/*
1302 * Pull the on-disk inode from the AGI unlinked list.
1303 */
1304STATIC int
1305xfs_iunlink_remove(
1306 xfs_trans_t *tp,
1307 xfs_inode_t *ip)
1308{
1309 xfs_ino_t next_ino;
1310 xfs_mount_t *mp;
1311 xfs_agi_t *agi;
1312 xfs_dinode_t *dip;
1313 xfs_buf_t *agibp;
1314 xfs_buf_t *ibp;
1315 xfs_agnumber_t agno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 xfs_agino_t agino;
1317 xfs_agino_t next_agino;
1318 xfs_buf_t *last_ibp;
Nathan Scott6fdf8cc2006-06-28 10:13:52 +10001319 xfs_dinode_t *last_dip = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 short bucket_index;
Nathan Scott6fdf8cc2006-06-28 10:13:52 +10001321 int offset, last_offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 mp = tp->t_mountp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 agno = XFS_INO_TO_AGNO(mp, ip->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
1327 /*
1328 * Get the agi buffer first. It ensures lock ordering
1329 * on the list.
1330 */
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11001331 error = xfs_read_agi(mp, tp, agno, &agibp);
1332 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 return error;
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11001334
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 agi = XFS_BUF_TO_AGI(agibp);
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11001336
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 /*
1338 * Get the index into the agi hash table for the
1339 * list this inode will go on.
1340 */
1341 agino = XFS_INO_TO_AGINO(mp, ip->i_ino);
1342 ASSERT(agino != 0);
1343 bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001344 ASSERT(agi->agi_unlinked[bucket_index] != cpu_to_be32(NULLAGINO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 ASSERT(agi->agi_unlinked[bucket_index]);
1346
Christoph Hellwig16259e72005-11-02 15:11:25 +11001347 if (be32_to_cpu(agi->agi_unlinked[bucket_index]) == agino) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 /*
Christoph Hellwig475ee412012-07-03 12:21:22 -04001349 * We're at the head of the list. Get the inode's on-disk
1350 * buffer to see if there is anyone after us on the list.
1351 * Only modify our next pointer if it is not already NULLAGINO.
1352 * This saves us the overhead of dealing with the buffer when
1353 * there is no need to change it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 */
Christoph Hellwig475ee412012-07-03 12:21:22 -04001355 error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp,
1356 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 if (error) {
Christoph Hellwig475ee412012-07-03 12:21:22 -04001358 xfs_warn(mp, "%s: xfs_imap_to_bp returned error %d.",
Dave Chinner0b932cc2011-03-07 10:08:35 +11001359 __func__, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 return error;
1361 }
Christoph Hellwig347d1c02007-08-28 13:57:51 +10001362 next_agino = be32_to_cpu(dip->di_next_unlinked);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 ASSERT(next_agino != 0);
1364 if (next_agino != NULLAGINO) {
Christoph Hellwig347d1c02007-08-28 13:57:51 +10001365 dip->di_next_unlinked = cpu_to_be32(NULLAGINO);
Christoph Hellwig92bfc6e2008-11-28 14:23:41 +11001366 offset = ip->i_imap.im_boffset +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 offsetof(xfs_dinode_t, di_next_unlinked);
1368 xfs_trans_inode_buf(tp, ibp);
1369 xfs_trans_log_buf(tp, ibp, offset,
1370 (offset + sizeof(xfs_agino_t) - 1));
1371 xfs_inobp_check(mp, ibp);
1372 } else {
1373 xfs_trans_brelse(tp, ibp);
1374 }
1375 /*
1376 * Point the bucket head pointer at the next inode.
1377 */
1378 ASSERT(next_agino != 0);
1379 ASSERT(next_agino != agino);
Christoph Hellwig16259e72005-11-02 15:11:25 +11001380 agi->agi_unlinked[bucket_index] = cpu_to_be32(next_agino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 offset = offsetof(xfs_agi_t, agi_unlinked) +
1382 (sizeof(xfs_agino_t) * bucket_index);
1383 xfs_trans_log_buf(tp, agibp, offset,
1384 (offset + sizeof(xfs_agino_t) - 1));
1385 } else {
1386 /*
1387 * We need to search the list for the inode being freed.
1388 */
Christoph Hellwig16259e72005-11-02 15:11:25 +11001389 next_agino = be32_to_cpu(agi->agi_unlinked[bucket_index]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 last_ibp = NULL;
1391 while (next_agino != agino) {
Christoph Hellwig129dbc92012-07-03 12:21:51 -04001392 struct xfs_imap imap;
1393
1394 if (last_ibp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 xfs_trans_brelse(tp, last_ibp);
Christoph Hellwig129dbc92012-07-03 12:21:51 -04001396
1397 imap.im_blkno = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 next_ino = XFS_AGINO_TO_INO(mp, agno, next_agino);
Christoph Hellwig129dbc92012-07-03 12:21:51 -04001399
1400 error = xfs_imap(mp, tp, next_ino, &imap, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 if (error) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001402 xfs_warn(mp,
Christoph Hellwig129dbc92012-07-03 12:21:51 -04001403 "%s: xfs_imap returned error %d.",
1404 __func__, error);
1405 return error;
1406 }
1407
1408 error = xfs_imap_to_bp(mp, tp, &imap, &last_dip,
1409 &last_ibp, 0, 0);
1410 if (error) {
1411 xfs_warn(mp,
1412 "%s: xfs_imap_to_bp returned error %d.",
Dave Chinner0b932cc2011-03-07 10:08:35 +11001413 __func__, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 return error;
1415 }
Christoph Hellwig129dbc92012-07-03 12:21:51 -04001416
1417 last_offset = imap.im_boffset;
Christoph Hellwig347d1c02007-08-28 13:57:51 +10001418 next_agino = be32_to_cpu(last_dip->di_next_unlinked);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 ASSERT(next_agino != NULLAGINO);
1420 ASSERT(next_agino != 0);
1421 }
Christoph Hellwig475ee412012-07-03 12:21:22 -04001422
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 /*
Christoph Hellwig475ee412012-07-03 12:21:22 -04001424 * Now last_ibp points to the buffer previous to us on the
1425 * unlinked list. Pull us from the list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 */
Christoph Hellwig475ee412012-07-03 12:21:22 -04001427 error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp,
1428 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 if (error) {
Christoph Hellwig475ee412012-07-03 12:21:22 -04001430 xfs_warn(mp, "%s: xfs_imap_to_bp(2) returned error %d.",
Dave Chinner0b932cc2011-03-07 10:08:35 +11001431 __func__, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 return error;
1433 }
Christoph Hellwig347d1c02007-08-28 13:57:51 +10001434 next_agino = be32_to_cpu(dip->di_next_unlinked);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 ASSERT(next_agino != 0);
1436 ASSERT(next_agino != agino);
1437 if (next_agino != NULLAGINO) {
Christoph Hellwig347d1c02007-08-28 13:57:51 +10001438 dip->di_next_unlinked = cpu_to_be32(NULLAGINO);
Christoph Hellwig92bfc6e2008-11-28 14:23:41 +11001439 offset = ip->i_imap.im_boffset +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 offsetof(xfs_dinode_t, di_next_unlinked);
1441 xfs_trans_inode_buf(tp, ibp);
1442 xfs_trans_log_buf(tp, ibp, offset,
1443 (offset + sizeof(xfs_agino_t) - 1));
1444 xfs_inobp_check(mp, ibp);
1445 } else {
1446 xfs_trans_brelse(tp, ibp);
1447 }
1448 /*
1449 * Point the previous inode on the list to the next inode.
1450 */
Christoph Hellwig347d1c02007-08-28 13:57:51 +10001451 last_dip->di_next_unlinked = cpu_to_be32(next_agino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 ASSERT(next_agino != 0);
1453 offset = last_offset + offsetof(xfs_dinode_t, di_next_unlinked);
1454 xfs_trans_inode_buf(tp, last_ibp);
1455 xfs_trans_log_buf(tp, last_ibp, offset,
1456 (offset + sizeof(xfs_agino_t) - 1));
1457 xfs_inobp_check(mp, last_ibp);
1458 }
1459 return 0;
1460}
1461
Dave Chinner5b3eed72010-08-24 11:42:41 +10001462/*
1463 * A big issue when freeing the inode cluster is is that we _cannot_ skip any
1464 * inodes that are in memory - they all must be marked stale and attached to
1465 * the cluster buffer.
1466 */
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +00001467STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468xfs_ifree_cluster(
1469 xfs_inode_t *free_ip,
1470 xfs_trans_t *tp,
1471 xfs_ino_t inum)
1472{
1473 xfs_mount_t *mp = free_ip->i_mount;
1474 int blks_per_cluster;
1475 int nbufs;
1476 int ninodes;
Dave Chinner5b257b42010-06-03 16:22:29 +10001477 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 xfs_daddr_t blkno;
1479 xfs_buf_t *bp;
Dave Chinner5b257b42010-06-03 16:22:29 +10001480 xfs_inode_t *ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 xfs_inode_log_item_t *iip;
1482 xfs_log_item_t *lip;
Dave Chinner5017e972010-01-11 11:47:40 +00001483 struct xfs_perag *pag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484
Dave Chinner5017e972010-01-11 11:47:40 +00001485 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, inum));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 if (mp->m_sb.sb_blocksize >= XFS_INODE_CLUSTER_SIZE(mp)) {
1487 blks_per_cluster = 1;
1488 ninodes = mp->m_sb.sb_inopblock;
1489 nbufs = XFS_IALLOC_BLOCKS(mp);
1490 } else {
1491 blks_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) /
1492 mp->m_sb.sb_blocksize;
1493 ninodes = blks_per_cluster * mp->m_sb.sb_inopblock;
1494 nbufs = XFS_IALLOC_BLOCKS(mp) / blks_per_cluster;
1495 }
1496
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 for (j = 0; j < nbufs; j++, inum += ninodes) {
1498 blkno = XFS_AGB_TO_DADDR(mp, XFS_INO_TO_AGNO(mp, inum),
1499 XFS_INO_TO_AGBNO(mp, inum));
1500
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 /*
Dave Chinner5b257b42010-06-03 16:22:29 +10001502 * We obtain and lock the backing buffer first in the process
1503 * here, as we have to ensure that any dirty inode that we
1504 * can't get the flush lock on is attached to the buffer.
1505 * If we scan the in-memory inodes first, then buffer IO can
1506 * complete before we get a lock on it, and hence we may fail
1507 * to mark all the active inodes on the buffer stale.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 */
Dave Chinner5b257b42010-06-03 16:22:29 +10001509 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, blkno,
Dave Chinnera8acad72012-04-23 15:58:54 +10001510 mp->m_bsize * blks_per_cluster, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +00001512 if (!bp)
1513 return ENOMEM;
Dave Chinner5b257b42010-06-03 16:22:29 +10001514 /*
1515 * Walk the inodes already attached to the buffer and mark them
1516 * stale. These will all have the flush locks held, so an
Dave Chinner5b3eed72010-08-24 11:42:41 +10001517 * in-memory inode walk can't lock them. By marking them all
1518 * stale first, we will not attempt to lock them in the loop
1519 * below as the XFS_ISTALE flag will be set.
Dave Chinner5b257b42010-06-03 16:22:29 +10001520 */
Christoph Hellwigadadbee2011-07-13 13:43:49 +02001521 lip = bp->b_fspriv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 while (lip) {
1523 if (lip->li_type == XFS_LI_INODE) {
1524 iip = (xfs_inode_log_item_t *)lip;
1525 ASSERT(iip->ili_logged == 1);
Christoph Hellwigca30b2a2010-06-23 18:11:15 +10001526 lip->li_cb = xfs_istale_done;
David Chinner7b2e2a32008-10-30 17:39:12 +11001527 xfs_trans_ail_copy_lsn(mp->m_ail,
1528 &iip->ili_flush_lsn,
1529 &iip->ili_item.li_lsn);
David Chinnere5ffd2b2006-11-21 18:55:33 +11001530 xfs_iflags_set(iip->ili_inode, XFS_ISTALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 }
1532 lip = lip->li_bio_list;
1533 }
1534
Dave Chinner5b3eed72010-08-24 11:42:41 +10001535
Dave Chinner5b257b42010-06-03 16:22:29 +10001536 /*
1537 * For each inode in memory attempt to add it to the inode
1538 * buffer and set it up for being staled on buffer IO
1539 * completion. This is safe as we've locked out tail pushing
1540 * and flushing by locking the buffer.
1541 *
1542 * We have already marked every inode that was part of a
1543 * transaction stale above, which means there is no point in
1544 * even trying to lock them.
1545 */
1546 for (i = 0; i < ninodes; i++) {
Dave Chinner5b3eed72010-08-24 11:42:41 +10001547retry:
Dave Chinner1a3e8f32010-12-17 17:29:43 +11001548 rcu_read_lock();
Dave Chinner5b257b42010-06-03 16:22:29 +10001549 ip = radix_tree_lookup(&pag->pag_ici_root,
1550 XFS_INO_TO_AGINO(mp, (inum + i)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551
Dave Chinner1a3e8f32010-12-17 17:29:43 +11001552 /* Inode not in memory, nothing to do */
1553 if (!ip) {
1554 rcu_read_unlock();
Dave Chinner5b257b42010-06-03 16:22:29 +10001555 continue;
1556 }
1557
Dave Chinner5b3eed72010-08-24 11:42:41 +10001558 /*
Dave Chinner1a3e8f32010-12-17 17:29:43 +11001559 * because this is an RCU protected lookup, we could
1560 * find a recently freed or even reallocated inode
1561 * during the lookup. We need to check under the
1562 * i_flags_lock for a valid inode here. Skip it if it
1563 * is not valid, the wrong inode or stale.
1564 */
1565 spin_lock(&ip->i_flags_lock);
1566 if (ip->i_ino != inum + i ||
1567 __xfs_iflags_test(ip, XFS_ISTALE)) {
1568 spin_unlock(&ip->i_flags_lock);
1569 rcu_read_unlock();
1570 continue;
1571 }
1572 spin_unlock(&ip->i_flags_lock);
1573
1574 /*
Dave Chinner5b3eed72010-08-24 11:42:41 +10001575 * Don't try to lock/unlock the current inode, but we
1576 * _cannot_ skip the other inodes that we did not find
1577 * in the list attached to the buffer and are not
1578 * already marked stale. If we can't lock it, back off
1579 * and retry.
1580 */
Dave Chinner5b257b42010-06-03 16:22:29 +10001581 if (ip != free_ip &&
1582 !xfs_ilock_nowait(ip, XFS_ILOCK_EXCL)) {
Dave Chinner1a3e8f32010-12-17 17:29:43 +11001583 rcu_read_unlock();
Dave Chinner5b3eed72010-08-24 11:42:41 +10001584 delay(1);
1585 goto retry;
Dave Chinner5b257b42010-06-03 16:22:29 +10001586 }
Dave Chinner1a3e8f32010-12-17 17:29:43 +11001587 rcu_read_unlock();
Dave Chinner5b257b42010-06-03 16:22:29 +10001588
Dave Chinner5b3eed72010-08-24 11:42:41 +10001589 xfs_iflock(ip);
Dave Chinner5b257b42010-06-03 16:22:29 +10001590 xfs_iflags_set(ip, XFS_ISTALE);
Dave Chinner5b257b42010-06-03 16:22:29 +10001591
Dave Chinner5b3eed72010-08-24 11:42:41 +10001592 /*
1593 * we don't need to attach clean inodes or those only
1594 * with unlogged changes (which we throw away, anyway).
1595 */
Dave Chinner5b257b42010-06-03 16:22:29 +10001596 iip = ip->i_itemp;
Dave Chinner5b3eed72010-08-24 11:42:41 +10001597 if (!iip || xfs_inode_clean(ip)) {
Dave Chinner5b257b42010-06-03 16:22:29 +10001598 ASSERT(ip != free_ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 xfs_ifunlock(ip);
1600 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1601 continue;
1602 }
1603
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +00001604 iip->ili_last_fields = iip->ili_fields;
1605 iip->ili_fields = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 iip->ili_logged = 1;
David Chinner7b2e2a32008-10-30 17:39:12 +11001607 xfs_trans_ail_copy_lsn(mp->m_ail, &iip->ili_flush_lsn,
1608 &iip->ili_item.li_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609
Christoph Hellwigca30b2a2010-06-23 18:11:15 +10001610 xfs_buf_attach_iodone(bp, xfs_istale_done,
1611 &iip->ili_item);
Dave Chinner5b257b42010-06-03 16:22:29 +10001612
1613 if (ip != free_ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 }
1616
Dave Chinner5b3eed72010-08-24 11:42:41 +10001617 xfs_trans_stale_inode_buf(tp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 xfs_trans_binval(tp, bp);
1619 }
1620
Dave Chinner5017e972010-01-11 11:47:40 +00001621 xfs_perag_put(pag);
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +00001622 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623}
1624
1625/*
1626 * This is called to return an inode to the inode free list.
1627 * The inode should already be truncated to 0 length and have
1628 * no pages associated with it. This routine also assumes that
1629 * the inode is already a part of the transaction.
1630 *
1631 * The on-disk copy of the inode will have been added to the list
1632 * of unlinked inodes in the AGI. We need to remove the inode from
1633 * that list atomically with respect to freeing it here.
1634 */
1635int
1636xfs_ifree(
1637 xfs_trans_t *tp,
1638 xfs_inode_t *ip,
1639 xfs_bmap_free_t *flist)
1640{
1641 int error;
1642 int delete;
1643 xfs_ino_t first_ino;
Vlad Apostolovc319b582007-11-23 16:27:51 +11001644 xfs_dinode_t *dip;
1645 xfs_buf_t *ibp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646
Christoph Hellwig579aa9c2008-04-22 17:34:00 +10001647 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 ASSERT(ip->i_d.di_nlink == 0);
1649 ASSERT(ip->i_d.di_nextents == 0);
1650 ASSERT(ip->i_d.di_anextents == 0);
Christoph Hellwigce7ae1512011-12-18 20:00:11 +00001651 ASSERT(ip->i_d.di_size == 0 || !S_ISREG(ip->i_d.di_mode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 ASSERT(ip->i_d.di_nblocks == 0);
1653
1654 /*
1655 * Pull the on-disk inode from the AGI unlinked list.
1656 */
1657 error = xfs_iunlink_remove(tp, ip);
1658 if (error != 0) {
1659 return error;
1660 }
1661
1662 error = xfs_difree(tp, ip->i_ino, flist, &delete, &first_ino);
1663 if (error != 0) {
1664 return error;
1665 }
1666 ip->i_d.di_mode = 0; /* mark incore inode as free */
1667 ip->i_d.di_flags = 0;
1668 ip->i_d.di_dmevmask = 0;
1669 ip->i_d.di_forkoff = 0; /* mark the attr fork not in use */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 ip->i_d.di_format = XFS_DINODE_FMT_EXTENTS;
1671 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
1672 /*
1673 * Bump the generation count so no one will be confused
1674 * by reincarnations of this inode.
1675 */
1676 ip->i_d.di_gen++;
Vlad Apostolovc319b582007-11-23 16:27:51 +11001677
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1679
Christoph Hellwig475ee412012-07-03 12:21:22 -04001680 error = xfs_imap_to_bp(ip->i_mount, tp, &ip->i_imap, &dip, &ibp,
1681 0, 0);
Vlad Apostolovc319b582007-11-23 16:27:51 +11001682 if (error)
1683 return error;
1684
1685 /*
1686 * Clear the on-disk di_mode. This is to prevent xfs_bulkstat
1687 * from picking up this inode when it is reclaimed (its incore state
1688 * initialzed but not flushed to disk yet). The in-core di_mode is
1689 * already cleared and a corresponding transaction logged.
1690 * The hack here just synchronizes the in-core to on-disk
1691 * di_mode value in advance before the actual inode sync to disk.
1692 * This is OK because the inode is already unlinked and would never
1693 * change its di_mode again for this inode generation.
1694 * This is a temporary hack that would require a proper fix
1695 * in the future.
1696 */
Christoph Hellwig81591fe2008-11-28 14:23:39 +11001697 dip->di_mode = 0;
Vlad Apostolovc319b582007-11-23 16:27:51 +11001698
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 if (delete) {
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +00001700 error = xfs_ifree_cluster(ip, tp, first_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 }
1702
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +00001703 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704}
1705
1706/*
1707 * Reallocate the space for if_broot based on the number of records
1708 * being added or deleted as indicated in rec_diff. Move the records
1709 * and pointers in if_broot to fit the new size. When shrinking this
1710 * will eliminate holes between the records and pointers created by
1711 * the caller. When growing this will create holes to be filled in
1712 * by the caller.
1713 *
1714 * The caller must not request to add more records than would fit in
1715 * the on-disk inode root. If the if_broot is currently NULL, then
1716 * if we adding records one will be allocated. The caller must also
1717 * not request that the number of records go below zero, although
1718 * it can go to zero.
1719 *
1720 * ip -- the inode whose if_broot area is changing
1721 * ext_diff -- the change in the number of records, positive or negative,
1722 * requested for the if_broot array.
1723 */
1724void
1725xfs_iroot_realloc(
1726 xfs_inode_t *ip,
1727 int rec_diff,
1728 int whichfork)
1729{
Christoph Hellwig60197e82008-10-30 17:11:19 +11001730 struct xfs_mount *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 int cur_max;
1732 xfs_ifork_t *ifp;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11001733 struct xfs_btree_block *new_broot;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 int new_max;
1735 size_t new_size;
1736 char *np;
1737 char *op;
1738
1739 /*
1740 * Handle the degenerate case quietly.
1741 */
1742 if (rec_diff == 0) {
1743 return;
1744 }
1745
1746 ifp = XFS_IFORK_PTR(ip, whichfork);
1747 if (rec_diff > 0) {
1748 /*
1749 * If there wasn't any memory allocated before, just
1750 * allocate it now and get out.
1751 */
1752 if (ifp->if_broot_bytes == 0) {
1753 new_size = (size_t)XFS_BMAP_BROOT_SPACE_CALC(rec_diff);
Dave Chinner4a7eddd2010-07-20 17:53:59 +10001754 ifp->if_broot = kmem_alloc(new_size, KM_SLEEP | KM_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 ifp->if_broot_bytes = (int)new_size;
1756 return;
1757 }
1758
1759 /*
1760 * If there is already an existing if_broot, then we need
1761 * to realloc() it and shift the pointers to their new
1762 * location. The records don't change location because
1763 * they are kept butted up against the btree block header.
1764 */
Christoph Hellwig60197e82008-10-30 17:11:19 +11001765 cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 new_max = cur_max + rec_diff;
1767 new_size = (size_t)XFS_BMAP_BROOT_SPACE_CALC(new_max);
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11001768 ifp->if_broot = kmem_realloc(ifp->if_broot, new_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 (size_t)XFS_BMAP_BROOT_SPACE_CALC(cur_max), /* old size */
Dave Chinner4a7eddd2010-07-20 17:53:59 +10001770 KM_SLEEP | KM_NOFS);
Christoph Hellwig60197e82008-10-30 17:11:19 +11001771 op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
1772 ifp->if_broot_bytes);
1773 np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
1774 (int)new_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 ifp->if_broot_bytes = (int)new_size;
1776 ASSERT(ifp->if_broot_bytes <=
1777 XFS_IFORK_SIZE(ip, whichfork) + XFS_BROOT_SIZE_ADJ);
1778 memmove(np, op, cur_max * (uint)sizeof(xfs_dfsbno_t));
1779 return;
1780 }
1781
1782 /*
1783 * rec_diff is less than 0. In this case, we are shrinking the
1784 * if_broot buffer. It must already exist. If we go to zero
1785 * records, just get rid of the root and clear the status bit.
1786 */
1787 ASSERT((ifp->if_broot != NULL) && (ifp->if_broot_bytes > 0));
Christoph Hellwig60197e82008-10-30 17:11:19 +11001788 cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 new_max = cur_max + rec_diff;
1790 ASSERT(new_max >= 0);
1791 if (new_max > 0)
1792 new_size = (size_t)XFS_BMAP_BROOT_SPACE_CALC(new_max);
1793 else
1794 new_size = 0;
1795 if (new_size > 0) {
Dave Chinner4a7eddd2010-07-20 17:53:59 +10001796 new_broot = kmem_alloc(new_size, KM_SLEEP | KM_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 /*
1798 * First copy over the btree block header.
1799 */
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11001800 memcpy(new_broot, ifp->if_broot, XFS_BTREE_LBLOCK_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 } else {
1802 new_broot = NULL;
1803 ifp->if_flags &= ~XFS_IFBROOT;
1804 }
1805
1806 /*
1807 * Only copy the records and pointers if there are any.
1808 */
1809 if (new_max > 0) {
1810 /*
1811 * First copy the records.
1812 */
Christoph Hellwig136341b2008-10-30 17:11:40 +11001813 op = (char *)XFS_BMBT_REC_ADDR(mp, ifp->if_broot, 1);
1814 np = (char *)XFS_BMBT_REC_ADDR(mp, new_broot, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 memcpy(np, op, new_max * (uint)sizeof(xfs_bmbt_rec_t));
1816
1817 /*
1818 * Then copy the pointers.
1819 */
Christoph Hellwig60197e82008-10-30 17:11:19 +11001820 op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 ifp->if_broot_bytes);
Christoph Hellwig60197e82008-10-30 17:11:19 +11001822 np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, new_broot, 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 (int)new_size);
1824 memcpy(np, op, new_max * (uint)sizeof(xfs_dfsbno_t));
1825 }
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001826 kmem_free(ifp->if_broot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 ifp->if_broot = new_broot;
1828 ifp->if_broot_bytes = (int)new_size;
1829 ASSERT(ifp->if_broot_bytes <=
1830 XFS_IFORK_SIZE(ip, whichfork) + XFS_BROOT_SIZE_ADJ);
1831 return;
1832}
1833
1834
1835/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 * This is called when the amount of space needed for if_data
1837 * is increased or decreased. The change in size is indicated by
1838 * the number of bytes that need to be added or deleted in the
1839 * byte_diff parameter.
1840 *
1841 * If the amount of space needed has decreased below the size of the
1842 * inline buffer, then switch to using the inline buffer. Otherwise,
1843 * use kmem_realloc() or kmem_alloc() to adjust the size of the buffer
1844 * to what is needed.
1845 *
1846 * ip -- the inode whose if_data area is changing
1847 * byte_diff -- the change in the number of bytes, positive or negative,
1848 * requested for the if_data array.
1849 */
1850void
1851xfs_idata_realloc(
1852 xfs_inode_t *ip,
1853 int byte_diff,
1854 int whichfork)
1855{
1856 xfs_ifork_t *ifp;
1857 int new_size;
1858 int real_size;
1859
1860 if (byte_diff == 0) {
1861 return;
1862 }
1863
1864 ifp = XFS_IFORK_PTR(ip, whichfork);
1865 new_size = (int)ifp->if_bytes + byte_diff;
1866 ASSERT(new_size >= 0);
1867
1868 if (new_size == 0) {
1869 if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) {
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001870 kmem_free(ifp->if_u1.if_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 }
1872 ifp->if_u1.if_data = NULL;
1873 real_size = 0;
1874 } else if (new_size <= sizeof(ifp->if_u2.if_inline_data)) {
1875 /*
1876 * If the valid extents/data can fit in if_inline_ext/data,
1877 * copy them from the malloc'd vector and free it.
1878 */
1879 if (ifp->if_u1.if_data == NULL) {
1880 ifp->if_u1.if_data = ifp->if_u2.if_inline_data;
1881 } else if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) {
1882 ASSERT(ifp->if_real_bytes != 0);
1883 memcpy(ifp->if_u2.if_inline_data, ifp->if_u1.if_data,
1884 new_size);
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001885 kmem_free(ifp->if_u1.if_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 ifp->if_u1.if_data = ifp->if_u2.if_inline_data;
1887 }
1888 real_size = 0;
1889 } else {
1890 /*
1891 * Stuck with malloc/realloc.
1892 * For inline data, the underlying buffer must be
1893 * a multiple of 4 bytes in size so that it can be
1894 * logged and stay on word boundaries. We enforce
1895 * that here.
1896 */
1897 real_size = roundup(new_size, 4);
1898 if (ifp->if_u1.if_data == NULL) {
1899 ASSERT(ifp->if_real_bytes == 0);
Dave Chinner4a7eddd2010-07-20 17:53:59 +10001900 ifp->if_u1.if_data = kmem_alloc(real_size,
1901 KM_SLEEP | KM_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 } else if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) {
1903 /*
1904 * Only do the realloc if the underlying size
1905 * is really changing.
1906 */
1907 if (ifp->if_real_bytes != real_size) {
1908 ifp->if_u1.if_data =
1909 kmem_realloc(ifp->if_u1.if_data,
1910 real_size,
1911 ifp->if_real_bytes,
Dave Chinner4a7eddd2010-07-20 17:53:59 +10001912 KM_SLEEP | KM_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 }
1914 } else {
1915 ASSERT(ifp->if_real_bytes == 0);
Dave Chinner4a7eddd2010-07-20 17:53:59 +10001916 ifp->if_u1.if_data = kmem_alloc(real_size,
1917 KM_SLEEP | KM_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 memcpy(ifp->if_u1.if_data, ifp->if_u2.if_inline_data,
1919 ifp->if_bytes);
1920 }
1921 }
1922 ifp->if_real_bytes = real_size;
1923 ifp->if_bytes = new_size;
1924 ASSERT(ifp->if_bytes <= XFS_IFORK_SIZE(ip, whichfork));
1925}
1926
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927void
1928xfs_idestroy_fork(
1929 xfs_inode_t *ip,
1930 int whichfork)
1931{
1932 xfs_ifork_t *ifp;
1933
1934 ifp = XFS_IFORK_PTR(ip, whichfork);
1935 if (ifp->if_broot != NULL) {
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001936 kmem_free(ifp->if_broot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 ifp->if_broot = NULL;
1938 }
1939
1940 /*
1941 * If the format is local, then we can't have an extents
1942 * array so just look for an inline data array. If we're
1943 * not local then we may or may not have an extents list,
1944 * so check and free it up if we do.
1945 */
1946 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
1947 if ((ifp->if_u1.if_data != ifp->if_u2.if_inline_data) &&
1948 (ifp->if_u1.if_data != NULL)) {
1949 ASSERT(ifp->if_real_bytes != 0);
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001950 kmem_free(ifp->if_u1.if_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 ifp->if_u1.if_data = NULL;
1952 ifp->if_real_bytes = 0;
1953 }
1954 } else if ((ifp->if_flags & XFS_IFEXTENTS) &&
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11001955 ((ifp->if_flags & XFS_IFEXTIREC) ||
1956 ((ifp->if_u1.if_extents != NULL) &&
1957 (ifp->if_u1.if_extents != ifp->if_u2.if_inline_ext)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 ASSERT(ifp->if_real_bytes != 0);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001959 xfs_iext_destroy(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 }
1961 ASSERT(ifp->if_u1.if_extents == NULL ||
1962 ifp->if_u1.if_extents == ifp->if_u2.if_inline_ext);
1963 ASSERT(ifp->if_real_bytes == 0);
1964 if (whichfork == XFS_ATTR_FORK) {
1965 kmem_zone_free(xfs_ifork_zone, ip->i_afp);
1966 ip->i_afp = NULL;
1967 }
1968}
1969
1970/*
Christoph Hellwig60ec6782010-02-17 19:43:56 +00001971 * This is called to unpin an inode. The caller must have the inode locked
1972 * in at least shared mode so that the buffer cannot be subsequently pinned
1973 * once someone is waiting for it to be unpinned.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 */
Christoph Hellwig60ec6782010-02-17 19:43:56 +00001975static void
Christoph Hellwigf392e632011-12-18 20:00:10 +00001976xfs_iunpin(
Christoph Hellwig60ec6782010-02-17 19:43:56 +00001977 struct xfs_inode *ip)
David Chinnera3f74ff2008-03-06 13:43:42 +11001978{
Christoph Hellwig579aa9c2008-04-22 17:34:00 +10001979 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
David Chinnera3f74ff2008-03-06 13:43:42 +11001980
Dave Chinner4aaf15d2010-03-08 11:24:07 +11001981 trace_xfs_inode_unpin_nowait(ip, _RET_IP_);
1982
David Chinnera3f74ff2008-03-06 13:43:42 +11001983 /* Give the log a push to start the unpinning I/O */
Christoph Hellwig60ec6782010-02-17 19:43:56 +00001984 xfs_log_force_lsn(ip->i_mount, ip->i_itemp->ili_last_lsn, 0);
Christoph Hellwiga14a3482010-01-19 09:56:46 +00001985
David Chinnera3f74ff2008-03-06 13:43:42 +11001986}
1987
Christoph Hellwigf392e632011-12-18 20:00:10 +00001988static void
1989__xfs_iunpin_wait(
1990 struct xfs_inode *ip)
1991{
1992 wait_queue_head_t *wq = bit_waitqueue(&ip->i_flags, __XFS_IPINNED_BIT);
1993 DEFINE_WAIT_BIT(wait, &ip->i_flags, __XFS_IPINNED_BIT);
1994
1995 xfs_iunpin(ip);
1996
1997 do {
1998 prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
1999 if (xfs_ipincount(ip))
2000 io_schedule();
2001 } while (xfs_ipincount(ip));
2002 finish_wait(wq, &wait.wait);
2003}
2004
Dave Chinner777df5a2010-02-06 12:37:26 +11002005void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006xfs_iunpin_wait(
Christoph Hellwig60ec6782010-02-17 19:43:56 +00002007 struct xfs_inode *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008{
Christoph Hellwigf392e632011-12-18 20:00:10 +00002009 if (xfs_ipincount(ip))
2010 __xfs_iunpin_wait(ip);
David Chinnera3f74ff2008-03-06 13:43:42 +11002011}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013/*
2014 * xfs_iextents_copy()
2015 *
2016 * This is called to copy the REAL extents (as opposed to the delayed
2017 * allocation extents) from the inode into the given buffer. It
2018 * returns the number of bytes copied into the buffer.
2019 *
2020 * If there are no delayed allocation extents, then we can just
2021 * memcpy() the extents into the buffer. Otherwise, we need to
2022 * examine each extent in turn and skip those which are delayed.
2023 */
2024int
2025xfs_iextents_copy(
2026 xfs_inode_t *ip,
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10002027 xfs_bmbt_rec_t *dp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 int whichfork)
2029{
2030 int copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 int i;
2032 xfs_ifork_t *ifp;
2033 int nrecs;
2034 xfs_fsblock_t start_block;
2035
2036 ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwig579aa9c2008-04-22 17:34:00 +10002037 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 ASSERT(ifp->if_bytes > 0);
2039
2040 nrecs = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
Eric Sandeen3a59c942007-07-11 11:09:47 +10002041 XFS_BMAP_TRACE_EXLIST(ip, nrecs, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 ASSERT(nrecs > 0);
2043
2044 /*
2045 * There are some delayed allocation extents in the
2046 * inode, so copy the extents one at a time and skip
2047 * the delayed ones. There must be at least one
2048 * non-delayed extent.
2049 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 copied = 0;
2051 for (i = 0; i < nrecs; i++) {
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10002052 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 start_block = xfs_bmbt_get_startblock(ep);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002054 if (isnullstartblock(start_block)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 /*
2056 * It's a delayed allocation extent, so skip it.
2057 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 continue;
2059 }
2060
2061 /* Translate to on disk format */
Christoph Hellwigcd8b0a92007-08-16 16:24:15 +10002062 put_unaligned(cpu_to_be64(ep->l0), &dp->l0);
2063 put_unaligned(cpu_to_be64(ep->l1), &dp->l1);
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10002064 dp++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 copied++;
2066 }
2067 ASSERT(copied != 0);
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10002068 xfs_validate_extents(ifp, copied, XFS_EXTFMT_INODE(ip));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069
2070 return (copied * (uint)sizeof(xfs_bmbt_rec_t));
2071}
2072
2073/*
2074 * Each of the following cases stores data into the same region
2075 * of the on-disk inode, so only one of them can be valid at
2076 * any given time. While it is possible to have conflicting formats
2077 * and log flags, e.g. having XFS_ILOG_?DATA set when the fork is
2078 * in EXTENTS format, this can only happen when the fork has
2079 * changed formats after being modified but before being flushed.
2080 * In these cases, the format always takes precedence, because the
2081 * format indicates the current state of the fork.
2082 */
2083/*ARGSUSED*/
David Chinnere4ac9672008-04-10 12:23:58 +10002084STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085xfs_iflush_fork(
2086 xfs_inode_t *ip,
2087 xfs_dinode_t *dip,
2088 xfs_inode_log_item_t *iip,
2089 int whichfork,
2090 xfs_buf_t *bp)
2091{
2092 char *cp;
2093 xfs_ifork_t *ifp;
2094 xfs_mount_t *mp;
2095#ifdef XFS_TRANS_DEBUG
2096 int first;
2097#endif
2098 static const short brootflag[2] =
2099 { XFS_ILOG_DBROOT, XFS_ILOG_ABROOT };
2100 static const short dataflag[2] =
2101 { XFS_ILOG_DDATA, XFS_ILOG_ADATA };
2102 static const short extflag[2] =
2103 { XFS_ILOG_DEXT, XFS_ILOG_AEXT };
2104
David Chinnere4ac9672008-04-10 12:23:58 +10002105 if (!iip)
2106 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 ifp = XFS_IFORK_PTR(ip, whichfork);
2108 /*
2109 * This can happen if we gave up in iformat in an error path,
2110 * for the attribute fork.
2111 */
David Chinnere4ac9672008-04-10 12:23:58 +10002112 if (!ifp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 ASSERT(whichfork == XFS_ATTR_FORK);
David Chinnere4ac9672008-04-10 12:23:58 +10002114 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 }
2116 cp = XFS_DFORK_PTR(dip, whichfork);
2117 mp = ip->i_mount;
2118 switch (XFS_IFORK_FORMAT(ip, whichfork)) {
2119 case XFS_DINODE_FMT_LOCAL:
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +00002120 if ((iip->ili_fields & dataflag[whichfork]) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 (ifp->if_bytes > 0)) {
2122 ASSERT(ifp->if_u1.if_data != NULL);
2123 ASSERT(ifp->if_bytes <= XFS_IFORK_SIZE(ip, whichfork));
2124 memcpy(cp, ifp->if_u1.if_data, ifp->if_bytes);
2125 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 break;
2127
2128 case XFS_DINODE_FMT_EXTENTS:
2129 ASSERT((ifp->if_flags & XFS_IFEXTENTS) ||
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +00002130 !(iip->ili_fields & extflag[whichfork]));
2131 if ((iip->ili_fields & extflag[whichfork]) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 (ifp->if_bytes > 0)) {
Christoph Hellwigab1908a2011-05-11 15:04:10 +00002133 ASSERT(xfs_iext_get_ext(ifp, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) > 0);
2135 (void)xfs_iextents_copy(ip, (xfs_bmbt_rec_t *)cp,
2136 whichfork);
2137 }
2138 break;
2139
2140 case XFS_DINODE_FMT_BTREE:
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +00002141 if ((iip->ili_fields & brootflag[whichfork]) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 (ifp->if_broot_bytes > 0)) {
2143 ASSERT(ifp->if_broot != NULL);
2144 ASSERT(ifp->if_broot_bytes <=
2145 (XFS_IFORK_SIZE(ip, whichfork) +
2146 XFS_BROOT_SIZE_ADJ));
Christoph Hellwig60197e82008-10-30 17:11:19 +11002147 xfs_bmbt_to_bmdr(mp, ifp->if_broot, ifp->if_broot_bytes,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 (xfs_bmdr_block_t *)cp,
2149 XFS_DFORK_SIZE(dip, mp, whichfork));
2150 }
2151 break;
2152
2153 case XFS_DINODE_FMT_DEV:
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +00002154 if (iip->ili_fields & XFS_ILOG_DEV) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 ASSERT(whichfork == XFS_DATA_FORK);
Christoph Hellwig81591fe2008-11-28 14:23:39 +11002156 xfs_dinode_put_rdev(dip, ip->i_df.if_u2.if_rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 }
2158 break;
2159
2160 case XFS_DINODE_FMT_UUID:
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +00002161 if (iip->ili_fields & XFS_ILOG_UUID) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 ASSERT(whichfork == XFS_DATA_FORK);
Christoph Hellwig81591fe2008-11-28 14:23:39 +11002163 memcpy(XFS_DFORK_DPTR(dip),
2164 &ip->i_df.if_u2.if_uuid,
2165 sizeof(uuid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 }
2167 break;
2168
2169 default:
2170 ASSERT(0);
2171 break;
2172 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173}
2174
David Chinnerbad55842008-03-06 13:43:49 +11002175STATIC int
2176xfs_iflush_cluster(
2177 xfs_inode_t *ip,
2178 xfs_buf_t *bp)
2179{
2180 xfs_mount_t *mp = ip->i_mount;
Dave Chinner5017e972010-01-11 11:47:40 +00002181 struct xfs_perag *pag;
David Chinnerbad55842008-03-06 13:43:49 +11002182 unsigned long first_index, mask;
David Chinnerc8f5f122008-05-20 11:30:15 +10002183 unsigned long inodes_per_cluster;
David Chinnerbad55842008-03-06 13:43:49 +11002184 int ilist_size;
2185 xfs_inode_t **ilist;
2186 xfs_inode_t *iq;
David Chinnerbad55842008-03-06 13:43:49 +11002187 int nr_found;
2188 int clcount = 0;
2189 int bufwasdelwri;
2190 int i;
2191
Dave Chinner5017e972010-01-11 11:47:40 +00002192 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
David Chinnerbad55842008-03-06 13:43:49 +11002193
David Chinnerc8f5f122008-05-20 11:30:15 +10002194 inodes_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_inodelog;
2195 ilist_size = inodes_per_cluster * sizeof(xfs_inode_t *);
David Chinner49383b02008-05-19 16:29:34 +10002196 ilist = kmem_alloc(ilist_size, KM_MAYFAIL|KM_NOFS);
David Chinnerbad55842008-03-06 13:43:49 +11002197 if (!ilist)
Dave Chinner44b56e02010-01-11 11:47:43 +00002198 goto out_put;
David Chinnerbad55842008-03-06 13:43:49 +11002199
2200 mask = ~(((XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_inodelog)) - 1);
2201 first_index = XFS_INO_TO_AGINO(mp, ip->i_ino) & mask;
Dave Chinner1a3e8f32010-12-17 17:29:43 +11002202 rcu_read_lock();
David Chinnerbad55842008-03-06 13:43:49 +11002203 /* really need a gang lookup range call here */
2204 nr_found = radix_tree_gang_lookup(&pag->pag_ici_root, (void**)ilist,
David Chinnerc8f5f122008-05-20 11:30:15 +10002205 first_index, inodes_per_cluster);
David Chinnerbad55842008-03-06 13:43:49 +11002206 if (nr_found == 0)
2207 goto out_free;
2208
2209 for (i = 0; i < nr_found; i++) {
2210 iq = ilist[i];
2211 if (iq == ip)
2212 continue;
Dave Chinner1a3e8f32010-12-17 17:29:43 +11002213
2214 /*
2215 * because this is an RCU protected lookup, we could find a
2216 * recently freed or even reallocated inode during the lookup.
2217 * We need to check under the i_flags_lock for a valid inode
2218 * here. Skip it if it is not valid or the wrong inode.
2219 */
2220 spin_lock(&ip->i_flags_lock);
2221 if (!ip->i_ino ||
2222 (XFS_INO_TO_AGINO(mp, iq->i_ino) & mask) != first_index) {
2223 spin_unlock(&ip->i_flags_lock);
2224 continue;
2225 }
2226 spin_unlock(&ip->i_flags_lock);
2227
David Chinnerbad55842008-03-06 13:43:49 +11002228 /*
2229 * Do an un-protected check to see if the inode is dirty and
2230 * is a candidate for flushing. These checks will be repeated
2231 * later after the appropriate locks are acquired.
2232 */
David Chinner33540402008-03-06 13:43:59 +11002233 if (xfs_inode_clean(iq) && xfs_ipincount(iq) == 0)
David Chinnerbad55842008-03-06 13:43:49 +11002234 continue;
David Chinnerbad55842008-03-06 13:43:49 +11002235
2236 /*
2237 * Try to get locks. If any are unavailable or it is pinned,
2238 * then this inode cannot be flushed and is skipped.
2239 */
2240
2241 if (!xfs_ilock_nowait(iq, XFS_ILOCK_SHARED))
2242 continue;
2243 if (!xfs_iflock_nowait(iq)) {
2244 xfs_iunlock(iq, XFS_ILOCK_SHARED);
2245 continue;
2246 }
2247 if (xfs_ipincount(iq)) {
2248 xfs_ifunlock(iq);
2249 xfs_iunlock(iq, XFS_ILOCK_SHARED);
2250 continue;
2251 }
2252
2253 /*
2254 * arriving here means that this inode can be flushed. First
2255 * re-check that it's dirty before flushing.
2256 */
David Chinner33540402008-03-06 13:43:59 +11002257 if (!xfs_inode_clean(iq)) {
2258 int error;
David Chinnerbad55842008-03-06 13:43:49 +11002259 error = xfs_iflush_int(iq, bp);
2260 if (error) {
2261 xfs_iunlock(iq, XFS_ILOCK_SHARED);
2262 goto cluster_corrupt_out;
2263 }
2264 clcount++;
2265 } else {
2266 xfs_ifunlock(iq);
2267 }
2268 xfs_iunlock(iq, XFS_ILOCK_SHARED);
2269 }
2270
2271 if (clcount) {
2272 XFS_STATS_INC(xs_icluster_flushcnt);
2273 XFS_STATS_ADD(xs_icluster_flushinode, clcount);
2274 }
2275
2276out_free:
Dave Chinner1a3e8f32010-12-17 17:29:43 +11002277 rcu_read_unlock();
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10002278 kmem_free(ilist);
Dave Chinner44b56e02010-01-11 11:47:43 +00002279out_put:
2280 xfs_perag_put(pag);
David Chinnerbad55842008-03-06 13:43:49 +11002281 return 0;
2282
2283
2284cluster_corrupt_out:
2285 /*
2286 * Corruption detected in the clustering loop. Invalidate the
2287 * inode buffer and shut down the filesystem.
2288 */
Dave Chinner1a3e8f32010-12-17 17:29:43 +11002289 rcu_read_unlock();
David Chinnerbad55842008-03-06 13:43:49 +11002290 /*
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002291 * Clean up the buffer. If it was delwri, just release it --
David Chinnerbad55842008-03-06 13:43:49 +11002292 * brelse can handle it with no problems. If not, shut down the
2293 * filesystem before releasing the buffer.
2294 */
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002295 bufwasdelwri = (bp->b_flags & _XBF_DELWRI_Q);
David Chinnerbad55842008-03-06 13:43:49 +11002296 if (bufwasdelwri)
2297 xfs_buf_relse(bp);
2298
2299 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
2300
2301 if (!bufwasdelwri) {
2302 /*
2303 * Just like incore_relse: if we have b_iodone functions,
2304 * mark the buffer as an error and call them. Otherwise
2305 * mark it as stale and brelse.
2306 */
Christoph Hellwigcb669ca2011-07-13 13:43:49 +02002307 if (bp->b_iodone) {
David Chinnerbad55842008-03-06 13:43:49 +11002308 XFS_BUF_UNDONE(bp);
Christoph Hellwigc867cb62011-10-10 16:52:46 +00002309 xfs_buf_stale(bp);
Chandra Seetharaman5a52c2a582011-07-22 23:39:51 +00002310 xfs_buf_ioerror(bp, EIO);
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00002311 xfs_buf_ioend(bp, 0);
David Chinnerbad55842008-03-06 13:43:49 +11002312 } else {
Christoph Hellwigc867cb62011-10-10 16:52:46 +00002313 xfs_buf_stale(bp);
David Chinnerbad55842008-03-06 13:43:49 +11002314 xfs_buf_relse(bp);
2315 }
2316 }
2317
2318 /*
2319 * Unlocks the flush lock
2320 */
Dave Chinner04913fd2012-04-23 15:58:41 +10002321 xfs_iflush_abort(iq, false);
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10002322 kmem_free(ilist);
Dave Chinner44b56e02010-01-11 11:47:43 +00002323 xfs_perag_put(pag);
David Chinnerbad55842008-03-06 13:43:49 +11002324 return XFS_ERROR(EFSCORRUPTED);
2325}
2326
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327/*
Christoph Hellwig4c468192012-04-23 15:58:36 +10002328 * Flush dirty inode metadata into the backing buffer.
2329 *
2330 * The caller must have the inode lock and the inode flush lock held. The
2331 * inode lock will still be held upon return to the caller, and the inode
2332 * flush lock will be released after the inode has reached the disk.
2333 *
2334 * The caller must write out the buffer returned in *bpp and release it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 */
2336int
2337xfs_iflush(
Christoph Hellwig4c468192012-04-23 15:58:36 +10002338 struct xfs_inode *ip,
2339 struct xfs_buf **bpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340{
Christoph Hellwig4c468192012-04-23 15:58:36 +10002341 struct xfs_mount *mp = ip->i_mount;
2342 struct xfs_buf *bp;
2343 struct xfs_dinode *dip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345
2346 XFS_STATS_INC(xs_iflush_count);
2347
Christoph Hellwig579aa9c2008-04-22 17:34:00 +10002348 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
Christoph Hellwig474fce02011-12-18 20:00:09 +00002349 ASSERT(xfs_isiflocked(ip));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE ||
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00002351 ip->i_d.di_nextents > XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352
Christoph Hellwig4c468192012-04-23 15:58:36 +10002353 *bpp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 xfs_iunpin_wait(ip);
2356
2357 /*
Dave Chinner4b6a4682010-01-11 11:45:21 +00002358 * For stale inodes we cannot rely on the backing buffer remaining
2359 * stale in cache for the remaining life of the stale inode and so
Christoph Hellwig475ee412012-07-03 12:21:22 -04002360 * xfs_imap_to_bp() below may give us a buffer that no longer contains
Dave Chinner4b6a4682010-01-11 11:45:21 +00002361 * inodes below. We have to check this after ensuring the inode is
2362 * unpinned so that it is safe to reclaim the stale inode after the
2363 * flush call.
2364 */
2365 if (xfs_iflags_test(ip, XFS_ISTALE)) {
2366 xfs_ifunlock(ip);
2367 return 0;
2368 }
2369
2370 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 * This may have been unpinned because the filesystem is shutting
2372 * down forcibly. If that's the case we must not write this inode
Christoph Hellwig32ce90a2012-04-23 15:58:32 +10002373 * to disk, because the log record didn't make it to disk.
2374 *
2375 * We also have to remove the log item from the AIL in this case,
2376 * as we wait for an empty AIL as part of the unmount process.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 */
2378 if (XFS_FORCED_SHUTDOWN(mp)) {
Christoph Hellwig32ce90a2012-04-23 15:58:32 +10002379 error = XFS_ERROR(EIO);
2380 goto abort_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 }
2382
2383 /*
David Chinnera3f74ff2008-03-06 13:43:42 +11002384 * Get the buffer containing the on-disk inode.
2385 */
Christoph Hellwig475ee412012-07-03 12:21:22 -04002386 error = xfs_imap_to_bp(mp, NULL, &ip->i_imap, &dip, &bp, XBF_TRYLOCK,
2387 0);
David Chinnera3f74ff2008-03-06 13:43:42 +11002388 if (error || !bp) {
2389 xfs_ifunlock(ip);
2390 return error;
2391 }
2392
2393 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 * First flush out the inode that xfs_iflush was called with.
2395 */
2396 error = xfs_iflush_int(ip, bp);
David Chinnerbad55842008-03-06 13:43:49 +11002397 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 goto corrupt_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399
2400 /*
David Chinnera3f74ff2008-03-06 13:43:42 +11002401 * If the buffer is pinned then push on the log now so we won't
2402 * get stuck waiting in the write for too long.
2403 */
Chandra Seetharaman811e64c2011-07-22 23:40:27 +00002404 if (xfs_buf_ispinned(bp))
Christoph Hellwiga14a3482010-01-19 09:56:46 +00002405 xfs_log_force(mp, 0);
David Chinnera3f74ff2008-03-06 13:43:42 +11002406
2407 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 * inode clustering:
2409 * see if other inodes can be gathered into this write
2410 */
David Chinnerbad55842008-03-06 13:43:49 +11002411 error = xfs_iflush_cluster(ip, bp);
2412 if (error)
2413 goto cluster_corrupt_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414
Christoph Hellwig4c468192012-04-23 15:58:36 +10002415 *bpp = bp;
2416 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417
2418corrupt_out:
2419 xfs_buf_relse(bp);
Nathan Scott7d04a332006-06-09 14:58:38 +10002420 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421cluster_corrupt_out:
Christoph Hellwig32ce90a2012-04-23 15:58:32 +10002422 error = XFS_ERROR(EFSCORRUPTED);
2423abort_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 /*
2425 * Unlocks the flush lock
2426 */
Dave Chinner04913fd2012-04-23 15:58:41 +10002427 xfs_iflush_abort(ip, false);
Christoph Hellwig32ce90a2012-04-23 15:58:32 +10002428 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429}
2430
2431
2432STATIC int
2433xfs_iflush_int(
2434 xfs_inode_t *ip,
2435 xfs_buf_t *bp)
2436{
2437 xfs_inode_log_item_t *iip;
2438 xfs_dinode_t *dip;
2439 xfs_mount_t *mp;
2440#ifdef XFS_TRANS_DEBUG
2441 int first;
2442#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443
Christoph Hellwig579aa9c2008-04-22 17:34:00 +10002444 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
Christoph Hellwig474fce02011-12-18 20:00:09 +00002445 ASSERT(xfs_isiflocked(ip));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE ||
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00002447 ip->i_d.di_nextents > XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448
2449 iip = ip->i_itemp;
2450 mp = ip->i_mount;
2451
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 /* set *dip = inode's place in the buffer */
Christoph Hellwig92bfc6e2008-11-28 14:23:41 +11002453 dip = (xfs_dinode_t *)xfs_buf_offset(bp, ip->i_imap.im_boffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454
Christoph Hellwig69ef9212011-07-08 14:36:05 +02002455 if (XFS_TEST_ERROR(dip->di_magic != cpu_to_be16(XFS_DINODE_MAGIC),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 mp, XFS_ERRTAG_IFLUSH_1, XFS_RANDOM_IFLUSH_1)) {
Dave Chinner6a19d932011-03-07 10:02:35 +11002457 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
2458 "%s: Bad inode %Lu magic number 0x%x, ptr 0x%p",
2459 __func__, ip->i_ino, be16_to_cpu(dip->di_magic), dip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 goto corrupt_out;
2461 }
2462 if (XFS_TEST_ERROR(ip->i_d.di_magic != XFS_DINODE_MAGIC,
2463 mp, XFS_ERRTAG_IFLUSH_2, XFS_RANDOM_IFLUSH_2)) {
Dave Chinner6a19d932011-03-07 10:02:35 +11002464 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
2465 "%s: Bad inode %Lu, ptr 0x%p, magic number 0x%x",
2466 __func__, ip->i_ino, ip, ip->i_d.di_magic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 goto corrupt_out;
2468 }
Al Viroabbede12011-07-26 02:31:30 -04002469 if (S_ISREG(ip->i_d.di_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 if (XFS_TEST_ERROR(
2471 (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS) &&
2472 (ip->i_d.di_format != XFS_DINODE_FMT_BTREE),
2473 mp, XFS_ERRTAG_IFLUSH_3, XFS_RANDOM_IFLUSH_3)) {
Dave Chinner6a19d932011-03-07 10:02:35 +11002474 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
2475 "%s: Bad regular inode %Lu, ptr 0x%p",
2476 __func__, ip->i_ino, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 goto corrupt_out;
2478 }
Al Viroabbede12011-07-26 02:31:30 -04002479 } else if (S_ISDIR(ip->i_d.di_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 if (XFS_TEST_ERROR(
2481 (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS) &&
2482 (ip->i_d.di_format != XFS_DINODE_FMT_BTREE) &&
2483 (ip->i_d.di_format != XFS_DINODE_FMT_LOCAL),
2484 mp, XFS_ERRTAG_IFLUSH_4, XFS_RANDOM_IFLUSH_4)) {
Dave Chinner6a19d932011-03-07 10:02:35 +11002485 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
2486 "%s: Bad directory inode %Lu, ptr 0x%p",
2487 __func__, ip->i_ino, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 goto corrupt_out;
2489 }
2490 }
2491 if (XFS_TEST_ERROR(ip->i_d.di_nextents + ip->i_d.di_anextents >
2492 ip->i_d.di_nblocks, mp, XFS_ERRTAG_IFLUSH_5,
2493 XFS_RANDOM_IFLUSH_5)) {
Dave Chinner6a19d932011-03-07 10:02:35 +11002494 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
2495 "%s: detected corrupt incore inode %Lu, "
2496 "total extents = %d, nblocks = %Ld, ptr 0x%p",
2497 __func__, ip->i_ino,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 ip->i_d.di_nextents + ip->i_d.di_anextents,
Dave Chinner6a19d932011-03-07 10:02:35 +11002499 ip->i_d.di_nblocks, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 goto corrupt_out;
2501 }
2502 if (XFS_TEST_ERROR(ip->i_d.di_forkoff > mp->m_sb.sb_inodesize,
2503 mp, XFS_ERRTAG_IFLUSH_6, XFS_RANDOM_IFLUSH_6)) {
Dave Chinner6a19d932011-03-07 10:02:35 +11002504 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
2505 "%s: bad inode %Lu, forkoff 0x%x, ptr 0x%p",
2506 __func__, ip->i_ino, ip->i_d.di_forkoff, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 goto corrupt_out;
2508 }
2509 /*
2510 * bump the flush iteration count, used to detect flushes which
2511 * postdate a log record during recovery.
2512 */
2513
2514 ip->i_d.di_flushiter++;
2515
2516 /*
2517 * Copy the dirty parts of the inode into the on-disk
2518 * inode. We always copy out the core of the inode,
2519 * because if the inode is dirty at all the core must
2520 * be.
2521 */
Christoph Hellwig81591fe2008-11-28 14:23:39 +11002522 xfs_dinode_to_disk(dip, &ip->i_d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523
2524 /* Wrap, we never let the log put out DI_MAX_FLUSH */
2525 if (ip->i_d.di_flushiter == DI_MAX_FLUSH)
2526 ip->i_d.di_flushiter = 0;
2527
2528 /*
2529 * If this is really an old format inode and the superblock version
2530 * has not been updated to support only new format inodes, then
2531 * convert back to the old inode format. If the superblock version
2532 * has been updated, then make the conversion permanent.
2533 */
Christoph Hellwig51ce16d2008-11-28 14:23:39 +11002534 ASSERT(ip->i_d.di_version == 1 || xfs_sb_version_hasnlink(&mp->m_sb));
2535 if (ip->i_d.di_version == 1) {
Eric Sandeen62118702008-03-06 13:44:28 +11002536 if (!xfs_sb_version_hasnlink(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 /*
2538 * Convert it back.
2539 */
2540 ASSERT(ip->i_d.di_nlink <= XFS_MAXLINK_1);
Christoph Hellwig81591fe2008-11-28 14:23:39 +11002541 dip->di_onlink = cpu_to_be16(ip->i_d.di_nlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 } else {
2543 /*
2544 * The superblock version has already been bumped,
2545 * so just make the conversion to the new inode
2546 * format permanent.
2547 */
Christoph Hellwig51ce16d2008-11-28 14:23:39 +11002548 ip->i_d.di_version = 2;
2549 dip->di_version = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 ip->i_d.di_onlink = 0;
Christoph Hellwig81591fe2008-11-28 14:23:39 +11002551 dip->di_onlink = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 memset(&(ip->i_d.di_pad[0]), 0, sizeof(ip->i_d.di_pad));
Christoph Hellwig81591fe2008-11-28 14:23:39 +11002553 memset(&(dip->di_pad[0]), 0,
2554 sizeof(dip->di_pad));
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +00002555 ASSERT(xfs_get_projid(ip) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 }
2557 }
2558
David Chinnere4ac9672008-04-10 12:23:58 +10002559 xfs_iflush_fork(ip, dip, iip, XFS_DATA_FORK, bp);
2560 if (XFS_IFORK_Q(ip))
2561 xfs_iflush_fork(ip, dip, iip, XFS_ATTR_FORK, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 xfs_inobp_check(mp, bp);
2563
2564 /*
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +00002565 * We've recorded everything logged in the inode, so we'd like to clear
2566 * the ili_fields bits so we don't log and flush things unnecessarily.
2567 * However, we can't stop logging all this information until the data
2568 * we've copied into the disk buffer is written to disk. If we did we
2569 * might overwrite the copy of the inode in the log with all the data
2570 * after re-logging only part of it, and in the face of a crash we
2571 * wouldn't have all the data we need to recover.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 *
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +00002573 * What we do is move the bits to the ili_last_fields field. When
2574 * logging the inode, these bits are moved back to the ili_fields field.
2575 * In the xfs_iflush_done() routine we clear ili_last_fields, since we
2576 * know that the information those bits represent is permanently on
2577 * disk. As long as the flush completes before the inode is logged
2578 * again, then both ili_fields and ili_last_fields will be cleared.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 *
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +00002580 * We can play with the ili_fields bits here, because the inode lock
2581 * must be held exclusively in order to set bits there and the flush
2582 * lock protects the ili_last_fields bits. Set ili_logged so the flush
2583 * done routine can tell whether or not to look in the AIL. Also, store
2584 * the current LSN of the inode so that we can tell whether the item has
2585 * moved in the AIL from xfs_iflush_done(). In order to read the lsn we
2586 * need the AIL lock, because it is a 64 bit value that cannot be read
2587 * atomically.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 */
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +00002589 if (iip != NULL && iip->ili_fields != 0) {
2590 iip->ili_last_fields = iip->ili_fields;
2591 iip->ili_fields = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 iip->ili_logged = 1;
2593
David Chinner7b2e2a32008-10-30 17:39:12 +11002594 xfs_trans_ail_copy_lsn(mp->m_ail, &iip->ili_flush_lsn,
2595 &iip->ili_item.li_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596
2597 /*
2598 * Attach the function xfs_iflush_done to the inode's
2599 * buffer. This will remove the inode from the AIL
2600 * and unlock the inode's flush lock when the inode is
2601 * completely written to disk.
2602 */
Christoph Hellwigca30b2a2010-06-23 18:11:15 +10002603 xfs_buf_attach_iodone(bp, xfs_iflush_done, &iip->ili_item);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604
Christoph Hellwigadadbee2011-07-13 13:43:49 +02002605 ASSERT(bp->b_fspriv != NULL);
Christoph Hellwigcb669ca2011-07-13 13:43:49 +02002606 ASSERT(bp->b_iodone != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 } else {
2608 /*
2609 * We're flushing an inode which is not in the AIL and has
Christoph Hellwig8a9c9982012-02-29 09:53:52 +00002610 * not been logged. For this case we can immediately drop
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 * the inode flush lock because we can avoid the whole
2612 * AIL state thing. It's OK to drop the flush lock now,
2613 * because we've already locked the buffer and to do anything
2614 * you really need both.
2615 */
2616 if (iip != NULL) {
2617 ASSERT(iip->ili_logged == 0);
2618 ASSERT(iip->ili_last_fields == 0);
2619 ASSERT((iip->ili_item.li_flags & XFS_LI_IN_AIL) == 0);
2620 }
2621 xfs_ifunlock(ip);
2622 }
2623
2624 return 0;
2625
2626corrupt_out:
2627 return XFS_ERROR(EFSCORRUPTED);
2628}
2629
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002630/*
2631 * Return a pointer to the extent record at file index idx.
2632 */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10002633xfs_bmbt_rec_host_t *
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002634xfs_iext_get_ext(
2635 xfs_ifork_t *ifp, /* inode fork pointer */
2636 xfs_extnum_t idx) /* index of target extent */
2637{
2638 ASSERT(idx >= 0);
Christoph Hellwig87bef182011-05-11 15:04:11 +00002639 ASSERT(idx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t));
2640
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11002641 if ((ifp->if_flags & XFS_IFEXTIREC) && (idx == 0)) {
2642 return ifp->if_u1.if_ext_irec->er_extbuf;
2643 } else if (ifp->if_flags & XFS_IFEXTIREC) {
2644 xfs_ext_irec_t *erp; /* irec pointer */
2645 int erp_idx = 0; /* irec index */
2646 xfs_extnum_t page_idx = idx; /* ext index in target list */
2647
2648 erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 0);
2649 return &erp->er_extbuf[page_idx];
2650 } else if (ifp->if_bytes) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002651 return &ifp->if_u1.if_extents[idx];
2652 } else {
2653 return NULL;
2654 }
2655}
2656
2657/*
2658 * Insert new item(s) into the extent records for incore inode
2659 * fork 'ifp'. 'count' new items are inserted at index 'idx'.
2660 */
2661void
2662xfs_iext_insert(
Christoph Hellwig6ef35542009-11-25 00:00:21 +00002663 xfs_inode_t *ip, /* incore inode pointer */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002664 xfs_extnum_t idx, /* starting index of new items */
2665 xfs_extnum_t count, /* number of inserted items */
Christoph Hellwig6ef35542009-11-25 00:00:21 +00002666 xfs_bmbt_irec_t *new, /* items to insert */
2667 int state) /* type of extent conversion */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002668{
Christoph Hellwig6ef35542009-11-25 00:00:21 +00002669 xfs_ifork_t *ifp = (state & BMAP_ATTRFORK) ? ip->i_afp : &ip->i_df;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002670 xfs_extnum_t i; /* extent record index */
2671
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002672 trace_xfs_iext_insert(ip, idx, new, state, _RET_IP_);
2673
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002674 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
2675 xfs_iext_add(ifp, idx, count);
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10002676 for (i = idx; i < idx + count; i++, new++)
2677 xfs_bmbt_set_all(xfs_iext_get_ext(ifp, i), new);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002678}
2679
2680/*
2681 * This is called when the amount of space required for incore file
2682 * extents needs to be increased. The ext_diff parameter stores the
2683 * number of new extents being added and the idx parameter contains
2684 * the extent index where the new extents will be added. If the new
2685 * extents are being appended, then we just need to (re)allocate and
2686 * initialize the space. Otherwise, if the new extents are being
2687 * inserted into the middle of the existing entries, a bit more work
2688 * is required to make room for the new extents to be inserted. The
2689 * caller is responsible for filling in the new extent entries upon
2690 * return.
2691 */
2692void
2693xfs_iext_add(
2694 xfs_ifork_t *ifp, /* inode fork pointer */
2695 xfs_extnum_t idx, /* index to begin adding exts */
Nathan Scottc41564b2006-03-29 08:55:14 +10002696 int ext_diff) /* number of extents to add */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002697{
2698 int byte_diff; /* new bytes being added */
2699 int new_size; /* size of extents after adding */
2700 xfs_extnum_t nextents; /* number of extents in file */
2701
2702 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
2703 ASSERT((idx >= 0) && (idx <= nextents));
2704 byte_diff = ext_diff * sizeof(xfs_bmbt_rec_t);
2705 new_size = ifp->if_bytes + byte_diff;
2706 /*
2707 * If the new number of extents (nextents + ext_diff)
2708 * fits inside the inode, then continue to use the inline
2709 * extent buffer.
2710 */
2711 if (nextents + ext_diff <= XFS_INLINE_EXTS) {
2712 if (idx < nextents) {
2713 memmove(&ifp->if_u2.if_inline_ext[idx + ext_diff],
2714 &ifp->if_u2.if_inline_ext[idx],
2715 (nextents - idx) * sizeof(xfs_bmbt_rec_t));
2716 memset(&ifp->if_u2.if_inline_ext[idx], 0, byte_diff);
2717 }
2718 ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext;
2719 ifp->if_real_bytes = 0;
2720 }
2721 /*
2722 * Otherwise use a linear (direct) extent list.
2723 * If the extents are currently inside the inode,
2724 * xfs_iext_realloc_direct will switch us from
2725 * inline to direct extent allocation mode.
2726 */
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11002727 else if (nextents + ext_diff <= XFS_LINEAR_EXTS) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002728 xfs_iext_realloc_direct(ifp, new_size);
2729 if (idx < nextents) {
2730 memmove(&ifp->if_u1.if_extents[idx + ext_diff],
2731 &ifp->if_u1.if_extents[idx],
2732 (nextents - idx) * sizeof(xfs_bmbt_rec_t));
2733 memset(&ifp->if_u1.if_extents[idx], 0, byte_diff);
2734 }
2735 }
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11002736 /* Indirection array */
2737 else {
2738 xfs_ext_irec_t *erp;
2739 int erp_idx = 0;
2740 int page_idx = idx;
2741
2742 ASSERT(nextents + ext_diff > XFS_LINEAR_EXTS);
2743 if (ifp->if_flags & XFS_IFEXTIREC) {
2744 erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 1);
2745 } else {
2746 xfs_iext_irec_init(ifp);
2747 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
2748 erp = ifp->if_u1.if_ext_irec;
2749 }
2750 /* Extents fit in target extent page */
2751 if (erp && erp->er_extcount + ext_diff <= XFS_LINEAR_EXTS) {
2752 if (page_idx < erp->er_extcount) {
2753 memmove(&erp->er_extbuf[page_idx + ext_diff],
2754 &erp->er_extbuf[page_idx],
2755 (erp->er_extcount - page_idx) *
2756 sizeof(xfs_bmbt_rec_t));
2757 memset(&erp->er_extbuf[page_idx], 0, byte_diff);
2758 }
2759 erp->er_extcount += ext_diff;
2760 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff);
2761 }
2762 /* Insert a new extent page */
2763 else if (erp) {
2764 xfs_iext_add_indirect_multi(ifp,
2765 erp_idx, page_idx, ext_diff);
2766 }
2767 /*
2768 * If extent(s) are being appended to the last page in
2769 * the indirection array and the new extent(s) don't fit
2770 * in the page, then erp is NULL and erp_idx is set to
2771 * the next index needed in the indirection array.
2772 */
2773 else {
2774 int count = ext_diff;
2775
2776 while (count) {
2777 erp = xfs_iext_irec_new(ifp, erp_idx);
2778 erp->er_extcount = count;
2779 count -= MIN(count, (int)XFS_LINEAR_EXTS);
2780 if (count) {
2781 erp_idx++;
2782 }
2783 }
2784 }
2785 }
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002786 ifp->if_bytes = new_size;
2787}
2788
2789/*
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11002790 * This is called when incore extents are being added to the indirection
2791 * array and the new extents do not fit in the target extent list. The
2792 * erp_idx parameter contains the irec index for the target extent list
2793 * in the indirection array, and the idx parameter contains the extent
2794 * index within the list. The number of extents being added is stored
2795 * in the count parameter.
2796 *
2797 * |-------| |-------|
2798 * | | | | idx - number of extents before idx
2799 * | idx | | count |
2800 * | | | | count - number of extents being inserted at idx
2801 * |-------| |-------|
2802 * | count | | nex2 | nex2 - number of extents after idx + count
2803 * |-------| |-------|
2804 */
2805void
2806xfs_iext_add_indirect_multi(
2807 xfs_ifork_t *ifp, /* inode fork pointer */
2808 int erp_idx, /* target extent irec index */
2809 xfs_extnum_t idx, /* index within target list */
2810 int count) /* new extents being added */
2811{
2812 int byte_diff; /* new bytes being added */
2813 xfs_ext_irec_t *erp; /* pointer to irec entry */
2814 xfs_extnum_t ext_diff; /* number of extents to add */
2815 xfs_extnum_t ext_cnt; /* new extents still needed */
2816 xfs_extnum_t nex2; /* extents after idx + count */
2817 xfs_bmbt_rec_t *nex2_ep = NULL; /* temp list for nex2 extents */
2818 int nlists; /* number of irec's (lists) */
2819
2820 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
2821 erp = &ifp->if_u1.if_ext_irec[erp_idx];
2822 nex2 = erp->er_extcount - idx;
2823 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
2824
2825 /*
2826 * Save second part of target extent list
2827 * (all extents past */
2828 if (nex2) {
2829 byte_diff = nex2 * sizeof(xfs_bmbt_rec_t);
David Chinner67850732008-08-13 16:02:51 +10002830 nex2_ep = (xfs_bmbt_rec_t *) kmem_alloc(byte_diff, KM_NOFS);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11002831 memmove(nex2_ep, &erp->er_extbuf[idx], byte_diff);
2832 erp->er_extcount -= nex2;
2833 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, -nex2);
2834 memset(&erp->er_extbuf[idx], 0, byte_diff);
2835 }
2836
2837 /*
2838 * Add the new extents to the end of the target
2839 * list, then allocate new irec record(s) and
2840 * extent buffer(s) as needed to store the rest
2841 * of the new extents.
2842 */
2843 ext_cnt = count;
2844 ext_diff = MIN(ext_cnt, (int)XFS_LINEAR_EXTS - erp->er_extcount);
2845 if (ext_diff) {
2846 erp->er_extcount += ext_diff;
2847 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff);
2848 ext_cnt -= ext_diff;
2849 }
2850 while (ext_cnt) {
2851 erp_idx++;
2852 erp = xfs_iext_irec_new(ifp, erp_idx);
2853 ext_diff = MIN(ext_cnt, (int)XFS_LINEAR_EXTS);
2854 erp->er_extcount = ext_diff;
2855 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff);
2856 ext_cnt -= ext_diff;
2857 }
2858
2859 /* Add nex2 extents back to indirection array */
2860 if (nex2) {
2861 xfs_extnum_t ext_avail;
2862 int i;
2863
2864 byte_diff = nex2 * sizeof(xfs_bmbt_rec_t);
2865 ext_avail = XFS_LINEAR_EXTS - erp->er_extcount;
2866 i = 0;
2867 /*
2868 * If nex2 extents fit in the current page, append
2869 * nex2_ep after the new extents.
2870 */
2871 if (nex2 <= ext_avail) {
2872 i = erp->er_extcount;
2873 }
2874 /*
2875 * Otherwise, check if space is available in the
2876 * next page.
2877 */
2878 else if ((erp_idx < nlists - 1) &&
2879 (nex2 <= (ext_avail = XFS_LINEAR_EXTS -
2880 ifp->if_u1.if_ext_irec[erp_idx+1].er_extcount))) {
2881 erp_idx++;
2882 erp++;
2883 /* Create a hole for nex2 extents */
2884 memmove(&erp->er_extbuf[nex2], erp->er_extbuf,
2885 erp->er_extcount * sizeof(xfs_bmbt_rec_t));
2886 }
2887 /*
2888 * Final choice, create a new extent page for
2889 * nex2 extents.
2890 */
2891 else {
2892 erp_idx++;
2893 erp = xfs_iext_irec_new(ifp, erp_idx);
2894 }
2895 memmove(&erp->er_extbuf[i], nex2_ep, byte_diff);
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10002896 kmem_free(nex2_ep);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11002897 erp->er_extcount += nex2;
2898 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, nex2);
2899 }
2900}
2901
2902/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002903 * This is called when the amount of space required for incore file
2904 * extents needs to be decreased. The ext_diff parameter stores the
2905 * number of extents to be removed and the idx parameter contains
2906 * the extent index where the extents will be removed from.
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11002907 *
2908 * If the amount of space needed has decreased below the linear
2909 * limit, XFS_IEXT_BUFSZ, then switch to using the contiguous
2910 * extent array. Otherwise, use kmem_realloc() to adjust the
2911 * size to what is needed.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002912 */
2913void
2914xfs_iext_remove(
Christoph Hellwig6ef35542009-11-25 00:00:21 +00002915 xfs_inode_t *ip, /* incore inode pointer */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002916 xfs_extnum_t idx, /* index to begin removing exts */
Christoph Hellwig6ef35542009-11-25 00:00:21 +00002917 int ext_diff, /* number of extents to remove */
2918 int state) /* type of extent conversion */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002919{
Christoph Hellwig6ef35542009-11-25 00:00:21 +00002920 xfs_ifork_t *ifp = (state & BMAP_ATTRFORK) ? ip->i_afp : &ip->i_df;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002921 xfs_extnum_t nextents; /* number of extents in file */
2922 int new_size; /* size of extents after removal */
2923
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002924 trace_xfs_iext_remove(ip, idx, state, _RET_IP_);
2925
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002926 ASSERT(ext_diff > 0);
2927 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
2928 new_size = (nextents - ext_diff) * sizeof(xfs_bmbt_rec_t);
2929
2930 if (new_size == 0) {
2931 xfs_iext_destroy(ifp);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11002932 } else if (ifp->if_flags & XFS_IFEXTIREC) {
2933 xfs_iext_remove_indirect(ifp, idx, ext_diff);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002934 } else if (ifp->if_real_bytes) {
2935 xfs_iext_remove_direct(ifp, idx, ext_diff);
2936 } else {
2937 xfs_iext_remove_inline(ifp, idx, ext_diff);
2938 }
2939 ifp->if_bytes = new_size;
2940}
2941
2942/*
2943 * This removes ext_diff extents from the inline buffer, beginning
2944 * at extent index idx.
2945 */
2946void
2947xfs_iext_remove_inline(
2948 xfs_ifork_t *ifp, /* inode fork pointer */
2949 xfs_extnum_t idx, /* index to begin removing exts */
2950 int ext_diff) /* number of extents to remove */
2951{
2952 int nextents; /* number of extents in file */
2953
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11002954 ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002955 ASSERT(idx < XFS_INLINE_EXTS);
2956 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
2957 ASSERT(((nextents - ext_diff) > 0) &&
2958 (nextents - ext_diff) < XFS_INLINE_EXTS);
2959
2960 if (idx + ext_diff < nextents) {
2961 memmove(&ifp->if_u2.if_inline_ext[idx],
2962 &ifp->if_u2.if_inline_ext[idx + ext_diff],
2963 (nextents - (idx + ext_diff)) *
2964 sizeof(xfs_bmbt_rec_t));
2965 memset(&ifp->if_u2.if_inline_ext[nextents - ext_diff],
2966 0, ext_diff * sizeof(xfs_bmbt_rec_t));
2967 } else {
2968 memset(&ifp->if_u2.if_inline_ext[idx], 0,
2969 ext_diff * sizeof(xfs_bmbt_rec_t));
2970 }
2971}
2972
2973/*
2974 * This removes ext_diff extents from a linear (direct) extent list,
2975 * beginning at extent index idx. If the extents are being removed
2976 * from the end of the list (ie. truncate) then we just need to re-
2977 * allocate the list to remove the extra space. Otherwise, if the
2978 * extents are being removed from the middle of the existing extent
2979 * entries, then we first need to move the extent records beginning
2980 * at idx + ext_diff up in the list to overwrite the records being
2981 * removed, then remove the extra space via kmem_realloc.
2982 */
2983void
2984xfs_iext_remove_direct(
2985 xfs_ifork_t *ifp, /* inode fork pointer */
2986 xfs_extnum_t idx, /* index to begin removing exts */
2987 int ext_diff) /* number of extents to remove */
2988{
2989 xfs_extnum_t nextents; /* number of extents in file */
2990 int new_size; /* size of extents after removal */
2991
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11002992 ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002993 new_size = ifp->if_bytes -
2994 (ext_diff * sizeof(xfs_bmbt_rec_t));
2995 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
2996
2997 if (new_size == 0) {
2998 xfs_iext_destroy(ifp);
2999 return;
3000 }
3001 /* Move extents up in the list (if needed) */
3002 if (idx + ext_diff < nextents) {
3003 memmove(&ifp->if_u1.if_extents[idx],
3004 &ifp->if_u1.if_extents[idx + ext_diff],
3005 (nextents - (idx + ext_diff)) *
3006 sizeof(xfs_bmbt_rec_t));
3007 }
3008 memset(&ifp->if_u1.if_extents[nextents - ext_diff],
3009 0, ext_diff * sizeof(xfs_bmbt_rec_t));
3010 /*
3011 * Reallocate the direct extent list. If the extents
3012 * will fit inside the inode then xfs_iext_realloc_direct
3013 * will switch from direct to inline extent allocation
3014 * mode for us.
3015 */
3016 xfs_iext_realloc_direct(ifp, new_size);
3017 ifp->if_bytes = new_size;
3018}
3019
3020/*
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003021 * This is called when incore extents are being removed from the
3022 * indirection array and the extents being removed span multiple extent
3023 * buffers. The idx parameter contains the file extent index where we
3024 * want to begin removing extents, and the count parameter contains
3025 * how many extents need to be removed.
3026 *
3027 * |-------| |-------|
3028 * | nex1 | | | nex1 - number of extents before idx
3029 * |-------| | count |
3030 * | | | | count - number of extents being removed at idx
3031 * | count | |-------|
3032 * | | | nex2 | nex2 - number of extents after idx + count
3033 * |-------| |-------|
3034 */
3035void
3036xfs_iext_remove_indirect(
3037 xfs_ifork_t *ifp, /* inode fork pointer */
3038 xfs_extnum_t idx, /* index to begin removing extents */
3039 int count) /* number of extents to remove */
3040{
3041 xfs_ext_irec_t *erp; /* indirection array pointer */
3042 int erp_idx = 0; /* indirection array index */
3043 xfs_extnum_t ext_cnt; /* extents left to remove */
3044 xfs_extnum_t ext_diff; /* extents to remove in current list */
3045 xfs_extnum_t nex1; /* number of extents before idx */
3046 xfs_extnum_t nex2; /* extents after idx + count */
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003047 int page_idx = idx; /* index in target extent list */
3048
3049 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3050 erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 0);
3051 ASSERT(erp != NULL);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003052 nex1 = page_idx;
3053 ext_cnt = count;
3054 while (ext_cnt) {
3055 nex2 = MAX((erp->er_extcount - (nex1 + ext_cnt)), 0);
3056 ext_diff = MIN(ext_cnt, (erp->er_extcount - nex1));
3057 /*
3058 * Check for deletion of entire list;
3059 * xfs_iext_irec_remove() updates extent offsets.
3060 */
3061 if (ext_diff == erp->er_extcount) {
3062 xfs_iext_irec_remove(ifp, erp_idx);
3063 ext_cnt -= ext_diff;
3064 nex1 = 0;
3065 if (ext_cnt) {
3066 ASSERT(erp_idx < ifp->if_real_bytes /
3067 XFS_IEXT_BUFSZ);
3068 erp = &ifp->if_u1.if_ext_irec[erp_idx];
3069 nex1 = 0;
3070 continue;
3071 } else {
3072 break;
3073 }
3074 }
3075 /* Move extents up (if needed) */
3076 if (nex2) {
3077 memmove(&erp->er_extbuf[nex1],
3078 &erp->er_extbuf[nex1 + ext_diff],
3079 nex2 * sizeof(xfs_bmbt_rec_t));
3080 }
3081 /* Zero out rest of page */
3082 memset(&erp->er_extbuf[nex1 + nex2], 0, (XFS_IEXT_BUFSZ -
3083 ((nex1 + nex2) * sizeof(xfs_bmbt_rec_t))));
3084 /* Update remaining counters */
3085 erp->er_extcount -= ext_diff;
3086 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, -ext_diff);
3087 ext_cnt -= ext_diff;
3088 nex1 = 0;
3089 erp_idx++;
3090 erp++;
3091 }
3092 ifp->if_bytes -= count * sizeof(xfs_bmbt_rec_t);
3093 xfs_iext_irec_compact(ifp);
3094}
3095
3096/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003097 * Create, destroy, or resize a linear (direct) block of extents.
3098 */
3099void
3100xfs_iext_realloc_direct(
3101 xfs_ifork_t *ifp, /* inode fork pointer */
3102 int new_size) /* new size of extents */
3103{
3104 int rnew_size; /* real new size of extents */
3105
3106 rnew_size = new_size;
3107
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003108 ASSERT(!(ifp->if_flags & XFS_IFEXTIREC) ||
3109 ((new_size >= 0) && (new_size <= XFS_IEXT_BUFSZ) &&
3110 (new_size != ifp->if_real_bytes)));
3111
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003112 /* Free extent records */
3113 if (new_size == 0) {
3114 xfs_iext_destroy(ifp);
3115 }
3116 /* Resize direct extent list and zero any new bytes */
3117 else if (ifp->if_real_bytes) {
3118 /* Check if extents will fit inside the inode */
3119 if (new_size <= XFS_INLINE_EXTS * sizeof(xfs_bmbt_rec_t)) {
3120 xfs_iext_direct_to_inline(ifp, new_size /
3121 (uint)sizeof(xfs_bmbt_rec_t));
3122 ifp->if_bytes = new_size;
3123 return;
3124 }
Vignesh Babu16a087d2007-06-28 16:46:37 +10003125 if (!is_power_of_2(new_size)){
Robert P. J. Day40ebd812007-11-23 16:30:51 +11003126 rnew_size = roundup_pow_of_two(new_size);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003127 }
3128 if (rnew_size != ifp->if_real_bytes) {
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003129 ifp->if_u1.if_extents =
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003130 kmem_realloc(ifp->if_u1.if_extents,
3131 rnew_size,
David Chinner67850732008-08-13 16:02:51 +10003132 ifp->if_real_bytes, KM_NOFS);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003133 }
3134 if (rnew_size > ifp->if_real_bytes) {
3135 memset(&ifp->if_u1.if_extents[ifp->if_bytes /
3136 (uint)sizeof(xfs_bmbt_rec_t)], 0,
3137 rnew_size - ifp->if_real_bytes);
3138 }
3139 }
3140 /*
3141 * Switch from the inline extent buffer to a direct
3142 * extent list. Be sure to include the inline extent
3143 * bytes in new_size.
3144 */
3145 else {
3146 new_size += ifp->if_bytes;
Vignesh Babu16a087d2007-06-28 16:46:37 +10003147 if (!is_power_of_2(new_size)) {
Robert P. J. Day40ebd812007-11-23 16:30:51 +11003148 rnew_size = roundup_pow_of_two(new_size);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003149 }
3150 xfs_iext_inline_to_direct(ifp, rnew_size);
3151 }
3152 ifp->if_real_bytes = rnew_size;
3153 ifp->if_bytes = new_size;
3154}
3155
3156/*
3157 * Switch from linear (direct) extent records to inline buffer.
3158 */
3159void
3160xfs_iext_direct_to_inline(
3161 xfs_ifork_t *ifp, /* inode fork pointer */
3162 xfs_extnum_t nextents) /* number of extents in file */
3163{
3164 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
3165 ASSERT(nextents <= XFS_INLINE_EXTS);
3166 /*
3167 * The inline buffer was zeroed when we switched
3168 * from inline to direct extent allocation mode,
3169 * so we don't need to clear it here.
3170 */
3171 memcpy(ifp->if_u2.if_inline_ext, ifp->if_u1.if_extents,
3172 nextents * sizeof(xfs_bmbt_rec_t));
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10003173 kmem_free(ifp->if_u1.if_extents);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003174 ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext;
3175 ifp->if_real_bytes = 0;
3176}
3177
3178/*
3179 * Switch from inline buffer to linear (direct) extent records.
3180 * new_size should already be rounded up to the next power of 2
3181 * by the caller (when appropriate), so use new_size as it is.
3182 * However, since new_size may be rounded up, we can't update
3183 * if_bytes here. It is the caller's responsibility to update
3184 * if_bytes upon return.
3185 */
3186void
3187xfs_iext_inline_to_direct(
3188 xfs_ifork_t *ifp, /* inode fork pointer */
3189 int new_size) /* number of extents in file */
3190{
David Chinner67850732008-08-13 16:02:51 +10003191 ifp->if_u1.if_extents = kmem_alloc(new_size, KM_NOFS);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003192 memset(ifp->if_u1.if_extents, 0, new_size);
3193 if (ifp->if_bytes) {
3194 memcpy(ifp->if_u1.if_extents, ifp->if_u2.if_inline_ext,
3195 ifp->if_bytes);
3196 memset(ifp->if_u2.if_inline_ext, 0, XFS_INLINE_EXTS *
3197 sizeof(xfs_bmbt_rec_t));
3198 }
3199 ifp->if_real_bytes = new_size;
3200}
3201
3202/*
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003203 * Resize an extent indirection array to new_size bytes.
3204 */
Eric Sandeend96f8f82009-07-02 00:09:33 -05003205STATIC void
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003206xfs_iext_realloc_indirect(
3207 xfs_ifork_t *ifp, /* inode fork pointer */
3208 int new_size) /* new indirection array size */
3209{
3210 int nlists; /* number of irec's (ex lists) */
3211 int size; /* current indirection array size */
3212
3213 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3214 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3215 size = nlists * sizeof(xfs_ext_irec_t);
3216 ASSERT(ifp->if_real_bytes);
3217 ASSERT((new_size >= 0) && (new_size != size));
3218 if (new_size == 0) {
3219 xfs_iext_destroy(ifp);
3220 } else {
3221 ifp->if_u1.if_ext_irec = (xfs_ext_irec_t *)
3222 kmem_realloc(ifp->if_u1.if_ext_irec,
David Chinner67850732008-08-13 16:02:51 +10003223 new_size, size, KM_NOFS);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003224 }
3225}
3226
3227/*
3228 * Switch from indirection array to linear (direct) extent allocations.
3229 */
Eric Sandeend96f8f82009-07-02 00:09:33 -05003230STATIC void
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003231xfs_iext_indirect_to_direct(
3232 xfs_ifork_t *ifp) /* inode fork pointer */
3233{
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003234 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003235 xfs_extnum_t nextents; /* number of extents in file */
3236 int size; /* size of file extents */
3237
3238 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3239 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3240 ASSERT(nextents <= XFS_LINEAR_EXTS);
3241 size = nextents * sizeof(xfs_bmbt_rec_t);
3242
Lachlan McIlroy71a8c872008-09-26 12:17:57 +10003243 xfs_iext_irec_compact_pages(ifp);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003244 ASSERT(ifp->if_real_bytes == XFS_IEXT_BUFSZ);
3245
3246 ep = ifp->if_u1.if_ext_irec->er_extbuf;
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10003247 kmem_free(ifp->if_u1.if_ext_irec);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003248 ifp->if_flags &= ~XFS_IFEXTIREC;
3249 ifp->if_u1.if_extents = ep;
3250 ifp->if_bytes = size;
3251 if (nextents < XFS_LINEAR_EXTS) {
3252 xfs_iext_realloc_direct(ifp, size);
3253 }
3254}
3255
3256/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003257 * Free incore file extents.
3258 */
3259void
3260xfs_iext_destroy(
3261 xfs_ifork_t *ifp) /* inode fork pointer */
3262{
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003263 if (ifp->if_flags & XFS_IFEXTIREC) {
3264 int erp_idx;
3265 int nlists;
3266
3267 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3268 for (erp_idx = nlists - 1; erp_idx >= 0 ; erp_idx--) {
3269 xfs_iext_irec_remove(ifp, erp_idx);
3270 }
3271 ifp->if_flags &= ~XFS_IFEXTIREC;
3272 } else if (ifp->if_real_bytes) {
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10003273 kmem_free(ifp->if_u1.if_extents);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003274 } else if (ifp->if_bytes) {
3275 memset(ifp->if_u2.if_inline_ext, 0, XFS_INLINE_EXTS *
3276 sizeof(xfs_bmbt_rec_t));
3277 }
3278 ifp->if_u1.if_extents = NULL;
3279 ifp->if_real_bytes = 0;
3280 ifp->if_bytes = 0;
3281}
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003282
3283/*
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003284 * Return a pointer to the extent record for file system block bno.
3285 */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003286xfs_bmbt_rec_host_t * /* pointer to found extent record */
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003287xfs_iext_bno_to_ext(
3288 xfs_ifork_t *ifp, /* inode fork pointer */
3289 xfs_fileoff_t bno, /* block number to search for */
3290 xfs_extnum_t *idxp) /* index of target extent */
3291{
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003292 xfs_bmbt_rec_host_t *base; /* pointer to first extent */
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003293 xfs_filblks_t blockcount = 0; /* number of blocks in extent */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003294 xfs_bmbt_rec_host_t *ep = NULL; /* pointer to target extent */
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003295 xfs_ext_irec_t *erp = NULL; /* indirection array pointer */
Nathan Scottc41564b2006-03-29 08:55:14 +10003296 int high; /* upper boundary in search */
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003297 xfs_extnum_t idx = 0; /* index of target extent */
Nathan Scottc41564b2006-03-29 08:55:14 +10003298 int low; /* lower boundary in search */
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003299 xfs_extnum_t nextents; /* number of file extents */
3300 xfs_fileoff_t startoff = 0; /* start offset of extent */
3301
3302 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3303 if (nextents == 0) {
3304 *idxp = 0;
3305 return NULL;
3306 }
3307 low = 0;
3308 if (ifp->if_flags & XFS_IFEXTIREC) {
3309 /* Find target extent list */
3310 int erp_idx = 0;
3311 erp = xfs_iext_bno_to_irec(ifp, bno, &erp_idx);
3312 base = erp->er_extbuf;
3313 high = erp->er_extcount - 1;
3314 } else {
3315 base = ifp->if_u1.if_extents;
3316 high = nextents - 1;
3317 }
3318 /* Binary search extent records */
3319 while (low <= high) {
3320 idx = (low + high) >> 1;
3321 ep = base + idx;
3322 startoff = xfs_bmbt_get_startoff(ep);
3323 blockcount = xfs_bmbt_get_blockcount(ep);
3324 if (bno < startoff) {
3325 high = idx - 1;
3326 } else if (bno >= startoff + blockcount) {
3327 low = idx + 1;
3328 } else {
3329 /* Convert back to file-based extent index */
3330 if (ifp->if_flags & XFS_IFEXTIREC) {
3331 idx += erp->er_extoff;
3332 }
3333 *idxp = idx;
3334 return ep;
3335 }
3336 }
3337 /* Convert back to file-based extent index */
3338 if (ifp->if_flags & XFS_IFEXTIREC) {
3339 idx += erp->er_extoff;
3340 }
3341 if (bno >= startoff + blockcount) {
3342 if (++idx == nextents) {
3343 ep = NULL;
3344 } else {
3345 ep = xfs_iext_get_ext(ifp, idx);
3346 }
3347 }
3348 *idxp = idx;
3349 return ep;
3350}
3351
3352/*
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003353 * Return a pointer to the indirection array entry containing the
3354 * extent record for filesystem block bno. Store the index of the
3355 * target irec in *erp_idxp.
3356 */
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003357xfs_ext_irec_t * /* pointer to found extent record */
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003358xfs_iext_bno_to_irec(
3359 xfs_ifork_t *ifp, /* inode fork pointer */
3360 xfs_fileoff_t bno, /* block number to search for */
3361 int *erp_idxp) /* irec index of target ext list */
3362{
3363 xfs_ext_irec_t *erp = NULL; /* indirection array pointer */
3364 xfs_ext_irec_t *erp_next; /* next indirection array entry */
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003365 int erp_idx; /* indirection array index */
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003366 int nlists; /* number of extent irec's (lists) */
3367 int high; /* binary search upper limit */
3368 int low; /* binary search lower limit */
3369
3370 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3371 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3372 erp_idx = 0;
3373 low = 0;
3374 high = nlists - 1;
3375 while (low <= high) {
3376 erp_idx = (low + high) >> 1;
3377 erp = &ifp->if_u1.if_ext_irec[erp_idx];
3378 erp_next = erp_idx < nlists - 1 ? erp + 1 : NULL;
3379 if (bno < xfs_bmbt_get_startoff(erp->er_extbuf)) {
3380 high = erp_idx - 1;
3381 } else if (erp_next && bno >=
3382 xfs_bmbt_get_startoff(erp_next->er_extbuf)) {
3383 low = erp_idx + 1;
3384 } else {
3385 break;
3386 }
3387 }
3388 *erp_idxp = erp_idx;
3389 return erp;
3390}
3391
3392/*
3393 * Return a pointer to the indirection array entry containing the
3394 * extent record at file extent index *idxp. Store the index of the
3395 * target irec in *erp_idxp and store the page index of the target
3396 * extent record in *idxp.
3397 */
3398xfs_ext_irec_t *
3399xfs_iext_idx_to_irec(
3400 xfs_ifork_t *ifp, /* inode fork pointer */
3401 xfs_extnum_t *idxp, /* extent index (file -> page) */
3402 int *erp_idxp, /* pointer to target irec */
3403 int realloc) /* new bytes were just added */
3404{
3405 xfs_ext_irec_t *prev; /* pointer to previous irec */
3406 xfs_ext_irec_t *erp = NULL; /* pointer to current irec */
3407 int erp_idx; /* indirection array index */
3408 int nlists; /* number of irec's (ex lists) */
3409 int high; /* binary search upper limit */
3410 int low; /* binary search lower limit */
3411 xfs_extnum_t page_idx = *idxp; /* extent index in target list */
3412
3413 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
Christoph Hellwig87bef182011-05-11 15:04:11 +00003414 ASSERT(page_idx >= 0);
3415 ASSERT(page_idx <= ifp->if_bytes / sizeof(xfs_bmbt_rec_t));
3416 ASSERT(page_idx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t) || realloc);
3417
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003418 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3419 erp_idx = 0;
3420 low = 0;
3421 high = nlists - 1;
3422
3423 /* Binary search extent irec's */
3424 while (low <= high) {
3425 erp_idx = (low + high) >> 1;
3426 erp = &ifp->if_u1.if_ext_irec[erp_idx];
3427 prev = erp_idx > 0 ? erp - 1 : NULL;
3428 if (page_idx < erp->er_extoff || (page_idx == erp->er_extoff &&
3429 realloc && prev && prev->er_extcount < XFS_LINEAR_EXTS)) {
3430 high = erp_idx - 1;
3431 } else if (page_idx > erp->er_extoff + erp->er_extcount ||
3432 (page_idx == erp->er_extoff + erp->er_extcount &&
3433 !realloc)) {
3434 low = erp_idx + 1;
3435 } else if (page_idx == erp->er_extoff + erp->er_extcount &&
3436 erp->er_extcount == XFS_LINEAR_EXTS) {
3437 ASSERT(realloc);
3438 page_idx = 0;
3439 erp_idx++;
3440 erp = erp_idx < nlists ? erp + 1 : NULL;
3441 break;
3442 } else {
3443 page_idx -= erp->er_extoff;
3444 break;
3445 }
3446 }
3447 *idxp = page_idx;
3448 *erp_idxp = erp_idx;
3449 return(erp);
3450}
3451
3452/*
3453 * Allocate and initialize an indirection array once the space needed
3454 * for incore extents increases above XFS_IEXT_BUFSZ.
3455 */
3456void
3457xfs_iext_irec_init(
3458 xfs_ifork_t *ifp) /* inode fork pointer */
3459{
3460 xfs_ext_irec_t *erp; /* indirection array pointer */
3461 xfs_extnum_t nextents; /* number of extents in file */
3462
3463 ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
3464 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3465 ASSERT(nextents <= XFS_LINEAR_EXTS);
3466
David Chinner67850732008-08-13 16:02:51 +10003467 erp = kmem_alloc(sizeof(xfs_ext_irec_t), KM_NOFS);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003468
3469 if (nextents == 0) {
David Chinner67850732008-08-13 16:02:51 +10003470 ifp->if_u1.if_extents = kmem_alloc(XFS_IEXT_BUFSZ, KM_NOFS);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003471 } else if (!ifp->if_real_bytes) {
3472 xfs_iext_inline_to_direct(ifp, XFS_IEXT_BUFSZ);
3473 } else if (ifp->if_real_bytes < XFS_IEXT_BUFSZ) {
3474 xfs_iext_realloc_direct(ifp, XFS_IEXT_BUFSZ);
3475 }
3476 erp->er_extbuf = ifp->if_u1.if_extents;
3477 erp->er_extcount = nextents;
3478 erp->er_extoff = 0;
3479
3480 ifp->if_flags |= XFS_IFEXTIREC;
3481 ifp->if_real_bytes = XFS_IEXT_BUFSZ;
3482 ifp->if_bytes = nextents * sizeof(xfs_bmbt_rec_t);
3483 ifp->if_u1.if_ext_irec = erp;
3484
3485 return;
3486}
3487
3488/*
3489 * Allocate and initialize a new entry in the indirection array.
3490 */
3491xfs_ext_irec_t *
3492xfs_iext_irec_new(
3493 xfs_ifork_t *ifp, /* inode fork pointer */
3494 int erp_idx) /* index for new irec */
3495{
3496 xfs_ext_irec_t *erp; /* indirection array pointer */
3497 int i; /* loop counter */
3498 int nlists; /* number of irec's (ex lists) */
3499
3500 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3501 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3502
3503 /* Resize indirection array */
3504 xfs_iext_realloc_indirect(ifp, ++nlists *
3505 sizeof(xfs_ext_irec_t));
3506 /*
3507 * Move records down in the array so the
3508 * new page can use erp_idx.
3509 */
3510 erp = ifp->if_u1.if_ext_irec;
3511 for (i = nlists - 1; i > erp_idx; i--) {
3512 memmove(&erp[i], &erp[i-1], sizeof(xfs_ext_irec_t));
3513 }
3514 ASSERT(i == erp_idx);
3515
3516 /* Initialize new extent record */
3517 erp = ifp->if_u1.if_ext_irec;
David Chinner67850732008-08-13 16:02:51 +10003518 erp[erp_idx].er_extbuf = kmem_alloc(XFS_IEXT_BUFSZ, KM_NOFS);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003519 ifp->if_real_bytes = nlists * XFS_IEXT_BUFSZ;
3520 memset(erp[erp_idx].er_extbuf, 0, XFS_IEXT_BUFSZ);
3521 erp[erp_idx].er_extcount = 0;
3522 erp[erp_idx].er_extoff = erp_idx > 0 ?
3523 erp[erp_idx-1].er_extoff + erp[erp_idx-1].er_extcount : 0;
3524 return (&erp[erp_idx]);
3525}
3526
3527/*
3528 * Remove a record from the indirection array.
3529 */
3530void
3531xfs_iext_irec_remove(
3532 xfs_ifork_t *ifp, /* inode fork pointer */
3533 int erp_idx) /* irec index to remove */
3534{
3535 xfs_ext_irec_t *erp; /* indirection array pointer */
3536 int i; /* loop counter */
3537 int nlists; /* number of irec's (ex lists) */
3538
3539 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3540 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3541 erp = &ifp->if_u1.if_ext_irec[erp_idx];
3542 if (erp->er_extbuf) {
3543 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1,
3544 -erp->er_extcount);
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10003545 kmem_free(erp->er_extbuf);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003546 }
3547 /* Compact extent records */
3548 erp = ifp->if_u1.if_ext_irec;
3549 for (i = erp_idx; i < nlists - 1; i++) {
3550 memmove(&erp[i], &erp[i+1], sizeof(xfs_ext_irec_t));
3551 }
3552 /*
3553 * Manually free the last extent record from the indirection
3554 * array. A call to xfs_iext_realloc_indirect() with a size
3555 * of zero would result in a call to xfs_iext_destroy() which
3556 * would in turn call this function again, creating a nasty
3557 * infinite loop.
3558 */
3559 if (--nlists) {
3560 xfs_iext_realloc_indirect(ifp,
3561 nlists * sizeof(xfs_ext_irec_t));
3562 } else {
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10003563 kmem_free(ifp->if_u1.if_ext_irec);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003564 }
3565 ifp->if_real_bytes = nlists * XFS_IEXT_BUFSZ;
3566}
3567
3568/*
3569 * This is called to clean up large amounts of unused memory allocated
3570 * by the indirection array. Before compacting anything though, verify
3571 * that the indirection array is still needed and switch back to the
3572 * linear extent list (or even the inline buffer) if possible. The
3573 * compaction policy is as follows:
3574 *
3575 * Full Compaction: Extents fit into a single page (or inline buffer)
Lachlan McIlroy71a8c872008-09-26 12:17:57 +10003576 * Partial Compaction: Extents occupy less than 50% of allocated space
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003577 * No Compaction: Extents occupy at least 50% of allocated space
3578 */
3579void
3580xfs_iext_irec_compact(
3581 xfs_ifork_t *ifp) /* inode fork pointer */
3582{
3583 xfs_extnum_t nextents; /* number of extents in file */
3584 int nlists; /* number of irec's (ex lists) */
3585
3586 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3587 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3588 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3589
3590 if (nextents == 0) {
3591 xfs_iext_destroy(ifp);
3592 } else if (nextents <= XFS_INLINE_EXTS) {
3593 xfs_iext_indirect_to_direct(ifp);
3594 xfs_iext_direct_to_inline(ifp, nextents);
3595 } else if (nextents <= XFS_LINEAR_EXTS) {
3596 xfs_iext_indirect_to_direct(ifp);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003597 } else if (nextents < (nlists * XFS_LINEAR_EXTS) >> 1) {
3598 xfs_iext_irec_compact_pages(ifp);
3599 }
3600}
3601
3602/*
3603 * Combine extents from neighboring extent pages.
3604 */
3605void
3606xfs_iext_irec_compact_pages(
3607 xfs_ifork_t *ifp) /* inode fork pointer */
3608{
3609 xfs_ext_irec_t *erp, *erp_next;/* pointers to irec entries */
3610 int erp_idx = 0; /* indirection array index */
3611 int nlists; /* number of irec's (ex lists) */
3612
3613 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3614 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3615 while (erp_idx < nlists - 1) {
3616 erp = &ifp->if_u1.if_ext_irec[erp_idx];
3617 erp_next = erp + 1;
3618 if (erp_next->er_extcount <=
3619 (XFS_LINEAR_EXTS - erp->er_extcount)) {
Lachlan McIlroy71a8c872008-09-26 12:17:57 +10003620 memcpy(&erp->er_extbuf[erp->er_extcount],
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003621 erp_next->er_extbuf, erp_next->er_extcount *
3622 sizeof(xfs_bmbt_rec_t));
3623 erp->er_extcount += erp_next->er_extcount;
3624 /*
3625 * Free page before removing extent record
3626 * so er_extoffs don't get modified in
3627 * xfs_iext_irec_remove.
3628 */
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10003629 kmem_free(erp_next->er_extbuf);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003630 erp_next->er_extbuf = NULL;
3631 xfs_iext_irec_remove(ifp, erp_idx + 1);
3632 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3633 } else {
3634 erp_idx++;
3635 }
3636 }
3637}
3638
3639/*
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003640 * This is called to update the er_extoff field in the indirection
3641 * array when extents have been added or removed from one of the
3642 * extent lists. erp_idx contains the irec index to begin updating
3643 * at and ext_diff contains the number of extents that were added
3644 * or removed.
3645 */
3646void
3647xfs_iext_irec_update_extoffs(
3648 xfs_ifork_t *ifp, /* inode fork pointer */
3649 int erp_idx, /* irec index to update */
3650 int ext_diff) /* number of new extents */
3651{
3652 int i; /* loop counter */
3653 int nlists; /* number of irec's (ex lists */
3654
3655 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3656 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3657 for (i = erp_idx; i < nlists; i++) {
3658 ifp->if_u1.if_ext_irec[i].er_extoff += ext_diff;
3659 }
3660}