summaryrefslogtreecommitdiff
path: root/fs/xfs/libxfs/xfs_imeta.c
blob: aadba5786d1c6262ed111006ae76276f5b101b43 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
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
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
// SPDX-License-Identifier: GPL-2.0+
/*
 * Copyright (C) 2019 Oracle.  All Rights Reserved.
 * Author: Darrick J. Wong <darrick.wong@oracle.com>
 */
#include "xfs.h"
#include "xfs_fs.h"
#include "xfs_shared.h"
#include "xfs_format.h"
#include "xfs_log_format.h"
#include "xfs_trans_resv.h"
#include "xfs_bit.h"
#include "xfs_sb.h"
#include "xfs_mount.h"
#include "xfs_defer.h"
#include "xfs_trans.h"
#include "xfs_imeta.h"
#include "xfs_trace.h"
#include "xfs_inode.h"
#include "xfs_bmap_btree.h"
#include "xfs_da_format.h"
#include "xfs_da_btree.h"
#include "xfs_trans_space.h"

/*
 * Metadata Inode Number Management
 * ================================
 *
 * These functions provide an abstraction layer for looking up, creating, and
 * deleting metadata inodes.  These pointers live in the in-core superblock,
 * so the functions moderate access to those fields and take care of logging.
 *
 * For the five existing metadata inodes (real time bitmap & summary; and the
 * user, group, and quotas) we'll continue to maintain the in-core superblock
 * inodes for reads and only require xfs_imeta_create and xfs_imeta_unlink to
 * persist changes.  New metadata inode types must only use the xfs_imeta_*
 * functions.
 *
 * Callers wishing to create or unlink a metadata inode must pass in a
 * xfs_imeta_end structure.  After committing or cancelling the transaction,
 * this structure must be passed to xfs_imeta_end_update to free resources that
 * cannot be freed during the transaction.
 *
 * Right now we only support callers passing in the predefined metadata inode
 * paths; the goal is that callers will some day locate metadata inodes based
 * on a metadata inode directory structure.
 */

/* Static metadata inode paths */

const struct xfs_imeta_path XFS_IMETA_RTBITMAP = {
	.bogus = 0,
};

const struct xfs_imeta_path XFS_IMETA_RTSUMMARY = {
	.bogus = 1,
};

const struct xfs_imeta_path XFS_IMETA_USRQUOTA = {
	.bogus = 2,
};

const struct xfs_imeta_path XFS_IMETA_GRPQUOTA = {
	.bogus = 3,
};

const struct xfs_imeta_path XFS_IMETA_PRJQUOTA = {
	.bogus = 4,
};

/* Are these two paths equal? */
STATIC bool
xfs_imeta_path_compare(
	const struct xfs_imeta_path	*a,
	const struct xfs_imeta_path	*b)
{
	return a == b;
}

/* Is this path ok? */
static inline bool
xfs_imeta_path_check(
	const struct xfs_imeta_path	*path)
{
	return true;
}

/* Functions for storing and retrieving superblock inode values. */

/* Mapping of metadata inode paths to in-core superblock values. */
static const struct xfs_imeta_sbmap {
	const struct xfs_imeta_path	*path;
	unsigned int			offset;
} xfs_imeta_sbmaps[] = {
	{
		.path	= &XFS_IMETA_RTBITMAP,
		.offset	= offsetof(struct xfs_sb, sb_rbmino),
	},
	{
		.path	= &XFS_IMETA_RTSUMMARY,
		.offset	= offsetof(struct xfs_sb, sb_rsumino),
	},
	{
		.path	= &XFS_IMETA_USRQUOTA,
		.offset	= offsetof(struct xfs_sb, sb_uquotino),
	},
	{
		.path	= &XFS_IMETA_GRPQUOTA,
		.offset	= offsetof(struct xfs_sb, sb_gquotino),
	},
	{
		.path	= &XFS_IMETA_PRJQUOTA,
		.offset	= offsetof(struct xfs_sb, sb_pquotino),
	},
	{ NULL, 0 },
};

/* Return a pointer to the in-core superblock inode value. */
static inline xfs_ino_t *
xfs_imeta_sbmap_to_inop(
	struct xfs_mount		*mp,
	const struct xfs_imeta_sbmap	*map)
{
	return (xfs_ino_t *)(((char *)&mp->m_sb) + map->offset);
}

/* Compute location of metadata inode pointer in the in-core superblock */
static inline xfs_ino_t *
xfs_imeta_path_to_sb_inop(
	struct xfs_mount		*mp,
	const struct xfs_imeta_path	*path)
{
	const struct xfs_imeta_sbmap	*p;

	for (p = xfs_imeta_sbmaps; p->path; p++)
		if (xfs_imeta_path_compare(p->path, path))
			return xfs_imeta_sbmap_to_inop(mp, p);

	return NULL;
}

/* Look up a superblock metadata inode by its path. */
STATIC int
xfs_imeta_sb_lookup(
	struct xfs_mount		*mp,
	const struct xfs_imeta_path	*path,
	xfs_ino_t			*inop)
{
	xfs_ino_t			*sb_inop;

	sb_inop = xfs_imeta_path_to_sb_inop(mp, path);
	if (!sb_inop)
		return -EINVAL;

	trace_xfs_imeta_sb_lookup(mp, sb_inop);
	*inop = *sb_inop;
	return 0;
}

/*
 * Create a new metadata inode and set a superblock pointer to this new inode.
 * The superblock field must not already be pointing to an inode.
 */
STATIC int
xfs_imeta_sb_create(
	struct xfs_trans		**tpp,
	const struct xfs_imeta_path	*path,
	umode_t				mode,
	struct xfs_inode		**ipp)
{
	struct xfs_ialloc_args args = {
		.nlink			= S_ISDIR(mode) ? 2 : 1,
		.mode			= mode,
	};
	struct xfs_mount		*mp = (*tpp)->t_mountp;
	xfs_ino_t			*sb_inop;
	int				error;

	/* Reject if the sb already points to some inode. */
	sb_inop = xfs_imeta_path_to_sb_inop(mp, path);
	if (!sb_inop)
		return -EINVAL;

	if (*sb_inop != NULLFSINO)
		return -EEXIST;

	/* Otherwise, create the inode and set the sb pointer. */
	error = xfs_dir_ialloc(tpp, &args, ipp);
	if (error)
		return error;


	*sb_inop = (*ipp)->i_ino;
	trace_xfs_imeta_sb_create(mp, sb_inop);
	xfs_log_sb(*tpp);
	return 0;
}

/*
 * Clear the given inode pointer from the superblock and drop the link count
 * of the metadata inode.
 */
STATIC int
xfs_imeta_sb_unlink(
	struct xfs_trans		**tpp,
	const struct xfs_imeta_path	*path,
	struct xfs_inode		*ip)
{
	struct xfs_mount		*mp = (*tpp)->t_mountp;
	xfs_ino_t			*sb_inop;

	sb_inop = xfs_imeta_path_to_sb_inop(mp, path);
	if (!sb_inop)
		return -EINVAL;

	/* Reject if the sb doesn't point to the inode that was passed in. */
	if (*sb_inop != ip->i_ino)
		return -ENOENT;

	*sb_inop = NULLFSINO;
	trace_xfs_imeta_sb_unlink(mp, sb_inop);
	xfs_log_sb(*tpp);
	return xfs_droplink(*tpp, ip);
}

/* Set the given inode pointer to NULL in the superblock. */
STATIC int
xfs_imeta_sb_zap(
	struct xfs_trans		**tpp,
	const struct xfs_imeta_path	*path)
{
	struct xfs_mount		*mp = (*tpp)->t_mountp;
	xfs_ino_t			*sb_inop;

	sb_inop = xfs_imeta_path_to_sb_inop(mp, path);
	if (!sb_inop)
		return -EINVAL;

	*sb_inop = NULLFSINO;
	trace_xfs_imeta_sb_zap(mp, sb_inop);
	xfs_log_sb(*tpp);
	return 0;
}

/* General functions for managing metadata inode pointers */

/*
 * Is this metadata inode pointer ok?  We allow the fields to be set to
 * NULLFSINO if the metadata structure isn't present, and we don't allow
 * obviously incorrect inode pointers.
 */
static inline bool
xfs_imeta_verify(
	struct xfs_mount	*mp,
	xfs_ino_t		ino)
{
	if (ino == NULLFSINO)
		return true;
	return xfs_verify_ino(mp, ino);
}

/* Look up a metadata inode by its path. */
int
xfs_imeta_lookup(
	struct xfs_mount		*mp,
	const struct xfs_imeta_path	*path,
	xfs_ino_t			*inop)
{
	xfs_ino_t			ino;
	int				error;

	ASSERT(xfs_imeta_path_check(path));

	error = xfs_imeta_sb_lookup(mp, path, &ino);
	if (error)
		return error;

	if (!xfs_imeta_verify(mp, ino))
		return -EFSCORRUPTED;

	*inop = ino;
	return 0;
}

/*
 * Create a metadata inode with the given @mode, and insert it into the
 * metadata directory tree at the given @path.  The path (up to the final
 * component) must already exist.  The new metadata inode @ipp will be ijoined
 * and logged to @tpp, with the ILOCK held until the next transaction commit.
 * The caller must provide a @cleanup structure.
 *
 * NOTE: This function may pass a child inode @ipp back to the caller along
 * with an error status code.  The caller must always check for a non-null
 * child inode and release it.
 */
int
xfs_imeta_create(
	struct xfs_trans		**tpp,
	const struct xfs_imeta_path	*path,
	umode_t				mode,
	struct xfs_inode		**ipp,
	struct xfs_imeta_end		*cleanup)
{
	ASSERT(xfs_imeta_path_check(path));
	*ipp = NULL;

	return xfs_imeta_sb_create(tpp, path, mode, ipp);
}

/*
 * Unlink a metadata inode @ip from the metadata directory given by @path.  The
 * metadata inode must not be ILOCKed.  Upon return, the inode will be ijoined
 * and logged to @tpp, and returned with reduced link count, ready to be
 * released.  The caller must provide a @cleanup structure.
 */
int
xfs_imeta_unlink(
	struct xfs_trans		**tpp,
	const struct xfs_imeta_path	*path,
	struct xfs_inode		*ip,
	struct xfs_imeta_end		*cleanup)
{
	ASSERT(xfs_imeta_path_check(path));
	ASSERT(xfs_imeta_verify((*tpp)->t_mountp, ip->i_ino));

	return xfs_imeta_sb_unlink(tpp, path, ip);
}

/*
 * Forcibly clear the metadata pointer noted by @path so that a subsequent
 * lookup will return NULLFSINO.  If the pointer was not already NULLFSINO, the
 * caller is responsible for cleaning up those resources; in other words, this
 * function is only to be used when blowing out a totally destroyed metadata
 * inode.  The caller must provide a @cleanup structure.
 */
int
xfs_imeta_zap(
	struct xfs_trans		**tpp,
	const struct xfs_imeta_path	*path,
	struct xfs_imeta_end		*cleanup)
{
	ASSERT(xfs_imeta_path_check(path));

	return xfs_imeta_sb_zap(tpp, path);
}

/*
 * Clean up after committing (or cancelling) a metadata inode creation or
 * removal.
 */
void
xfs_imeta_end_update(
	struct xfs_mount		*mp,
	struct xfs_imeta_end		*cleanup,
	int				error)
{
	trace_xfs_imeta_end_update(mp, 0, error, _RET_IP_);
}

/* Does this inode number refer to a static metadata inode? */
bool
xfs_is_static_meta_ino(
	struct xfs_mount		*mp,
	xfs_ino_t			ino)
{
	const struct xfs_imeta_sbmap	*p;

	if (ino == NULLFSINO)
		return false;

	for (p = xfs_imeta_sbmaps; p->path; p++)
		if (ino == *xfs_imeta_sbmap_to_inop(mp, p))
			return true;

	return false;
}

/* Ensure that the in-core superblock has all the values that it should. */
int
xfs_imeta_mount(
	struct xfs_mount	*mp)
{
	return 0;
}

/* Calculate the log block reservation to create a metadata inode. */
unsigned int
xfs_imeta_create_space_res(
	struct xfs_mount	*mp)
{
	return XFS_IALLOC_SPACE_RES(mp);
}

/* Calculate the log block reservation to unlink a metadata inode. */
unsigned int
xfs_imeta_unlink_space_res(
	struct xfs_mount	*mp)
{
	return XFS_REMOVE_SPACE_RES(mp);
}