xfs_itable.c 10.0 KB
Newer Older
D
Dave Chinner 已提交
1
// SPDX-License-Identifier: GPL-2.0
L
Linus Torvalds 已提交
2
/*
3 4
 * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
 * All Rights Reserved.
L
Linus Torvalds 已提交
5 6
 */
#include "xfs.h"
7
#include "xfs_fs.h"
8
#include "xfs_shared.h"
9
#include "xfs_format.h"
10 11
#include "xfs_log_format.h"
#include "xfs_trans_resv.h"
L
Linus Torvalds 已提交
12 13
#include "xfs_mount.h"
#include "xfs_inode.h"
14
#include "xfs_btree.h"
L
Linus Torvalds 已提交
15
#include "xfs_ialloc.h"
16
#include "xfs_ialloc_btree.h"
17
#include "xfs_iwalk.h"
L
Linus Torvalds 已提交
18 19
#include "xfs_itable.h"
#include "xfs_error.h"
D
Dave Chinner 已提交
20
#include "xfs_icache.h"
21
#include "xfs_health.h"
L
Linus Torvalds 已提交
22

23
/*
24 25 26
 * Bulk Stat
 * =========
 *
27
 * Use the inode walking functions to fill out struct xfs_bulkstat for every
28 29
 * allocated inode, then pass the stat information to some externally provided
 * iteration function.
30
 */
31 32 33 34

struct xfs_bstat_chunk {
	bulkstat_one_fmt_pf	formatter;
	struct xfs_ibulk	*breq;
35
	struct xfs_bulkstat	*buf;
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
};

/*
 * Fill out the bulkstat info for a single inode and report it somewhere.
 *
 * bc->breq->lastino is effectively the inode cursor as we walk through the
 * filesystem.  Therefore, we update it any time we need to move the cursor
 * forward, regardless of whether or not we're sending any bstat information
 * back to userspace.  If the inode is internal metadata or, has been freed
 * out from under us, we just simply keep going.
 *
 * However, if any other type of error happens we want to stop right where we
 * are so that userspace will call back with exact number of the bad inode and
 * we can send back an error code.
 *
 * Note that if the formatter tells us there's no space left in the buffer we
 * move the cursor forward and abort the walk.
 */
STATIC int
55
xfs_bulkstat_one_int(
56 57 58 59
	struct xfs_mount	*mp,
	struct xfs_trans	*tp,
	xfs_ino_t		ino,
	struct xfs_bstat_chunk	*bc)
L
Linus Torvalds 已提交
60
{
61 62
	struct xfs_icdinode	*dic;		/* dinode core info pointer */
	struct xfs_inode	*ip;		/* incore inode pointer */
63
	struct inode		*inode;
64
	struct xfs_bulkstat	*buf = bc->buf;
65
	int			error = -EINVAL;
66

67 68
	if (xfs_internal_inum(mp, ino))
		goto out_advance;
69

70
	error = xfs_iget(mp, tp, ino,
71 72
			 (XFS_IGET_DONTCACHE | XFS_IGET_UNTRUSTED),
			 XFS_ILOCK_SHARED, &ip);
73 74
	if (error == -ENOENT || error == -EINVAL)
		goto out_advance;
75
	if (error)
76
		goto out;
L
Linus Torvalds 已提交
77 78

	ASSERT(ip != NULL);
79
	ASSERT(ip->i_imap.im_blkno != 0);
80
	inode = VFS_I(ip);
L
Linus Torvalds 已提交
81 82 83 84 85 86

	dic = &ip->i_d;

	/* xfs_iget returns the following without needing
	 * further change.
	 */
87
	buf->bs_projectid = xfs_get_projid(ip);
L
Linus Torvalds 已提交
88 89 90 91
	buf->bs_ino = ino;
	buf->bs_uid = dic->di_uid;
	buf->bs_gid = dic->di_gid;
	buf->bs_size = dic->di_size;
92

93
	buf->bs_nlink = inode->i_nlink;
94 95 96 97 98 99 100 101
	buf->bs_atime = inode->i_atime.tv_sec;
	buf->bs_atime_nsec = inode->i_atime.tv_nsec;
	buf->bs_mtime = inode->i_mtime.tv_sec;
	buf->bs_mtime_nsec = inode->i_mtime.tv_nsec;
	buf->bs_ctime = inode->i_ctime.tv_sec;
	buf->bs_ctime_nsec = inode->i_ctime.tv_nsec;
	buf->bs_btime = dic->di_crtime.t_sec;
	buf->bs_btime_nsec = dic->di_crtime.t_nsec;
102
	buf->bs_gen = inode->i_generation;
D
Dave Chinner 已提交
103
	buf->bs_mode = inode->i_mode;
104

L
Linus Torvalds 已提交
105
	buf->bs_xflags = xfs_ip2xflags(ip);
106
	buf->bs_extsize_blks = dic->di_extsize;
L
Linus Torvalds 已提交
107
	buf->bs_extents = dic->di_nextents;
108
	xfs_bulkstat_health(ip, buf);
L
Linus Torvalds 已提交
109
	buf->bs_aextents = dic->di_anextents;
110
	buf->bs_forkoff = XFS_IFORK_BOFF(ip);
111
	buf->bs_version = XFS_BULKSTAT_VERSION_V5;
L
Linus Torvalds 已提交
112

113 114
	if (dic->di_version == 3) {
		if (dic->di_flags2 & XFS_DIFLAG2_COWEXTSIZE)
115
			buf->bs_cowextsize_blks = dic->di_cowextsize;
116 117
	}

L
Linus Torvalds 已提交
118 119
	switch (dic->di_format) {
	case XFS_DINODE_FMT_DEV:
C
Christoph Hellwig 已提交
120
		buf->bs_rdev = sysv_encode_dev(inode->i_rdev);
L
Linus Torvalds 已提交
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
		buf->bs_blksize = BLKDEV_IOSIZE;
		buf->bs_blocks = 0;
		break;
	case XFS_DINODE_FMT_LOCAL:
		buf->bs_rdev = 0;
		buf->bs_blksize = mp->m_sb.sb_blocksize;
		buf->bs_blocks = 0;
		break;
	case XFS_DINODE_FMT_EXTENTS:
	case XFS_DINODE_FMT_BTREE:
		buf->bs_rdev = 0;
		buf->bs_blksize = mp->m_sb.sb_blocksize;
		buf->bs_blocks = dic->di_nblocks + ip->i_delayed_blks;
		break;
	}
C
Christoph Hellwig 已提交
136
	xfs_iunlock(ip, XFS_ILOCK_SHARED);
137
	xfs_irele(ip);
L
Linus Torvalds 已提交
138

139 140 141 142 143
	error = bc->formatter(bc->breq, buf);
	if (error == XFS_IBULK_ABORT)
		goto out_advance;
	if (error)
		goto out;
L
Linus Torvalds 已提交
144

145 146 147 148 149 150 151 152 153
out_advance:
	/*
	 * Advance the cursor to the inode that comes after the one we just
	 * looked at.  We want the caller to move along if the bulkstat
	 * information was copied successfully; if we tried to grab the inode
	 * but it's no longer allocated; or if it's internal metadata.
	 */
	bc->breq->startino = ino + 1;
out:
154
	return error;
L
Linus Torvalds 已提交
155 156
}

157
/* Bulkstat a single inode. */
158 159
int
xfs_bulkstat_one(
160 161
	struct xfs_ibulk	*breq,
	bulkstat_one_fmt_pf	formatter)
162
{
163 164 165 166 167 168 169 170
	struct xfs_bstat_chunk	bc = {
		.formatter	= formatter,
		.breq		= breq,
	};
	int			error;

	ASSERT(breq->icount == 1);

171 172
	bc.buf = kmem_zalloc(sizeof(struct xfs_bulkstat),
			KM_SLEEP | KM_MAYFAIL);
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
	if (!bc.buf)
		return -ENOMEM;

	error = xfs_bulkstat_one_int(breq->mp, NULL, breq->startino, &bc);

	kmem_free(bc.buf);

	/*
	 * If we reported one inode to userspace then we abort because we hit
	 * the end of the buffer.  Don't leak that back to userspace.
	 */
	if (error == XFS_IWALK_ABORT)
		error = 0;

	return error;
188 189
}

190
static int
191 192 193 194 195
xfs_bulkstat_iwalk(
	struct xfs_mount	*mp,
	struct xfs_trans	*tp,
	xfs_ino_t		ino,
	void			*data)
J
Jie Liu 已提交
196
{
197
	int			error;
198

199 200 201 202
	error = xfs_bulkstat_one_int(mp, tp, ino, data);
	/* bulkstat just skips over missing inodes */
	if (error == -ENOENT || error == -EINVAL)
		return 0;
J
Jie Liu 已提交
203 204 205
	return error;
}

L
Linus Torvalds 已提交
206
/*
207 208 209 210 211 212 213 214 215 216
 * Check the incoming lastino parameter.
 *
 * We allow any inode value that could map to physical space inside the
 * filesystem because if there are no inodes there, bulkstat moves on to the
 * next chunk.  In other words, the magic agino value of zero takes us to the
 * first chunk in the AG, and an agino value past the end of the AG takes us to
 * the first chunk in the next AG.
 *
 * Therefore we can end early if the requested inode is beyond the end of the
 * filesystem or doesn't map properly.
L
Linus Torvalds 已提交
217
 */
218 219 220 221
static inline bool
xfs_bulkstat_already_done(
	struct xfs_mount	*mp,
	xfs_ino_t		startino)
L
Linus Torvalds 已提交
222
{
223 224
	xfs_agnumber_t		agno = XFS_INO_TO_AGNO(mp, startino);
	xfs_agino_t		agino = XFS_INO_TO_AGINO(mp, startino);
L
Linus Torvalds 已提交
225

226 227 228
	return agno >= mp->m_sb.sb_agcount ||
	       startino != XFS_AGINO_TO_INO(mp, agno, agino);
}
229

230 231 232 233 234 235 236 237 238 239 240
/* Return stat information in bulk (by-inode) for the filesystem. */
int
xfs_bulkstat(
	struct xfs_ibulk	*breq,
	bulkstat_one_fmt_pf	formatter)
{
	struct xfs_bstat_chunk	bc = {
		.formatter	= formatter,
		.breq		= breq,
	};
	int			error;
241

242 243
	if (xfs_bulkstat_already_done(breq->mp, breq->startino))
		return 0;
244

245 246
	bc.buf = kmem_zalloc(sizeof(struct xfs_bulkstat),
			KM_SLEEP | KM_MAYFAIL);
247
	if (!bc.buf)
D
Dave Chinner 已提交
248
		return -ENOMEM;
249

250 251
	error = xfs_iwalk(breq->mp, NULL, breq->startino, xfs_bulkstat_iwalk,
			breq->icount, &bc);
252

253
	kmem_free(bc.buf);
254

255
	/*
256 257 258 259 260
	 * We found some inodes, so clear the error status and return them.
	 * The lastino pointer will point directly at the inode that triggered
	 * any error that occurred, so on the next call the error will be
	 * triggered again and propagated to userspace as there will be no
	 * formatted inodes in the buffer.
261
	 */
262
	if (breq->ocount > 0)
263 264 265
		error = 0;

	return error;
L
Linus Torvalds 已提交
266 267
}

268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
/* Convert bulkstat (v5) to bstat (v1). */
void
xfs_bulkstat_to_bstat(
	struct xfs_mount		*mp,
	struct xfs_bstat		*bs1,
	const struct xfs_bulkstat	*bstat)
{
	memset(bs1, 0, sizeof(struct xfs_bstat));
	bs1->bs_ino = bstat->bs_ino;
	bs1->bs_mode = bstat->bs_mode;
	bs1->bs_nlink = bstat->bs_nlink;
	bs1->bs_uid = bstat->bs_uid;
	bs1->bs_gid = bstat->bs_gid;
	bs1->bs_rdev = bstat->bs_rdev;
	bs1->bs_blksize = bstat->bs_blksize;
	bs1->bs_size = bstat->bs_size;
	bs1->bs_atime.tv_sec = bstat->bs_atime;
	bs1->bs_mtime.tv_sec = bstat->bs_mtime;
	bs1->bs_ctime.tv_sec = bstat->bs_ctime;
	bs1->bs_atime.tv_nsec = bstat->bs_atime_nsec;
	bs1->bs_mtime.tv_nsec = bstat->bs_mtime_nsec;
	bs1->bs_ctime.tv_nsec = bstat->bs_ctime_nsec;
	bs1->bs_blocks = bstat->bs_blocks;
	bs1->bs_xflags = bstat->bs_xflags;
	bs1->bs_extsize = XFS_FSB_TO_B(mp, bstat->bs_extsize_blks);
	bs1->bs_extents = bstat->bs_extents;
	bs1->bs_gen = bstat->bs_gen;
	bs1->bs_projid_lo = bstat->bs_projectid & 0xFFFF;
	bs1->bs_forkoff = bstat->bs_forkoff;
	bs1->bs_projid_hi = bstat->bs_projectid >> 16;
	bs1->bs_sick = bstat->bs_sick;
	bs1->bs_checked = bstat->bs_checked;
	bs1->bs_cowextsize = XFS_FSB_TO_B(mp, bstat->bs_cowextsize_blks);
	bs1->bs_dmevmask = 0;
	bs1->bs_dmstate = 0;
	bs1->bs_aextents = bstat->bs_aextents;
}

306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
struct xfs_inumbers_chunk {
	inumbers_fmt_pf		formatter;
	struct xfs_ibulk	*breq;
};

/*
 * INUMBERS
 * ========
 * This is how we export inode btree records to userspace, so that XFS tools
 * can figure out where inodes are allocated.
 */

/*
 * Format the inode group structure and report it somewhere.
 *
 * Similar to xfs_bulkstat_one_int, lastino is the inode cursor as we walk
 * through the filesystem so we move it forward unless there was a runtime
 * error.  If the formatter tells us the buffer is now full we also move the
 * cursor forward and abort the walk.
 */
STATIC int
xfs_inumbers_walk(
	struct xfs_mount	*mp,
	struct xfs_trans	*tp,
	xfs_agnumber_t		agno,
	const struct xfs_inobt_rec_incore *irec,
	void			*data)
333
{
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
	struct xfs_inogrp	inogrp = {
		.xi_startino	= XFS_AGINO_TO_INO(mp, agno, irec->ir_startino),
		.xi_alloccount	= irec->ir_count - irec->ir_freecount,
		.xi_allocmask	= ~irec->ir_free,
	};
	struct xfs_inumbers_chunk *ic = data;
	xfs_agino_t		agino;
	int			error;

	error = ic->formatter(ic->breq, &inogrp);
	if (error && error != XFS_IBULK_ABORT)
		return error;

	agino = irec->ir_startino + XFS_INODES_PER_CHUNK;
	ic->breq->startino = XFS_AGINO_TO_INO(mp, agno, agino);
	return error;
350 351
}

L
Linus Torvalds 已提交
352 353 354
/*
 * Return inode number table for the filesystem.
 */
355
int
L
Linus Torvalds 已提交
356
xfs_inumbers(
357
	struct xfs_ibulk	*breq,
J
Jie Liu 已提交
358
	inumbers_fmt_pf		formatter)
L
Linus Torvalds 已提交
359
{
360 361 362 363
	struct xfs_inumbers_chunk ic = {
		.formatter	= formatter,
		.breq		= breq,
	};
J
Jie Liu 已提交
364 365
	int			error = 0;

366 367
	if (xfs_bulkstat_already_done(breq->mp, breq->startino))
		return 0;
J
Jie Liu 已提交
368

369 370
	error = xfs_inobt_walk(breq->mp, NULL, breq->startino,
			xfs_inumbers_walk, breq->icount, &ic);
371

372 373 374 375 376 377 378 379 380
	/*
	 * We found some inode groups, so clear the error status and return
	 * them.  The lastino pointer will point directly at the inode that
	 * triggered any error that occurred, so on the next call the error
	 * will be triggered again and propagated to userspace as there will be
	 * no formatted inode groups in the buffer.
	 */
	if (breq->ocount > 0)
		error = 0;
381

L
Linus Torvalds 已提交
382 383
	return error;
}