blob: b21022499c2e8f302699f80ca2af344301fee941 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
Nathan Scott7b718762005-11-02 14:58:39 +11003 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Robert P. J. Day40ebd812007-11-23 16:30:51 +110018#include <linux/log2.h>
19
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110021#include "xfs_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "xfs_types.h"
Nathan Scotta844f452005-11-02 14:38:42 +110023#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "xfs_log.h"
Nathan Scotta844f452005-11-02 14:38:42 +110025#include "xfs_inum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include "xfs_trans.h"
27#include "xfs_trans_priv.h"
28#include "xfs_sb.h"
29#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include "xfs_mount.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include "xfs_bmap_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110032#include "xfs_alloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "xfs_ialloc_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110034#include "xfs_attr_sf.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "xfs_dinode.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include "xfs_inode.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include "xfs_buf_item.h"
Nathan Scotta844f452005-11-02 14:38:42 +110038#include "xfs_inode_item.h"
39#include "xfs_btree.h"
40#include "xfs_alloc.h"
41#include "xfs_ialloc.h"
42#include "xfs_bmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include "xfs_error.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include "xfs_utils.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include "xfs_quota.h"
David Chinner2a82b8b2007-07-11 11:09:12 +100046#include "xfs_filestream.h"
Christoph Hellwig739bfb22007-08-29 10:58:01 +100047#include "xfs_vnodeops.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000048#include "xfs_trace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Linus Torvalds1da177e2005-04-16 15:20:36 -070050kmem_zone_t *xfs_ifork_zone;
51kmem_zone_t *xfs_inode_zone;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53/*
Christoph Hellwig8f04c472011-07-08 14:34:34 +020054 * Used in xfs_itruncate_extents(). This is the maximum number of extents
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 * freed from a file in a single transaction.
56 */
57#define XFS_ITRUNC_MAX_EXTENTS 2
58
59STATIC int xfs_iflush_int(xfs_inode_t *, xfs_buf_t *);
60STATIC int xfs_iformat_local(xfs_inode_t *, xfs_dinode_t *, int, int);
61STATIC int xfs_iformat_extents(xfs_inode_t *, xfs_dinode_t *, int);
62STATIC int xfs_iformat_btree(xfs_inode_t *, xfs_dinode_t *, int);
63
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#ifdef DEBUG
65/*
66 * Make sure that the extents in the given memory buffer
67 * are valid.
68 */
69STATIC void
70xfs_validate_extents(
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +110071 xfs_ifork_t *ifp,
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 int nrecs,
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 xfs_exntfmt_t fmt)
74{
75 xfs_bmbt_irec_t irec;
Christoph Hellwiga6f64d42007-08-16 16:23:40 +100076 xfs_bmbt_rec_host_t rec;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 int i;
78
79 for (i = 0; i < nrecs; i++) {
Christoph Hellwiga6f64d42007-08-16 16:23:40 +100080 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, i);
81 rec.l0 = get_unaligned(&ep->l0);
82 rec.l1 = get_unaligned(&ep->l1);
83 xfs_bmbt_get_all(&rec, &irec);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 if (fmt == XFS_EXTFMT_NOSTATE)
85 ASSERT(irec.br_state == XFS_EXT_NORM);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 }
87}
88#else /* DEBUG */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +100089#define xfs_validate_extents(ifp, nrecs, fmt)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090#endif /* DEBUG */
91
92/*
93 * Check that none of the inode's in the buffer have a next
94 * unlinked field of 0.
95 */
96#if defined(DEBUG)
97void
98xfs_inobp_check(
99 xfs_mount_t *mp,
100 xfs_buf_t *bp)
101{
102 int i;
103 int j;
104 xfs_dinode_t *dip;
105
106 j = mp->m_inode_cluster_size >> mp->m_sb.sb_inodelog;
107
108 for (i = 0; i < j; i++) {
109 dip = (xfs_dinode_t *)xfs_buf_offset(bp,
110 i * mp->m_sb.sb_inodesize);
111 if (!dip->di_next_unlinked) {
Dave Chinner53487782011-03-07 10:05:35 +1100112 xfs_alert(mp,
113 "Detected bogus zero next_unlinked field in incore inode buffer 0x%p.",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 bp);
115 ASSERT(dip->di_next_unlinked);
116 }
117 }
118}
119#endif
120
121/*
David Chinner4ae29b42008-03-06 13:43:34 +1100122 * Find the buffer associated with the given inode map
123 * We do basic validation checks on the buffer once it has been
124 * retrieved from disk.
125 */
126STATIC int
127xfs_imap_to_bp(
128 xfs_mount_t *mp,
129 xfs_trans_t *tp,
Christoph Hellwig92bfc6e2008-11-28 14:23:41 +1100130 struct xfs_imap *imap,
David Chinner4ae29b42008-03-06 13:43:34 +1100131 xfs_buf_t **bpp,
132 uint buf_flags,
Christoph Hellwigb48d8d62008-11-28 14:23:41 +1100133 uint iget_flags)
David Chinner4ae29b42008-03-06 13:43:34 +1100134{
135 int error;
136 int i;
137 int ni;
138 xfs_buf_t *bp;
139
140 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, imap->im_blkno,
David Chinnera3f74ff2008-03-06 13:43:42 +1100141 (int)imap->im_len, buf_flags, &bp);
David Chinner4ae29b42008-03-06 13:43:34 +1100142 if (error) {
David Chinnera3f74ff2008-03-06 13:43:42 +1100143 if (error != EAGAIN) {
Dave Chinner0b932cc2011-03-07 10:08:35 +1100144 xfs_warn(mp,
145 "%s: xfs_trans_read_buf() returned error %d.",
146 __func__, error);
David Chinnera3f74ff2008-03-06 13:43:42 +1100147 } else {
Christoph Hellwig0cadda12010-01-19 09:56:44 +0000148 ASSERT(buf_flags & XBF_TRYLOCK);
David Chinnera3f74ff2008-03-06 13:43:42 +1100149 }
David Chinner4ae29b42008-03-06 13:43:34 +1100150 return error;
151 }
152
153 /*
154 * Validate the magic number and version of every inode in the buffer
155 * (if DEBUG kernel) or the first inode in the buffer, otherwise.
156 */
157#ifdef DEBUG
158 ni = BBTOB(imap->im_len) >> mp->m_sb.sb_inodelog;
159#else /* usual case */
160 ni = 1;
161#endif
162
163 for (i = 0; i < ni; i++) {
164 int di_ok;
165 xfs_dinode_t *dip;
166
167 dip = (xfs_dinode_t *)xfs_buf_offset(bp,
168 (i << mp->m_sb.sb_inodelog));
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200169 di_ok = dip->di_magic == cpu_to_be16(XFS_DINODE_MAGIC) &&
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100170 XFS_DINODE_GOOD_VERSION(dip->di_version);
David Chinner4ae29b42008-03-06 13:43:34 +1100171 if (unlikely(XFS_TEST_ERROR(!di_ok, mp,
172 XFS_ERRTAG_ITOBP_INOTOBP,
173 XFS_RANDOM_ITOBP_INOTOBP))) {
Dave Chinner19207792010-06-24 11:15:47 +1000174 if (iget_flags & XFS_IGET_UNTRUSTED) {
David Chinner4ae29b42008-03-06 13:43:34 +1100175 xfs_trans_brelse(tp, bp);
176 return XFS_ERROR(EINVAL);
177 }
178 XFS_CORRUPTION_ERROR("xfs_imap_to_bp",
179 XFS_ERRLEVEL_HIGH, mp, dip);
180#ifdef DEBUG
Dave Chinner0b932cc2011-03-07 10:08:35 +1100181 xfs_emerg(mp,
182 "bad inode magic/vsn daddr %lld #%d (magic=%x)",
David Chinner4ae29b42008-03-06 13:43:34 +1100183 (unsigned long long)imap->im_blkno, i,
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100184 be16_to_cpu(dip->di_magic));
Dave Chinner0b932cc2011-03-07 10:08:35 +1100185 ASSERT(0);
David Chinner4ae29b42008-03-06 13:43:34 +1100186#endif
187 xfs_trans_brelse(tp, bp);
188 return XFS_ERROR(EFSCORRUPTED);
189 }
190 }
191
192 xfs_inobp_check(mp, bp);
David Chinner4ae29b42008-03-06 13:43:34 +1100193 *bpp = bp;
194 return 0;
195}
196
197/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 * This routine is called to map an inode number within a file
199 * system to the buffer containing the on-disk version of the
200 * inode. It returns a pointer to the buffer containing the
201 * on-disk inode in the bpp parameter, and in the dip parameter
202 * it returns a pointer to the on-disk inode within that buffer.
203 *
204 * If a non-zero error is returned, then the contents of bpp and
205 * dipp are undefined.
206 *
207 * Use xfs_imap() to determine the size and location of the
208 * buffer to read from disk.
209 */
Christoph Hellwigc679eef2008-10-30 18:04:13 +1100210int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211xfs_inotobp(
212 xfs_mount_t *mp,
213 xfs_trans_t *tp,
214 xfs_ino_t ino,
215 xfs_dinode_t **dipp,
216 xfs_buf_t **bpp,
Christoph Hellwigc679eef2008-10-30 18:04:13 +1100217 int *offset,
218 uint imap_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219{
Christoph Hellwig92bfc6e2008-11-28 14:23:41 +1100220 struct xfs_imap imap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 xfs_buf_t *bp;
222 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 imap.im_blkno = 0;
Christoph Hellwiga1941892008-11-28 14:23:40 +1100225 error = xfs_imap(mp, tp, ino, &imap, imap_flags);
David Chinner4ae29b42008-03-06 13:43:34 +1100226 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
Christoph Hellwig0cadda12010-01-19 09:56:44 +0000229 error = xfs_imap_to_bp(mp, tp, &imap, &bp, XBF_LOCK, imap_flags);
David Chinner4ae29b42008-03-06 13:43:34 +1100230 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 *dipp = (xfs_dinode_t *)xfs_buf_offset(bp, imap.im_boffset);
234 *bpp = bp;
235 *offset = imap.im_boffset;
236 return 0;
237}
238
239
240/*
241 * This routine is called to map an inode to the buffer containing
242 * the on-disk version of the inode. It returns a pointer to the
243 * buffer containing the on-disk inode in the bpp parameter, and in
244 * the dip parameter it returns a pointer to the on-disk inode within
245 * that buffer.
246 *
247 * If a non-zero error is returned, then the contents of bpp and
248 * dipp are undefined.
249 *
Christoph Hellwig76d8b272008-11-28 14:23:40 +1100250 * The inode is expected to already been mapped to its buffer and read
251 * in once, thus we can use the mapping information stored in the inode
252 * rather than calling xfs_imap(). This allows us to avoid the overhead
253 * of looking at the inode btree for small block file systems
Christoph Hellwig94e1b692008-11-28 14:23:41 +1100254 * (see xfs_imap()).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 */
256int
257xfs_itobp(
258 xfs_mount_t *mp,
259 xfs_trans_t *tp,
260 xfs_inode_t *ip,
261 xfs_dinode_t **dipp,
262 xfs_buf_t **bpp,
David Chinnera3f74ff2008-03-06 13:43:42 +1100263 uint buf_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264{
265 xfs_buf_t *bp;
266 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Christoph Hellwig92bfc6e2008-11-28 14:23:41 +1100268 ASSERT(ip->i_imap.im_blkno != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
Christoph Hellwig92bfc6e2008-11-28 14:23:41 +1100270 error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &bp, buf_flags, 0);
David Chinner4ae29b42008-03-06 13:43:34 +1100271 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 return error;
Nathan Scott4d1a2ed2006-06-09 17:12:28 +1000273
David Chinnera3f74ff2008-03-06 13:43:42 +1100274 if (!bp) {
Christoph Hellwig0cadda12010-01-19 09:56:44 +0000275 ASSERT(buf_flags & XBF_TRYLOCK);
David Chinnera3f74ff2008-03-06 13:43:42 +1100276 ASSERT(tp == NULL);
277 *bpp = NULL;
278 return EAGAIN;
279 }
280
Christoph Hellwig92bfc6e2008-11-28 14:23:41 +1100281 *dipp = (xfs_dinode_t *)xfs_buf_offset(bp, ip->i_imap.im_boffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 *bpp = bp;
283 return 0;
284}
285
286/*
287 * Move inode type and inode format specific information from the
288 * on-disk inode to the in-core inode. For fifos, devs, and sockets
289 * this means set if_rdev to the proper value. For files, directories,
290 * and symlinks this means to bring in the in-line data or extent
291 * pointers. For a file in B-tree format, only the root is immediately
292 * brought in-core. The rest will be in-lined in if_extents when it
293 * is first referenced (see xfs_iread_extents()).
294 */
295STATIC int
296xfs_iformat(
297 xfs_inode_t *ip,
298 xfs_dinode_t *dip)
299{
300 xfs_attr_shortform_t *atp;
301 int size;
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000302 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 xfs_fsize_t di_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100305 if (unlikely(be32_to_cpu(dip->di_nextents) +
306 be16_to_cpu(dip->di_anextents) >
307 be64_to_cpu(dip->di_nblocks))) {
Dave Chinner65333b42011-03-07 10:03:35 +1100308 xfs_warn(ip->i_mount,
Nathan Scott3762ec62006-01-12 10:29:53 +1100309 "corrupt dinode %Lu, extent total = %d, nblocks = %Lu.",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 (unsigned long long)ip->i_ino,
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100311 (int)(be32_to_cpu(dip->di_nextents) +
312 be16_to_cpu(dip->di_anextents)),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 (unsigned long long)
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100314 be64_to_cpu(dip->di_nblocks));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 XFS_CORRUPTION_ERROR("xfs_iformat(1)", XFS_ERRLEVEL_LOW,
316 ip->i_mount, dip);
317 return XFS_ERROR(EFSCORRUPTED);
318 }
319
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100320 if (unlikely(dip->di_forkoff > ip->i_mount->m_sb.sb_inodesize)) {
Dave Chinner65333b42011-03-07 10:03:35 +1100321 xfs_warn(ip->i_mount, "corrupt dinode %Lu, forkoff = 0x%x.",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 (unsigned long long)ip->i_ino,
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100323 dip->di_forkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 XFS_CORRUPTION_ERROR("xfs_iformat(2)", XFS_ERRLEVEL_LOW,
325 ip->i_mount, dip);
326 return XFS_ERROR(EFSCORRUPTED);
327 }
328
Christoph Hellwigb89d4202009-08-10 11:32:18 -0300329 if (unlikely((ip->i_d.di_flags & XFS_DIFLAG_REALTIME) &&
330 !ip->i_mount->m_rtdev_targp)) {
Dave Chinner65333b42011-03-07 10:03:35 +1100331 xfs_warn(ip->i_mount,
Christoph Hellwigb89d4202009-08-10 11:32:18 -0300332 "corrupt dinode %Lu, has realtime flag set.",
333 ip->i_ino);
334 XFS_CORRUPTION_ERROR("xfs_iformat(realtime)",
335 XFS_ERRLEVEL_LOW, ip->i_mount, dip);
336 return XFS_ERROR(EFSCORRUPTED);
337 }
338
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 switch (ip->i_d.di_mode & S_IFMT) {
340 case S_IFIFO:
341 case S_IFCHR:
342 case S_IFBLK:
343 case S_IFSOCK:
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100344 if (unlikely(dip->di_format != XFS_DINODE_FMT_DEV)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 XFS_CORRUPTION_ERROR("xfs_iformat(3)", XFS_ERRLEVEL_LOW,
346 ip->i_mount, dip);
347 return XFS_ERROR(EFSCORRUPTED);
348 }
349 ip->i_d.di_size = 0;
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100350 ip->i_df.if_u2.if_rdev = xfs_dinode_get_rdev(dip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 break;
352
353 case S_IFREG:
354 case S_IFLNK:
355 case S_IFDIR:
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100356 switch (dip->di_format) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 case XFS_DINODE_FMT_LOCAL:
358 /*
359 * no local regular files yet
360 */
Al Viroabbede12011-07-26 02:31:30 -0400361 if (unlikely(S_ISREG(be16_to_cpu(dip->di_mode)))) {
Dave Chinner65333b42011-03-07 10:03:35 +1100362 xfs_warn(ip->i_mount,
363 "corrupt inode %Lu (local format for regular file).",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 (unsigned long long) ip->i_ino);
365 XFS_CORRUPTION_ERROR("xfs_iformat(4)",
366 XFS_ERRLEVEL_LOW,
367 ip->i_mount, dip);
368 return XFS_ERROR(EFSCORRUPTED);
369 }
370
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100371 di_size = be64_to_cpu(dip->di_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 if (unlikely(di_size > XFS_DFORK_DSIZE(dip, ip->i_mount))) {
Dave Chinner65333b42011-03-07 10:03:35 +1100373 xfs_warn(ip->i_mount,
374 "corrupt inode %Lu (bad size %Ld for local inode).",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 (unsigned long long) ip->i_ino,
376 (long long) di_size);
377 XFS_CORRUPTION_ERROR("xfs_iformat(5)",
378 XFS_ERRLEVEL_LOW,
379 ip->i_mount, dip);
380 return XFS_ERROR(EFSCORRUPTED);
381 }
382
383 size = (int)di_size;
384 error = xfs_iformat_local(ip, dip, XFS_DATA_FORK, size);
385 break;
386 case XFS_DINODE_FMT_EXTENTS:
387 error = xfs_iformat_extents(ip, dip, XFS_DATA_FORK);
388 break;
389 case XFS_DINODE_FMT_BTREE:
390 error = xfs_iformat_btree(ip, dip, XFS_DATA_FORK);
391 break;
392 default:
393 XFS_ERROR_REPORT("xfs_iformat(6)", XFS_ERRLEVEL_LOW,
394 ip->i_mount);
395 return XFS_ERROR(EFSCORRUPTED);
396 }
397 break;
398
399 default:
400 XFS_ERROR_REPORT("xfs_iformat(7)", XFS_ERRLEVEL_LOW, ip->i_mount);
401 return XFS_ERROR(EFSCORRUPTED);
402 }
403 if (error) {
404 return error;
405 }
406 if (!XFS_DFORK_Q(dip))
407 return 0;
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000408
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 ASSERT(ip->i_afp == NULL);
Dave Chinner4a7eddd2010-07-20 17:53:59 +1000410 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP | KM_NOFS);
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000411
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100412 switch (dip->di_aformat) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 case XFS_DINODE_FMT_LOCAL:
414 atp = (xfs_attr_shortform_t *)XFS_DFORK_APTR(dip);
Nathan Scott3b244aa2006-03-17 17:29:25 +1100415 size = be16_to_cpu(atp->hdr.totsize);
Christoph Hellwig2809f762009-01-19 02:04:16 +0100416
417 if (unlikely(size < sizeof(struct xfs_attr_sf_hdr))) {
Dave Chinner65333b42011-03-07 10:03:35 +1100418 xfs_warn(ip->i_mount,
419 "corrupt inode %Lu (bad attr fork size %Ld).",
Christoph Hellwig2809f762009-01-19 02:04:16 +0100420 (unsigned long long) ip->i_ino,
421 (long long) size);
422 XFS_CORRUPTION_ERROR("xfs_iformat(8)",
423 XFS_ERRLEVEL_LOW,
424 ip->i_mount, dip);
425 return XFS_ERROR(EFSCORRUPTED);
426 }
427
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 error = xfs_iformat_local(ip, dip, XFS_ATTR_FORK, size);
429 break;
430 case XFS_DINODE_FMT_EXTENTS:
431 error = xfs_iformat_extents(ip, dip, XFS_ATTR_FORK);
432 break;
433 case XFS_DINODE_FMT_BTREE:
434 error = xfs_iformat_btree(ip, dip, XFS_ATTR_FORK);
435 break;
436 default:
437 error = XFS_ERROR(EFSCORRUPTED);
438 break;
439 }
440 if (error) {
441 kmem_zone_free(xfs_ifork_zone, ip->i_afp);
442 ip->i_afp = NULL;
443 xfs_idestroy_fork(ip, XFS_DATA_FORK);
444 }
445 return error;
446}
447
448/*
449 * The file is in-lined in the on-disk inode.
450 * If it fits into if_inline_data, then copy
451 * it there, otherwise allocate a buffer for it
452 * and copy the data there. Either way, set
453 * if_data to point at the data.
454 * If we allocate a buffer for the data, make
455 * sure that its size is a multiple of 4 and
456 * record the real size in i_real_bytes.
457 */
458STATIC int
459xfs_iformat_local(
460 xfs_inode_t *ip,
461 xfs_dinode_t *dip,
462 int whichfork,
463 int size)
464{
465 xfs_ifork_t *ifp;
466 int real_size;
467
468 /*
469 * If the size is unreasonable, then something
470 * is wrong and we just bail out rather than crash in
471 * kmem_alloc() or memcpy() below.
472 */
473 if (unlikely(size > XFS_DFORK_SIZE(dip, ip->i_mount, whichfork))) {
Dave Chinner65333b42011-03-07 10:03:35 +1100474 xfs_warn(ip->i_mount,
475 "corrupt inode %Lu (bad size %d for local fork, size = %d).",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 (unsigned long long) ip->i_ino, size,
477 XFS_DFORK_SIZE(dip, ip->i_mount, whichfork));
478 XFS_CORRUPTION_ERROR("xfs_iformat_local", XFS_ERRLEVEL_LOW,
479 ip->i_mount, dip);
480 return XFS_ERROR(EFSCORRUPTED);
481 }
482 ifp = XFS_IFORK_PTR(ip, whichfork);
483 real_size = 0;
484 if (size == 0)
485 ifp->if_u1.if_data = NULL;
486 else if (size <= sizeof(ifp->if_u2.if_inline_data))
487 ifp->if_u1.if_data = ifp->if_u2.if_inline_data;
488 else {
489 real_size = roundup(size, 4);
Dave Chinner4a7eddd2010-07-20 17:53:59 +1000490 ifp->if_u1.if_data = kmem_alloc(real_size, KM_SLEEP | KM_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 }
492 ifp->if_bytes = size;
493 ifp->if_real_bytes = real_size;
494 if (size)
495 memcpy(ifp->if_u1.if_data, XFS_DFORK_PTR(dip, whichfork), size);
496 ifp->if_flags &= ~XFS_IFEXTENTS;
497 ifp->if_flags |= XFS_IFINLINE;
498 return 0;
499}
500
501/*
502 * The file consists of a set of extents all
503 * of which fit into the on-disk inode.
504 * If there are few enough extents to fit into
505 * the if_inline_ext, then copy them there.
506 * Otherwise allocate a buffer for them and copy
507 * them into it. Either way, set if_extents
508 * to point at the extents.
509 */
510STATIC int
511xfs_iformat_extents(
512 xfs_inode_t *ip,
513 xfs_dinode_t *dip,
514 int whichfork)
515{
Christoph Hellwiga6f64d42007-08-16 16:23:40 +1000516 xfs_bmbt_rec_t *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 xfs_ifork_t *ifp;
518 int nex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 int size;
520 int i;
521
522 ifp = XFS_IFORK_PTR(ip, whichfork);
523 nex = XFS_DFORK_NEXTENTS(dip, whichfork);
524 size = nex * (uint)sizeof(xfs_bmbt_rec_t);
525
526 /*
527 * If the number of extents is unreasonable, then something
528 * is wrong and we just bail out rather than crash in
529 * kmem_alloc() or memcpy() below.
530 */
531 if (unlikely(size < 0 || size > XFS_DFORK_SIZE(dip, ip->i_mount, whichfork))) {
Dave Chinner65333b42011-03-07 10:03:35 +1100532 xfs_warn(ip->i_mount, "corrupt inode %Lu ((a)extents = %d).",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 (unsigned long long) ip->i_ino, nex);
534 XFS_CORRUPTION_ERROR("xfs_iformat_extents(1)", XFS_ERRLEVEL_LOW,
535 ip->i_mount, dip);
536 return XFS_ERROR(EFSCORRUPTED);
537 }
538
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100539 ifp->if_real_bytes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 if (nex == 0)
541 ifp->if_u1.if_extents = NULL;
542 else if (nex <= XFS_INLINE_EXTS)
543 ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100544 else
545 xfs_iext_add(ifp, 0, nex);
546
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 ifp->if_bytes = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 if (size) {
549 dp = (xfs_bmbt_rec_t *) XFS_DFORK_PTR(dip, whichfork);
Christoph Hellwiga6f64d42007-08-16 16:23:40 +1000550 xfs_validate_extents(ifp, nex, XFS_EXTFMT_INODE(ip));
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100551 for (i = 0; i < nex; i++, dp++) {
Christoph Hellwiga6f64d42007-08-16 16:23:40 +1000552 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, i);
Harvey Harrison597bca62008-08-13 16:29:21 +1000553 ep->l0 = get_unaligned_be64(&dp->l0);
554 ep->l1 = get_unaligned_be64(&dp->l1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 }
Eric Sandeen3a59c942007-07-11 11:09:47 +1000556 XFS_BMAP_TRACE_EXLIST(ip, nex, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 if (whichfork != XFS_DATA_FORK ||
558 XFS_EXTFMT_INODE(ip) == XFS_EXTFMT_NOSTATE)
559 if (unlikely(xfs_check_nostate_extents(
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100560 ifp, 0, nex))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 XFS_ERROR_REPORT("xfs_iformat_extents(2)",
562 XFS_ERRLEVEL_LOW,
563 ip->i_mount);
564 return XFS_ERROR(EFSCORRUPTED);
565 }
566 }
567 ifp->if_flags |= XFS_IFEXTENTS;
568 return 0;
569}
570
571/*
572 * The file has too many extents to fit into
573 * the inode, so they are in B-tree format.
574 * Allocate a buffer for the root of the B-tree
575 * and copy the root into it. The i_extents
576 * field will remain NULL until all of the
577 * extents are read in (when they are needed).
578 */
579STATIC int
580xfs_iformat_btree(
581 xfs_inode_t *ip,
582 xfs_dinode_t *dip,
583 int whichfork)
584{
585 xfs_bmdr_block_t *dfp;
586 xfs_ifork_t *ifp;
587 /* REFERENCED */
588 int nrecs;
589 int size;
590
591 ifp = XFS_IFORK_PTR(ip, whichfork);
592 dfp = (xfs_bmdr_block_t *)XFS_DFORK_PTR(dip, whichfork);
593 size = XFS_BMAP_BROOT_SPACE(dfp);
Christoph Hellwig60197e82008-10-30 17:11:19 +1100594 nrecs = be16_to_cpu(dfp->bb_numrecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
596 /*
597 * blow out if -- fork has less extents than can fit in
598 * fork (fork shouldn't be a btree format), root btree
599 * block has more records than can fit into the fork,
600 * or the number of extents is greater than the number of
601 * blocks.
602 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000603 if (unlikely(XFS_IFORK_NEXTENTS(ip, whichfork) <=
604 XFS_IFORK_MAXEXT(ip, whichfork) ||
605 XFS_BMDR_SPACE_CALC(nrecs) >
606 XFS_DFORK_SIZE(dip, ip->i_mount, whichfork) ||
607 XFS_IFORK_NEXTENTS(ip, whichfork) > ip->i_d.di_nblocks)) {
Dave Chinner65333b42011-03-07 10:03:35 +1100608 xfs_warn(ip->i_mount, "corrupt inode %Lu (btree).",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 (unsigned long long) ip->i_ino);
Dave Chinner65333b42011-03-07 10:03:35 +1100610 XFS_CORRUPTION_ERROR("xfs_iformat_btree", XFS_ERRLEVEL_LOW,
611 ip->i_mount, dip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 return XFS_ERROR(EFSCORRUPTED);
613 }
614
615 ifp->if_broot_bytes = size;
Dave Chinner4a7eddd2010-07-20 17:53:59 +1000616 ifp->if_broot = kmem_alloc(size, KM_SLEEP | KM_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 ASSERT(ifp->if_broot != NULL);
618 /*
619 * Copy and convert from the on-disk structure
620 * to the in-memory structure.
621 */
Christoph Hellwig60197e82008-10-30 17:11:19 +1100622 xfs_bmdr_to_bmbt(ip->i_mount, dfp,
623 XFS_DFORK_SIZE(dip, ip->i_mount, whichfork),
624 ifp->if_broot, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 ifp->if_flags &= ~XFS_IFEXTENTS;
626 ifp->if_flags |= XFS_IFBROOT;
627
628 return 0;
629}
630
Eric Sandeend96f8f82009-07-02 00:09:33 -0500631STATIC void
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000632xfs_dinode_from_disk(
633 xfs_icdinode_t *to,
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100634 xfs_dinode_t *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635{
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000636 to->di_magic = be16_to_cpu(from->di_magic);
637 to->di_mode = be16_to_cpu(from->di_mode);
638 to->di_version = from ->di_version;
639 to->di_format = from->di_format;
640 to->di_onlink = be16_to_cpu(from->di_onlink);
641 to->di_uid = be32_to_cpu(from->di_uid);
642 to->di_gid = be32_to_cpu(from->di_gid);
643 to->di_nlink = be32_to_cpu(from->di_nlink);
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +0000644 to->di_projid_lo = be16_to_cpu(from->di_projid_lo);
645 to->di_projid_hi = be16_to_cpu(from->di_projid_hi);
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000646 memcpy(to->di_pad, from->di_pad, sizeof(to->di_pad));
647 to->di_flushiter = be16_to_cpu(from->di_flushiter);
648 to->di_atime.t_sec = be32_to_cpu(from->di_atime.t_sec);
649 to->di_atime.t_nsec = be32_to_cpu(from->di_atime.t_nsec);
650 to->di_mtime.t_sec = be32_to_cpu(from->di_mtime.t_sec);
651 to->di_mtime.t_nsec = be32_to_cpu(from->di_mtime.t_nsec);
652 to->di_ctime.t_sec = be32_to_cpu(from->di_ctime.t_sec);
653 to->di_ctime.t_nsec = be32_to_cpu(from->di_ctime.t_nsec);
654 to->di_size = be64_to_cpu(from->di_size);
655 to->di_nblocks = be64_to_cpu(from->di_nblocks);
656 to->di_extsize = be32_to_cpu(from->di_extsize);
657 to->di_nextents = be32_to_cpu(from->di_nextents);
658 to->di_anextents = be16_to_cpu(from->di_anextents);
659 to->di_forkoff = from->di_forkoff;
660 to->di_aformat = from->di_aformat;
661 to->di_dmevmask = be32_to_cpu(from->di_dmevmask);
662 to->di_dmstate = be16_to_cpu(from->di_dmstate);
663 to->di_flags = be16_to_cpu(from->di_flags);
664 to->di_gen = be32_to_cpu(from->di_gen);
665}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000667void
668xfs_dinode_to_disk(
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100669 xfs_dinode_t *to,
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000670 xfs_icdinode_t *from)
671{
672 to->di_magic = cpu_to_be16(from->di_magic);
673 to->di_mode = cpu_to_be16(from->di_mode);
674 to->di_version = from ->di_version;
675 to->di_format = from->di_format;
676 to->di_onlink = cpu_to_be16(from->di_onlink);
677 to->di_uid = cpu_to_be32(from->di_uid);
678 to->di_gid = cpu_to_be32(from->di_gid);
679 to->di_nlink = cpu_to_be32(from->di_nlink);
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +0000680 to->di_projid_lo = cpu_to_be16(from->di_projid_lo);
681 to->di_projid_hi = cpu_to_be16(from->di_projid_hi);
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000682 memcpy(to->di_pad, from->di_pad, sizeof(to->di_pad));
683 to->di_flushiter = cpu_to_be16(from->di_flushiter);
684 to->di_atime.t_sec = cpu_to_be32(from->di_atime.t_sec);
685 to->di_atime.t_nsec = cpu_to_be32(from->di_atime.t_nsec);
686 to->di_mtime.t_sec = cpu_to_be32(from->di_mtime.t_sec);
687 to->di_mtime.t_nsec = cpu_to_be32(from->di_mtime.t_nsec);
688 to->di_ctime.t_sec = cpu_to_be32(from->di_ctime.t_sec);
689 to->di_ctime.t_nsec = cpu_to_be32(from->di_ctime.t_nsec);
690 to->di_size = cpu_to_be64(from->di_size);
691 to->di_nblocks = cpu_to_be64(from->di_nblocks);
692 to->di_extsize = cpu_to_be32(from->di_extsize);
693 to->di_nextents = cpu_to_be32(from->di_nextents);
694 to->di_anextents = cpu_to_be16(from->di_anextents);
695 to->di_forkoff = from->di_forkoff;
696 to->di_aformat = from->di_aformat;
697 to->di_dmevmask = cpu_to_be32(from->di_dmevmask);
698 to->di_dmstate = cpu_to_be16(from->di_dmstate);
699 to->di_flags = cpu_to_be16(from->di_flags);
700 to->di_gen = cpu_to_be32(from->di_gen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701}
702
703STATIC uint
704_xfs_dic2xflags(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 __uint16_t di_flags)
706{
707 uint flags = 0;
708
709 if (di_flags & XFS_DIFLAG_ANY) {
710 if (di_flags & XFS_DIFLAG_REALTIME)
711 flags |= XFS_XFLAG_REALTIME;
712 if (di_flags & XFS_DIFLAG_PREALLOC)
713 flags |= XFS_XFLAG_PREALLOC;
714 if (di_flags & XFS_DIFLAG_IMMUTABLE)
715 flags |= XFS_XFLAG_IMMUTABLE;
716 if (di_flags & XFS_DIFLAG_APPEND)
717 flags |= XFS_XFLAG_APPEND;
718 if (di_flags & XFS_DIFLAG_SYNC)
719 flags |= XFS_XFLAG_SYNC;
720 if (di_flags & XFS_DIFLAG_NOATIME)
721 flags |= XFS_XFLAG_NOATIME;
722 if (di_flags & XFS_DIFLAG_NODUMP)
723 flags |= XFS_XFLAG_NODUMP;
724 if (di_flags & XFS_DIFLAG_RTINHERIT)
725 flags |= XFS_XFLAG_RTINHERIT;
726 if (di_flags & XFS_DIFLAG_PROJINHERIT)
727 flags |= XFS_XFLAG_PROJINHERIT;
728 if (di_flags & XFS_DIFLAG_NOSYMLINKS)
729 flags |= XFS_XFLAG_NOSYMLINKS;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100730 if (di_flags & XFS_DIFLAG_EXTSIZE)
731 flags |= XFS_XFLAG_EXTSIZE;
732 if (di_flags & XFS_DIFLAG_EXTSZINHERIT)
733 flags |= XFS_XFLAG_EXTSZINHERIT;
Barry Naujokd3446ea2006-06-09 14:54:19 +1000734 if (di_flags & XFS_DIFLAG_NODEFRAG)
735 flags |= XFS_XFLAG_NODEFRAG;
David Chinner2a82b8b2007-07-11 11:09:12 +1000736 if (di_flags & XFS_DIFLAG_FILESTREAM)
737 flags |= XFS_XFLAG_FILESTREAM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 }
739
740 return flags;
741}
742
743uint
744xfs_ip2xflags(
745 xfs_inode_t *ip)
746{
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000747 xfs_icdinode_t *dic = &ip->i_d;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
Nathan Scotta916e2b2006-06-09 17:12:17 +1000749 return _xfs_dic2xflags(dic->di_flags) |
Christoph Hellwig45ba5982007-12-07 14:07:20 +1100750 (XFS_IFORK_Q(ip) ? XFS_XFLAG_HASATTR : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751}
752
753uint
754xfs_dic2xflags(
Christoph Hellwig45ba5982007-12-07 14:07:20 +1100755 xfs_dinode_t *dip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756{
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100757 return _xfs_dic2xflags(be16_to_cpu(dip->di_flags)) |
Christoph Hellwig45ba5982007-12-07 14:07:20 +1100758 (XFS_DFORK_Q(dip) ? XFS_XFLAG_HASATTR : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759}
760
761/*
Christoph Hellwig24f211b2008-11-28 14:23:42 +1100762 * Read the disk inode attributes into the in-core inode structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 */
764int
765xfs_iread(
766 xfs_mount_t *mp,
767 xfs_trans_t *tp,
Christoph Hellwig24f211b2008-11-28 14:23:42 +1100768 xfs_inode_t *ip,
Christoph Hellwig24f211b2008-11-28 14:23:42 +1100769 uint iget_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770{
771 xfs_buf_t *bp;
772 xfs_dinode_t *dip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 int error;
774
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 /*
Christoph Hellwig92bfc6e2008-11-28 14:23:41 +1100776 * Fill in the location information in the in-core inode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 */
Christoph Hellwig24f211b2008-11-28 14:23:42 +1100778 error = xfs_imap(mp, tp, ip->i_ino, &ip->i_imap, iget_flags);
Christoph Hellwig9ed04512008-10-30 18:26:04 +1100779 if (error)
Christoph Hellwig24f211b2008-11-28 14:23:42 +1100780 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
782 /*
Christoph Hellwig92bfc6e2008-11-28 14:23:41 +1100783 * Get pointers to the on-disk inode and the buffer containing it.
Christoph Hellwig76d8b272008-11-28 14:23:40 +1100784 */
Christoph Hellwig92bfc6e2008-11-28 14:23:41 +1100785 error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &bp,
Christoph Hellwig0cadda12010-01-19 09:56:44 +0000786 XBF_LOCK, iget_flags);
Christoph Hellwig76d8b272008-11-28 14:23:40 +1100787 if (error)
Christoph Hellwig24f211b2008-11-28 14:23:42 +1100788 return error;
Christoph Hellwig92bfc6e2008-11-28 14:23:41 +1100789 dip = (xfs_dinode_t *)xfs_buf_offset(bp, ip->i_imap.im_boffset);
Christoph Hellwig76d8b272008-11-28 14:23:40 +1100790
791 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 * If we got something that isn't an inode it means someone
793 * (nfs or dmi) has a stale handle.
794 */
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200795 if (dip->di_magic != cpu_to_be16(XFS_DINODE_MAGIC)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796#ifdef DEBUG
Dave Chinner53487782011-03-07 10:05:35 +1100797 xfs_alert(mp,
798 "%s: dip->di_magic (0x%x) != XFS_DINODE_MAGIC (0x%x)",
799 __func__, be16_to_cpu(dip->di_magic), XFS_DINODE_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800#endif /* DEBUG */
Christoph Hellwig9ed04512008-10-30 18:26:04 +1100801 error = XFS_ERROR(EINVAL);
802 goto out_brelse;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 }
804
805 /*
806 * If the on-disk inode is already linked to a directory
807 * entry, copy all of the inode into the in-core inode.
808 * xfs_iformat() handles copying in the inode format
809 * specific information.
810 * Otherwise, just get the truly permanent information.
811 */
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100812 if (dip->di_mode) {
813 xfs_dinode_from_disk(&ip->i_d, dip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 error = xfs_iformat(ip, dip);
815 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816#ifdef DEBUG
Dave Chinner53487782011-03-07 10:05:35 +1100817 xfs_alert(mp, "%s: xfs_iformat() returned error %d",
818 __func__, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819#endif /* DEBUG */
Christoph Hellwig9ed04512008-10-30 18:26:04 +1100820 goto out_brelse;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 }
822 } else {
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100823 ip->i_d.di_magic = be16_to_cpu(dip->di_magic);
824 ip->i_d.di_version = dip->di_version;
825 ip->i_d.di_gen = be32_to_cpu(dip->di_gen);
826 ip->i_d.di_flushiter = be16_to_cpu(dip->di_flushiter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 /*
828 * Make sure to pull in the mode here as well in
829 * case the inode is released without being used.
830 * This ensures that xfs_inactive() will see that
831 * the inode is already free and not try to mess
832 * with the uninitialized part of it.
833 */
834 ip->i_d.di_mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 }
836
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 /*
838 * The inode format changed when we moved the link count and
839 * made it 32 bits long. If this is an old format inode,
840 * convert it in memory to look like a new one. If it gets
841 * flushed to disk we will convert back before flushing or
842 * logging it. We zero out the new projid field and the old link
843 * count field. We'll handle clearing the pad field (the remains
844 * of the old uuid field) when we actually convert the inode to
845 * the new format. We don't change the version number so that we
846 * can distinguish this from a real new format inode.
847 */
Christoph Hellwig51ce16d2008-11-28 14:23:39 +1100848 if (ip->i_d.di_version == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 ip->i_d.di_nlink = ip->i_d.di_onlink;
850 ip->i_d.di_onlink = 0;
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +0000851 xfs_set_projid(ip, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 }
853
854 ip->i_delayed_blks = 0;
855
856 /*
857 * Mark the buffer containing the inode as something to keep
858 * around for a while. This helps to keep recently accessed
859 * meta-data in-core longer.
860 */
Dave Chinner821eb212010-12-02 16:31:13 +1100861 xfs_buf_set_ref(bp, XFS_INO_REF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
863 /*
864 * Use xfs_trans_brelse() to release the buffer containing the
865 * on-disk inode, because it was acquired with xfs_trans_read_buf()
866 * in xfs_itobp() above. If tp is NULL, this is just a normal
867 * brelse(). If we're within a transaction, then xfs_trans_brelse()
868 * will only release the buffer if it is not dirty within the
869 * transaction. It will be OK to release the buffer in this case,
870 * because inodes on disk are never destroyed and we will be
871 * locking the new in-core inode before putting it in the hash
872 * table where other processes can find it. Thus we don't have
873 * to worry about the inode being changed just because we released
874 * the buffer.
875 */
Christoph Hellwig9ed04512008-10-30 18:26:04 +1100876 out_brelse:
877 xfs_trans_brelse(tp, bp);
Christoph Hellwig9ed04512008-10-30 18:26:04 +1100878 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879}
880
881/*
882 * Read in extents from a btree-format inode.
883 * Allocate and fill in if_extents. Real work is done in xfs_bmap.c.
884 */
885int
886xfs_iread_extents(
887 xfs_trans_t *tp,
888 xfs_inode_t *ip,
889 int whichfork)
890{
891 int error;
892 xfs_ifork_t *ifp;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100893 xfs_extnum_t nextents;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
895 if (unlikely(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
896 XFS_ERROR_REPORT("xfs_iread_extents", XFS_ERRLEVEL_LOW,
897 ip->i_mount);
898 return XFS_ERROR(EFSCORRUPTED);
899 }
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100900 nextents = XFS_IFORK_NEXTENTS(ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 ifp = XFS_IFORK_PTR(ip, whichfork);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100902
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 /*
904 * We know that the size is valid (it's checked in iformat_btree)
905 */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100906 ifp->if_bytes = ifp->if_real_bytes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 ifp->if_flags |= XFS_IFEXTENTS;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100908 xfs_iext_add(ifp, 0, nextents);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 error = xfs_bmap_read_extents(tp, ip, whichfork);
910 if (error) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100911 xfs_iext_destroy(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 ifp->if_flags &= ~XFS_IFEXTENTS;
913 return error;
914 }
Christoph Hellwiga6f64d42007-08-16 16:23:40 +1000915 xfs_validate_extents(ifp, nextents, XFS_EXTFMT_INODE(ip));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 return 0;
917}
918
919/*
920 * Allocate an inode on disk and return a copy of its in-core version.
921 * The in-core inode is locked exclusively. Set mode, nlink, and rdev
922 * appropriately within the inode. The uid and gid for the inode are
923 * set according to the contents of the given cred structure.
924 *
925 * Use xfs_dialloc() to allocate the on-disk inode. If xfs_dialloc()
926 * has a free inode available, call xfs_iget()
927 * to obtain the in-core version of the allocated inode. Finally,
928 * fill in the inode and log its initial contents. In this case,
929 * ialloc_context would be set to NULL and call_again set to false.
930 *
931 * If xfs_dialloc() does not have an available inode,
932 * it will replenish its supply by doing an allocation. Since we can
933 * only do one allocation within a transaction without deadlocks, we
934 * must commit the current transaction before returning the inode itself.
935 * In this case, therefore, we will set call_again to true and return.
936 * The caller should then commit the current transaction, start a new
937 * transaction, and call xfs_ialloc() again to actually get the inode.
938 *
939 * To ensure that some other process does not grab the inode that
940 * was allocated during the first call to xfs_ialloc(), this routine
941 * also returns the [locked] bp pointing to the head of the freelist
942 * as ialloc_context. The caller should hold this buffer across
943 * the commit and pass it back into this routine on the second call.
David Chinnerb11f94d2007-07-11 11:09:33 +1000944 *
945 * If we are allocating quota inodes, we do not have a parent inode
946 * to attach to or associate with (i.e. pip == NULL) because they
947 * are not linked into the directory structure - they are attached
948 * directly to the superblock - and so have no parent.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 */
950int
951xfs_ialloc(
952 xfs_trans_t *tp,
953 xfs_inode_t *pip,
Al Viro576b1d62011-07-26 02:50:15 -0400954 umode_t mode,
Nathan Scott31b084a2005-05-05 13:25:00 -0700955 xfs_nlink_t nlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 xfs_dev_t rdev,
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +0000957 prid_t prid,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 int okalloc,
959 xfs_buf_t **ialloc_context,
960 boolean_t *call_again,
961 xfs_inode_t **ipp)
962{
963 xfs_ino_t ino;
964 xfs_inode_t *ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 uint flags;
966 int error;
Christoph Hellwigdff35fd2008-08-13 16:44:15 +1000967 timespec_t tv;
David Chinnerbf904242008-10-30 17:36:14 +1100968 int filestreams = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
970 /*
971 * Call the space management code to pick
972 * the on-disk inode to be allocated.
973 */
David Chinnerb11f94d2007-07-11 11:09:33 +1000974 error = xfs_dialloc(tp, pip ? pip->i_ino : 0, mode, okalloc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 ialloc_context, call_again, &ino);
David Chinnerbf904242008-10-30 17:36:14 +1100976 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 if (*call_again || ino == NULLFSINO) {
979 *ipp = NULL;
980 return 0;
981 }
982 ASSERT(*ialloc_context == NULL);
983
984 /*
985 * Get the in-core inode with the lock held exclusively.
986 * This is because we're setting fields here we need
987 * to prevent others from looking at until we're done.
988 */
Christoph Hellwigec3ba852011-02-13 13:26:42 +0000989 error = xfs_iget(tp->t_mountp, tp, ino, XFS_IGET_CREATE,
990 XFS_ILOCK_EXCL, &ip);
David Chinnerbf904242008-10-30 17:36:14 +1100991 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 ASSERT(ip != NULL);
994
Al Viro576b1d62011-07-26 02:50:15 -0400995 ip->i_d.di_mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 ip->i_d.di_onlink = 0;
997 ip->i_d.di_nlink = nlink;
998 ASSERT(ip->i_d.di_nlink == nlink);
David Howells9e2b2dc2008-08-13 16:20:04 +0100999 ip->i_d.di_uid = current_fsuid();
1000 ip->i_d.di_gid = current_fsgid();
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +00001001 xfs_set_projid(ip, prid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 memset(&(ip->i_d.di_pad[0]), 0, sizeof(ip->i_d.di_pad));
1003
1004 /*
1005 * If the superblock version is up to where we support new format
1006 * inodes and this is currently an old format inode, then change
1007 * the inode version number now. This way we only do the conversion
1008 * here rather than here and in the flush/logging code.
1009 */
Eric Sandeen62118702008-03-06 13:44:28 +11001010 if (xfs_sb_version_hasnlink(&tp->t_mountp->m_sb) &&
Christoph Hellwig51ce16d2008-11-28 14:23:39 +11001011 ip->i_d.di_version == 1) {
1012 ip->i_d.di_version = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 /*
1014 * We've already zeroed the old link count, the projid field,
1015 * and the pad field.
1016 */
1017 }
1018
1019 /*
1020 * Project ids won't be stored on disk if we are using a version 1 inode.
1021 */
Christoph Hellwig51ce16d2008-11-28 14:23:39 +11001022 if ((prid != 0) && (ip->i_d.di_version == 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 xfs_bump_ino_vers2(tp, ip);
1024
Christoph Hellwigbd186aa2007-08-30 17:21:12 +10001025 if (pip && XFS_INHERIT_GID(pip)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 ip->i_d.di_gid = pip->i_d.di_gid;
Al Viroabbede12011-07-26 02:31:30 -04001027 if ((pip->i_d.di_mode & S_ISGID) && S_ISDIR(mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 ip->i_d.di_mode |= S_ISGID;
1029 }
1030 }
1031
1032 /*
1033 * If the group ID of the new file does not match the effective group
1034 * ID or one of the supplementary group IDs, the S_ISGID bit is cleared
1035 * (and only if the irix_sgid_inherit compatibility variable is set).
1036 */
1037 if ((irix_sgid_inherit) &&
1038 (ip->i_d.di_mode & S_ISGID) &&
1039 (!in_group_p((gid_t)ip->i_d.di_gid))) {
1040 ip->i_d.di_mode &= ~S_ISGID;
1041 }
1042
1043 ip->i_d.di_size = 0;
1044 ip->i_d.di_nextents = 0;
1045 ASSERT(ip->i_d.di_nblocks == 0);
Christoph Hellwigdff35fd2008-08-13 16:44:15 +10001046
1047 nanotime(&tv);
1048 ip->i_d.di_mtime.t_sec = (__int32_t)tv.tv_sec;
1049 ip->i_d.di_mtime.t_nsec = (__int32_t)tv.tv_nsec;
1050 ip->i_d.di_atime = ip->i_d.di_mtime;
1051 ip->i_d.di_ctime = ip->i_d.di_mtime;
1052
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 /*
1054 * di_gen will have been taken care of in xfs_iread.
1055 */
1056 ip->i_d.di_extsize = 0;
1057 ip->i_d.di_dmevmask = 0;
1058 ip->i_d.di_dmstate = 0;
1059 ip->i_d.di_flags = 0;
1060 flags = XFS_ILOG_CORE;
1061 switch (mode & S_IFMT) {
1062 case S_IFIFO:
1063 case S_IFCHR:
1064 case S_IFBLK:
1065 case S_IFSOCK:
1066 ip->i_d.di_format = XFS_DINODE_FMT_DEV;
1067 ip->i_df.if_u2.if_rdev = rdev;
1068 ip->i_df.if_flags = 0;
1069 flags |= XFS_ILOG_DEV;
1070 break;
1071 case S_IFREG:
David Chinnerbf904242008-10-30 17:36:14 +11001072 /*
1073 * we can't set up filestreams until after the VFS inode
1074 * is set up properly.
1075 */
1076 if (pip && xfs_inode_is_filestream(pip))
1077 filestreams = 1;
David Chinner2a82b8b2007-07-11 11:09:12 +10001078 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 case S_IFDIR:
David Chinnerb11f94d2007-07-11 11:09:33 +10001080 if (pip && (pip->i_d.di_flags & XFS_DIFLAG_ANY)) {
Nathan Scott365ca832005-06-21 15:39:12 +10001081 uint di_flags = 0;
1082
Al Viroabbede12011-07-26 02:31:30 -04001083 if (S_ISDIR(mode)) {
Nathan Scott365ca832005-06-21 15:39:12 +10001084 if (pip->i_d.di_flags & XFS_DIFLAG_RTINHERIT)
1085 di_flags |= XFS_DIFLAG_RTINHERIT;
Nathan Scottdd9f4382006-01-11 15:28:28 +11001086 if (pip->i_d.di_flags & XFS_DIFLAG_EXTSZINHERIT) {
1087 di_flags |= XFS_DIFLAG_EXTSZINHERIT;
1088 ip->i_d.di_extsize = pip->i_d.di_extsize;
1089 }
Al Viroabbede12011-07-26 02:31:30 -04001090 } else if (S_ISREG(mode)) {
Christoph Hellwig613d7042007-10-11 17:44:08 +10001091 if (pip->i_d.di_flags & XFS_DIFLAG_RTINHERIT)
Nathan Scott365ca832005-06-21 15:39:12 +10001092 di_flags |= XFS_DIFLAG_REALTIME;
Nathan Scottdd9f4382006-01-11 15:28:28 +11001093 if (pip->i_d.di_flags & XFS_DIFLAG_EXTSZINHERIT) {
1094 di_flags |= XFS_DIFLAG_EXTSIZE;
1095 ip->i_d.di_extsize = pip->i_d.di_extsize;
1096 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 }
1098 if ((pip->i_d.di_flags & XFS_DIFLAG_NOATIME) &&
1099 xfs_inherit_noatime)
Nathan Scott365ca832005-06-21 15:39:12 +10001100 di_flags |= XFS_DIFLAG_NOATIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 if ((pip->i_d.di_flags & XFS_DIFLAG_NODUMP) &&
1102 xfs_inherit_nodump)
Nathan Scott365ca832005-06-21 15:39:12 +10001103 di_flags |= XFS_DIFLAG_NODUMP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 if ((pip->i_d.di_flags & XFS_DIFLAG_SYNC) &&
1105 xfs_inherit_sync)
Nathan Scott365ca832005-06-21 15:39:12 +10001106 di_flags |= XFS_DIFLAG_SYNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 if ((pip->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) &&
1108 xfs_inherit_nosymlinks)
Nathan Scott365ca832005-06-21 15:39:12 +10001109 di_flags |= XFS_DIFLAG_NOSYMLINKS;
1110 if (pip->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
1111 di_flags |= XFS_DIFLAG_PROJINHERIT;
Barry Naujokd3446ea2006-06-09 14:54:19 +10001112 if ((pip->i_d.di_flags & XFS_DIFLAG_NODEFRAG) &&
1113 xfs_inherit_nodefrag)
1114 di_flags |= XFS_DIFLAG_NODEFRAG;
David Chinner2a82b8b2007-07-11 11:09:12 +10001115 if (pip->i_d.di_flags & XFS_DIFLAG_FILESTREAM)
1116 di_flags |= XFS_DIFLAG_FILESTREAM;
Nathan Scott365ca832005-06-21 15:39:12 +10001117 ip->i_d.di_flags |= di_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 }
1119 /* FALLTHROUGH */
1120 case S_IFLNK:
1121 ip->i_d.di_format = XFS_DINODE_FMT_EXTENTS;
1122 ip->i_df.if_flags = XFS_IFEXTENTS;
1123 ip->i_df.if_bytes = ip->i_df.if_real_bytes = 0;
1124 ip->i_df.if_u1.if_extents = NULL;
1125 break;
1126 default:
1127 ASSERT(0);
1128 }
1129 /*
1130 * Attribute fork settings for new inode.
1131 */
1132 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
1133 ip->i_d.di_anextents = 0;
1134
1135 /*
1136 * Log the new values stuffed into the inode.
1137 */
Christoph Hellwigddc34152011-09-19 15:00:54 +00001138 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 xfs_trans_log_inode(tp, ip, flags);
1140
Nathan Scottb83bd132006-06-09 16:48:30 +10001141 /* now that we have an i_mode we can setup inode ops and unlock */
Christoph Hellwig41be8be2008-08-13 16:23:13 +10001142 xfs_setup_inode(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
David Chinnerbf904242008-10-30 17:36:14 +11001144 /* now we have set up the vfs inode we can associate the filestream */
1145 if (filestreams) {
1146 error = xfs_filestream_associate(pip, ip);
1147 if (error < 0)
1148 return -error;
1149 if (!error)
1150 xfs_iflags_set(ip, XFS_IFILESTREAM);
1151 }
1152
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 *ipp = ip;
1154 return 0;
1155}
1156
1157/*
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001158 * Free up the underlying blocks past new_size. The new size must be smaller
1159 * than the current size. This routine can be used both for the attribute and
1160 * data fork, and does not modify the inode size, which is left to the caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 *
David Chinnerf6485052008-04-17 16:50:04 +10001162 * The transaction passed to this routine must have made a permanent log
1163 * reservation of at least XFS_ITRUNCATE_LOG_RES. This routine may commit the
1164 * given transaction and start new ones, so make sure everything involved in
1165 * the transaction is tidy before calling here. Some transaction will be
1166 * returned to the caller to be committed. The incoming transaction must
1167 * already include the inode, and both inode locks must be held exclusively.
1168 * The inode must also be "held" within the transaction. On return the inode
1169 * will be "held" within the returned transaction. This routine does NOT
1170 * require any disk space to be reserved for it within the transaction.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 *
David Chinnerf6485052008-04-17 16:50:04 +10001172 * If we get an error, we must return with the inode locked and linked into the
1173 * current transaction. This keeps things simple for the higher level code,
1174 * because it always knows that the inode is locked and held in the transaction
1175 * that returns to it whether errors occur or not. We don't mark the inode
1176 * dirty on error so that transactions can be easily aborted if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 */
1178int
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001179xfs_itruncate_extents(
1180 struct xfs_trans **tpp,
1181 struct xfs_inode *ip,
1182 int whichfork,
1183 xfs_fsize_t new_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184{
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001185 struct xfs_mount *mp = ip->i_mount;
1186 struct xfs_trans *tp = *tpp;
1187 struct xfs_trans *ntp;
1188 xfs_bmap_free_t free_list;
1189 xfs_fsblock_t first_block;
1190 xfs_fileoff_t first_unmap_block;
1191 xfs_fileoff_t last_block;
1192 xfs_filblks_t unmap_len;
1193 int committed;
1194 int error = 0;
1195 int done = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
Christoph Hellwig579aa9c2008-04-22 17:34:00 +10001197 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_IOLOCK_EXCL));
Christoph Hellwigce7ae1512011-12-18 20:00:11 +00001198 ASSERT(new_size <= XFS_ISIZE(ip));
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001199 ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 ASSERT(ip->i_itemp != NULL);
Christoph Hellwig898621d2010-06-24 11:36:58 +10001201 ASSERT(ip->i_itemp->ili_lock_flags == 0);
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001202 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
Christoph Hellwig673e8e52011-12-18 20:00:04 +00001204 trace_xfs_itruncate_extents_start(ip, new_size);
1205
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 /*
1207 * Since it is possible for space to become allocated beyond
1208 * the end of the file (in a crash where the space is allocated
1209 * but the inode size is not yet updated), simply remove any
1210 * blocks which show up between the new EOF and the maximum
1211 * possible file size. If the first block to be removed is
1212 * beyond the maximum file size (ie it is the same as last_block),
1213 * then there is nothing to do.
1214 */
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001215 first_unmap_block = XFS_B_TO_FSB(mp, (xfs_ufsize_t)new_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 last_block = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp));
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001217 if (first_unmap_block == last_block)
1218 return 0;
1219
1220 ASSERT(first_unmap_block < last_block);
1221 unmap_len = last_block - first_unmap_block + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 while (!done) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06001223 xfs_bmap_init(&free_list, &first_block);
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001224 error = xfs_bunmapi(tp, ip,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001225 first_unmap_block, unmap_len,
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001226 xfs_bmapi_aflag(whichfork),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 XFS_ITRUNC_MAX_EXTENTS,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001228 &first_block, &free_list,
Christoph Hellwigb4e91812010-06-23 18:11:15 +10001229 &done);
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001230 if (error)
1231 goto out_bmap_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
1233 /*
1234 * Duplicate the transaction that has the permanent
1235 * reservation and commit the old transaction.
1236 */
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001237 error = xfs_bmap_finish(&tp, &free_list, &committed);
Christoph Hellwig898621d2010-06-24 11:36:58 +10001238 if (committed)
Christoph Hellwigddc34152011-09-19 15:00:54 +00001239 xfs_trans_ijoin(tp, ip, 0);
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001240 if (error)
1241 goto out_bmap_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
1243 if (committed) {
1244 /*
David Chinnerf6485052008-04-17 16:50:04 +10001245 * Mark the inode dirty so it will be logged and
David Chinnere5720ee2008-04-10 12:21:18 +10001246 * moved forward in the log as part of every commit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 */
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001248 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 }
David Chinnerf6485052008-04-17 16:50:04 +10001250
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001251 ntp = xfs_trans_dup(tp);
1252 error = xfs_trans_commit(tp, 0);
1253 tp = ntp;
David Chinnere5720ee2008-04-10 12:21:18 +10001254
Christoph Hellwigddc34152011-09-19 15:00:54 +00001255 xfs_trans_ijoin(tp, ip, 0);
David Chinnerf6485052008-04-17 16:50:04 +10001256
Dave Chinnercc09c0d2008-11-17 17:37:10 +11001257 if (error)
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001258 goto out;
1259
Dave Chinnercc09c0d2008-11-17 17:37:10 +11001260 /*
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001261 * Transaction commit worked ok so we can drop the extra ticket
Dave Chinnercc09c0d2008-11-17 17:37:10 +11001262 * reference that we gained in xfs_trans_dup()
1263 */
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001264 xfs_log_ticket_put(tp->t_ticket);
1265 error = xfs_trans_reserve(tp, 0,
David Chinnerf6485052008-04-17 16:50:04 +10001266 XFS_ITRUNCATE_LOG_RES(mp), 0,
1267 XFS_TRANS_PERM_LOG_RES,
1268 XFS_ITRUNCATE_LOG_COUNT);
1269 if (error)
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001270 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 }
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001272
Christoph Hellwig673e8e52011-12-18 20:00:04 +00001273 /*
1274 * Always re-log the inode so that our permanent transaction can keep
1275 * on rolling it forward in the log.
1276 */
1277 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1278
1279 trace_xfs_itruncate_extents_end(ip, new_size);
1280
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001281out:
1282 *tpp = tp;
1283 return error;
1284out_bmap_cancel:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 /*
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001286 * If the bunmapi call encounters an error, return to the caller where
1287 * the transaction can be properly aborted. We just need to make sure
1288 * we're not holding any resources that we were not when we came in.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 */
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001290 xfs_bmap_cancel(&free_list);
1291 goto out;
1292}
1293
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294/*
1295 * This is called when the inode's link count goes to 0.
1296 * We place the on-disk inode on a list in the AGI. It
1297 * will be pulled from this list when the inode is freed.
1298 */
1299int
1300xfs_iunlink(
1301 xfs_trans_t *tp,
1302 xfs_inode_t *ip)
1303{
1304 xfs_mount_t *mp;
1305 xfs_agi_t *agi;
1306 xfs_dinode_t *dip;
1307 xfs_buf_t *agibp;
1308 xfs_buf_t *ibp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 xfs_agino_t agino;
1310 short bucket_index;
1311 int offset;
1312 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
1314 ASSERT(ip->i_d.di_nlink == 0);
1315 ASSERT(ip->i_d.di_mode != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
1317 mp = tp->t_mountp;
1318
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 /*
1320 * Get the agi buffer first. It ensures lock ordering
1321 * on the list.
1322 */
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11001323 error = xfs_read_agi(mp, tp, XFS_INO_TO_AGNO(mp, ip->i_ino), &agibp);
Vlad Apostolov859d7182007-10-11 17:44:18 +10001324 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 agi = XFS_BUF_TO_AGI(agibp);
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11001327
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 /*
1329 * Get the index into the agi hash table for the
1330 * list this inode will go on.
1331 */
1332 agino = XFS_INO_TO_AGINO(mp, ip->i_ino);
1333 ASSERT(agino != 0);
1334 bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS;
1335 ASSERT(agi->agi_unlinked[bucket_index]);
Christoph Hellwig16259e72005-11-02 15:11:25 +11001336 ASSERT(be32_to_cpu(agi->agi_unlinked[bucket_index]) != agino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001338 if (agi->agi_unlinked[bucket_index] != cpu_to_be32(NULLAGINO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 /*
1340 * There is already another inode in the bucket we need
1341 * to add ourselves to. Add us at the front of the list.
1342 * Here we put the head pointer into our next pointer,
1343 * and then we fall through to point the head at us.
1344 */
Christoph Hellwig0cadda12010-01-19 09:56:44 +00001345 error = xfs_itobp(mp, tp, ip, &dip, &ibp, XBF_LOCK);
Vlad Apostolovc319b582007-11-23 16:27:51 +11001346 if (error)
1347 return error;
1348
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001349 ASSERT(dip->di_next_unlinked == cpu_to_be32(NULLAGINO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 dip->di_next_unlinked = agi->agi_unlinked[bucket_index];
Christoph Hellwig92bfc6e2008-11-28 14:23:41 +11001351 offset = ip->i_imap.im_boffset +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 offsetof(xfs_dinode_t, di_next_unlinked);
1353 xfs_trans_inode_buf(tp, ibp);
1354 xfs_trans_log_buf(tp, ibp, offset,
1355 (offset + sizeof(xfs_agino_t) - 1));
1356 xfs_inobp_check(mp, ibp);
1357 }
1358
1359 /*
1360 * Point the bucket head pointer at the inode being inserted.
1361 */
1362 ASSERT(agino != 0);
Christoph Hellwig16259e72005-11-02 15:11:25 +11001363 agi->agi_unlinked[bucket_index] = cpu_to_be32(agino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 offset = offsetof(xfs_agi_t, agi_unlinked) +
1365 (sizeof(xfs_agino_t) * bucket_index);
1366 xfs_trans_log_buf(tp, agibp, offset,
1367 (offset + sizeof(xfs_agino_t) - 1));
1368 return 0;
1369}
1370
1371/*
1372 * Pull the on-disk inode from the AGI unlinked list.
1373 */
1374STATIC int
1375xfs_iunlink_remove(
1376 xfs_trans_t *tp,
1377 xfs_inode_t *ip)
1378{
1379 xfs_ino_t next_ino;
1380 xfs_mount_t *mp;
1381 xfs_agi_t *agi;
1382 xfs_dinode_t *dip;
1383 xfs_buf_t *agibp;
1384 xfs_buf_t *ibp;
1385 xfs_agnumber_t agno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 xfs_agino_t agino;
1387 xfs_agino_t next_agino;
1388 xfs_buf_t *last_ibp;
Nathan Scott6fdf8cc2006-06-28 10:13:52 +10001389 xfs_dinode_t *last_dip = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 short bucket_index;
Nathan Scott6fdf8cc2006-06-28 10:13:52 +10001391 int offset, last_offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 mp = tp->t_mountp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 agno = XFS_INO_TO_AGNO(mp, ip->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396
1397 /*
1398 * Get the agi buffer first. It ensures lock ordering
1399 * on the list.
1400 */
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11001401 error = xfs_read_agi(mp, tp, agno, &agibp);
1402 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 return error;
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11001404
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 agi = XFS_BUF_TO_AGI(agibp);
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11001406
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 /*
1408 * Get the index into the agi hash table for the
1409 * list this inode will go on.
1410 */
1411 agino = XFS_INO_TO_AGINO(mp, ip->i_ino);
1412 ASSERT(agino != 0);
1413 bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001414 ASSERT(agi->agi_unlinked[bucket_index] != cpu_to_be32(NULLAGINO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 ASSERT(agi->agi_unlinked[bucket_index]);
1416
Christoph Hellwig16259e72005-11-02 15:11:25 +11001417 if (be32_to_cpu(agi->agi_unlinked[bucket_index]) == agino) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 /*
1419 * We're at the head of the list. Get the inode's
1420 * on-disk buffer to see if there is anyone after us
1421 * on the list. Only modify our next pointer if it
1422 * is not already NULLAGINO. This saves us the overhead
1423 * of dealing with the buffer when there is no need to
1424 * change it.
1425 */
Christoph Hellwig0cadda12010-01-19 09:56:44 +00001426 error = xfs_itobp(mp, tp, ip, &dip, &ibp, XBF_LOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 if (error) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001428 xfs_warn(mp, "%s: xfs_itobp() returned error %d.",
1429 __func__, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 return error;
1431 }
Christoph Hellwig347d1c02007-08-28 13:57:51 +10001432 next_agino = be32_to_cpu(dip->di_next_unlinked);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 ASSERT(next_agino != 0);
1434 if (next_agino != NULLAGINO) {
Christoph Hellwig347d1c02007-08-28 13:57:51 +10001435 dip->di_next_unlinked = cpu_to_be32(NULLAGINO);
Christoph Hellwig92bfc6e2008-11-28 14:23:41 +11001436 offset = ip->i_imap.im_boffset +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 offsetof(xfs_dinode_t, di_next_unlinked);
1438 xfs_trans_inode_buf(tp, ibp);
1439 xfs_trans_log_buf(tp, ibp, offset,
1440 (offset + sizeof(xfs_agino_t) - 1));
1441 xfs_inobp_check(mp, ibp);
1442 } else {
1443 xfs_trans_brelse(tp, ibp);
1444 }
1445 /*
1446 * Point the bucket head pointer at the next inode.
1447 */
1448 ASSERT(next_agino != 0);
1449 ASSERT(next_agino != agino);
Christoph Hellwig16259e72005-11-02 15:11:25 +11001450 agi->agi_unlinked[bucket_index] = cpu_to_be32(next_agino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 offset = offsetof(xfs_agi_t, agi_unlinked) +
1452 (sizeof(xfs_agino_t) * bucket_index);
1453 xfs_trans_log_buf(tp, agibp, offset,
1454 (offset + sizeof(xfs_agino_t) - 1));
1455 } else {
1456 /*
1457 * We need to search the list for the inode being freed.
1458 */
Christoph Hellwig16259e72005-11-02 15:11:25 +11001459 next_agino = be32_to_cpu(agi->agi_unlinked[bucket_index]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 last_ibp = NULL;
1461 while (next_agino != agino) {
1462 /*
1463 * If the last inode wasn't the one pointing to
1464 * us, then release its buffer since we're not
1465 * going to do anything with it.
1466 */
1467 if (last_ibp != NULL) {
1468 xfs_trans_brelse(tp, last_ibp);
1469 }
1470 next_ino = XFS_AGINO_TO_INO(mp, agno, next_agino);
1471 error = xfs_inotobp(mp, tp, next_ino, &last_dip,
Christoph Hellwigc679eef2008-10-30 18:04:13 +11001472 &last_ibp, &last_offset, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 if (error) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001474 xfs_warn(mp,
1475 "%s: xfs_inotobp() returned error %d.",
1476 __func__, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 return error;
1478 }
Christoph Hellwig347d1c02007-08-28 13:57:51 +10001479 next_agino = be32_to_cpu(last_dip->di_next_unlinked);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 ASSERT(next_agino != NULLAGINO);
1481 ASSERT(next_agino != 0);
1482 }
1483 /*
1484 * Now last_ibp points to the buffer previous to us on
1485 * the unlinked list. Pull us from the list.
1486 */
Christoph Hellwig0cadda12010-01-19 09:56:44 +00001487 error = xfs_itobp(mp, tp, ip, &dip, &ibp, XBF_LOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 if (error) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001489 xfs_warn(mp, "%s: xfs_itobp(2) returned error %d.",
1490 __func__, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 return error;
1492 }
Christoph Hellwig347d1c02007-08-28 13:57:51 +10001493 next_agino = be32_to_cpu(dip->di_next_unlinked);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 ASSERT(next_agino != 0);
1495 ASSERT(next_agino != agino);
1496 if (next_agino != NULLAGINO) {
Christoph Hellwig347d1c02007-08-28 13:57:51 +10001497 dip->di_next_unlinked = cpu_to_be32(NULLAGINO);
Christoph Hellwig92bfc6e2008-11-28 14:23:41 +11001498 offset = ip->i_imap.im_boffset +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 offsetof(xfs_dinode_t, di_next_unlinked);
1500 xfs_trans_inode_buf(tp, ibp);
1501 xfs_trans_log_buf(tp, ibp, offset,
1502 (offset + sizeof(xfs_agino_t) - 1));
1503 xfs_inobp_check(mp, ibp);
1504 } else {
1505 xfs_trans_brelse(tp, ibp);
1506 }
1507 /*
1508 * Point the previous inode on the list to the next inode.
1509 */
Christoph Hellwig347d1c02007-08-28 13:57:51 +10001510 last_dip->di_next_unlinked = cpu_to_be32(next_agino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 ASSERT(next_agino != 0);
1512 offset = last_offset + offsetof(xfs_dinode_t, di_next_unlinked);
1513 xfs_trans_inode_buf(tp, last_ibp);
1514 xfs_trans_log_buf(tp, last_ibp, offset,
1515 (offset + sizeof(xfs_agino_t) - 1));
1516 xfs_inobp_check(mp, last_ibp);
1517 }
1518 return 0;
1519}
1520
Dave Chinner5b3eed72010-08-24 11:42:41 +10001521/*
1522 * A big issue when freeing the inode cluster is is that we _cannot_ skip any
1523 * inodes that are in memory - they all must be marked stale and attached to
1524 * the cluster buffer.
1525 */
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +00001526STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527xfs_ifree_cluster(
1528 xfs_inode_t *free_ip,
1529 xfs_trans_t *tp,
1530 xfs_ino_t inum)
1531{
1532 xfs_mount_t *mp = free_ip->i_mount;
1533 int blks_per_cluster;
1534 int nbufs;
1535 int ninodes;
Dave Chinner5b257b42010-06-03 16:22:29 +10001536 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 xfs_daddr_t blkno;
1538 xfs_buf_t *bp;
Dave Chinner5b257b42010-06-03 16:22:29 +10001539 xfs_inode_t *ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 xfs_inode_log_item_t *iip;
1541 xfs_log_item_t *lip;
Dave Chinner5017e972010-01-11 11:47:40 +00001542 struct xfs_perag *pag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543
Dave Chinner5017e972010-01-11 11:47:40 +00001544 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, inum));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 if (mp->m_sb.sb_blocksize >= XFS_INODE_CLUSTER_SIZE(mp)) {
1546 blks_per_cluster = 1;
1547 ninodes = mp->m_sb.sb_inopblock;
1548 nbufs = XFS_IALLOC_BLOCKS(mp);
1549 } else {
1550 blks_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) /
1551 mp->m_sb.sb_blocksize;
1552 ninodes = blks_per_cluster * mp->m_sb.sb_inopblock;
1553 nbufs = XFS_IALLOC_BLOCKS(mp) / blks_per_cluster;
1554 }
1555
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 for (j = 0; j < nbufs; j++, inum += ninodes) {
1557 blkno = XFS_AGB_TO_DADDR(mp, XFS_INO_TO_AGNO(mp, inum),
1558 XFS_INO_TO_AGBNO(mp, inum));
1559
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 /*
Dave Chinner5b257b42010-06-03 16:22:29 +10001561 * We obtain and lock the backing buffer first in the process
1562 * here, as we have to ensure that any dirty inode that we
1563 * can't get the flush lock on is attached to the buffer.
1564 * If we scan the in-memory inodes first, then buffer IO can
1565 * complete before we get a lock on it, and hence we may fail
1566 * to mark all the active inodes on the buffer stale.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 */
Dave Chinner5b257b42010-06-03 16:22:29 +10001568 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, blkno,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 mp->m_bsize * blks_per_cluster,
Christoph Hellwig0cadda12010-01-19 09:56:44 +00001570 XBF_LOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +00001572 if (!bp)
1573 return ENOMEM;
Dave Chinner5b257b42010-06-03 16:22:29 +10001574 /*
1575 * Walk the inodes already attached to the buffer and mark them
1576 * stale. These will all have the flush locks held, so an
Dave Chinner5b3eed72010-08-24 11:42:41 +10001577 * in-memory inode walk can't lock them. By marking them all
1578 * stale first, we will not attempt to lock them in the loop
1579 * below as the XFS_ISTALE flag will be set.
Dave Chinner5b257b42010-06-03 16:22:29 +10001580 */
Christoph Hellwigadadbee2011-07-13 13:43:49 +02001581 lip = bp->b_fspriv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 while (lip) {
1583 if (lip->li_type == XFS_LI_INODE) {
1584 iip = (xfs_inode_log_item_t *)lip;
1585 ASSERT(iip->ili_logged == 1);
Christoph Hellwigca30b2a2010-06-23 18:11:15 +10001586 lip->li_cb = xfs_istale_done;
David Chinner7b2e2a32008-10-30 17:39:12 +11001587 xfs_trans_ail_copy_lsn(mp->m_ail,
1588 &iip->ili_flush_lsn,
1589 &iip->ili_item.li_lsn);
David Chinnere5ffd2b2006-11-21 18:55:33 +11001590 xfs_iflags_set(iip->ili_inode, XFS_ISTALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 }
1592 lip = lip->li_bio_list;
1593 }
1594
Dave Chinner5b3eed72010-08-24 11:42:41 +10001595
Dave Chinner5b257b42010-06-03 16:22:29 +10001596 /*
1597 * For each inode in memory attempt to add it to the inode
1598 * buffer and set it up for being staled on buffer IO
1599 * completion. This is safe as we've locked out tail pushing
1600 * and flushing by locking the buffer.
1601 *
1602 * We have already marked every inode that was part of a
1603 * transaction stale above, which means there is no point in
1604 * even trying to lock them.
1605 */
1606 for (i = 0; i < ninodes; i++) {
Dave Chinner5b3eed72010-08-24 11:42:41 +10001607retry:
Dave Chinner1a3e8f32010-12-17 17:29:43 +11001608 rcu_read_lock();
Dave Chinner5b257b42010-06-03 16:22:29 +10001609 ip = radix_tree_lookup(&pag->pag_ici_root,
1610 XFS_INO_TO_AGINO(mp, (inum + i)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611
Dave Chinner1a3e8f32010-12-17 17:29:43 +11001612 /* Inode not in memory, nothing to do */
1613 if (!ip) {
1614 rcu_read_unlock();
Dave Chinner5b257b42010-06-03 16:22:29 +10001615 continue;
1616 }
1617
Dave Chinner5b3eed72010-08-24 11:42:41 +10001618 /*
Dave Chinner1a3e8f32010-12-17 17:29:43 +11001619 * because this is an RCU protected lookup, we could
1620 * find a recently freed or even reallocated inode
1621 * during the lookup. We need to check under the
1622 * i_flags_lock for a valid inode here. Skip it if it
1623 * is not valid, the wrong inode or stale.
1624 */
1625 spin_lock(&ip->i_flags_lock);
1626 if (ip->i_ino != inum + i ||
1627 __xfs_iflags_test(ip, XFS_ISTALE)) {
1628 spin_unlock(&ip->i_flags_lock);
1629 rcu_read_unlock();
1630 continue;
1631 }
1632 spin_unlock(&ip->i_flags_lock);
1633
1634 /*
Dave Chinner5b3eed72010-08-24 11:42:41 +10001635 * Don't try to lock/unlock the current inode, but we
1636 * _cannot_ skip the other inodes that we did not find
1637 * in the list attached to the buffer and are not
1638 * already marked stale. If we can't lock it, back off
1639 * and retry.
1640 */
Dave Chinner5b257b42010-06-03 16:22:29 +10001641 if (ip != free_ip &&
1642 !xfs_ilock_nowait(ip, XFS_ILOCK_EXCL)) {
Dave Chinner1a3e8f32010-12-17 17:29:43 +11001643 rcu_read_unlock();
Dave Chinner5b3eed72010-08-24 11:42:41 +10001644 delay(1);
1645 goto retry;
Dave Chinner5b257b42010-06-03 16:22:29 +10001646 }
Dave Chinner1a3e8f32010-12-17 17:29:43 +11001647 rcu_read_unlock();
Dave Chinner5b257b42010-06-03 16:22:29 +10001648
Dave Chinner5b3eed72010-08-24 11:42:41 +10001649 xfs_iflock(ip);
Dave Chinner5b257b42010-06-03 16:22:29 +10001650 xfs_iflags_set(ip, XFS_ISTALE);
Dave Chinner5b257b42010-06-03 16:22:29 +10001651
Dave Chinner5b3eed72010-08-24 11:42:41 +10001652 /*
1653 * we don't need to attach clean inodes or those only
1654 * with unlogged changes (which we throw away, anyway).
1655 */
Dave Chinner5b257b42010-06-03 16:22:29 +10001656 iip = ip->i_itemp;
Dave Chinner5b3eed72010-08-24 11:42:41 +10001657 if (!iip || xfs_inode_clean(ip)) {
Dave Chinner5b257b42010-06-03 16:22:29 +10001658 ASSERT(ip != free_ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 ip->i_update_core = 0;
1660 xfs_ifunlock(ip);
1661 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1662 continue;
1663 }
1664
1665 iip->ili_last_fields = iip->ili_format.ilf_fields;
1666 iip->ili_format.ilf_fields = 0;
1667 iip->ili_logged = 1;
David Chinner7b2e2a32008-10-30 17:39:12 +11001668 xfs_trans_ail_copy_lsn(mp->m_ail, &iip->ili_flush_lsn,
1669 &iip->ili_item.li_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670
Christoph Hellwigca30b2a2010-06-23 18:11:15 +10001671 xfs_buf_attach_iodone(bp, xfs_istale_done,
1672 &iip->ili_item);
Dave Chinner5b257b42010-06-03 16:22:29 +10001673
1674 if (ip != free_ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 }
1677
Dave Chinner5b3eed72010-08-24 11:42:41 +10001678 xfs_trans_stale_inode_buf(tp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 xfs_trans_binval(tp, bp);
1680 }
1681
Dave Chinner5017e972010-01-11 11:47:40 +00001682 xfs_perag_put(pag);
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +00001683 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684}
1685
1686/*
1687 * This is called to return an inode to the inode free list.
1688 * The inode should already be truncated to 0 length and have
1689 * no pages associated with it. This routine also assumes that
1690 * the inode is already a part of the transaction.
1691 *
1692 * The on-disk copy of the inode will have been added to the list
1693 * of unlinked inodes in the AGI. We need to remove the inode from
1694 * that list atomically with respect to freeing it here.
1695 */
1696int
1697xfs_ifree(
1698 xfs_trans_t *tp,
1699 xfs_inode_t *ip,
1700 xfs_bmap_free_t *flist)
1701{
1702 int error;
1703 int delete;
1704 xfs_ino_t first_ino;
Vlad Apostolovc319b582007-11-23 16:27:51 +11001705 xfs_dinode_t *dip;
1706 xfs_buf_t *ibp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707
Christoph Hellwig579aa9c2008-04-22 17:34:00 +10001708 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 ASSERT(ip->i_d.di_nlink == 0);
1710 ASSERT(ip->i_d.di_nextents == 0);
1711 ASSERT(ip->i_d.di_anextents == 0);
Christoph Hellwigce7ae1512011-12-18 20:00:11 +00001712 ASSERT(ip->i_d.di_size == 0 || !S_ISREG(ip->i_d.di_mode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 ASSERT(ip->i_d.di_nblocks == 0);
1714
1715 /*
1716 * Pull the on-disk inode from the AGI unlinked list.
1717 */
1718 error = xfs_iunlink_remove(tp, ip);
1719 if (error != 0) {
1720 return error;
1721 }
1722
1723 error = xfs_difree(tp, ip->i_ino, flist, &delete, &first_ino);
1724 if (error != 0) {
1725 return error;
1726 }
1727 ip->i_d.di_mode = 0; /* mark incore inode as free */
1728 ip->i_d.di_flags = 0;
1729 ip->i_d.di_dmevmask = 0;
1730 ip->i_d.di_forkoff = 0; /* mark the attr fork not in use */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 ip->i_d.di_format = XFS_DINODE_FMT_EXTENTS;
1732 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
1733 /*
1734 * Bump the generation count so no one will be confused
1735 * by reincarnations of this inode.
1736 */
1737 ip->i_d.di_gen++;
Vlad Apostolovc319b582007-11-23 16:27:51 +11001738
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1740
Christoph Hellwig0cadda12010-01-19 09:56:44 +00001741 error = xfs_itobp(ip->i_mount, tp, ip, &dip, &ibp, XBF_LOCK);
Vlad Apostolovc319b582007-11-23 16:27:51 +11001742 if (error)
1743 return error;
1744
1745 /*
1746 * Clear the on-disk di_mode. This is to prevent xfs_bulkstat
1747 * from picking up this inode when it is reclaimed (its incore state
1748 * initialzed but not flushed to disk yet). The in-core di_mode is
1749 * already cleared and a corresponding transaction logged.
1750 * The hack here just synchronizes the in-core to on-disk
1751 * di_mode value in advance before the actual inode sync to disk.
1752 * This is OK because the inode is already unlinked and would never
1753 * change its di_mode again for this inode generation.
1754 * This is a temporary hack that would require a proper fix
1755 * in the future.
1756 */
Christoph Hellwig81591fe2008-11-28 14:23:39 +11001757 dip->di_mode = 0;
Vlad Apostolovc319b582007-11-23 16:27:51 +11001758
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 if (delete) {
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +00001760 error = xfs_ifree_cluster(ip, tp, first_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 }
1762
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +00001763 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764}
1765
1766/*
1767 * Reallocate the space for if_broot based on the number of records
1768 * being added or deleted as indicated in rec_diff. Move the records
1769 * and pointers in if_broot to fit the new size. When shrinking this
1770 * will eliminate holes between the records and pointers created by
1771 * the caller. When growing this will create holes to be filled in
1772 * by the caller.
1773 *
1774 * The caller must not request to add more records than would fit in
1775 * the on-disk inode root. If the if_broot is currently NULL, then
1776 * if we adding records one will be allocated. The caller must also
1777 * not request that the number of records go below zero, although
1778 * it can go to zero.
1779 *
1780 * ip -- the inode whose if_broot area is changing
1781 * ext_diff -- the change in the number of records, positive or negative,
1782 * requested for the if_broot array.
1783 */
1784void
1785xfs_iroot_realloc(
1786 xfs_inode_t *ip,
1787 int rec_diff,
1788 int whichfork)
1789{
Christoph Hellwig60197e82008-10-30 17:11:19 +11001790 struct xfs_mount *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 int cur_max;
1792 xfs_ifork_t *ifp;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11001793 struct xfs_btree_block *new_broot;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 int new_max;
1795 size_t new_size;
1796 char *np;
1797 char *op;
1798
1799 /*
1800 * Handle the degenerate case quietly.
1801 */
1802 if (rec_diff == 0) {
1803 return;
1804 }
1805
1806 ifp = XFS_IFORK_PTR(ip, whichfork);
1807 if (rec_diff > 0) {
1808 /*
1809 * If there wasn't any memory allocated before, just
1810 * allocate it now and get out.
1811 */
1812 if (ifp->if_broot_bytes == 0) {
1813 new_size = (size_t)XFS_BMAP_BROOT_SPACE_CALC(rec_diff);
Dave Chinner4a7eddd2010-07-20 17:53:59 +10001814 ifp->if_broot = kmem_alloc(new_size, KM_SLEEP | KM_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 ifp->if_broot_bytes = (int)new_size;
1816 return;
1817 }
1818
1819 /*
1820 * If there is already an existing if_broot, then we need
1821 * to realloc() it and shift the pointers to their new
1822 * location. The records don't change location because
1823 * they are kept butted up against the btree block header.
1824 */
Christoph Hellwig60197e82008-10-30 17:11:19 +11001825 cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 new_max = cur_max + rec_diff;
1827 new_size = (size_t)XFS_BMAP_BROOT_SPACE_CALC(new_max);
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11001828 ifp->if_broot = kmem_realloc(ifp->if_broot, new_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 (size_t)XFS_BMAP_BROOT_SPACE_CALC(cur_max), /* old size */
Dave Chinner4a7eddd2010-07-20 17:53:59 +10001830 KM_SLEEP | KM_NOFS);
Christoph Hellwig60197e82008-10-30 17:11:19 +11001831 op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
1832 ifp->if_broot_bytes);
1833 np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
1834 (int)new_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 ifp->if_broot_bytes = (int)new_size;
1836 ASSERT(ifp->if_broot_bytes <=
1837 XFS_IFORK_SIZE(ip, whichfork) + XFS_BROOT_SIZE_ADJ);
1838 memmove(np, op, cur_max * (uint)sizeof(xfs_dfsbno_t));
1839 return;
1840 }
1841
1842 /*
1843 * rec_diff is less than 0. In this case, we are shrinking the
1844 * if_broot buffer. It must already exist. If we go to zero
1845 * records, just get rid of the root and clear the status bit.
1846 */
1847 ASSERT((ifp->if_broot != NULL) && (ifp->if_broot_bytes > 0));
Christoph Hellwig60197e82008-10-30 17:11:19 +11001848 cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 new_max = cur_max + rec_diff;
1850 ASSERT(new_max >= 0);
1851 if (new_max > 0)
1852 new_size = (size_t)XFS_BMAP_BROOT_SPACE_CALC(new_max);
1853 else
1854 new_size = 0;
1855 if (new_size > 0) {
Dave Chinner4a7eddd2010-07-20 17:53:59 +10001856 new_broot = kmem_alloc(new_size, KM_SLEEP | KM_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 /*
1858 * First copy over the btree block header.
1859 */
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11001860 memcpy(new_broot, ifp->if_broot, XFS_BTREE_LBLOCK_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 } else {
1862 new_broot = NULL;
1863 ifp->if_flags &= ~XFS_IFBROOT;
1864 }
1865
1866 /*
1867 * Only copy the records and pointers if there are any.
1868 */
1869 if (new_max > 0) {
1870 /*
1871 * First copy the records.
1872 */
Christoph Hellwig136341b2008-10-30 17:11:40 +11001873 op = (char *)XFS_BMBT_REC_ADDR(mp, ifp->if_broot, 1);
1874 np = (char *)XFS_BMBT_REC_ADDR(mp, new_broot, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 memcpy(np, op, new_max * (uint)sizeof(xfs_bmbt_rec_t));
1876
1877 /*
1878 * Then copy the pointers.
1879 */
Christoph Hellwig60197e82008-10-30 17:11:19 +11001880 op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 ifp->if_broot_bytes);
Christoph Hellwig60197e82008-10-30 17:11:19 +11001882 np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, new_broot, 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 (int)new_size);
1884 memcpy(np, op, new_max * (uint)sizeof(xfs_dfsbno_t));
1885 }
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001886 kmem_free(ifp->if_broot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 ifp->if_broot = new_broot;
1888 ifp->if_broot_bytes = (int)new_size;
1889 ASSERT(ifp->if_broot_bytes <=
1890 XFS_IFORK_SIZE(ip, whichfork) + XFS_BROOT_SIZE_ADJ);
1891 return;
1892}
1893
1894
1895/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 * This is called when the amount of space needed for if_data
1897 * is increased or decreased. The change in size is indicated by
1898 * the number of bytes that need to be added or deleted in the
1899 * byte_diff parameter.
1900 *
1901 * If the amount of space needed has decreased below the size of the
1902 * inline buffer, then switch to using the inline buffer. Otherwise,
1903 * use kmem_realloc() or kmem_alloc() to adjust the size of the buffer
1904 * to what is needed.
1905 *
1906 * ip -- the inode whose if_data area is changing
1907 * byte_diff -- the change in the number of bytes, positive or negative,
1908 * requested for the if_data array.
1909 */
1910void
1911xfs_idata_realloc(
1912 xfs_inode_t *ip,
1913 int byte_diff,
1914 int whichfork)
1915{
1916 xfs_ifork_t *ifp;
1917 int new_size;
1918 int real_size;
1919
1920 if (byte_diff == 0) {
1921 return;
1922 }
1923
1924 ifp = XFS_IFORK_PTR(ip, whichfork);
1925 new_size = (int)ifp->if_bytes + byte_diff;
1926 ASSERT(new_size >= 0);
1927
1928 if (new_size == 0) {
1929 if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) {
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001930 kmem_free(ifp->if_u1.if_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 }
1932 ifp->if_u1.if_data = NULL;
1933 real_size = 0;
1934 } else if (new_size <= sizeof(ifp->if_u2.if_inline_data)) {
1935 /*
1936 * If the valid extents/data can fit in if_inline_ext/data,
1937 * copy them from the malloc'd vector and free it.
1938 */
1939 if (ifp->if_u1.if_data == NULL) {
1940 ifp->if_u1.if_data = ifp->if_u2.if_inline_data;
1941 } else if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) {
1942 ASSERT(ifp->if_real_bytes != 0);
1943 memcpy(ifp->if_u2.if_inline_data, ifp->if_u1.if_data,
1944 new_size);
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001945 kmem_free(ifp->if_u1.if_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 ifp->if_u1.if_data = ifp->if_u2.if_inline_data;
1947 }
1948 real_size = 0;
1949 } else {
1950 /*
1951 * Stuck with malloc/realloc.
1952 * For inline data, the underlying buffer must be
1953 * a multiple of 4 bytes in size so that it can be
1954 * logged and stay on word boundaries. We enforce
1955 * that here.
1956 */
1957 real_size = roundup(new_size, 4);
1958 if (ifp->if_u1.if_data == NULL) {
1959 ASSERT(ifp->if_real_bytes == 0);
Dave Chinner4a7eddd2010-07-20 17:53:59 +10001960 ifp->if_u1.if_data = kmem_alloc(real_size,
1961 KM_SLEEP | KM_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 } else if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) {
1963 /*
1964 * Only do the realloc if the underlying size
1965 * is really changing.
1966 */
1967 if (ifp->if_real_bytes != real_size) {
1968 ifp->if_u1.if_data =
1969 kmem_realloc(ifp->if_u1.if_data,
1970 real_size,
1971 ifp->if_real_bytes,
Dave Chinner4a7eddd2010-07-20 17:53:59 +10001972 KM_SLEEP | KM_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 }
1974 } else {
1975 ASSERT(ifp->if_real_bytes == 0);
Dave Chinner4a7eddd2010-07-20 17:53:59 +10001976 ifp->if_u1.if_data = kmem_alloc(real_size,
1977 KM_SLEEP | KM_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 memcpy(ifp->if_u1.if_data, ifp->if_u2.if_inline_data,
1979 ifp->if_bytes);
1980 }
1981 }
1982 ifp->if_real_bytes = real_size;
1983 ifp->if_bytes = new_size;
1984 ASSERT(ifp->if_bytes <= XFS_IFORK_SIZE(ip, whichfork));
1985}
1986
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987void
1988xfs_idestroy_fork(
1989 xfs_inode_t *ip,
1990 int whichfork)
1991{
1992 xfs_ifork_t *ifp;
1993
1994 ifp = XFS_IFORK_PTR(ip, whichfork);
1995 if (ifp->if_broot != NULL) {
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001996 kmem_free(ifp->if_broot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 ifp->if_broot = NULL;
1998 }
1999
2000 /*
2001 * If the format is local, then we can't have an extents
2002 * array so just look for an inline data array. If we're
2003 * not local then we may or may not have an extents list,
2004 * so check and free it up if we do.
2005 */
2006 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
2007 if ((ifp->if_u1.if_data != ifp->if_u2.if_inline_data) &&
2008 (ifp->if_u1.if_data != NULL)) {
2009 ASSERT(ifp->if_real_bytes != 0);
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10002010 kmem_free(ifp->if_u1.if_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 ifp->if_u1.if_data = NULL;
2012 ifp->if_real_bytes = 0;
2013 }
2014 } else if ((ifp->if_flags & XFS_IFEXTENTS) &&
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11002015 ((ifp->if_flags & XFS_IFEXTIREC) ||
2016 ((ifp->if_u1.if_extents != NULL) &&
2017 (ifp->if_u1.if_extents != ifp->if_u2.if_inline_ext)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 ASSERT(ifp->if_real_bytes != 0);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002019 xfs_iext_destroy(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 }
2021 ASSERT(ifp->if_u1.if_extents == NULL ||
2022 ifp->if_u1.if_extents == ifp->if_u2.if_inline_ext);
2023 ASSERT(ifp->if_real_bytes == 0);
2024 if (whichfork == XFS_ATTR_FORK) {
2025 kmem_zone_free(xfs_ifork_zone, ip->i_afp);
2026 ip->i_afp = NULL;
2027 }
2028}
2029
2030/*
Christoph Hellwig60ec67832010-02-17 19:43:56 +00002031 * This is called to unpin an inode. The caller must have the inode locked
2032 * in at least shared mode so that the buffer cannot be subsequently pinned
2033 * once someone is waiting for it to be unpinned.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 */
Christoph Hellwig60ec67832010-02-17 19:43:56 +00002035static void
Christoph Hellwigf392e632011-12-18 20:00:10 +00002036xfs_iunpin(
Christoph Hellwig60ec67832010-02-17 19:43:56 +00002037 struct xfs_inode *ip)
David Chinnera3f74ff2008-03-06 13:43:42 +11002038{
Christoph Hellwig579aa9c2008-04-22 17:34:00 +10002039 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
David Chinnera3f74ff2008-03-06 13:43:42 +11002040
Dave Chinner4aaf15d2010-03-08 11:24:07 +11002041 trace_xfs_inode_unpin_nowait(ip, _RET_IP_);
2042
David Chinnera3f74ff2008-03-06 13:43:42 +11002043 /* Give the log a push to start the unpinning I/O */
Christoph Hellwig60ec67832010-02-17 19:43:56 +00002044 xfs_log_force_lsn(ip->i_mount, ip->i_itemp->ili_last_lsn, 0);
Christoph Hellwiga14a3482010-01-19 09:56:46 +00002045
David Chinnera3f74ff2008-03-06 13:43:42 +11002046}
2047
Christoph Hellwigf392e632011-12-18 20:00:10 +00002048static void
2049__xfs_iunpin_wait(
2050 struct xfs_inode *ip)
2051{
2052 wait_queue_head_t *wq = bit_waitqueue(&ip->i_flags, __XFS_IPINNED_BIT);
2053 DEFINE_WAIT_BIT(wait, &ip->i_flags, __XFS_IPINNED_BIT);
2054
2055 xfs_iunpin(ip);
2056
2057 do {
2058 prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
2059 if (xfs_ipincount(ip))
2060 io_schedule();
2061 } while (xfs_ipincount(ip));
2062 finish_wait(wq, &wait.wait);
2063}
2064
Dave Chinner777df5a2010-02-06 12:37:26 +11002065void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066xfs_iunpin_wait(
Christoph Hellwig60ec67832010-02-17 19:43:56 +00002067 struct xfs_inode *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068{
Christoph Hellwigf392e632011-12-18 20:00:10 +00002069 if (xfs_ipincount(ip))
2070 __xfs_iunpin_wait(ip);
David Chinnera3f74ff2008-03-06 13:43:42 +11002071}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073/*
2074 * xfs_iextents_copy()
2075 *
2076 * This is called to copy the REAL extents (as opposed to the delayed
2077 * allocation extents) from the inode into the given buffer. It
2078 * returns the number of bytes copied into the buffer.
2079 *
2080 * If there are no delayed allocation extents, then we can just
2081 * memcpy() the extents into the buffer. Otherwise, we need to
2082 * examine each extent in turn and skip those which are delayed.
2083 */
2084int
2085xfs_iextents_copy(
2086 xfs_inode_t *ip,
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10002087 xfs_bmbt_rec_t *dp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 int whichfork)
2089{
2090 int copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 int i;
2092 xfs_ifork_t *ifp;
2093 int nrecs;
2094 xfs_fsblock_t start_block;
2095
2096 ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwig579aa9c2008-04-22 17:34:00 +10002097 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 ASSERT(ifp->if_bytes > 0);
2099
2100 nrecs = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
Eric Sandeen3a59c942007-07-11 11:09:47 +10002101 XFS_BMAP_TRACE_EXLIST(ip, nrecs, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 ASSERT(nrecs > 0);
2103
2104 /*
2105 * There are some delayed allocation extents in the
2106 * inode, so copy the extents one at a time and skip
2107 * the delayed ones. There must be at least one
2108 * non-delayed extent.
2109 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 copied = 0;
2111 for (i = 0; i < nrecs; i++) {
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10002112 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 start_block = xfs_bmbt_get_startblock(ep);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002114 if (isnullstartblock(start_block)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 /*
2116 * It's a delayed allocation extent, so skip it.
2117 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 continue;
2119 }
2120
2121 /* Translate to on disk format */
Christoph Hellwigcd8b0a92007-08-16 16:24:15 +10002122 put_unaligned(cpu_to_be64(ep->l0), &dp->l0);
2123 put_unaligned(cpu_to_be64(ep->l1), &dp->l1);
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10002124 dp++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 copied++;
2126 }
2127 ASSERT(copied != 0);
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10002128 xfs_validate_extents(ifp, copied, XFS_EXTFMT_INODE(ip));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129
2130 return (copied * (uint)sizeof(xfs_bmbt_rec_t));
2131}
2132
2133/*
2134 * Each of the following cases stores data into the same region
2135 * of the on-disk inode, so only one of them can be valid at
2136 * any given time. While it is possible to have conflicting formats
2137 * and log flags, e.g. having XFS_ILOG_?DATA set when the fork is
2138 * in EXTENTS format, this can only happen when the fork has
2139 * changed formats after being modified but before being flushed.
2140 * In these cases, the format always takes precedence, because the
2141 * format indicates the current state of the fork.
2142 */
2143/*ARGSUSED*/
David Chinnere4ac9672008-04-10 12:23:58 +10002144STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145xfs_iflush_fork(
2146 xfs_inode_t *ip,
2147 xfs_dinode_t *dip,
2148 xfs_inode_log_item_t *iip,
2149 int whichfork,
2150 xfs_buf_t *bp)
2151{
2152 char *cp;
2153 xfs_ifork_t *ifp;
2154 xfs_mount_t *mp;
2155#ifdef XFS_TRANS_DEBUG
2156 int first;
2157#endif
2158 static const short brootflag[2] =
2159 { XFS_ILOG_DBROOT, XFS_ILOG_ABROOT };
2160 static const short dataflag[2] =
2161 { XFS_ILOG_DDATA, XFS_ILOG_ADATA };
2162 static const short extflag[2] =
2163 { XFS_ILOG_DEXT, XFS_ILOG_AEXT };
2164
David Chinnere4ac9672008-04-10 12:23:58 +10002165 if (!iip)
2166 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 ifp = XFS_IFORK_PTR(ip, whichfork);
2168 /*
2169 * This can happen if we gave up in iformat in an error path,
2170 * for the attribute fork.
2171 */
David Chinnere4ac9672008-04-10 12:23:58 +10002172 if (!ifp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 ASSERT(whichfork == XFS_ATTR_FORK);
David Chinnere4ac9672008-04-10 12:23:58 +10002174 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 }
2176 cp = XFS_DFORK_PTR(dip, whichfork);
2177 mp = ip->i_mount;
2178 switch (XFS_IFORK_FORMAT(ip, whichfork)) {
2179 case XFS_DINODE_FMT_LOCAL:
2180 if ((iip->ili_format.ilf_fields & dataflag[whichfork]) &&
2181 (ifp->if_bytes > 0)) {
2182 ASSERT(ifp->if_u1.if_data != NULL);
2183 ASSERT(ifp->if_bytes <= XFS_IFORK_SIZE(ip, whichfork));
2184 memcpy(cp, ifp->if_u1.if_data, ifp->if_bytes);
2185 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 break;
2187
2188 case XFS_DINODE_FMT_EXTENTS:
2189 ASSERT((ifp->if_flags & XFS_IFEXTENTS) ||
2190 !(iip->ili_format.ilf_fields & extflag[whichfork]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 if ((iip->ili_format.ilf_fields & extflag[whichfork]) &&
2192 (ifp->if_bytes > 0)) {
Christoph Hellwigab1908a2011-05-11 15:04:10 +00002193 ASSERT(xfs_iext_get_ext(ifp, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) > 0);
2195 (void)xfs_iextents_copy(ip, (xfs_bmbt_rec_t *)cp,
2196 whichfork);
2197 }
2198 break;
2199
2200 case XFS_DINODE_FMT_BTREE:
2201 if ((iip->ili_format.ilf_fields & brootflag[whichfork]) &&
2202 (ifp->if_broot_bytes > 0)) {
2203 ASSERT(ifp->if_broot != NULL);
2204 ASSERT(ifp->if_broot_bytes <=
2205 (XFS_IFORK_SIZE(ip, whichfork) +
2206 XFS_BROOT_SIZE_ADJ));
Christoph Hellwig60197e82008-10-30 17:11:19 +11002207 xfs_bmbt_to_bmdr(mp, ifp->if_broot, ifp->if_broot_bytes,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 (xfs_bmdr_block_t *)cp,
2209 XFS_DFORK_SIZE(dip, mp, whichfork));
2210 }
2211 break;
2212
2213 case XFS_DINODE_FMT_DEV:
2214 if (iip->ili_format.ilf_fields & XFS_ILOG_DEV) {
2215 ASSERT(whichfork == XFS_DATA_FORK);
Christoph Hellwig81591fe2008-11-28 14:23:39 +11002216 xfs_dinode_put_rdev(dip, ip->i_df.if_u2.if_rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 }
2218 break;
2219
2220 case XFS_DINODE_FMT_UUID:
2221 if (iip->ili_format.ilf_fields & XFS_ILOG_UUID) {
2222 ASSERT(whichfork == XFS_DATA_FORK);
Christoph Hellwig81591fe2008-11-28 14:23:39 +11002223 memcpy(XFS_DFORK_DPTR(dip),
2224 &ip->i_df.if_u2.if_uuid,
2225 sizeof(uuid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 }
2227 break;
2228
2229 default:
2230 ASSERT(0);
2231 break;
2232 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233}
2234
David Chinnerbad55842008-03-06 13:43:49 +11002235STATIC int
2236xfs_iflush_cluster(
2237 xfs_inode_t *ip,
2238 xfs_buf_t *bp)
2239{
2240 xfs_mount_t *mp = ip->i_mount;
Dave Chinner5017e972010-01-11 11:47:40 +00002241 struct xfs_perag *pag;
David Chinnerbad55842008-03-06 13:43:49 +11002242 unsigned long first_index, mask;
David Chinnerc8f5f122008-05-20 11:30:15 +10002243 unsigned long inodes_per_cluster;
David Chinnerbad55842008-03-06 13:43:49 +11002244 int ilist_size;
2245 xfs_inode_t **ilist;
2246 xfs_inode_t *iq;
David Chinnerbad55842008-03-06 13:43:49 +11002247 int nr_found;
2248 int clcount = 0;
2249 int bufwasdelwri;
2250 int i;
2251
Dave Chinner5017e972010-01-11 11:47:40 +00002252 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
David Chinnerbad55842008-03-06 13:43:49 +11002253
David Chinnerc8f5f122008-05-20 11:30:15 +10002254 inodes_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_inodelog;
2255 ilist_size = inodes_per_cluster * sizeof(xfs_inode_t *);
David Chinner49383b02008-05-19 16:29:34 +10002256 ilist = kmem_alloc(ilist_size, KM_MAYFAIL|KM_NOFS);
David Chinnerbad55842008-03-06 13:43:49 +11002257 if (!ilist)
Dave Chinner44b56e02010-01-11 11:47:43 +00002258 goto out_put;
David Chinnerbad55842008-03-06 13:43:49 +11002259
2260 mask = ~(((XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_inodelog)) - 1);
2261 first_index = XFS_INO_TO_AGINO(mp, ip->i_ino) & mask;
Dave Chinner1a3e8f32010-12-17 17:29:43 +11002262 rcu_read_lock();
David Chinnerbad55842008-03-06 13:43:49 +11002263 /* really need a gang lookup range call here */
2264 nr_found = radix_tree_gang_lookup(&pag->pag_ici_root, (void**)ilist,
David Chinnerc8f5f122008-05-20 11:30:15 +10002265 first_index, inodes_per_cluster);
David Chinnerbad55842008-03-06 13:43:49 +11002266 if (nr_found == 0)
2267 goto out_free;
2268
2269 for (i = 0; i < nr_found; i++) {
2270 iq = ilist[i];
2271 if (iq == ip)
2272 continue;
Dave Chinner1a3e8f32010-12-17 17:29:43 +11002273
2274 /*
2275 * because this is an RCU protected lookup, we could find a
2276 * recently freed or even reallocated inode during the lookup.
2277 * We need to check under the i_flags_lock for a valid inode
2278 * here. Skip it if it is not valid or the wrong inode.
2279 */
2280 spin_lock(&ip->i_flags_lock);
2281 if (!ip->i_ino ||
2282 (XFS_INO_TO_AGINO(mp, iq->i_ino) & mask) != first_index) {
2283 spin_unlock(&ip->i_flags_lock);
2284 continue;
2285 }
2286 spin_unlock(&ip->i_flags_lock);
2287
David Chinnerbad55842008-03-06 13:43:49 +11002288 /*
2289 * Do an un-protected check to see if the inode is dirty and
2290 * is a candidate for flushing. These checks will be repeated
2291 * later after the appropriate locks are acquired.
2292 */
David Chinner33540402008-03-06 13:43:59 +11002293 if (xfs_inode_clean(iq) && xfs_ipincount(iq) == 0)
David Chinnerbad55842008-03-06 13:43:49 +11002294 continue;
David Chinnerbad55842008-03-06 13:43:49 +11002295
2296 /*
2297 * Try to get locks. If any are unavailable or it is pinned,
2298 * then this inode cannot be flushed and is skipped.
2299 */
2300
2301 if (!xfs_ilock_nowait(iq, XFS_ILOCK_SHARED))
2302 continue;
2303 if (!xfs_iflock_nowait(iq)) {
2304 xfs_iunlock(iq, XFS_ILOCK_SHARED);
2305 continue;
2306 }
2307 if (xfs_ipincount(iq)) {
2308 xfs_ifunlock(iq);
2309 xfs_iunlock(iq, XFS_ILOCK_SHARED);
2310 continue;
2311 }
2312
2313 /*
2314 * arriving here means that this inode can be flushed. First
2315 * re-check that it's dirty before flushing.
2316 */
David Chinner33540402008-03-06 13:43:59 +11002317 if (!xfs_inode_clean(iq)) {
2318 int error;
David Chinnerbad55842008-03-06 13:43:49 +11002319 error = xfs_iflush_int(iq, bp);
2320 if (error) {
2321 xfs_iunlock(iq, XFS_ILOCK_SHARED);
2322 goto cluster_corrupt_out;
2323 }
2324 clcount++;
2325 } else {
2326 xfs_ifunlock(iq);
2327 }
2328 xfs_iunlock(iq, XFS_ILOCK_SHARED);
2329 }
2330
2331 if (clcount) {
2332 XFS_STATS_INC(xs_icluster_flushcnt);
2333 XFS_STATS_ADD(xs_icluster_flushinode, clcount);
2334 }
2335
2336out_free:
Dave Chinner1a3e8f32010-12-17 17:29:43 +11002337 rcu_read_unlock();
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10002338 kmem_free(ilist);
Dave Chinner44b56e02010-01-11 11:47:43 +00002339out_put:
2340 xfs_perag_put(pag);
David Chinnerbad55842008-03-06 13:43:49 +11002341 return 0;
2342
2343
2344cluster_corrupt_out:
2345 /*
2346 * Corruption detected in the clustering loop. Invalidate the
2347 * inode buffer and shut down the filesystem.
2348 */
Dave Chinner1a3e8f32010-12-17 17:29:43 +11002349 rcu_read_unlock();
David Chinnerbad55842008-03-06 13:43:49 +11002350 /*
2351 * Clean up the buffer. If it was B_DELWRI, just release it --
2352 * brelse can handle it with no problems. If not, shut down the
2353 * filesystem before releasing the buffer.
2354 */
2355 bufwasdelwri = XFS_BUF_ISDELAYWRITE(bp);
2356 if (bufwasdelwri)
2357 xfs_buf_relse(bp);
2358
2359 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
2360
2361 if (!bufwasdelwri) {
2362 /*
2363 * Just like incore_relse: if we have b_iodone functions,
2364 * mark the buffer as an error and call them. Otherwise
2365 * mark it as stale and brelse.
2366 */
Christoph Hellwigcb669ca2011-07-13 13:43:49 +02002367 if (bp->b_iodone) {
David Chinnerbad55842008-03-06 13:43:49 +11002368 XFS_BUF_UNDONE(bp);
Christoph Hellwigc867cb62011-10-10 16:52:46 +00002369 xfs_buf_stale(bp);
Chandra Seetharaman5a52c2a582011-07-22 23:39:51 +00002370 xfs_buf_ioerror(bp, EIO);
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00002371 xfs_buf_ioend(bp, 0);
David Chinnerbad55842008-03-06 13:43:49 +11002372 } else {
Christoph Hellwigc867cb62011-10-10 16:52:46 +00002373 xfs_buf_stale(bp);
David Chinnerbad55842008-03-06 13:43:49 +11002374 xfs_buf_relse(bp);
2375 }
2376 }
2377
2378 /*
2379 * Unlocks the flush lock
2380 */
2381 xfs_iflush_abort(iq);
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10002382 kmem_free(ilist);
Dave Chinner44b56e02010-01-11 11:47:43 +00002383 xfs_perag_put(pag);
David Chinnerbad55842008-03-06 13:43:49 +11002384 return XFS_ERROR(EFSCORRUPTED);
2385}
2386
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387/*
2388 * xfs_iflush() will write a modified inode's changes out to the
2389 * inode's on disk home. The caller must have the inode lock held
David Chinnerc63942d2008-08-13 16:41:16 +10002390 * in at least shared mode and the inode flush completion must be
2391 * active as well. The inode lock will still be held upon return from
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 * the call and the caller is free to unlock it.
David Chinnerc63942d2008-08-13 16:41:16 +10002393 * The inode flush will be completed when the inode reaches the disk.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 * The flags indicate how the inode's buffer should be written out.
2395 */
2396int
2397xfs_iflush(
2398 xfs_inode_t *ip,
2399 uint flags)
2400{
2401 xfs_inode_log_item_t *iip;
2402 xfs_buf_t *bp;
2403 xfs_dinode_t *dip;
2404 xfs_mount_t *mp;
2405 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406
2407 XFS_STATS_INC(xs_iflush_count);
2408
Christoph Hellwig579aa9c2008-04-22 17:34:00 +10002409 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
Christoph Hellwig474fce02011-12-18 20:00:09 +00002410 ASSERT(xfs_isiflocked(ip));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE ||
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00002412 ip->i_d.di_nextents > XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413
2414 iip = ip->i_itemp;
2415 mp = ip->i_mount;
2416
2417 /*
David Chinnera3f74ff2008-03-06 13:43:42 +11002418 * We can't flush the inode until it is unpinned, so wait for it if we
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002419 * are allowed to block. We know no one new can pin it, because we are
David Chinnera3f74ff2008-03-06 13:43:42 +11002420 * holding the inode lock shared and you need to hold it exclusively to
2421 * pin the inode.
2422 *
2423 * If we are not allowed to block, force the log out asynchronously so
2424 * that when we come back the inode will be unpinned. If other inodes
2425 * in the same cluster are dirty, they will probably write the inode
2426 * out for us if they occur after the log force completes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 */
Dave Chinnerc8543632010-02-06 12:39:36 +11002428 if (!(flags & SYNC_WAIT) && xfs_ipincount(ip)) {
Christoph Hellwigf392e632011-12-18 20:00:10 +00002429 xfs_iunpin(ip);
David Chinnera3f74ff2008-03-06 13:43:42 +11002430 xfs_ifunlock(ip);
2431 return EAGAIN;
2432 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 xfs_iunpin_wait(ip);
2434
2435 /*
Dave Chinner4b6a4682010-01-11 11:45:21 +00002436 * For stale inodes we cannot rely on the backing buffer remaining
2437 * stale in cache for the remaining life of the stale inode and so
2438 * xfs_itobp() below may give us a buffer that no longer contains
2439 * inodes below. We have to check this after ensuring the inode is
2440 * unpinned so that it is safe to reclaim the stale inode after the
2441 * flush call.
2442 */
2443 if (xfs_iflags_test(ip, XFS_ISTALE)) {
2444 xfs_ifunlock(ip);
2445 return 0;
2446 }
2447
2448 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 * This may have been unpinned because the filesystem is shutting
2450 * down forcibly. If that's the case we must not write this inode
2451 * to disk, because the log record didn't make it to disk!
2452 */
2453 if (XFS_FORCED_SHUTDOWN(mp)) {
2454 ip->i_update_core = 0;
2455 if (iip)
2456 iip->ili_format.ilf_fields = 0;
2457 xfs_ifunlock(ip);
2458 return XFS_ERROR(EIO);
2459 }
2460
2461 /*
David Chinnera3f74ff2008-03-06 13:43:42 +11002462 * Get the buffer containing the on-disk inode.
2463 */
Christoph Hellwig76d8b272008-11-28 14:23:40 +11002464 error = xfs_itobp(mp, NULL, ip, &dip, &bp,
Dave Chinner1bfd8d02011-03-26 09:13:55 +11002465 (flags & SYNC_TRYLOCK) ? XBF_TRYLOCK : XBF_LOCK);
David Chinnera3f74ff2008-03-06 13:43:42 +11002466 if (error || !bp) {
2467 xfs_ifunlock(ip);
2468 return error;
2469 }
2470
2471 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 * First flush out the inode that xfs_iflush was called with.
2473 */
2474 error = xfs_iflush_int(ip, bp);
David Chinnerbad55842008-03-06 13:43:49 +11002475 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 goto corrupt_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477
2478 /*
David Chinnera3f74ff2008-03-06 13:43:42 +11002479 * If the buffer is pinned then push on the log now so we won't
2480 * get stuck waiting in the write for too long.
2481 */
Chandra Seetharaman811e64c2011-07-22 23:40:27 +00002482 if (xfs_buf_ispinned(bp))
Christoph Hellwiga14a3482010-01-19 09:56:46 +00002483 xfs_log_force(mp, 0);
David Chinnera3f74ff2008-03-06 13:43:42 +11002484
2485 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 * inode clustering:
2487 * see if other inodes can be gathered into this write
2488 */
David Chinnerbad55842008-03-06 13:43:49 +11002489 error = xfs_iflush_cluster(ip, bp);
2490 if (error)
2491 goto cluster_corrupt_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492
Dave Chinnerc8543632010-02-06 12:39:36 +11002493 if (flags & SYNC_WAIT)
Christoph Hellwigc2b006c2011-08-23 08:28:07 +00002494 error = xfs_bwrite(bp);
2495 else
Christoph Hellwig61551f12011-08-23 08:28:06 +00002496 xfs_buf_delwri_queue(bp);
Christoph Hellwigc2b006c2011-08-23 08:28:07 +00002497
2498 xfs_buf_relse(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 return error;
2500
2501corrupt_out:
2502 xfs_buf_relse(bp);
Nathan Scott7d04a332006-06-09 14:58:38 +10002503 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504cluster_corrupt_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 /*
2506 * Unlocks the flush lock
2507 */
David Chinnerbad55842008-03-06 13:43:49 +11002508 xfs_iflush_abort(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 return XFS_ERROR(EFSCORRUPTED);
2510}
2511
2512
2513STATIC int
2514xfs_iflush_int(
2515 xfs_inode_t *ip,
2516 xfs_buf_t *bp)
2517{
2518 xfs_inode_log_item_t *iip;
2519 xfs_dinode_t *dip;
2520 xfs_mount_t *mp;
2521#ifdef XFS_TRANS_DEBUG
2522 int first;
2523#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524
Christoph Hellwig579aa9c2008-04-22 17:34:00 +10002525 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
Christoph Hellwig474fce02011-12-18 20:00:09 +00002526 ASSERT(xfs_isiflocked(ip));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE ||
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00002528 ip->i_d.di_nextents > XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529
2530 iip = ip->i_itemp;
2531 mp = ip->i_mount;
2532
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 /* set *dip = inode's place in the buffer */
Christoph Hellwig92bfc6e2008-11-28 14:23:41 +11002534 dip = (xfs_dinode_t *)xfs_buf_offset(bp, ip->i_imap.im_boffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535
2536 /*
2537 * Clear i_update_core before copying out the data.
2538 * This is for coordination with our timestamp updates
2539 * that don't hold the inode lock. They will always
2540 * update the timestamps BEFORE setting i_update_core,
2541 * so if we clear i_update_core after they set it we
2542 * are guaranteed to see their updates to the timestamps.
2543 * I believe that this depends on strongly ordered memory
2544 * semantics, but we have that. We use the SYNCHRONIZE
2545 * macro to make sure that the compiler does not reorder
2546 * the i_update_core access below the data copy below.
2547 */
2548 ip->i_update_core = 0;
2549 SYNCHRONIZE();
2550
Christoph Hellwig42fe2b12006-01-11 15:35:17 +11002551 /*
Christoph Hellwigf9581b12009-10-06 20:29:26 +00002552 * Make sure to get the latest timestamps from the Linux inode.
Christoph Hellwig42fe2b12006-01-11 15:35:17 +11002553 */
Christoph Hellwigf9581b12009-10-06 20:29:26 +00002554 xfs_synchronize_times(ip);
Christoph Hellwig42fe2b12006-01-11 15:35:17 +11002555
Christoph Hellwig69ef9212011-07-08 14:36:05 +02002556 if (XFS_TEST_ERROR(dip->di_magic != cpu_to_be16(XFS_DINODE_MAGIC),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 mp, XFS_ERRTAG_IFLUSH_1, XFS_RANDOM_IFLUSH_1)) {
Dave Chinner6a19d932011-03-07 10:02:35 +11002558 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
2559 "%s: Bad inode %Lu magic number 0x%x, ptr 0x%p",
2560 __func__, ip->i_ino, be16_to_cpu(dip->di_magic), dip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 goto corrupt_out;
2562 }
2563 if (XFS_TEST_ERROR(ip->i_d.di_magic != XFS_DINODE_MAGIC,
2564 mp, XFS_ERRTAG_IFLUSH_2, XFS_RANDOM_IFLUSH_2)) {
Dave Chinner6a19d932011-03-07 10:02:35 +11002565 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
2566 "%s: Bad inode %Lu, ptr 0x%p, magic number 0x%x",
2567 __func__, ip->i_ino, ip, ip->i_d.di_magic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 goto corrupt_out;
2569 }
Al Viroabbede12011-07-26 02:31:30 -04002570 if (S_ISREG(ip->i_d.di_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 if (XFS_TEST_ERROR(
2572 (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS) &&
2573 (ip->i_d.di_format != XFS_DINODE_FMT_BTREE),
2574 mp, XFS_ERRTAG_IFLUSH_3, XFS_RANDOM_IFLUSH_3)) {
Dave Chinner6a19d932011-03-07 10:02:35 +11002575 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
2576 "%s: Bad regular inode %Lu, ptr 0x%p",
2577 __func__, ip->i_ino, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 goto corrupt_out;
2579 }
Al Viroabbede12011-07-26 02:31:30 -04002580 } else if (S_ISDIR(ip->i_d.di_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 if (XFS_TEST_ERROR(
2582 (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS) &&
2583 (ip->i_d.di_format != XFS_DINODE_FMT_BTREE) &&
2584 (ip->i_d.di_format != XFS_DINODE_FMT_LOCAL),
2585 mp, XFS_ERRTAG_IFLUSH_4, XFS_RANDOM_IFLUSH_4)) {
Dave Chinner6a19d932011-03-07 10:02:35 +11002586 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
2587 "%s: Bad directory inode %Lu, ptr 0x%p",
2588 __func__, ip->i_ino, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 goto corrupt_out;
2590 }
2591 }
2592 if (XFS_TEST_ERROR(ip->i_d.di_nextents + ip->i_d.di_anextents >
2593 ip->i_d.di_nblocks, mp, XFS_ERRTAG_IFLUSH_5,
2594 XFS_RANDOM_IFLUSH_5)) {
Dave Chinner6a19d932011-03-07 10:02:35 +11002595 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
2596 "%s: detected corrupt incore inode %Lu, "
2597 "total extents = %d, nblocks = %Ld, ptr 0x%p",
2598 __func__, ip->i_ino,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 ip->i_d.di_nextents + ip->i_d.di_anextents,
Dave Chinner6a19d932011-03-07 10:02:35 +11002600 ip->i_d.di_nblocks, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 goto corrupt_out;
2602 }
2603 if (XFS_TEST_ERROR(ip->i_d.di_forkoff > mp->m_sb.sb_inodesize,
2604 mp, XFS_ERRTAG_IFLUSH_6, XFS_RANDOM_IFLUSH_6)) {
Dave Chinner6a19d932011-03-07 10:02:35 +11002605 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
2606 "%s: bad inode %Lu, forkoff 0x%x, ptr 0x%p",
2607 __func__, ip->i_ino, ip->i_d.di_forkoff, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 goto corrupt_out;
2609 }
2610 /*
2611 * bump the flush iteration count, used to detect flushes which
2612 * postdate a log record during recovery.
2613 */
2614
2615 ip->i_d.di_flushiter++;
2616
2617 /*
2618 * Copy the dirty parts of the inode into the on-disk
2619 * inode. We always copy out the core of the inode,
2620 * because if the inode is dirty at all the core must
2621 * be.
2622 */
Christoph Hellwig81591fe2008-11-28 14:23:39 +11002623 xfs_dinode_to_disk(dip, &ip->i_d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624
2625 /* Wrap, we never let the log put out DI_MAX_FLUSH */
2626 if (ip->i_d.di_flushiter == DI_MAX_FLUSH)
2627 ip->i_d.di_flushiter = 0;
2628
2629 /*
2630 * If this is really an old format inode and the superblock version
2631 * has not been updated to support only new format inodes, then
2632 * convert back to the old inode format. If the superblock version
2633 * has been updated, then make the conversion permanent.
2634 */
Christoph Hellwig51ce16d2008-11-28 14:23:39 +11002635 ASSERT(ip->i_d.di_version == 1 || xfs_sb_version_hasnlink(&mp->m_sb));
2636 if (ip->i_d.di_version == 1) {
Eric Sandeen62118702008-03-06 13:44:28 +11002637 if (!xfs_sb_version_hasnlink(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 /*
2639 * Convert it back.
2640 */
2641 ASSERT(ip->i_d.di_nlink <= XFS_MAXLINK_1);
Christoph Hellwig81591fe2008-11-28 14:23:39 +11002642 dip->di_onlink = cpu_to_be16(ip->i_d.di_nlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 } else {
2644 /*
2645 * The superblock version has already been bumped,
2646 * so just make the conversion to the new inode
2647 * format permanent.
2648 */
Christoph Hellwig51ce16d2008-11-28 14:23:39 +11002649 ip->i_d.di_version = 2;
2650 dip->di_version = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 ip->i_d.di_onlink = 0;
Christoph Hellwig81591fe2008-11-28 14:23:39 +11002652 dip->di_onlink = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 memset(&(ip->i_d.di_pad[0]), 0, sizeof(ip->i_d.di_pad));
Christoph Hellwig81591fe2008-11-28 14:23:39 +11002654 memset(&(dip->di_pad[0]), 0,
2655 sizeof(dip->di_pad));
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +00002656 ASSERT(xfs_get_projid(ip) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 }
2658 }
2659
David Chinnere4ac9672008-04-10 12:23:58 +10002660 xfs_iflush_fork(ip, dip, iip, XFS_DATA_FORK, bp);
2661 if (XFS_IFORK_Q(ip))
2662 xfs_iflush_fork(ip, dip, iip, XFS_ATTR_FORK, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 xfs_inobp_check(mp, bp);
2664
2665 /*
2666 * We've recorded everything logged in the inode, so we'd
2667 * like to clear the ilf_fields bits so we don't log and
2668 * flush things unnecessarily. However, we can't stop
2669 * logging all this information until the data we've copied
2670 * into the disk buffer is written to disk. If we did we might
2671 * overwrite the copy of the inode in the log with all the
2672 * data after re-logging only part of it, and in the face of
2673 * a crash we wouldn't have all the data we need to recover.
2674 *
2675 * What we do is move the bits to the ili_last_fields field.
2676 * When logging the inode, these bits are moved back to the
2677 * ilf_fields field. In the xfs_iflush_done() routine we
2678 * clear ili_last_fields, since we know that the information
2679 * those bits represent is permanently on disk. As long as
2680 * the flush completes before the inode is logged again, then
2681 * both ilf_fields and ili_last_fields will be cleared.
2682 *
2683 * We can play with the ilf_fields bits here, because the inode
2684 * lock must be held exclusively in order to set bits there
2685 * and the flush lock protects the ili_last_fields bits.
2686 * Set ili_logged so the flush done
2687 * routine can tell whether or not to look in the AIL.
2688 * Also, store the current LSN of the inode so that we can tell
2689 * whether the item has moved in the AIL from xfs_iflush_done().
2690 * In order to read the lsn we need the AIL lock, because
2691 * it is a 64 bit value that cannot be read atomically.
2692 */
2693 if (iip != NULL && iip->ili_format.ilf_fields != 0) {
2694 iip->ili_last_fields = iip->ili_format.ilf_fields;
2695 iip->ili_format.ilf_fields = 0;
2696 iip->ili_logged = 1;
2697
David Chinner7b2e2a32008-10-30 17:39:12 +11002698 xfs_trans_ail_copy_lsn(mp->m_ail, &iip->ili_flush_lsn,
2699 &iip->ili_item.li_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700
2701 /*
2702 * Attach the function xfs_iflush_done to the inode's
2703 * buffer. This will remove the inode from the AIL
2704 * and unlock the inode's flush lock when the inode is
2705 * completely written to disk.
2706 */
Christoph Hellwigca30b2a2010-06-23 18:11:15 +10002707 xfs_buf_attach_iodone(bp, xfs_iflush_done, &iip->ili_item);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708
Christoph Hellwigadadbee2011-07-13 13:43:49 +02002709 ASSERT(bp->b_fspriv != NULL);
Christoph Hellwigcb669ca2011-07-13 13:43:49 +02002710 ASSERT(bp->b_iodone != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 } else {
2712 /*
2713 * We're flushing an inode which is not in the AIL and has
2714 * not been logged but has i_update_core set. For this
2715 * case we can use a B_DELWRI flush and immediately drop
2716 * the inode flush lock because we can avoid the whole
2717 * AIL state thing. It's OK to drop the flush lock now,
2718 * because we've already locked the buffer and to do anything
2719 * you really need both.
2720 */
2721 if (iip != NULL) {
2722 ASSERT(iip->ili_logged == 0);
2723 ASSERT(iip->ili_last_fields == 0);
2724 ASSERT((iip->ili_item.li_flags & XFS_LI_IN_AIL) == 0);
2725 }
2726 xfs_ifunlock(ip);
2727 }
2728
2729 return 0;
2730
2731corrupt_out:
2732 return XFS_ERROR(EFSCORRUPTED);
2733}
2734
Christoph Hellwig4dd2cb42011-11-29 12:06:14 -06002735void
2736xfs_promote_inode(
2737 struct xfs_inode *ip)
2738{
2739 struct xfs_buf *bp;
2740
2741 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
2742
2743 bp = xfs_incore(ip->i_mount->m_ddev_targp, ip->i_imap.im_blkno,
2744 ip->i_imap.im_len, XBF_TRYLOCK);
2745 if (!bp)
2746 return;
2747
2748 if (XFS_BUF_ISDELAYWRITE(bp)) {
2749 xfs_buf_delwri_promote(bp);
2750 wake_up_process(ip->i_mount->m_ddev_targp->bt_task);
2751 }
2752
2753 xfs_buf_relse(bp);
2754}
2755
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002756/*
2757 * Return a pointer to the extent record at file index idx.
2758 */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10002759xfs_bmbt_rec_host_t *
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002760xfs_iext_get_ext(
2761 xfs_ifork_t *ifp, /* inode fork pointer */
2762 xfs_extnum_t idx) /* index of target extent */
2763{
2764 ASSERT(idx >= 0);
Christoph Hellwig87bef182011-05-11 15:04:11 +00002765 ASSERT(idx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t));
2766
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11002767 if ((ifp->if_flags & XFS_IFEXTIREC) && (idx == 0)) {
2768 return ifp->if_u1.if_ext_irec->er_extbuf;
2769 } else if (ifp->if_flags & XFS_IFEXTIREC) {
2770 xfs_ext_irec_t *erp; /* irec pointer */
2771 int erp_idx = 0; /* irec index */
2772 xfs_extnum_t page_idx = idx; /* ext index in target list */
2773
2774 erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 0);
2775 return &erp->er_extbuf[page_idx];
2776 } else if (ifp->if_bytes) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002777 return &ifp->if_u1.if_extents[idx];
2778 } else {
2779 return NULL;
2780 }
2781}
2782
2783/*
2784 * Insert new item(s) into the extent records for incore inode
2785 * fork 'ifp'. 'count' new items are inserted at index 'idx'.
2786 */
2787void
2788xfs_iext_insert(
Christoph Hellwig6ef35542009-11-25 00:00:21 +00002789 xfs_inode_t *ip, /* incore inode pointer */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002790 xfs_extnum_t idx, /* starting index of new items */
2791 xfs_extnum_t count, /* number of inserted items */
Christoph Hellwig6ef35542009-11-25 00:00:21 +00002792 xfs_bmbt_irec_t *new, /* items to insert */
2793 int state) /* type of extent conversion */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002794{
Christoph Hellwig6ef35542009-11-25 00:00:21 +00002795 xfs_ifork_t *ifp = (state & BMAP_ATTRFORK) ? ip->i_afp : &ip->i_df;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002796 xfs_extnum_t i; /* extent record index */
2797
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002798 trace_xfs_iext_insert(ip, idx, new, state, _RET_IP_);
2799
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002800 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
2801 xfs_iext_add(ifp, idx, count);
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10002802 for (i = idx; i < idx + count; i++, new++)
2803 xfs_bmbt_set_all(xfs_iext_get_ext(ifp, i), new);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002804}
2805
2806/*
2807 * This is called when the amount of space required for incore file
2808 * extents needs to be increased. The ext_diff parameter stores the
2809 * number of new extents being added and the idx parameter contains
2810 * the extent index where the new extents will be added. If the new
2811 * extents are being appended, then we just need to (re)allocate and
2812 * initialize the space. Otherwise, if the new extents are being
2813 * inserted into the middle of the existing entries, a bit more work
2814 * is required to make room for the new extents to be inserted. The
2815 * caller is responsible for filling in the new extent entries upon
2816 * return.
2817 */
2818void
2819xfs_iext_add(
2820 xfs_ifork_t *ifp, /* inode fork pointer */
2821 xfs_extnum_t idx, /* index to begin adding exts */
Nathan Scottc41564b2006-03-29 08:55:14 +10002822 int ext_diff) /* number of extents to add */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002823{
2824 int byte_diff; /* new bytes being added */
2825 int new_size; /* size of extents after adding */
2826 xfs_extnum_t nextents; /* number of extents in file */
2827
2828 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
2829 ASSERT((idx >= 0) && (idx <= nextents));
2830 byte_diff = ext_diff * sizeof(xfs_bmbt_rec_t);
2831 new_size = ifp->if_bytes + byte_diff;
2832 /*
2833 * If the new number of extents (nextents + ext_diff)
2834 * fits inside the inode, then continue to use the inline
2835 * extent buffer.
2836 */
2837 if (nextents + ext_diff <= XFS_INLINE_EXTS) {
2838 if (idx < nextents) {
2839 memmove(&ifp->if_u2.if_inline_ext[idx + ext_diff],
2840 &ifp->if_u2.if_inline_ext[idx],
2841 (nextents - idx) * sizeof(xfs_bmbt_rec_t));
2842 memset(&ifp->if_u2.if_inline_ext[idx], 0, byte_diff);
2843 }
2844 ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext;
2845 ifp->if_real_bytes = 0;
2846 }
2847 /*
2848 * Otherwise use a linear (direct) extent list.
2849 * If the extents are currently inside the inode,
2850 * xfs_iext_realloc_direct will switch us from
2851 * inline to direct extent allocation mode.
2852 */
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11002853 else if (nextents + ext_diff <= XFS_LINEAR_EXTS) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002854 xfs_iext_realloc_direct(ifp, new_size);
2855 if (idx < nextents) {
2856 memmove(&ifp->if_u1.if_extents[idx + ext_diff],
2857 &ifp->if_u1.if_extents[idx],
2858 (nextents - idx) * sizeof(xfs_bmbt_rec_t));
2859 memset(&ifp->if_u1.if_extents[idx], 0, byte_diff);
2860 }
2861 }
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11002862 /* Indirection array */
2863 else {
2864 xfs_ext_irec_t *erp;
2865 int erp_idx = 0;
2866 int page_idx = idx;
2867
2868 ASSERT(nextents + ext_diff > XFS_LINEAR_EXTS);
2869 if (ifp->if_flags & XFS_IFEXTIREC) {
2870 erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 1);
2871 } else {
2872 xfs_iext_irec_init(ifp);
2873 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
2874 erp = ifp->if_u1.if_ext_irec;
2875 }
2876 /* Extents fit in target extent page */
2877 if (erp && erp->er_extcount + ext_diff <= XFS_LINEAR_EXTS) {
2878 if (page_idx < erp->er_extcount) {
2879 memmove(&erp->er_extbuf[page_idx + ext_diff],
2880 &erp->er_extbuf[page_idx],
2881 (erp->er_extcount - page_idx) *
2882 sizeof(xfs_bmbt_rec_t));
2883 memset(&erp->er_extbuf[page_idx], 0, byte_diff);
2884 }
2885 erp->er_extcount += ext_diff;
2886 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff);
2887 }
2888 /* Insert a new extent page */
2889 else if (erp) {
2890 xfs_iext_add_indirect_multi(ifp,
2891 erp_idx, page_idx, ext_diff);
2892 }
2893 /*
2894 * If extent(s) are being appended to the last page in
2895 * the indirection array and the new extent(s) don't fit
2896 * in the page, then erp is NULL and erp_idx is set to
2897 * the next index needed in the indirection array.
2898 */
2899 else {
2900 int count = ext_diff;
2901
2902 while (count) {
2903 erp = xfs_iext_irec_new(ifp, erp_idx);
2904 erp->er_extcount = count;
2905 count -= MIN(count, (int)XFS_LINEAR_EXTS);
2906 if (count) {
2907 erp_idx++;
2908 }
2909 }
2910 }
2911 }
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002912 ifp->if_bytes = new_size;
2913}
2914
2915/*
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11002916 * This is called when incore extents are being added to the indirection
2917 * array and the new extents do not fit in the target extent list. The
2918 * erp_idx parameter contains the irec index for the target extent list
2919 * in the indirection array, and the idx parameter contains the extent
2920 * index within the list. The number of extents being added is stored
2921 * in the count parameter.
2922 *
2923 * |-------| |-------|
2924 * | | | | idx - number of extents before idx
2925 * | idx | | count |
2926 * | | | | count - number of extents being inserted at idx
2927 * |-------| |-------|
2928 * | count | | nex2 | nex2 - number of extents after idx + count
2929 * |-------| |-------|
2930 */
2931void
2932xfs_iext_add_indirect_multi(
2933 xfs_ifork_t *ifp, /* inode fork pointer */
2934 int erp_idx, /* target extent irec index */
2935 xfs_extnum_t idx, /* index within target list */
2936 int count) /* new extents being added */
2937{
2938 int byte_diff; /* new bytes being added */
2939 xfs_ext_irec_t *erp; /* pointer to irec entry */
2940 xfs_extnum_t ext_diff; /* number of extents to add */
2941 xfs_extnum_t ext_cnt; /* new extents still needed */
2942 xfs_extnum_t nex2; /* extents after idx + count */
2943 xfs_bmbt_rec_t *nex2_ep = NULL; /* temp list for nex2 extents */
2944 int nlists; /* number of irec's (lists) */
2945
2946 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
2947 erp = &ifp->if_u1.if_ext_irec[erp_idx];
2948 nex2 = erp->er_extcount - idx;
2949 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
2950
2951 /*
2952 * Save second part of target extent list
2953 * (all extents past */
2954 if (nex2) {
2955 byte_diff = nex2 * sizeof(xfs_bmbt_rec_t);
David Chinner67850732008-08-13 16:02:51 +10002956 nex2_ep = (xfs_bmbt_rec_t *) kmem_alloc(byte_diff, KM_NOFS);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11002957 memmove(nex2_ep, &erp->er_extbuf[idx], byte_diff);
2958 erp->er_extcount -= nex2;
2959 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, -nex2);
2960 memset(&erp->er_extbuf[idx], 0, byte_diff);
2961 }
2962
2963 /*
2964 * Add the new extents to the end of the target
2965 * list, then allocate new irec record(s) and
2966 * extent buffer(s) as needed to store the rest
2967 * of the new extents.
2968 */
2969 ext_cnt = count;
2970 ext_diff = MIN(ext_cnt, (int)XFS_LINEAR_EXTS - erp->er_extcount);
2971 if (ext_diff) {
2972 erp->er_extcount += ext_diff;
2973 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff);
2974 ext_cnt -= ext_diff;
2975 }
2976 while (ext_cnt) {
2977 erp_idx++;
2978 erp = xfs_iext_irec_new(ifp, erp_idx);
2979 ext_diff = MIN(ext_cnt, (int)XFS_LINEAR_EXTS);
2980 erp->er_extcount = ext_diff;
2981 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff);
2982 ext_cnt -= ext_diff;
2983 }
2984
2985 /* Add nex2 extents back to indirection array */
2986 if (nex2) {
2987 xfs_extnum_t ext_avail;
2988 int i;
2989
2990 byte_diff = nex2 * sizeof(xfs_bmbt_rec_t);
2991 ext_avail = XFS_LINEAR_EXTS - erp->er_extcount;
2992 i = 0;
2993 /*
2994 * If nex2 extents fit in the current page, append
2995 * nex2_ep after the new extents.
2996 */
2997 if (nex2 <= ext_avail) {
2998 i = erp->er_extcount;
2999 }
3000 /*
3001 * Otherwise, check if space is available in the
3002 * next page.
3003 */
3004 else if ((erp_idx < nlists - 1) &&
3005 (nex2 <= (ext_avail = XFS_LINEAR_EXTS -
3006 ifp->if_u1.if_ext_irec[erp_idx+1].er_extcount))) {
3007 erp_idx++;
3008 erp++;
3009 /* Create a hole for nex2 extents */
3010 memmove(&erp->er_extbuf[nex2], erp->er_extbuf,
3011 erp->er_extcount * sizeof(xfs_bmbt_rec_t));
3012 }
3013 /*
3014 * Final choice, create a new extent page for
3015 * nex2 extents.
3016 */
3017 else {
3018 erp_idx++;
3019 erp = xfs_iext_irec_new(ifp, erp_idx);
3020 }
3021 memmove(&erp->er_extbuf[i], nex2_ep, byte_diff);
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10003022 kmem_free(nex2_ep);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003023 erp->er_extcount += nex2;
3024 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, nex2);
3025 }
3026}
3027
3028/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003029 * This is called when the amount of space required for incore file
3030 * extents needs to be decreased. The ext_diff parameter stores the
3031 * number of extents to be removed and the idx parameter contains
3032 * the extent index where the extents will be removed from.
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003033 *
3034 * If the amount of space needed has decreased below the linear
3035 * limit, XFS_IEXT_BUFSZ, then switch to using the contiguous
3036 * extent array. Otherwise, use kmem_realloc() to adjust the
3037 * size to what is needed.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003038 */
3039void
3040xfs_iext_remove(
Christoph Hellwig6ef35542009-11-25 00:00:21 +00003041 xfs_inode_t *ip, /* incore inode pointer */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003042 xfs_extnum_t idx, /* index to begin removing exts */
Christoph Hellwig6ef35542009-11-25 00:00:21 +00003043 int ext_diff, /* number of extents to remove */
3044 int state) /* type of extent conversion */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003045{
Christoph Hellwig6ef35542009-11-25 00:00:21 +00003046 xfs_ifork_t *ifp = (state & BMAP_ATTRFORK) ? ip->i_afp : &ip->i_df;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003047 xfs_extnum_t nextents; /* number of extents in file */
3048 int new_size; /* size of extents after removal */
3049
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00003050 trace_xfs_iext_remove(ip, idx, state, _RET_IP_);
3051
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003052 ASSERT(ext_diff > 0);
3053 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3054 new_size = (nextents - ext_diff) * sizeof(xfs_bmbt_rec_t);
3055
3056 if (new_size == 0) {
3057 xfs_iext_destroy(ifp);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003058 } else if (ifp->if_flags & XFS_IFEXTIREC) {
3059 xfs_iext_remove_indirect(ifp, idx, ext_diff);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003060 } else if (ifp->if_real_bytes) {
3061 xfs_iext_remove_direct(ifp, idx, ext_diff);
3062 } else {
3063 xfs_iext_remove_inline(ifp, idx, ext_diff);
3064 }
3065 ifp->if_bytes = new_size;
3066}
3067
3068/*
3069 * This removes ext_diff extents from the inline buffer, beginning
3070 * at extent index idx.
3071 */
3072void
3073xfs_iext_remove_inline(
3074 xfs_ifork_t *ifp, /* inode fork pointer */
3075 xfs_extnum_t idx, /* index to begin removing exts */
3076 int ext_diff) /* number of extents to remove */
3077{
3078 int nextents; /* number of extents in file */
3079
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003080 ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003081 ASSERT(idx < XFS_INLINE_EXTS);
3082 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3083 ASSERT(((nextents - ext_diff) > 0) &&
3084 (nextents - ext_diff) < XFS_INLINE_EXTS);
3085
3086 if (idx + ext_diff < nextents) {
3087 memmove(&ifp->if_u2.if_inline_ext[idx],
3088 &ifp->if_u2.if_inline_ext[idx + ext_diff],
3089 (nextents - (idx + ext_diff)) *
3090 sizeof(xfs_bmbt_rec_t));
3091 memset(&ifp->if_u2.if_inline_ext[nextents - ext_diff],
3092 0, ext_diff * sizeof(xfs_bmbt_rec_t));
3093 } else {
3094 memset(&ifp->if_u2.if_inline_ext[idx], 0,
3095 ext_diff * sizeof(xfs_bmbt_rec_t));
3096 }
3097}
3098
3099/*
3100 * This removes ext_diff extents from a linear (direct) extent list,
3101 * beginning at extent index idx. If the extents are being removed
3102 * from the end of the list (ie. truncate) then we just need to re-
3103 * allocate the list to remove the extra space. Otherwise, if the
3104 * extents are being removed from the middle of the existing extent
3105 * entries, then we first need to move the extent records beginning
3106 * at idx + ext_diff up in the list to overwrite the records being
3107 * removed, then remove the extra space via kmem_realloc.
3108 */
3109void
3110xfs_iext_remove_direct(
3111 xfs_ifork_t *ifp, /* inode fork pointer */
3112 xfs_extnum_t idx, /* index to begin removing exts */
3113 int ext_diff) /* number of extents to remove */
3114{
3115 xfs_extnum_t nextents; /* number of extents in file */
3116 int new_size; /* size of extents after removal */
3117
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003118 ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003119 new_size = ifp->if_bytes -
3120 (ext_diff * sizeof(xfs_bmbt_rec_t));
3121 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3122
3123 if (new_size == 0) {
3124 xfs_iext_destroy(ifp);
3125 return;
3126 }
3127 /* Move extents up in the list (if needed) */
3128 if (idx + ext_diff < nextents) {
3129 memmove(&ifp->if_u1.if_extents[idx],
3130 &ifp->if_u1.if_extents[idx + ext_diff],
3131 (nextents - (idx + ext_diff)) *
3132 sizeof(xfs_bmbt_rec_t));
3133 }
3134 memset(&ifp->if_u1.if_extents[nextents - ext_diff],
3135 0, ext_diff * sizeof(xfs_bmbt_rec_t));
3136 /*
3137 * Reallocate the direct extent list. If the extents
3138 * will fit inside the inode then xfs_iext_realloc_direct
3139 * will switch from direct to inline extent allocation
3140 * mode for us.
3141 */
3142 xfs_iext_realloc_direct(ifp, new_size);
3143 ifp->if_bytes = new_size;
3144}
3145
3146/*
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003147 * This is called when incore extents are being removed from the
3148 * indirection array and the extents being removed span multiple extent
3149 * buffers. The idx parameter contains the file extent index where we
3150 * want to begin removing extents, and the count parameter contains
3151 * how many extents need to be removed.
3152 *
3153 * |-------| |-------|
3154 * | nex1 | | | nex1 - number of extents before idx
3155 * |-------| | count |
3156 * | | | | count - number of extents being removed at idx
3157 * | count | |-------|
3158 * | | | nex2 | nex2 - number of extents after idx + count
3159 * |-------| |-------|
3160 */
3161void
3162xfs_iext_remove_indirect(
3163 xfs_ifork_t *ifp, /* inode fork pointer */
3164 xfs_extnum_t idx, /* index to begin removing extents */
3165 int count) /* number of extents to remove */
3166{
3167 xfs_ext_irec_t *erp; /* indirection array pointer */
3168 int erp_idx = 0; /* indirection array index */
3169 xfs_extnum_t ext_cnt; /* extents left to remove */
3170 xfs_extnum_t ext_diff; /* extents to remove in current list */
3171 xfs_extnum_t nex1; /* number of extents before idx */
3172 xfs_extnum_t nex2; /* extents after idx + count */
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003173 int page_idx = idx; /* index in target extent list */
3174
3175 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3176 erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 0);
3177 ASSERT(erp != NULL);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003178 nex1 = page_idx;
3179 ext_cnt = count;
3180 while (ext_cnt) {
3181 nex2 = MAX((erp->er_extcount - (nex1 + ext_cnt)), 0);
3182 ext_diff = MIN(ext_cnt, (erp->er_extcount - nex1));
3183 /*
3184 * Check for deletion of entire list;
3185 * xfs_iext_irec_remove() updates extent offsets.
3186 */
3187 if (ext_diff == erp->er_extcount) {
3188 xfs_iext_irec_remove(ifp, erp_idx);
3189 ext_cnt -= ext_diff;
3190 nex1 = 0;
3191 if (ext_cnt) {
3192 ASSERT(erp_idx < ifp->if_real_bytes /
3193 XFS_IEXT_BUFSZ);
3194 erp = &ifp->if_u1.if_ext_irec[erp_idx];
3195 nex1 = 0;
3196 continue;
3197 } else {
3198 break;
3199 }
3200 }
3201 /* Move extents up (if needed) */
3202 if (nex2) {
3203 memmove(&erp->er_extbuf[nex1],
3204 &erp->er_extbuf[nex1 + ext_diff],
3205 nex2 * sizeof(xfs_bmbt_rec_t));
3206 }
3207 /* Zero out rest of page */
3208 memset(&erp->er_extbuf[nex1 + nex2], 0, (XFS_IEXT_BUFSZ -
3209 ((nex1 + nex2) * sizeof(xfs_bmbt_rec_t))));
3210 /* Update remaining counters */
3211 erp->er_extcount -= ext_diff;
3212 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, -ext_diff);
3213 ext_cnt -= ext_diff;
3214 nex1 = 0;
3215 erp_idx++;
3216 erp++;
3217 }
3218 ifp->if_bytes -= count * sizeof(xfs_bmbt_rec_t);
3219 xfs_iext_irec_compact(ifp);
3220}
3221
3222/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003223 * Create, destroy, or resize a linear (direct) block of extents.
3224 */
3225void
3226xfs_iext_realloc_direct(
3227 xfs_ifork_t *ifp, /* inode fork pointer */
3228 int new_size) /* new size of extents */
3229{
3230 int rnew_size; /* real new size of extents */
3231
3232 rnew_size = new_size;
3233
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003234 ASSERT(!(ifp->if_flags & XFS_IFEXTIREC) ||
3235 ((new_size >= 0) && (new_size <= XFS_IEXT_BUFSZ) &&
3236 (new_size != ifp->if_real_bytes)));
3237
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003238 /* Free extent records */
3239 if (new_size == 0) {
3240 xfs_iext_destroy(ifp);
3241 }
3242 /* Resize direct extent list and zero any new bytes */
3243 else if (ifp->if_real_bytes) {
3244 /* Check if extents will fit inside the inode */
3245 if (new_size <= XFS_INLINE_EXTS * sizeof(xfs_bmbt_rec_t)) {
3246 xfs_iext_direct_to_inline(ifp, new_size /
3247 (uint)sizeof(xfs_bmbt_rec_t));
3248 ifp->if_bytes = new_size;
3249 return;
3250 }
Vignesh Babu16a087d2007-06-28 16:46:37 +10003251 if (!is_power_of_2(new_size)){
Robert P. J. Day40ebd812007-11-23 16:30:51 +11003252 rnew_size = roundup_pow_of_two(new_size);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003253 }
3254 if (rnew_size != ifp->if_real_bytes) {
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003255 ifp->if_u1.if_extents =
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003256 kmem_realloc(ifp->if_u1.if_extents,
3257 rnew_size,
David Chinner67850732008-08-13 16:02:51 +10003258 ifp->if_real_bytes, KM_NOFS);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003259 }
3260 if (rnew_size > ifp->if_real_bytes) {
3261 memset(&ifp->if_u1.if_extents[ifp->if_bytes /
3262 (uint)sizeof(xfs_bmbt_rec_t)], 0,
3263 rnew_size - ifp->if_real_bytes);
3264 }
3265 }
3266 /*
3267 * Switch from the inline extent buffer to a direct
3268 * extent list. Be sure to include the inline extent
3269 * bytes in new_size.
3270 */
3271 else {
3272 new_size += ifp->if_bytes;
Vignesh Babu16a087d2007-06-28 16:46:37 +10003273 if (!is_power_of_2(new_size)) {
Robert P. J. Day40ebd812007-11-23 16:30:51 +11003274 rnew_size = roundup_pow_of_two(new_size);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003275 }
3276 xfs_iext_inline_to_direct(ifp, rnew_size);
3277 }
3278 ifp->if_real_bytes = rnew_size;
3279 ifp->if_bytes = new_size;
3280}
3281
3282/*
3283 * Switch from linear (direct) extent records to inline buffer.
3284 */
3285void
3286xfs_iext_direct_to_inline(
3287 xfs_ifork_t *ifp, /* inode fork pointer */
3288 xfs_extnum_t nextents) /* number of extents in file */
3289{
3290 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
3291 ASSERT(nextents <= XFS_INLINE_EXTS);
3292 /*
3293 * The inline buffer was zeroed when we switched
3294 * from inline to direct extent allocation mode,
3295 * so we don't need to clear it here.
3296 */
3297 memcpy(ifp->if_u2.if_inline_ext, ifp->if_u1.if_extents,
3298 nextents * sizeof(xfs_bmbt_rec_t));
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10003299 kmem_free(ifp->if_u1.if_extents);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003300 ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext;
3301 ifp->if_real_bytes = 0;
3302}
3303
3304/*
3305 * Switch from inline buffer to linear (direct) extent records.
3306 * new_size should already be rounded up to the next power of 2
3307 * by the caller (when appropriate), so use new_size as it is.
3308 * However, since new_size may be rounded up, we can't update
3309 * if_bytes here. It is the caller's responsibility to update
3310 * if_bytes upon return.
3311 */
3312void
3313xfs_iext_inline_to_direct(
3314 xfs_ifork_t *ifp, /* inode fork pointer */
3315 int new_size) /* number of extents in file */
3316{
David Chinner67850732008-08-13 16:02:51 +10003317 ifp->if_u1.if_extents = kmem_alloc(new_size, KM_NOFS);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003318 memset(ifp->if_u1.if_extents, 0, new_size);
3319 if (ifp->if_bytes) {
3320 memcpy(ifp->if_u1.if_extents, ifp->if_u2.if_inline_ext,
3321 ifp->if_bytes);
3322 memset(ifp->if_u2.if_inline_ext, 0, XFS_INLINE_EXTS *
3323 sizeof(xfs_bmbt_rec_t));
3324 }
3325 ifp->if_real_bytes = new_size;
3326}
3327
3328/*
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003329 * Resize an extent indirection array to new_size bytes.
3330 */
Eric Sandeend96f8f82009-07-02 00:09:33 -05003331STATIC void
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003332xfs_iext_realloc_indirect(
3333 xfs_ifork_t *ifp, /* inode fork pointer */
3334 int new_size) /* new indirection array size */
3335{
3336 int nlists; /* number of irec's (ex lists) */
3337 int size; /* current indirection array size */
3338
3339 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3340 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3341 size = nlists * sizeof(xfs_ext_irec_t);
3342 ASSERT(ifp->if_real_bytes);
3343 ASSERT((new_size >= 0) && (new_size != size));
3344 if (new_size == 0) {
3345 xfs_iext_destroy(ifp);
3346 } else {
3347 ifp->if_u1.if_ext_irec = (xfs_ext_irec_t *)
3348 kmem_realloc(ifp->if_u1.if_ext_irec,
David Chinner67850732008-08-13 16:02:51 +10003349 new_size, size, KM_NOFS);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003350 }
3351}
3352
3353/*
3354 * Switch from indirection array to linear (direct) extent allocations.
3355 */
Eric Sandeend96f8f82009-07-02 00:09:33 -05003356STATIC void
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003357xfs_iext_indirect_to_direct(
3358 xfs_ifork_t *ifp) /* inode fork pointer */
3359{
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003360 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003361 xfs_extnum_t nextents; /* number of extents in file */
3362 int size; /* size of file extents */
3363
3364 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3365 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3366 ASSERT(nextents <= XFS_LINEAR_EXTS);
3367 size = nextents * sizeof(xfs_bmbt_rec_t);
3368
Lachlan McIlroy71a8c872008-09-26 12:17:57 +10003369 xfs_iext_irec_compact_pages(ifp);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003370 ASSERT(ifp->if_real_bytes == XFS_IEXT_BUFSZ);
3371
3372 ep = ifp->if_u1.if_ext_irec->er_extbuf;
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10003373 kmem_free(ifp->if_u1.if_ext_irec);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003374 ifp->if_flags &= ~XFS_IFEXTIREC;
3375 ifp->if_u1.if_extents = ep;
3376 ifp->if_bytes = size;
3377 if (nextents < XFS_LINEAR_EXTS) {
3378 xfs_iext_realloc_direct(ifp, size);
3379 }
3380}
3381
3382/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003383 * Free incore file extents.
3384 */
3385void
3386xfs_iext_destroy(
3387 xfs_ifork_t *ifp) /* inode fork pointer */
3388{
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003389 if (ifp->if_flags & XFS_IFEXTIREC) {
3390 int erp_idx;
3391 int nlists;
3392
3393 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3394 for (erp_idx = nlists - 1; erp_idx >= 0 ; erp_idx--) {
3395 xfs_iext_irec_remove(ifp, erp_idx);
3396 }
3397 ifp->if_flags &= ~XFS_IFEXTIREC;
3398 } else if (ifp->if_real_bytes) {
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10003399 kmem_free(ifp->if_u1.if_extents);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003400 } else if (ifp->if_bytes) {
3401 memset(ifp->if_u2.if_inline_ext, 0, XFS_INLINE_EXTS *
3402 sizeof(xfs_bmbt_rec_t));
3403 }
3404 ifp->if_u1.if_extents = NULL;
3405 ifp->if_real_bytes = 0;
3406 ifp->if_bytes = 0;
3407}
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003408
3409/*
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003410 * Return a pointer to the extent record for file system block bno.
3411 */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003412xfs_bmbt_rec_host_t * /* pointer to found extent record */
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003413xfs_iext_bno_to_ext(
3414 xfs_ifork_t *ifp, /* inode fork pointer */
3415 xfs_fileoff_t bno, /* block number to search for */
3416 xfs_extnum_t *idxp) /* index of target extent */
3417{
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003418 xfs_bmbt_rec_host_t *base; /* pointer to first extent */
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003419 xfs_filblks_t blockcount = 0; /* number of blocks in extent */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003420 xfs_bmbt_rec_host_t *ep = NULL; /* pointer to target extent */
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003421 xfs_ext_irec_t *erp = NULL; /* indirection array pointer */
Nathan Scottc41564b2006-03-29 08:55:14 +10003422 int high; /* upper boundary in search */
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003423 xfs_extnum_t idx = 0; /* index of target extent */
Nathan Scottc41564b2006-03-29 08:55:14 +10003424 int low; /* lower boundary in search */
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003425 xfs_extnum_t nextents; /* number of file extents */
3426 xfs_fileoff_t startoff = 0; /* start offset of extent */
3427
3428 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3429 if (nextents == 0) {
3430 *idxp = 0;
3431 return NULL;
3432 }
3433 low = 0;
3434 if (ifp->if_flags & XFS_IFEXTIREC) {
3435 /* Find target extent list */
3436 int erp_idx = 0;
3437 erp = xfs_iext_bno_to_irec(ifp, bno, &erp_idx);
3438 base = erp->er_extbuf;
3439 high = erp->er_extcount - 1;
3440 } else {
3441 base = ifp->if_u1.if_extents;
3442 high = nextents - 1;
3443 }
3444 /* Binary search extent records */
3445 while (low <= high) {
3446 idx = (low + high) >> 1;
3447 ep = base + idx;
3448 startoff = xfs_bmbt_get_startoff(ep);
3449 blockcount = xfs_bmbt_get_blockcount(ep);
3450 if (bno < startoff) {
3451 high = idx - 1;
3452 } else if (bno >= startoff + blockcount) {
3453 low = idx + 1;
3454 } else {
3455 /* Convert back to file-based extent index */
3456 if (ifp->if_flags & XFS_IFEXTIREC) {
3457 idx += erp->er_extoff;
3458 }
3459 *idxp = idx;
3460 return ep;
3461 }
3462 }
3463 /* Convert back to file-based extent index */
3464 if (ifp->if_flags & XFS_IFEXTIREC) {
3465 idx += erp->er_extoff;
3466 }
3467 if (bno >= startoff + blockcount) {
3468 if (++idx == nextents) {
3469 ep = NULL;
3470 } else {
3471 ep = xfs_iext_get_ext(ifp, idx);
3472 }
3473 }
3474 *idxp = idx;
3475 return ep;
3476}
3477
3478/*
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003479 * Return a pointer to the indirection array entry containing the
3480 * extent record for filesystem block bno. Store the index of the
3481 * target irec in *erp_idxp.
3482 */
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003483xfs_ext_irec_t * /* pointer to found extent record */
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003484xfs_iext_bno_to_irec(
3485 xfs_ifork_t *ifp, /* inode fork pointer */
3486 xfs_fileoff_t bno, /* block number to search for */
3487 int *erp_idxp) /* irec index of target ext list */
3488{
3489 xfs_ext_irec_t *erp = NULL; /* indirection array pointer */
3490 xfs_ext_irec_t *erp_next; /* next indirection array entry */
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003491 int erp_idx; /* indirection array index */
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003492 int nlists; /* number of extent irec's (lists) */
3493 int high; /* binary search upper limit */
3494 int low; /* binary search lower limit */
3495
3496 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3497 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3498 erp_idx = 0;
3499 low = 0;
3500 high = nlists - 1;
3501 while (low <= high) {
3502 erp_idx = (low + high) >> 1;
3503 erp = &ifp->if_u1.if_ext_irec[erp_idx];
3504 erp_next = erp_idx < nlists - 1 ? erp + 1 : NULL;
3505 if (bno < xfs_bmbt_get_startoff(erp->er_extbuf)) {
3506 high = erp_idx - 1;
3507 } else if (erp_next && bno >=
3508 xfs_bmbt_get_startoff(erp_next->er_extbuf)) {
3509 low = erp_idx + 1;
3510 } else {
3511 break;
3512 }
3513 }
3514 *erp_idxp = erp_idx;
3515 return erp;
3516}
3517
3518/*
3519 * Return a pointer to the indirection array entry containing the
3520 * extent record at file extent index *idxp. Store the index of the
3521 * target irec in *erp_idxp and store the page index of the target
3522 * extent record in *idxp.
3523 */
3524xfs_ext_irec_t *
3525xfs_iext_idx_to_irec(
3526 xfs_ifork_t *ifp, /* inode fork pointer */
3527 xfs_extnum_t *idxp, /* extent index (file -> page) */
3528 int *erp_idxp, /* pointer to target irec */
3529 int realloc) /* new bytes were just added */
3530{
3531 xfs_ext_irec_t *prev; /* pointer to previous irec */
3532 xfs_ext_irec_t *erp = NULL; /* pointer to current irec */
3533 int erp_idx; /* indirection array index */
3534 int nlists; /* number of irec's (ex lists) */
3535 int high; /* binary search upper limit */
3536 int low; /* binary search lower limit */
3537 xfs_extnum_t page_idx = *idxp; /* extent index in target list */
3538
3539 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
Christoph Hellwig87bef182011-05-11 15:04:11 +00003540 ASSERT(page_idx >= 0);
3541 ASSERT(page_idx <= ifp->if_bytes / sizeof(xfs_bmbt_rec_t));
3542 ASSERT(page_idx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t) || realloc);
3543
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003544 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3545 erp_idx = 0;
3546 low = 0;
3547 high = nlists - 1;
3548
3549 /* Binary search extent irec's */
3550 while (low <= high) {
3551 erp_idx = (low + high) >> 1;
3552 erp = &ifp->if_u1.if_ext_irec[erp_idx];
3553 prev = erp_idx > 0 ? erp - 1 : NULL;
3554 if (page_idx < erp->er_extoff || (page_idx == erp->er_extoff &&
3555 realloc && prev && prev->er_extcount < XFS_LINEAR_EXTS)) {
3556 high = erp_idx - 1;
3557 } else if (page_idx > erp->er_extoff + erp->er_extcount ||
3558 (page_idx == erp->er_extoff + erp->er_extcount &&
3559 !realloc)) {
3560 low = erp_idx + 1;
3561 } else if (page_idx == erp->er_extoff + erp->er_extcount &&
3562 erp->er_extcount == XFS_LINEAR_EXTS) {
3563 ASSERT(realloc);
3564 page_idx = 0;
3565 erp_idx++;
3566 erp = erp_idx < nlists ? erp + 1 : NULL;
3567 break;
3568 } else {
3569 page_idx -= erp->er_extoff;
3570 break;
3571 }
3572 }
3573 *idxp = page_idx;
3574 *erp_idxp = erp_idx;
3575 return(erp);
3576}
3577
3578/*
3579 * Allocate and initialize an indirection array once the space needed
3580 * for incore extents increases above XFS_IEXT_BUFSZ.
3581 */
3582void
3583xfs_iext_irec_init(
3584 xfs_ifork_t *ifp) /* inode fork pointer */
3585{
3586 xfs_ext_irec_t *erp; /* indirection array pointer */
3587 xfs_extnum_t nextents; /* number of extents in file */
3588
3589 ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
3590 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3591 ASSERT(nextents <= XFS_LINEAR_EXTS);
3592
David Chinner67850732008-08-13 16:02:51 +10003593 erp = kmem_alloc(sizeof(xfs_ext_irec_t), KM_NOFS);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003594
3595 if (nextents == 0) {
David Chinner67850732008-08-13 16:02:51 +10003596 ifp->if_u1.if_extents = kmem_alloc(XFS_IEXT_BUFSZ, KM_NOFS);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003597 } else if (!ifp->if_real_bytes) {
3598 xfs_iext_inline_to_direct(ifp, XFS_IEXT_BUFSZ);
3599 } else if (ifp->if_real_bytes < XFS_IEXT_BUFSZ) {
3600 xfs_iext_realloc_direct(ifp, XFS_IEXT_BUFSZ);
3601 }
3602 erp->er_extbuf = ifp->if_u1.if_extents;
3603 erp->er_extcount = nextents;
3604 erp->er_extoff = 0;
3605
3606 ifp->if_flags |= XFS_IFEXTIREC;
3607 ifp->if_real_bytes = XFS_IEXT_BUFSZ;
3608 ifp->if_bytes = nextents * sizeof(xfs_bmbt_rec_t);
3609 ifp->if_u1.if_ext_irec = erp;
3610
3611 return;
3612}
3613
3614/*
3615 * Allocate and initialize a new entry in the indirection array.
3616 */
3617xfs_ext_irec_t *
3618xfs_iext_irec_new(
3619 xfs_ifork_t *ifp, /* inode fork pointer */
3620 int erp_idx) /* index for new irec */
3621{
3622 xfs_ext_irec_t *erp; /* indirection array pointer */
3623 int i; /* loop counter */
3624 int nlists; /* number of irec's (ex lists) */
3625
3626 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3627 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3628
3629 /* Resize indirection array */
3630 xfs_iext_realloc_indirect(ifp, ++nlists *
3631 sizeof(xfs_ext_irec_t));
3632 /*
3633 * Move records down in the array so the
3634 * new page can use erp_idx.
3635 */
3636 erp = ifp->if_u1.if_ext_irec;
3637 for (i = nlists - 1; i > erp_idx; i--) {
3638 memmove(&erp[i], &erp[i-1], sizeof(xfs_ext_irec_t));
3639 }
3640 ASSERT(i == erp_idx);
3641
3642 /* Initialize new extent record */
3643 erp = ifp->if_u1.if_ext_irec;
David Chinner67850732008-08-13 16:02:51 +10003644 erp[erp_idx].er_extbuf = kmem_alloc(XFS_IEXT_BUFSZ, KM_NOFS);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003645 ifp->if_real_bytes = nlists * XFS_IEXT_BUFSZ;
3646 memset(erp[erp_idx].er_extbuf, 0, XFS_IEXT_BUFSZ);
3647 erp[erp_idx].er_extcount = 0;
3648 erp[erp_idx].er_extoff = erp_idx > 0 ?
3649 erp[erp_idx-1].er_extoff + erp[erp_idx-1].er_extcount : 0;
3650 return (&erp[erp_idx]);
3651}
3652
3653/*
3654 * Remove a record from the indirection array.
3655 */
3656void
3657xfs_iext_irec_remove(
3658 xfs_ifork_t *ifp, /* inode fork pointer */
3659 int erp_idx) /* irec index to remove */
3660{
3661 xfs_ext_irec_t *erp; /* indirection array pointer */
3662 int i; /* loop counter */
3663 int nlists; /* number of irec's (ex lists) */
3664
3665 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3666 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3667 erp = &ifp->if_u1.if_ext_irec[erp_idx];
3668 if (erp->er_extbuf) {
3669 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1,
3670 -erp->er_extcount);
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10003671 kmem_free(erp->er_extbuf);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003672 }
3673 /* Compact extent records */
3674 erp = ifp->if_u1.if_ext_irec;
3675 for (i = erp_idx; i < nlists - 1; i++) {
3676 memmove(&erp[i], &erp[i+1], sizeof(xfs_ext_irec_t));
3677 }
3678 /*
3679 * Manually free the last extent record from the indirection
3680 * array. A call to xfs_iext_realloc_indirect() with a size
3681 * of zero would result in a call to xfs_iext_destroy() which
3682 * would in turn call this function again, creating a nasty
3683 * infinite loop.
3684 */
3685 if (--nlists) {
3686 xfs_iext_realloc_indirect(ifp,
3687 nlists * sizeof(xfs_ext_irec_t));
3688 } else {
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10003689 kmem_free(ifp->if_u1.if_ext_irec);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003690 }
3691 ifp->if_real_bytes = nlists * XFS_IEXT_BUFSZ;
3692}
3693
3694/*
3695 * This is called to clean up large amounts of unused memory allocated
3696 * by the indirection array. Before compacting anything though, verify
3697 * that the indirection array is still needed and switch back to the
3698 * linear extent list (or even the inline buffer) if possible. The
3699 * compaction policy is as follows:
3700 *
3701 * Full Compaction: Extents fit into a single page (or inline buffer)
Lachlan McIlroy71a8c872008-09-26 12:17:57 +10003702 * Partial Compaction: Extents occupy less than 50% of allocated space
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003703 * No Compaction: Extents occupy at least 50% of allocated space
3704 */
3705void
3706xfs_iext_irec_compact(
3707 xfs_ifork_t *ifp) /* inode fork pointer */
3708{
3709 xfs_extnum_t nextents; /* number of extents in file */
3710 int nlists; /* number of irec's (ex lists) */
3711
3712 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3713 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3714 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3715
3716 if (nextents == 0) {
3717 xfs_iext_destroy(ifp);
3718 } else if (nextents <= XFS_INLINE_EXTS) {
3719 xfs_iext_indirect_to_direct(ifp);
3720 xfs_iext_direct_to_inline(ifp, nextents);
3721 } else if (nextents <= XFS_LINEAR_EXTS) {
3722 xfs_iext_indirect_to_direct(ifp);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003723 } else if (nextents < (nlists * XFS_LINEAR_EXTS) >> 1) {
3724 xfs_iext_irec_compact_pages(ifp);
3725 }
3726}
3727
3728/*
3729 * Combine extents from neighboring extent pages.
3730 */
3731void
3732xfs_iext_irec_compact_pages(
3733 xfs_ifork_t *ifp) /* inode fork pointer */
3734{
3735 xfs_ext_irec_t *erp, *erp_next;/* pointers to irec entries */
3736 int erp_idx = 0; /* indirection array index */
3737 int nlists; /* number of irec's (ex lists) */
3738
3739 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3740 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3741 while (erp_idx < nlists - 1) {
3742 erp = &ifp->if_u1.if_ext_irec[erp_idx];
3743 erp_next = erp + 1;
3744 if (erp_next->er_extcount <=
3745 (XFS_LINEAR_EXTS - erp->er_extcount)) {
Lachlan McIlroy71a8c872008-09-26 12:17:57 +10003746 memcpy(&erp->er_extbuf[erp->er_extcount],
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003747 erp_next->er_extbuf, erp_next->er_extcount *
3748 sizeof(xfs_bmbt_rec_t));
3749 erp->er_extcount += erp_next->er_extcount;
3750 /*
3751 * Free page before removing extent record
3752 * so er_extoffs don't get modified in
3753 * xfs_iext_irec_remove.
3754 */
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10003755 kmem_free(erp_next->er_extbuf);
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003756 erp_next->er_extbuf = NULL;
3757 xfs_iext_irec_remove(ifp, erp_idx + 1);
3758 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3759 } else {
3760 erp_idx++;
3761 }
3762 }
3763}
3764
3765/*
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003766 * This is called to update the er_extoff field in the indirection
3767 * array when extents have been added or removed from one of the
3768 * extent lists. erp_idx contains the irec index to begin updating
3769 * at and ext_diff contains the number of extents that were added
3770 * or removed.
3771 */
3772void
3773xfs_iext_irec_update_extoffs(
3774 xfs_ifork_t *ifp, /* inode fork pointer */
3775 int erp_idx, /* irec index to update */
3776 int ext_diff) /* number of new extents */
3777{
3778 int i; /* loop counter */
3779 int nlists; /* number of irec's (ex lists */
3780
3781 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3782 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3783 for (i = erp_idx; i < nlists; i++) {
3784 ifp->if_u1.if_ext_irec[i].er_extoff += ext_diff;
3785 }
3786}