]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blame - libxfs/xfs_mount.c
xfsprogs: Die dir1 Die!
[thirdparty/xfsprogs-dev.git] / libxfs / xfs_mount.c
CommitLineData
2bd0ea18 1/*
5e656dbb 2 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
da23017d 3 * All Rights Reserved.
2bd0ea18 4 *
da23017d
NS
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
2bd0ea18
NS
7 * published by the Free Software Foundation.
8 *
da23017d
NS
9 * 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.
2bd0ea18 13 *
da23017d
NS
14 * 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
2bd0ea18
NS
17 */
18
19#include <xfs.h>
20
5e656dbb
BN
21static const struct {
22 short offset;
23 short type; /* 0 = integer
24 * 1 = binary / string (no translation)
25 */
2bd0ea18 26} xfs_sb_info[] = {
5e656dbb
BN
27 { offsetof(xfs_sb_t, sb_magicnum), 0 },
28 { offsetof(xfs_sb_t, sb_blocksize), 0 },
29 { offsetof(xfs_sb_t, sb_dblocks), 0 },
30 { offsetof(xfs_sb_t, sb_rblocks), 0 },
31 { offsetof(xfs_sb_t, sb_rextents), 0 },
32 { offsetof(xfs_sb_t, sb_uuid), 1 },
33 { offsetof(xfs_sb_t, sb_logstart), 0 },
34 { offsetof(xfs_sb_t, sb_rootino), 0 },
35 { offsetof(xfs_sb_t, sb_rbmino), 0 },
36 { offsetof(xfs_sb_t, sb_rsumino), 0 },
37 { offsetof(xfs_sb_t, sb_rextsize), 0 },
38 { offsetof(xfs_sb_t, sb_agblocks), 0 },
39 { offsetof(xfs_sb_t, sb_agcount), 0 },
40 { offsetof(xfs_sb_t, sb_rbmblocks), 0 },
41 { offsetof(xfs_sb_t, sb_logblocks), 0 },
2bd0ea18 42 { offsetof(xfs_sb_t, sb_versionnum), 0 },
5e656dbb
BN
43 { offsetof(xfs_sb_t, sb_sectsize), 0 },
44 { offsetof(xfs_sb_t, sb_inodesize), 0 },
45 { offsetof(xfs_sb_t, sb_inopblock), 0 },
46 { offsetof(xfs_sb_t, sb_fname[0]), 1 },
47 { offsetof(xfs_sb_t, sb_blocklog), 0 },
48 { offsetof(xfs_sb_t, sb_sectlog), 0 },
49 { offsetof(xfs_sb_t, sb_inodelog), 0 },
50 { offsetof(xfs_sb_t, sb_inopblog), 0 },
51 { offsetof(xfs_sb_t, sb_agblklog), 0 },
52 { offsetof(xfs_sb_t, sb_rextslog), 0 },
2bd0ea18 53 { offsetof(xfs_sb_t, sb_inprogress), 0 },
5e656dbb
BN
54 { offsetof(xfs_sb_t, sb_imax_pct), 0 },
55 { offsetof(xfs_sb_t, sb_icount), 0 },
56 { offsetof(xfs_sb_t, sb_ifree), 0 },
57 { offsetof(xfs_sb_t, sb_fdblocks), 0 },
58 { offsetof(xfs_sb_t, sb_frextents), 0 },
59 { offsetof(xfs_sb_t, sb_uquotino), 0 },
60 { offsetof(xfs_sb_t, sb_gquotino), 0 },
61 { offsetof(xfs_sb_t, sb_qflags), 0 },
62 { offsetof(xfs_sb_t, sb_flags), 0 },
63 { offsetof(xfs_sb_t, sb_shared_vn), 0 },
2bd0ea18 64 { offsetof(xfs_sb_t, sb_inoalignmt), 0 },
5000d01d
SL
65 { offsetof(xfs_sb_t, sb_unit), 0 },
66 { offsetof(xfs_sb_t, sb_width), 0 },
67 { offsetof(xfs_sb_t, sb_dirblklog), 0 },
9440d84d
NS
68 { offsetof(xfs_sb_t, sb_logsectlog), 0 },
69 { offsetof(xfs_sb_t, sb_logsectsize),0 },
5000d01d 70 { offsetof(xfs_sb_t, sb_logsunit), 0 },
2999b9c1 71 { offsetof(xfs_sb_t, sb_features2), 0 },
45935737 72 { offsetof(xfs_sb_t, sb_bad_features2), 0 },
5000d01d 73 { sizeof(xfs_sb_t), 0 }
2bd0ea18
NS
74};
75
56b2de80
DC
76/*
77 * Reference counting access wrappers to the perag structures.
78 * Because we never free per-ag structures, the only thing we
79 * have to protect against changes is the tree structure itself.
80 */
81struct xfs_perag *
82xfs_perag_get(struct xfs_mount *mp, xfs_agnumber_t agno)
a33a9e62 83{
56b2de80
DC
84 struct xfs_perag *pag;
85 int ref = 0;
86
87 rcu_read_lock();
88 pag = radix_tree_lookup(&mp->m_perag_tree, agno);
89 if (pag) {
90 ASSERT(atomic_read(&pag->pag_ref) >= 0);
91 ref = atomic_inc_return(&pag->pag_ref);
a33a9e62 92 }
56b2de80
DC
93 trace_xfs_perag_get(mp, agno, ref, _RET_IP_);
94 rcu_read_unlock();
95 return pag;
96}
a33a9e62 97
56b2de80
DC
98void
99xfs_perag_put(struct xfs_perag *pag)
100{
101 int ref;
a33a9e62 102
56b2de80
DC
103 ASSERT(atomic_read(&pag->pag_ref) > 0);
104 ref = atomic_dec_return(&pag->pag_ref);
105 trace_xfs_perag_put(pag->pag_mount, pag->pag_agno, ref, _RET_IP_);
a33a9e62 106}
cdded3d8 107
5e656dbb
BN
108void
109xfs_sb_from_disk(
110 xfs_sb_t *to,
111 xfs_dsb_t *from)
112{
113 to->sb_magicnum = be32_to_cpu(from->sb_magicnum);
114 to->sb_blocksize = be32_to_cpu(from->sb_blocksize);
115 to->sb_dblocks = be64_to_cpu(from->sb_dblocks);
116 to->sb_rblocks = be64_to_cpu(from->sb_rblocks);
117 to->sb_rextents = be64_to_cpu(from->sb_rextents);
118 memcpy(&to->sb_uuid, &from->sb_uuid, sizeof(to->sb_uuid));
119 to->sb_logstart = be64_to_cpu(from->sb_logstart);
120 to->sb_rootino = be64_to_cpu(from->sb_rootino);
121 to->sb_rbmino = be64_to_cpu(from->sb_rbmino);
122 to->sb_rsumino = be64_to_cpu(from->sb_rsumino);
123 to->sb_rextsize = be32_to_cpu(from->sb_rextsize);
124 to->sb_agblocks = be32_to_cpu(from->sb_agblocks);
125 to->sb_agcount = be32_to_cpu(from->sb_agcount);
126 to->sb_rbmblocks = be32_to_cpu(from->sb_rbmblocks);
127 to->sb_logblocks = be32_to_cpu(from->sb_logblocks);
128 to->sb_versionnum = be16_to_cpu(from->sb_versionnum);
129 to->sb_sectsize = be16_to_cpu(from->sb_sectsize);
130 to->sb_inodesize = be16_to_cpu(from->sb_inodesize);
131 to->sb_inopblock = be16_to_cpu(from->sb_inopblock);
132 memcpy(&to->sb_fname, &from->sb_fname, sizeof(to->sb_fname));
133 to->sb_blocklog = from->sb_blocklog;
134 to->sb_sectlog = from->sb_sectlog;
135 to->sb_inodelog = from->sb_inodelog;
136 to->sb_inopblog = from->sb_inopblog;
137 to->sb_agblklog = from->sb_agblklog;
138 to->sb_rextslog = from->sb_rextslog;
139 to->sb_inprogress = from->sb_inprogress;
140 to->sb_imax_pct = from->sb_imax_pct;
141 to->sb_icount = be64_to_cpu(from->sb_icount);
142 to->sb_ifree = be64_to_cpu(from->sb_ifree);
143 to->sb_fdblocks = be64_to_cpu(from->sb_fdblocks);
144 to->sb_frextents = be64_to_cpu(from->sb_frextents);
145 to->sb_uquotino = be64_to_cpu(from->sb_uquotino);
146 to->sb_gquotino = be64_to_cpu(from->sb_gquotino);
147 to->sb_qflags = be16_to_cpu(from->sb_qflags);
148 to->sb_flags = from->sb_flags;
149 to->sb_shared_vn = from->sb_shared_vn;
150 to->sb_inoalignmt = be32_to_cpu(from->sb_inoalignmt);
151 to->sb_unit = be32_to_cpu(from->sb_unit);
152 to->sb_width = be32_to_cpu(from->sb_width);
153 to->sb_dirblklog = from->sb_dirblklog;
154 to->sb_logsectlog = from->sb_logsectlog;
155 to->sb_logsectsize = be16_to_cpu(from->sb_logsectsize);
156 to->sb_logsunit = be32_to_cpu(from->sb_logsunit);
157 to->sb_features2 = be32_to_cpu(from->sb_features2);
158 to->sb_bad_features2 = be32_to_cpu(from->sb_bad_features2);
159}
160
161/*
162 * Copy in core superblock to ondisk one.
163 *
164 * The fields argument is mask of superblock fields to copy.
165 */
166void
167xfs_sb_to_disk(
168 xfs_dsb_t *to,
169 xfs_sb_t *from,
170 __int64_t fields)
171{
172 xfs_caddr_t to_ptr = (xfs_caddr_t)to;
173 xfs_caddr_t from_ptr = (xfs_caddr_t)from;
174 xfs_sb_field_t f;
175 int first;
176 int size;
177
178 ASSERT(fields);
179 if (!fields)
180 return;
181
182 while (fields) {
183 f = (xfs_sb_field_t)xfs_lowbit64((__uint64_t)fields);
184 first = xfs_sb_info[f].offset;
185 size = xfs_sb_info[f + 1].offset - first;
186
187 ASSERT(xfs_sb_info[f].type == 0 || xfs_sb_info[f].type == 1);
188
189 if (size == 1 || xfs_sb_info[f].type == 1) {
190 memcpy(to_ptr + first, from_ptr + first, size);
191 } else {
192 switch (size) {
193 case 2:
194 *(__be16 *)(to_ptr + first) =
195 cpu_to_be16(*(__u16 *)(from_ptr + first));
196 break;
197 case 4:
198 *(__be32 *)(to_ptr + first) =
199 cpu_to_be32(*(__u32 *)(from_ptr + first));
200 break;
201 case 8:
202 *(__be64 *)(to_ptr + first) =
203 cpu_to_be64(*(__u64 *)(from_ptr + first));
204 break;
205 default:
206 ASSERT(0);
207 }
208 }
209
210 fields &= ~(1LL << f);
211 }
212}
213
214/*
215 * xfs_mount_common
216 *
217 * Mount initialization code establishing various mount
218 * fields from the superblock associated with the given
219 * mount structure
220 *
221 * Note: this requires user-space public scope for libxfs_mount
222 */
223void
224xfs_mount_common(xfs_mount_t *mp, xfs_sb_t *sbp)
225{
5e656dbb
BN
226 mp->m_agfrotor = mp->m_agirotor = 0;
227 spin_lock_init(&mp->m_agirotor_lock);
228 mp->m_maxagi = mp->m_sb.sb_agcount;
229 mp->m_blkbit_log = sbp->sb_blocklog + XFS_NBBYLOG;
230 mp->m_blkbb_log = sbp->sb_blocklog - BBSHIFT;
231 mp->m_sectbb_log = sbp->sb_sectlog - BBSHIFT;
232 mp->m_agno_log = xfs_highbit32(sbp->sb_agcount - 1) + 1;
233 mp->m_agino_log = sbp->sb_inopblog + sbp->sb_agblklog;
5e656dbb
BN
234 mp->m_blockmask = sbp->sb_blocksize - 1;
235 mp->m_blockwsize = sbp->sb_blocksize >> XFS_WORDLOG;
236 mp->m_blockwmask = mp->m_blockwsize - 1;
5e656dbb 237
b3563c19
BN
238 mp->m_alloc_mxr[0] = xfs_allocbt_maxrecs(mp, sbp->sb_blocksize, 1);
239 mp->m_alloc_mxr[1] = xfs_allocbt_maxrecs(mp, sbp->sb_blocksize, 0);
240 mp->m_alloc_mnr[0] = mp->m_alloc_mxr[0] / 2;
241 mp->m_alloc_mnr[1] = mp->m_alloc_mxr[1] / 2;
242
243 mp->m_inobt_mxr[0] = xfs_inobt_maxrecs(mp, sbp->sb_blocksize, 1);
244 mp->m_inobt_mxr[1] = xfs_inobt_maxrecs(mp, sbp->sb_blocksize, 0);
245 mp->m_inobt_mnr[0] = mp->m_inobt_mxr[0] / 2;
246 mp->m_inobt_mnr[1] = mp->m_inobt_mxr[1] / 2;
247
248 mp->m_bmap_dmxr[0] = xfs_bmbt_maxrecs(mp, sbp->sb_blocksize, 1);
249 mp->m_bmap_dmxr[1] = xfs_bmbt_maxrecs(mp, sbp->sb_blocksize, 0);
250 mp->m_bmap_dmnr[0] = mp->m_bmap_dmxr[0] / 2;
251 mp->m_bmap_dmnr[1] = mp->m_bmap_dmxr[1] / 2;
5e656dbb
BN
252
253 mp->m_bsize = XFS_FSB_TO_BB(mp, 1);
254 mp->m_ialloc_inos = (int)MAX((__uint16_t)XFS_INODES_PER_CHUNK,
255 sbp->sb_inopblock);
256 mp->m_ialloc_blks = mp->m_ialloc_inos >> sbp->sb_inopblog;
257}
258
cdded3d8
DC
259/*
260 * xfs_initialize_perag_data
261 *
262 * Read in each per-ag structure so we can count up the number of
263 * allocated inodes, free inodes and used filesystem blocks as this
264 * information is no longer persistent in the superblock. Once we have
265 * this information, write it into the in-core superblock structure.
5e656dbb
BN
266 *
267 * Note: this requires user-space public scope for libxfs_mount
cdded3d8 268 */
5e656dbb 269int
cdded3d8
DC
270xfs_initialize_perag_data(xfs_mount_t *mp, xfs_agnumber_t agcount)
271{
272 xfs_agnumber_t index;
273 xfs_perag_t *pag;
274 xfs_sb_t *sbp = &mp->m_sb;
5e656dbb
BN
275 uint64_t ifree = 0;
276 uint64_t ialloc = 0;
277 uint64_t bfree = 0;
278 uint64_t bfreelst = 0;
279 uint64_t btree = 0;
cdded3d8 280 int error;
cdded3d8
DC
281
282 for (index = 0; index < agcount; index++) {
283 /*
284 * read the agf, then the agi. This gets us
56b2de80 285 * all the information we need and populates the
cdded3d8
DC
286 * per-ag structures for us.
287 */
288 error = xfs_alloc_pagf_init(mp, NULL, index, 0);
289 if (error)
290 return error;
291
292 error = xfs_ialloc_pagi_init(mp, NULL, index);
293 if (error)
294 return error;
56b2de80 295 pag = xfs_perag_get(mp, index);
cdded3d8
DC
296 ifree += pag->pagi_freecount;
297 ialloc += pag->pagi_count;
298 bfree += pag->pagf_freeblks;
299 bfreelst += pag->pagf_flcount;
300 btree += pag->pagf_btreeblks;
56b2de80 301 xfs_perag_put(pag);
cdded3d8
DC
302 }
303 /*
304 * Overwrite incore superblock counters with just-read data
305 */
5e656dbb 306 spin_lock(&mp->m_sb_lock);
cdded3d8
DC
307 sbp->sb_ifree = ifree;
308 sbp->sb_icount = ialloc;
309 sbp->sb_fdblocks = bfree + bfreelst + btree;
5e656dbb
BN
310 spin_unlock(&mp->m_sb_lock);
311
312 /* Fixup the per-cpu counters as well. */
313 xfs_icsb_reinit_counters(mp);
314
cdded3d8
DC
315 return 0;
316}
5e656dbb
BN
317
318/*
319 * xfs_mod_sb() can be used to copy arbitrary changes to the
320 * in-core superblock into the superblock buffer to be logged.
321 * It does not provide the higher level of locking that is
322 * needed to protect the in-core superblock from concurrent
323 * access.
324 */
325void
326xfs_mod_sb(xfs_trans_t *tp, __int64_t fields)
327{
328 xfs_buf_t *bp;
329 int first;
330 int last;
331 xfs_mount_t *mp;
332 xfs_sb_field_t f;
333
334 ASSERT(fields);
335 if (!fields)
336 return;
337 mp = tp->t_mountp;
338 bp = xfs_trans_getsb(tp, mp, 0);
339 first = sizeof(xfs_sb_t);
340 last = 0;
341
342 /* translate/copy */
5e656dbb
BN
343 xfs_sb_to_disk(XFS_BUF_TO_SBP(bp), &mp->m_sb, fields);
344
345 /* find modified range */
56b2de80
DC
346 f = (xfs_sb_field_t)xfs_highbit64((__uint64_t)fields);
347 ASSERT((1LL << f) & XFS_SB_MOD_BITS);
348 last = xfs_sb_info[f + 1].offset - 1;
5e656dbb
BN
349
350 f = (xfs_sb_field_t)xfs_lowbit64((__uint64_t)fields);
351 ASSERT((1LL << f) & XFS_SB_MOD_BITS);
352 first = xfs_sb_info[f].offset;
353
5e656dbb
BN
354 xfs_trans_log_buf(tp, bp, first, last);
355}