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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
|
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (c) 2022-2024 Oracle. All Rights Reserved.
* Author: Darrick J. Wong <djwong@kernel.org>
*/
#include "xfs.h"
#include "xfs_fs.h"
#include "xfs_shared.h"
#include "xfs_format.h"
#include "xfs_trans_resv.h"
#include "xfs_bit.h"
#include "xfs_sb.h"
#include "xfs_mount.h"
#include "xfs_btree.h"
#include "xfs_alloc_btree.h"
#include "xfs_rmap_btree.h"
#include "xfs_alloc.h"
#include "xfs_ialloc.h"
#include "xfs_rmap.h"
#include "xfs_ag.h"
#include "xfs_ag_resv.h"
#include "xfs_health.h"
#include "xfs_error.h"
#include "xfs_bmap.h"
#include "xfs_defer.h"
#include "xfs_log_format.h"
#include "xfs_trans.h"
#include "xfs_trace.h"
#include "xfs_inode.h"
#include "xfs_icache.h"
#include "xfs_rtgroup.h"
#include "xfs_rtbitmap.h"
#include "xfs_metafile.h"
#include "xfs_metadir.h"
int
xfs_rtgroup_alloc(
struct xfs_mount *mp,
xfs_rgnumber_t rgno,
xfs_rgnumber_t rgcount,
xfs_rtbxlen_t rextents)
{
struct xfs_rtgroup *rtg;
int error;
rtg = kzalloc(sizeof(struct xfs_rtgroup), GFP_KERNEL);
if (!rtg)
return -ENOMEM;
error = xfs_group_insert(mp, rtg_group(rtg), rgno, XG_TYPE_RTG);
if (error)
goto out_free_rtg;
return 0;
out_free_rtg:
kfree(rtg);
return error;
}
void
xfs_rtgroup_free(
struct xfs_mount *mp,
xfs_rgnumber_t rgno)
{
xfs_group_free(mp, rgno, XG_TYPE_RTG, NULL);
}
/* Free a range of incore rtgroup objects. */
void
xfs_free_rtgroups(
struct xfs_mount *mp,
xfs_rgnumber_t first_rgno,
xfs_rgnumber_t end_rgno)
{
xfs_rgnumber_t rgno;
for (rgno = first_rgno; rgno < end_rgno; rgno++)
xfs_rtgroup_free(mp, rgno);
}
/* Initialize some range of incore rtgroup objects. */
int
xfs_initialize_rtgroups(
struct xfs_mount *mp,
xfs_rgnumber_t first_rgno,
xfs_rgnumber_t end_rgno,
xfs_rtbxlen_t rextents)
{
xfs_rgnumber_t index;
int error;
if (first_rgno >= end_rgno)
return 0;
for (index = first_rgno; index < end_rgno; index++) {
error = xfs_rtgroup_alloc(mp, index, end_rgno, rextents);
if (error)
goto out_unwind_new_rtgs;
}
return 0;
out_unwind_new_rtgs:
xfs_free_rtgroups(mp, first_rgno, index);
return error;
}
/* Compute the number of rt extents in this realtime group. */
xfs_rtxnum_t
__xfs_rtgroup_extents(
struct xfs_mount *mp,
xfs_rgnumber_t rgno,
xfs_rgnumber_t rgcount,
xfs_rtbxlen_t rextents)
{
ASSERT(rgno < rgcount);
if (rgno == rgcount - 1)
return rextents - ((xfs_rtxnum_t)rgno * mp->m_sb.sb_rgextents);
ASSERT(xfs_has_rtgroups(mp));
return mp->m_sb.sb_rgextents;
}
xfs_rtxnum_t
xfs_rtgroup_extents(
struct xfs_mount *mp,
xfs_rgnumber_t rgno)
{
return __xfs_rtgroup_extents(mp, rgno, mp->m_sb.sb_rgcount,
mp->m_sb.sb_rextents);
}
/*
* Update the rt extent count of the previous tail rtgroup if it changed during
* recovery (i.e. recovery of a growfs).
*/
int
xfs_update_last_rtgroup_size(
struct xfs_mount *mp,
xfs_rgnumber_t prev_rgcount)
{
struct xfs_rtgroup *rtg;
ASSERT(prev_rgcount > 0);
rtg = xfs_rtgroup_grab(mp, prev_rgcount - 1);
if (!rtg)
return -EFSCORRUPTED;
rtg->rtg_extents = __xfs_rtgroup_extents(mp, prev_rgcount - 1,
mp->m_sb.sb_rgcount, mp->m_sb.sb_rextents);
xfs_rtgroup_rele(rtg);
return 0;
}
/* Lock metadata inodes associated with this rt group. */
void
xfs_rtgroup_lock(
struct xfs_rtgroup *rtg,
unsigned int rtglock_flags)
{
ASSERT(!(rtglock_flags & ~XFS_RTGLOCK_ALL_FLAGS));
ASSERT(!(rtglock_flags & XFS_RTGLOCK_BITMAP_SHARED) ||
!(rtglock_flags & XFS_RTGLOCK_BITMAP));
if (rtglock_flags & XFS_RTGLOCK_BITMAP) {
/*
* Lock both realtime free space metadata inodes for a freespace
* update.
*/
xfs_ilock(rtg->rtg_inodes[XFS_RTGI_BITMAP], XFS_ILOCK_EXCL);
xfs_ilock(rtg->rtg_inodes[XFS_RTGI_SUMMARY], XFS_ILOCK_EXCL);
} else if (rtglock_flags & XFS_RTGLOCK_BITMAP_SHARED) {
xfs_ilock(rtg->rtg_inodes[XFS_RTGI_BITMAP], XFS_ILOCK_SHARED);
}
}
/* Unlock metadata inodes associated with this rt group. */
void
xfs_rtgroup_unlock(
struct xfs_rtgroup *rtg,
unsigned int rtglock_flags)
{
ASSERT(!(rtglock_flags & ~XFS_RTGLOCK_ALL_FLAGS));
ASSERT(!(rtglock_flags & XFS_RTGLOCK_BITMAP_SHARED) ||
!(rtglock_flags & XFS_RTGLOCK_BITMAP));
if (rtglock_flags & XFS_RTGLOCK_BITMAP) {
xfs_iunlock(rtg->rtg_inodes[XFS_RTGI_SUMMARY], XFS_ILOCK_EXCL);
xfs_iunlock(rtg->rtg_inodes[XFS_RTGI_BITMAP], XFS_ILOCK_EXCL);
} else if (rtglock_flags & XFS_RTGLOCK_BITMAP_SHARED) {
xfs_iunlock(rtg->rtg_inodes[XFS_RTGI_BITMAP], XFS_ILOCK_SHARED);
}
}
/*
* Join realtime group metadata inodes to the transaction. The ILOCKs will be
* released on transaction commit.
*/
void
xfs_rtgroup_trans_join(
struct xfs_trans *tp,
struct xfs_rtgroup *rtg,
unsigned int rtglock_flags)
{
ASSERT(!(rtglock_flags & ~XFS_RTGLOCK_ALL_FLAGS));
ASSERT(!(rtglock_flags & XFS_RTGLOCK_BITMAP_SHARED));
if (rtglock_flags & XFS_RTGLOCK_BITMAP) {
xfs_trans_ijoin(tp, rtg->rtg_inodes[XFS_RTGI_BITMAP],
XFS_ILOCK_EXCL);
xfs_trans_ijoin(tp, rtg->rtg_inodes[XFS_RTGI_SUMMARY],
XFS_ILOCK_EXCL);
}
}
#ifdef CONFIG_PROVE_LOCKING
static struct lock_class_key xfs_rtginode_lock_class;
static int
xfs_rtginode_ilock_cmp_fn(
const struct lockdep_map *m1,
const struct lockdep_map *m2)
{
const struct xfs_inode *ip1 =
container_of(m1, struct xfs_inode, i_lock.dep_map);
const struct xfs_inode *ip2 =
container_of(m2, struct xfs_inode, i_lock.dep_map);
if (ip1->i_projid < ip2->i_projid)
return -1;
if (ip1->i_projid > ip2->i_projid)
return 1;
return 0;
}
static inline void
xfs_rtginode_ilock_print_fn(
const struct lockdep_map *m)
{
const struct xfs_inode *ip =
container_of(m, struct xfs_inode, i_lock.dep_map);
printk(KERN_CONT " rgno=%u", ip->i_projid);
}
/*
* Most of the time each of the RTG inode locks are only taken one at a time.
* But when committing deferred ops, more than one of a kind can be taken.
* However, deferred rt ops will be committed in rgno order so there is no
* potential for deadlocks. The code here is needed to tell lockdep about this
* order.
*/
static inline void
xfs_rtginode_lockdep_setup(
struct xfs_inode *ip,
xfs_rgnumber_t rgno,
enum xfs_rtg_inodes type)
{
lockdep_set_class_and_subclass(&ip->i_lock, &xfs_rtginode_lock_class,
type);
lock_set_cmp_fn(&ip->i_lock, xfs_rtginode_ilock_cmp_fn,
xfs_rtginode_ilock_print_fn);
}
#else
#define xfs_rtginode_lockdep_setup(ip, rgno, type) do { } while (0)
#endif /* CONFIG_PROVE_LOCKING */
struct xfs_rtginode_ops {
const char *name; /* short name */
enum xfs_metafile_type metafile_type;
/* Does the fs have this feature? */
bool (*enabled)(struct xfs_mount *mp);
/* Create this rtgroup metadata inode and initialize it. */
int (*create)(struct xfs_rtgroup *rtg,
struct xfs_inode *ip,
struct xfs_trans *tp,
bool init);
};
static const struct xfs_rtginode_ops xfs_rtginode_ops[XFS_RTGI_MAX] = {
[XFS_RTGI_BITMAP] = {
.name = "bitmap",
.metafile_type = XFS_METAFILE_RTBITMAP,
.create = xfs_rtbitmap_create,
},
[XFS_RTGI_SUMMARY] = {
.name = "summary",
.metafile_type = XFS_METAFILE_RTSUMMARY,
.create = xfs_rtsummary_create,
},
};
/* Return the shortname of this rtgroup inode. */
const char *
xfs_rtginode_name(
enum xfs_rtg_inodes type)
{
return xfs_rtginode_ops[type].name;
}
/* Return the metafile type of this rtgroup inode. */
enum xfs_metafile_type
xfs_rtginode_metafile_type(
enum xfs_rtg_inodes type)
{
return xfs_rtginode_ops[type].metafile_type;
}
/* Should this rtgroup inode be present? */
bool
xfs_rtginode_enabled(
struct xfs_rtgroup *rtg,
enum xfs_rtg_inodes type)
{
const struct xfs_rtginode_ops *ops = &xfs_rtginode_ops[type];
if (!ops->enabled)
return true;
return ops->enabled(rtg_mount(rtg));
}
/* Load and existing rtgroup inode into the rtgroup structure. */
int
xfs_rtginode_load(
struct xfs_rtgroup *rtg,
enum xfs_rtg_inodes type,
struct xfs_trans *tp)
{
struct xfs_mount *mp = tp->t_mountp;
struct xfs_inode *ip;
const struct xfs_rtginode_ops *ops = &xfs_rtginode_ops[type];
int error;
if (!xfs_rtginode_enabled(rtg, type))
return 0;
if (!xfs_has_rtgroups(mp)) {
xfs_ino_t ino;
switch (type) {
case XFS_RTGI_BITMAP:
ino = mp->m_sb.sb_rbmino;
break;
case XFS_RTGI_SUMMARY:
ino = mp->m_sb.sb_rsumino;
break;
default:
/* None of the other types exist on !rtgroups */
return 0;
}
error = xfs_trans_metafile_iget(tp, ino, ops->metafile_type,
&ip);
} else {
const char *path;
if (!mp->m_rtdirip)
return -EFSCORRUPTED;
path = xfs_rtginode_path(rtg_rgno(rtg), type);
if (!path)
return -ENOMEM;
error = xfs_metadir_load(tp, mp->m_rtdirip, path,
ops->metafile_type, &ip);
kfree(path);
}
if (error)
return error;
if (XFS_IS_CORRUPT(mp, ip->i_df.if_format != XFS_DINODE_FMT_EXTENTS &&
ip->i_df.if_format != XFS_DINODE_FMT_BTREE)) {
xfs_irele(ip);
return -EFSCORRUPTED;
}
if (XFS_IS_CORRUPT(mp, ip->i_projid != rtg_rgno(rtg))) {
xfs_irele(ip);
return -EFSCORRUPTED;
}
xfs_rtginode_lockdep_setup(ip, rtg_rgno(rtg), type);
rtg->rtg_inodes[type] = ip;
return 0;
}
/* Release an rtgroup metadata inode. */
void
xfs_rtginode_irele(
struct xfs_inode **ipp)
{
if (*ipp)
xfs_irele(*ipp);
*ipp = NULL;
}
/* Add a metadata inode for a realtime rmap btree. */
int
xfs_rtginode_create(
struct xfs_rtgroup *rtg,
enum xfs_rtg_inodes type,
bool init)
{
const struct xfs_rtginode_ops *ops = &xfs_rtginode_ops[type];
struct xfs_mount *mp = rtg_mount(rtg);
struct xfs_metadir_update upd = {
.dp = mp->m_rtdirip,
.metafile_type = ops->metafile_type,
};
int error;
if (!xfs_rtginode_enabled(rtg, type))
return 0;
if (!mp->m_rtdirip)
return -EFSCORRUPTED;
upd.path = xfs_rtginode_path(rtg_rgno(rtg), type);
if (!upd.path)
return -ENOMEM;
error = xfs_metadir_start_create(&upd);
if (error)
goto out_path;
error = xfs_metadir_create(&upd, S_IFREG);
if (error)
return error;
xfs_rtginode_lockdep_setup(upd.ip, rtg_rgno(rtg), type);
upd.ip->i_projid = rtg_rgno(rtg);
error = ops->create(rtg, upd.ip, upd.tp, init);
if (error)
goto out_cancel;
error = xfs_metadir_commit(&upd);
if (error)
goto out_path;
kfree(upd.path);
xfs_finish_inode_setup(upd.ip);
rtg->rtg_inodes[type] = upd.ip;
return 0;
out_cancel:
xfs_metadir_cancel(&upd, error);
/* Have to finish setting up the inode to ensure it's deleted. */
if (upd.ip) {
xfs_finish_inode_setup(upd.ip);
xfs_irele(upd.ip);
}
out_path:
kfree(upd.path);
return error;
}
/* Create the parent directory for all rtgroup inodes and load it. */
int
xfs_rtginode_mkdir_parent(
struct xfs_mount *mp)
{
if (!mp->m_metadirip)
return -EFSCORRUPTED;
return xfs_metadir_mkdir(mp->m_metadirip, "rtgroups", &mp->m_rtdirip);
}
/* Load the parent directory of all rtgroup inodes. */
int
xfs_rtginode_load_parent(
struct xfs_trans *tp)
{
struct xfs_mount *mp = tp->t_mountp;
if (!mp->m_metadirip)
return -EFSCORRUPTED;
return xfs_metadir_load(tp, mp->m_metadirip, "rtgroups",
XFS_METAFILE_DIR, &mp->m_rtdirip);
}
|