blob: 9378408a69d50f3b1e8acd289aec82142662bb84 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * 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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110019#include "xfs_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "xfs_types.h"
Nathan Scotta844f452005-11-02 14:38:42 +110021#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "xfs_log.h"
Nathan Scotta844f452005-11-02 14:38:42 +110023#include "xfs_inum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "xfs_trans.h"
25#include "xfs_sb.h"
26#include "xfs_ag.h"
27#include "xfs_dir.h"
28#include "xfs_dir2.h"
29#include "xfs_dmapi.h"
30#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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "xfs_dir_sf.h"
35#include "xfs_dir2_sf.h"
Nathan Scotta844f452005-11-02 14:38:42 +110036#include "xfs_attr_sf.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include "xfs_dinode.h"
38#include "xfs_inode.h"
Nathan Scotta844f452005-11-02 14:38:42 +110039#include "xfs_btree.h"
40#include "xfs_ialloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include "xfs_alloc.h"
42#include "xfs_rtalloc.h"
43#include "xfs_bmap.h"
44#include "xfs_error.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include "xfs_rw.h"
46#include "xfs_quota.h"
47#include "xfs_fsops.h"
48
49STATIC void xfs_mount_log_sbunit(xfs_mount_t *, __int64_t);
50STATIC int xfs_uuid_mount(xfs_mount_t *);
51STATIC void xfs_uuid_unmount(xfs_mount_t *mp);
Christoph Hellwigba0f32d2005-06-21 15:36:52 +100052STATIC void xfs_unmountfs_wait(xfs_mount_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
David Chinner8d280b92006-03-14 13:13:09 +110054
55#ifdef HAVE_PERCPU_SB
56STATIC void xfs_icsb_destroy_counters(xfs_mount_t *);
57STATIC void xfs_icsb_balance_counter(xfs_mount_t *, xfs_sb_field_t, int);
58STATIC void xfs_icsb_sync_counters(xfs_mount_t *);
59STATIC int xfs_icsb_modify_counters(xfs_mount_t *, xfs_sb_field_t,
60 int, int);
61STATIC int xfs_icsb_modify_counters_locked(xfs_mount_t *, xfs_sb_field_t,
62 int, int);
David Chinnere8234a62006-03-14 13:23:52 +110063STATIC int xfs_icsb_disable_counter(xfs_mount_t *, xfs_sb_field_t);
David Chinner8d280b92006-03-14 13:13:09 +110064
65#else
66
67#define xfs_icsb_destroy_counters(mp) do { } while (0)
68#define xfs_icsb_balance_counter(mp, a, b) do { } while (0)
69#define xfs_icsb_sync_counters(mp) do { } while (0)
70#define xfs_icsb_modify_counters(mp, a, b, c) do { } while (0)
71#define xfs_icsb_modify_counters_locked(mp, a, b, c) do { } while (0)
72
73#endif
74
Christoph Hellwig1df84c92006-01-11 15:29:52 +110075static const struct {
David Chinner8d280b92006-03-14 13:13:09 +110076 short offset;
77 short type; /* 0 = integer
78 * 1 = binary / string (no translation)
79 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070080} xfs_sb_info[] = {
81 { offsetof(xfs_sb_t, sb_magicnum), 0 },
82 { offsetof(xfs_sb_t, sb_blocksize), 0 },
83 { offsetof(xfs_sb_t, sb_dblocks), 0 },
84 { offsetof(xfs_sb_t, sb_rblocks), 0 },
85 { offsetof(xfs_sb_t, sb_rextents), 0 },
86 { offsetof(xfs_sb_t, sb_uuid), 1 },
87 { offsetof(xfs_sb_t, sb_logstart), 0 },
88 { offsetof(xfs_sb_t, sb_rootino), 0 },
89 { offsetof(xfs_sb_t, sb_rbmino), 0 },
90 { offsetof(xfs_sb_t, sb_rsumino), 0 },
91 { offsetof(xfs_sb_t, sb_rextsize), 0 },
92 { offsetof(xfs_sb_t, sb_agblocks), 0 },
93 { offsetof(xfs_sb_t, sb_agcount), 0 },
94 { offsetof(xfs_sb_t, sb_rbmblocks), 0 },
95 { offsetof(xfs_sb_t, sb_logblocks), 0 },
96 { offsetof(xfs_sb_t, sb_versionnum), 0 },
97 { offsetof(xfs_sb_t, sb_sectsize), 0 },
98 { offsetof(xfs_sb_t, sb_inodesize), 0 },
99 { offsetof(xfs_sb_t, sb_inopblock), 0 },
100 { offsetof(xfs_sb_t, sb_fname[0]), 1 },
101 { offsetof(xfs_sb_t, sb_blocklog), 0 },
102 { offsetof(xfs_sb_t, sb_sectlog), 0 },
103 { offsetof(xfs_sb_t, sb_inodelog), 0 },
104 { offsetof(xfs_sb_t, sb_inopblog), 0 },
105 { offsetof(xfs_sb_t, sb_agblklog), 0 },
106 { offsetof(xfs_sb_t, sb_rextslog), 0 },
107 { offsetof(xfs_sb_t, sb_inprogress), 0 },
108 { offsetof(xfs_sb_t, sb_imax_pct), 0 },
109 { offsetof(xfs_sb_t, sb_icount), 0 },
110 { offsetof(xfs_sb_t, sb_ifree), 0 },
111 { offsetof(xfs_sb_t, sb_fdblocks), 0 },
112 { offsetof(xfs_sb_t, sb_frextents), 0 },
113 { offsetof(xfs_sb_t, sb_uquotino), 0 },
114 { offsetof(xfs_sb_t, sb_gquotino), 0 },
115 { offsetof(xfs_sb_t, sb_qflags), 0 },
116 { offsetof(xfs_sb_t, sb_flags), 0 },
117 { offsetof(xfs_sb_t, sb_shared_vn), 0 },
118 { offsetof(xfs_sb_t, sb_inoalignmt), 0 },
119 { offsetof(xfs_sb_t, sb_unit), 0 },
120 { offsetof(xfs_sb_t, sb_width), 0 },
121 { offsetof(xfs_sb_t, sb_dirblklog), 0 },
122 { offsetof(xfs_sb_t, sb_logsectlog), 0 },
123 { offsetof(xfs_sb_t, sb_logsectsize),0 },
124 { offsetof(xfs_sb_t, sb_logsunit), 0 },
125 { offsetof(xfs_sb_t, sb_features2), 0 },
126 { sizeof(xfs_sb_t), 0 }
127};
128
129/*
130 * Return a pointer to an initialized xfs_mount structure.
131 */
132xfs_mount_t *
133xfs_mount_init(void)
134{
135 xfs_mount_t *mp;
136
David Chinner8d280b92006-03-14 13:13:09 +1100137 mp = kmem_zalloc(sizeof(xfs_mount_t), KM_SLEEP);
138
139 if (xfs_icsb_init_counters(mp)) {
140 mp->m_flags |= XFS_MOUNT_NO_PERCPU_SB;
141 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
143 AIL_LOCKINIT(&mp->m_ail_lock, "xfs_ail");
144 spinlock_init(&mp->m_sb_lock, "xfs_sb");
Jes Sorensen794ee1b2006-01-09 15:59:21 -0800145 mutex_init(&mp->m_ilock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 initnsema(&mp->m_growlock, 1, "xfs_grow");
147 /*
148 * Initialize the AIL.
149 */
150 xfs_trans_ail_init(mp);
151
152 atomic_set(&mp->m_active_trans, 0);
153
154 return mp;
155}
156
157/*
158 * Free up the resources associated with a mount structure. Assume that
159 * the structure was initially zeroed, so we can tell which fields got
160 * initialized.
161 */
162void
163xfs_mount_free(
David Chinner8d280b92006-03-14 13:13:09 +1100164 xfs_mount_t *mp,
165 int remove_bhv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
167 if (mp->m_ihash)
168 xfs_ihash_free(mp);
169 if (mp->m_chash)
170 xfs_chash_free(mp);
171
172 if (mp->m_perag) {
173 int agno;
174
175 for (agno = 0; agno < mp->m_maxagi; agno++)
176 if (mp->m_perag[agno].pagb_list)
177 kmem_free(mp->m_perag[agno].pagb_list,
178 sizeof(xfs_perag_busy_t) *
179 XFS_PAGB_NUM_SLOTS);
180 kmem_free(mp->m_perag,
181 sizeof(xfs_perag_t) * mp->m_sb.sb_agcount);
182 }
183
184 AIL_LOCK_DESTROY(&mp->m_ail_lock);
185 spinlock_destroy(&mp->m_sb_lock);
186 mutex_destroy(&mp->m_ilock);
187 freesema(&mp->m_growlock);
188 if (mp->m_quotainfo)
189 XFS_QM_DONE(mp);
190
191 if (mp->m_fsname != NULL)
192 kmem_free(mp->m_fsname, mp->m_fsname_len);
Nathan Scottfc1f8c12005-11-02 11:44:33 +1100193 if (mp->m_rtname != NULL)
194 kmem_free(mp->m_rtname, strlen(mp->m_rtname) + 1);
195 if (mp->m_logname != NULL)
196 kmem_free(mp->m_logname, strlen(mp->m_logname) + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
198 if (remove_bhv) {
199 struct vfs *vfsp = XFS_MTOVFS(mp);
200
201 bhv_remove_all_vfsops(vfsp, 0);
202 VFS_REMOVEBHV(vfsp, &mp->m_bhv);
203 }
204
David Chinner8d280b92006-03-14 13:13:09 +1100205 xfs_icsb_destroy_counters(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 kmem_free(mp, sizeof(xfs_mount_t));
207}
208
209
210/*
211 * Check the validity of the SB found.
212 */
213STATIC int
214xfs_mount_validate_sb(
215 xfs_mount_t *mp,
Nathan Scott764d1f82006-03-31 13:04:17 +1000216 xfs_sb_t *sbp,
217 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218{
219 /*
220 * If the log device and data device have the
221 * same device number, the log is internal.
222 * Consequently, the sb_logstart should be non-zero. If
223 * we have a zero sb_logstart in this case, we may be trying to mount
224 * a volume filesystem in a non-volume manner.
225 */
226 if (sbp->sb_magicnum != XFS_SB_MAGIC) {
Nathan Scott764d1f82006-03-31 13:04:17 +1000227 xfs_fs_mount_cmn_err(flags, "bad magic number");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 return XFS_ERROR(EWRONGFS);
229 }
230
231 if (!XFS_SB_GOOD_VERSION(sbp)) {
Nathan Scott764d1f82006-03-31 13:04:17 +1000232 xfs_fs_mount_cmn_err(flags, "bad version");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 return XFS_ERROR(EWRONGFS);
234 }
235
236 if (unlikely(
237 sbp->sb_logstart == 0 && mp->m_logdev_targp == mp->m_ddev_targp)) {
Nathan Scott764d1f82006-03-31 13:04:17 +1000238 xfs_fs_mount_cmn_err(flags,
239 "filesystem is marked as having an external log; "
240 "specify logdev on the\nmount command line.");
241 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 }
243
244 if (unlikely(
245 sbp->sb_logstart != 0 && mp->m_logdev_targp != mp->m_ddev_targp)) {
Nathan Scott764d1f82006-03-31 13:04:17 +1000246 xfs_fs_mount_cmn_err(flags,
247 "filesystem is marked as having an internal log; "
248 "do not specify logdev on\nthe mount command line.");
249 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 }
251
252 /*
253 * More sanity checking. These were stolen directly from
254 * xfs_repair.
255 */
256 if (unlikely(
257 sbp->sb_agcount <= 0 ||
258 sbp->sb_sectsize < XFS_MIN_SECTORSIZE ||
259 sbp->sb_sectsize > XFS_MAX_SECTORSIZE ||
260 sbp->sb_sectlog < XFS_MIN_SECTORSIZE_LOG ||
261 sbp->sb_sectlog > XFS_MAX_SECTORSIZE_LOG ||
262 sbp->sb_blocksize < XFS_MIN_BLOCKSIZE ||
263 sbp->sb_blocksize > XFS_MAX_BLOCKSIZE ||
264 sbp->sb_blocklog < XFS_MIN_BLOCKSIZE_LOG ||
265 sbp->sb_blocklog > XFS_MAX_BLOCKSIZE_LOG ||
266 sbp->sb_inodesize < XFS_DINODE_MIN_SIZE ||
267 sbp->sb_inodesize > XFS_DINODE_MAX_SIZE ||
Nathan Scott9f989c92006-03-14 13:29:32 +1100268 sbp->sb_inodelog < XFS_DINODE_MIN_LOG ||
269 sbp->sb_inodelog > XFS_DINODE_MAX_LOG ||
270 (sbp->sb_blocklog - sbp->sb_inodelog != sbp->sb_inopblog) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 (sbp->sb_rextsize * sbp->sb_blocksize > XFS_MAX_RTEXTSIZE) ||
272 (sbp->sb_rextsize * sbp->sb_blocksize < XFS_MIN_RTEXTSIZE) ||
Nathan Scotte50bd162006-04-11 15:10:45 +1000273 (sbp->sb_imax_pct > 100 /* zero sb_imax_pct is valid */))) {
Nathan Scott764d1f82006-03-31 13:04:17 +1000274 xfs_fs_mount_cmn_err(flags, "SB sanity check 1 failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 return XFS_ERROR(EFSCORRUPTED);
276 }
277
278 /*
279 * Sanity check AG count, size fields against data size field
280 */
281 if (unlikely(
282 sbp->sb_dblocks == 0 ||
283 sbp->sb_dblocks >
284 (xfs_drfsbno_t)sbp->sb_agcount * sbp->sb_agblocks ||
285 sbp->sb_dblocks < (xfs_drfsbno_t)(sbp->sb_agcount - 1) *
286 sbp->sb_agblocks + XFS_MIN_AG_BLOCKS)) {
Nathan Scott764d1f82006-03-31 13:04:17 +1000287 xfs_fs_mount_cmn_err(flags, "SB sanity check 2 failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 return XFS_ERROR(EFSCORRUPTED);
289 }
290
291 ASSERT(PAGE_SHIFT >= sbp->sb_blocklog);
292 ASSERT(sbp->sb_blocklog >= BBSHIFT);
293
294#if XFS_BIG_BLKNOS /* Limited by ULONG_MAX of page cache index */
295 if (unlikely(
296 (sbp->sb_dblocks >> (PAGE_SHIFT - sbp->sb_blocklog)) > ULONG_MAX ||
297 (sbp->sb_rblocks >> (PAGE_SHIFT - sbp->sb_blocklog)) > ULONG_MAX)) {
298#else /* Limited by UINT_MAX of sectors */
299 if (unlikely(
300 (sbp->sb_dblocks << (sbp->sb_blocklog - BBSHIFT)) > UINT_MAX ||
301 (sbp->sb_rblocks << (sbp->sb_blocklog - BBSHIFT)) > UINT_MAX)) {
302#endif
Nathan Scott764d1f82006-03-31 13:04:17 +1000303 xfs_fs_mount_cmn_err(flags,
304 "file system too large to be mounted on this system.");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 return XFS_ERROR(E2BIG);
306 }
307
308 if (unlikely(sbp->sb_inprogress)) {
Nathan Scott764d1f82006-03-31 13:04:17 +1000309 xfs_fs_mount_cmn_err(flags, "file system busy");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 return XFS_ERROR(EFSCORRUPTED);
311 }
312
313 /*
Nathan Scottde206142005-05-05 13:24:13 -0700314 * Version 1 directory format has never worked on Linux.
315 */
316 if (unlikely(!XFS_SB_VERSION_HASDIRV2(sbp))) {
Nathan Scott764d1f82006-03-31 13:04:17 +1000317 xfs_fs_mount_cmn_err(flags,
318 "file system using version 1 directory format");
Nathan Scottde206142005-05-05 13:24:13 -0700319 return XFS_ERROR(ENOSYS);
320 }
321
322 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 * Until this is fixed only page-sized or smaller data blocks work.
324 */
325 if (unlikely(sbp->sb_blocksize > PAGE_SIZE)) {
Nathan Scott764d1f82006-03-31 13:04:17 +1000326 xfs_fs_mount_cmn_err(flags,
327 "file system with blocksize %d bytes",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 sbp->sb_blocksize);
Nathan Scott764d1f82006-03-31 13:04:17 +1000329 xfs_fs_mount_cmn_err(flags,
330 "only pagesize (%ld) or less will currently work.",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 PAGE_SIZE);
332 return XFS_ERROR(ENOSYS);
333 }
334
335 return 0;
336}
337
338xfs_agnumber_t
Nathan Scottc11e2c32005-11-02 15:11:45 +1100339xfs_initialize_perag(
340 struct vfs *vfs,
341 xfs_mount_t *mp,
342 xfs_agnumber_t agcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343{
344 xfs_agnumber_t index, max_metadata;
345 xfs_perag_t *pag;
346 xfs_agino_t agino;
347 xfs_ino_t ino;
348 xfs_sb_t *sbp = &mp->m_sb;
349 xfs_ino_t max_inum = XFS_MAXINUMBER_32;
350
351 /* Check to see if the filesystem can overflow 32 bit inodes */
352 agino = XFS_OFFBNO_TO_AGINO(mp, sbp->sb_agblocks - 1, 0);
353 ino = XFS_AGINO_TO_INO(mp, agcount - 1, agino);
354
355 /* Clear the mount flag if no inode can overflow 32 bits
356 * on this filesystem, or if specifically requested..
357 */
Nathan Scottc11e2c32005-11-02 15:11:45 +1100358 if ((vfs->vfs_flag & VFS_32BITINODES) && ino > max_inum) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 mp->m_flags |= XFS_MOUNT_32BITINODES;
360 } else {
361 mp->m_flags &= ~XFS_MOUNT_32BITINODES;
362 }
363
364 /* If we can overflow then setup the ag headers accordingly */
365 if (mp->m_flags & XFS_MOUNT_32BITINODES) {
366 /* Calculate how much should be reserved for inodes to
367 * meet the max inode percentage.
368 */
369 if (mp->m_maxicount) {
370 __uint64_t icount;
371
372 icount = sbp->sb_dblocks * sbp->sb_imax_pct;
373 do_div(icount, 100);
374 icount += sbp->sb_agblocks - 1;
Eric Sandeena749ee82005-11-02 15:13:42 +1100375 do_div(icount, sbp->sb_agblocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 max_metadata = icount;
377 } else {
378 max_metadata = agcount;
379 }
380 for (index = 0; index < agcount; index++) {
381 ino = XFS_AGINO_TO_INO(mp, index, agino);
382 if (ino > max_inum) {
383 index++;
384 break;
385 }
386
Nathan Scottc41564b2006-03-29 08:55:14 +1000387 /* This ag is preferred for inodes */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 pag = &mp->m_perag[index];
389 pag->pagi_inodeok = 1;
390 if (index < max_metadata)
391 pag->pagf_metadata = 1;
392 }
393 } else {
394 /* Setup default behavior for smaller filesystems */
395 for (index = 0; index < agcount; index++) {
396 pag = &mp->m_perag[index];
397 pag->pagi_inodeok = 1;
398 }
399 }
400 return index;
401}
402
403/*
404 * xfs_xlatesb
405 *
406 * data - on disk version of sb
407 * sb - a superblock
408 * dir - conversion direction: <0 - convert sb to buf
409 * >0 - convert buf to sb
410 * fields - which fields to copy (bitmask)
411 */
412void
413xfs_xlatesb(
414 void *data,
415 xfs_sb_t *sb,
416 int dir,
417 __int64_t fields)
418{
419 xfs_caddr_t buf_ptr;
420 xfs_caddr_t mem_ptr;
421 xfs_sb_field_t f;
422 int first;
423 int size;
424
425 ASSERT(dir);
426 ASSERT(fields);
427
428 if (!fields)
429 return;
430
431 buf_ptr = (xfs_caddr_t)data;
432 mem_ptr = (xfs_caddr_t)sb;
433
434 while (fields) {
435 f = (xfs_sb_field_t)xfs_lowbit64((__uint64_t)fields);
436 first = xfs_sb_info[f].offset;
437 size = xfs_sb_info[f + 1].offset - first;
438
439 ASSERT(xfs_sb_info[f].type == 0 || xfs_sb_info[f].type == 1);
440
441 if (size == 1 || xfs_sb_info[f].type == 1) {
442 if (dir > 0) {
443 memcpy(mem_ptr + first, buf_ptr + first, size);
444 } else {
445 memcpy(buf_ptr + first, mem_ptr + first, size);
446 }
447 } else {
448 switch (size) {
449 case 2:
450 INT_XLATE(*(__uint16_t*)(buf_ptr+first),
451 *(__uint16_t*)(mem_ptr+first),
452 dir, ARCH_CONVERT);
453 break;
454 case 4:
455 INT_XLATE(*(__uint32_t*)(buf_ptr+first),
456 *(__uint32_t*)(mem_ptr+first),
457 dir, ARCH_CONVERT);
458 break;
459 case 8:
460 INT_XLATE(*(__uint64_t*)(buf_ptr+first),
461 *(__uint64_t*)(mem_ptr+first), dir, ARCH_CONVERT);
462 break;
463 default:
464 ASSERT(0);
465 }
466 }
467
468 fields &= ~(1LL << f);
469 }
470}
471
472/*
473 * xfs_readsb
474 *
475 * Does the initial read of the superblock.
476 */
477int
Nathan Scott764d1f82006-03-31 13:04:17 +1000478xfs_readsb(xfs_mount_t *mp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479{
480 unsigned int sector_size;
481 unsigned int extra_flags;
482 xfs_buf_t *bp;
483 xfs_sb_t *sbp;
484 int error;
485
486 ASSERT(mp->m_sb_bp == NULL);
487 ASSERT(mp->m_ddev_targp != NULL);
488
489 /*
490 * Allocate a (locked) buffer to hold the superblock.
491 * This will be kept around at all times to optimize
492 * access to the superblock.
493 */
494 sector_size = xfs_getsize_buftarg(mp->m_ddev_targp);
495 extra_flags = XFS_BUF_LOCK | XFS_BUF_MANAGE | XFS_BUF_MAPPED;
496
497 bp = xfs_buf_read_flags(mp->m_ddev_targp, XFS_SB_DADDR,
498 BTOBB(sector_size), extra_flags);
499 if (!bp || XFS_BUF_ISERROR(bp)) {
Nathan Scott764d1f82006-03-31 13:04:17 +1000500 xfs_fs_mount_cmn_err(flags, "SB read failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 error = bp ? XFS_BUF_GETERROR(bp) : ENOMEM;
502 goto fail;
503 }
504 ASSERT(XFS_BUF_ISBUSY(bp));
505 ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
506
507 /*
508 * Initialize the mount structure from the superblock.
509 * But first do some basic consistency checking.
510 */
511 sbp = XFS_BUF_TO_SBP(bp);
512 xfs_xlatesb(XFS_BUF_PTR(bp), &(mp->m_sb), 1, XFS_SB_ALL_BITS);
513
Nathan Scott764d1f82006-03-31 13:04:17 +1000514 error = xfs_mount_validate_sb(mp, &(mp->m_sb), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 if (error) {
Nathan Scott764d1f82006-03-31 13:04:17 +1000516 xfs_fs_mount_cmn_err(flags, "SB validate failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 goto fail;
518 }
519
520 /*
521 * We must be able to do sector-sized and sector-aligned IO.
522 */
523 if (sector_size > mp->m_sb.sb_sectsize) {
Nathan Scott764d1f82006-03-31 13:04:17 +1000524 xfs_fs_mount_cmn_err(flags,
525 "device supports only %u byte sectors (not %u)",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 sector_size, mp->m_sb.sb_sectsize);
527 error = ENOSYS;
528 goto fail;
529 }
530
531 /*
532 * If device sector size is smaller than the superblock size,
533 * re-read the superblock so the buffer is correctly sized.
534 */
535 if (sector_size < mp->m_sb.sb_sectsize) {
536 XFS_BUF_UNMANAGE(bp);
537 xfs_buf_relse(bp);
538 sector_size = mp->m_sb.sb_sectsize;
539 bp = xfs_buf_read_flags(mp->m_ddev_targp, XFS_SB_DADDR,
540 BTOBB(sector_size), extra_flags);
541 if (!bp || XFS_BUF_ISERROR(bp)) {
Nathan Scott764d1f82006-03-31 13:04:17 +1000542 xfs_fs_mount_cmn_err(flags, "SB re-read failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 error = bp ? XFS_BUF_GETERROR(bp) : ENOMEM;
544 goto fail;
545 }
546 ASSERT(XFS_BUF_ISBUSY(bp));
547 ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
548 }
549
David Chinner8d280b92006-03-14 13:13:09 +1100550 xfs_icsb_balance_counter(mp, XFS_SBS_ICOUNT, 0);
551 xfs_icsb_balance_counter(mp, XFS_SBS_IFREE, 0);
552 xfs_icsb_balance_counter(mp, XFS_SBS_FDBLOCKS, 0);
553
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 mp->m_sb_bp = bp;
555 xfs_buf_relse(bp);
556 ASSERT(XFS_BUF_VALUSEMA(bp) > 0);
557 return 0;
558
559 fail:
560 if (bp) {
561 XFS_BUF_UNMANAGE(bp);
562 xfs_buf_relse(bp);
563 }
564 return error;
565}
566
567
568/*
569 * xfs_mount_common
570 *
571 * Mount initialization code establishing various mount
572 * fields from the superblock associated with the given
573 * mount structure
574 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000575STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576xfs_mount_common(xfs_mount_t *mp, xfs_sb_t *sbp)
577{
578 int i;
579
580 mp->m_agfrotor = mp->m_agirotor = 0;
581 spinlock_init(&mp->m_agirotor_lock, "m_agirotor_lock");
582 mp->m_maxagi = mp->m_sb.sb_agcount;
583 mp->m_blkbit_log = sbp->sb_blocklog + XFS_NBBYLOG;
584 mp->m_blkbb_log = sbp->sb_blocklog - BBSHIFT;
585 mp->m_sectbb_log = sbp->sb_sectlog - BBSHIFT;
586 mp->m_agno_log = xfs_highbit32(sbp->sb_agcount - 1) + 1;
587 mp->m_agino_log = sbp->sb_inopblog + sbp->sb_agblklog;
588 mp->m_litino = sbp->sb_inodesize -
589 ((uint)sizeof(xfs_dinode_core_t) + (uint)sizeof(xfs_agino_t));
590 mp->m_blockmask = sbp->sb_blocksize - 1;
591 mp->m_blockwsize = sbp->sb_blocksize >> XFS_WORDLOG;
592 mp->m_blockwmask = mp->m_blockwsize - 1;
593 INIT_LIST_HEAD(&mp->m_del_inodes);
594
595 /*
596 * Setup for attributes, in case they get created.
597 * This value is for inodes getting attributes for the first time,
598 * the per-inode value is for old attribute values.
599 */
600 ASSERT(sbp->sb_inodesize >= 256 && sbp->sb_inodesize <= 2048);
601 switch (sbp->sb_inodesize) {
602 case 256:
Nathan Scottd8cc8902005-11-02 10:34:53 +1100603 mp->m_attroffset = XFS_LITINO(mp) -
604 XFS_BMDR_SPACE_CALC(MINABTPTRS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 break;
606 case 512:
607 case 1024:
608 case 2048:
Nathan Scottd8cc8902005-11-02 10:34:53 +1100609 mp->m_attroffset = XFS_BMDR_SPACE_CALC(6 * MINABTPTRS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 break;
611 default:
612 ASSERT(0);
613 }
614 ASSERT(mp->m_attroffset < XFS_LITINO(mp));
615
616 for (i = 0; i < 2; i++) {
617 mp->m_alloc_mxr[i] = XFS_BTREE_BLOCK_MAXRECS(sbp->sb_blocksize,
618 xfs_alloc, i == 0);
619 mp->m_alloc_mnr[i] = XFS_BTREE_BLOCK_MINRECS(sbp->sb_blocksize,
620 xfs_alloc, i == 0);
621 }
622 for (i = 0; i < 2; i++) {
623 mp->m_bmap_dmxr[i] = XFS_BTREE_BLOCK_MAXRECS(sbp->sb_blocksize,
624 xfs_bmbt, i == 0);
625 mp->m_bmap_dmnr[i] = XFS_BTREE_BLOCK_MINRECS(sbp->sb_blocksize,
626 xfs_bmbt, i == 0);
627 }
628 for (i = 0; i < 2; i++) {
629 mp->m_inobt_mxr[i] = XFS_BTREE_BLOCK_MAXRECS(sbp->sb_blocksize,
630 xfs_inobt, i == 0);
631 mp->m_inobt_mnr[i] = XFS_BTREE_BLOCK_MINRECS(sbp->sb_blocksize,
632 xfs_inobt, i == 0);
633 }
634
635 mp->m_bsize = XFS_FSB_TO_BB(mp, 1);
636 mp->m_ialloc_inos = (int)MAX((__uint16_t)XFS_INODES_PER_CHUNK,
637 sbp->sb_inopblock);
638 mp->m_ialloc_blks = mp->m_ialloc_inos >> sbp->sb_inopblog;
639}
640/*
641 * xfs_mountfs
642 *
643 * This function does the following on an initial mount of a file system:
644 * - reads the superblock from disk and init the mount struct
645 * - if we're a 32-bit kernel, do a size check on the superblock
646 * so we don't mount terabyte filesystems
647 * - init mount struct realtime fields
648 * - allocate inode hash table for fs
649 * - init directory manager
650 * - perform recovery and init the log manager
651 */
652int
653xfs_mountfs(
654 vfs_t *vfsp,
655 xfs_mount_t *mp,
656 int mfsi_flags)
657{
658 xfs_buf_t *bp;
659 xfs_sb_t *sbp = &(mp->m_sb);
660 xfs_inode_t *rip;
661 vnode_t *rvp = NULL;
662 int readio_log, writeio_log;
663 xfs_daddr_t d;
664 __uint64_t ret64;
665 __int64_t update_flags;
666 uint quotamount, quotaflags;
667 int agno;
668 int uuid_mounted = 0;
669 int error = 0;
670
671 if (mp->m_sb_bp == NULL) {
Nathan Scott764d1f82006-03-31 13:04:17 +1000672 if ((error = xfs_readsb(mp, mfsi_flags))) {
Jesper Juhl014c2542006-01-15 02:37:08 +0100673 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 }
675 }
676 xfs_mount_common(mp, sbp);
677
678 /*
679 * Check if sb_agblocks is aligned at stripe boundary
680 * If sb_agblocks is NOT aligned turn off m_dalign since
681 * allocator alignment is within an ag, therefore ag has
682 * to be aligned at stripe boundary.
683 */
684 update_flags = 0LL;
685 if (mp->m_dalign && !(mfsi_flags & XFS_MFSI_SECOND)) {
686 /*
687 * If stripe unit and stripe width are not multiples
688 * of the fs blocksize turn off alignment.
689 */
690 if ((BBTOB(mp->m_dalign) & mp->m_blockmask) ||
691 (BBTOB(mp->m_swidth) & mp->m_blockmask)) {
692 if (mp->m_flags & XFS_MOUNT_RETERR) {
693 cmn_err(CE_WARN,
694 "XFS: alignment check 1 failed");
695 error = XFS_ERROR(EINVAL);
696 goto error1;
697 }
698 mp->m_dalign = mp->m_swidth = 0;
699 } else {
700 /*
701 * Convert the stripe unit and width to FSBs.
702 */
703 mp->m_dalign = XFS_BB_TO_FSBT(mp, mp->m_dalign);
704 if (mp->m_dalign && (sbp->sb_agblocks % mp->m_dalign)) {
705 if (mp->m_flags & XFS_MOUNT_RETERR) {
706 error = XFS_ERROR(EINVAL);
707 goto error1;
708 }
709 xfs_fs_cmn_err(CE_WARN, mp,
710"stripe alignment turned off: sunit(%d)/swidth(%d) incompatible with agsize(%d)",
711 mp->m_dalign, mp->m_swidth,
712 sbp->sb_agblocks);
713
714 mp->m_dalign = 0;
715 mp->m_swidth = 0;
716 } else if (mp->m_dalign) {
717 mp->m_swidth = XFS_BB_TO_FSBT(mp, mp->m_swidth);
718 } else {
719 if (mp->m_flags & XFS_MOUNT_RETERR) {
720 xfs_fs_cmn_err(CE_WARN, mp,
721"stripe alignment turned off: sunit(%d) less than bsize(%d)",
722 mp->m_dalign,
723 mp->m_blockmask +1);
724 error = XFS_ERROR(EINVAL);
725 goto error1;
726 }
727 mp->m_swidth = 0;
728 }
729 }
730
731 /*
732 * Update superblock with new values
733 * and log changes
734 */
735 if (XFS_SB_VERSION_HASDALIGN(sbp)) {
736 if (sbp->sb_unit != mp->m_dalign) {
737 sbp->sb_unit = mp->m_dalign;
738 update_flags |= XFS_SB_UNIT;
739 }
740 if (sbp->sb_width != mp->m_swidth) {
741 sbp->sb_width = mp->m_swidth;
742 update_flags |= XFS_SB_WIDTH;
743 }
744 }
745 } else if ((mp->m_flags & XFS_MOUNT_NOALIGN) != XFS_MOUNT_NOALIGN &&
746 XFS_SB_VERSION_HASDALIGN(&mp->m_sb)) {
747 mp->m_dalign = sbp->sb_unit;
748 mp->m_swidth = sbp->sb_width;
749 }
750
751 xfs_alloc_compute_maxlevels(mp);
752 xfs_bmap_compute_maxlevels(mp, XFS_DATA_FORK);
753 xfs_bmap_compute_maxlevels(mp, XFS_ATTR_FORK);
754 xfs_ialloc_compute_maxlevels(mp);
755
756 if (sbp->sb_imax_pct) {
757 __uint64_t icount;
758
759 /* Make sure the maximum inode count is a multiple of the
760 * units we allocate inodes in.
761 */
762
763 icount = sbp->sb_dblocks * sbp->sb_imax_pct;
764 do_div(icount, 100);
765 do_div(icount, mp->m_ialloc_blks);
766 mp->m_maxicount = (icount * mp->m_ialloc_blks) <<
767 sbp->sb_inopblog;
768 } else
769 mp->m_maxicount = 0;
770
771 mp->m_maxioffset = xfs_max_file_offset(sbp->sb_blocklog);
772
773 /*
774 * XFS uses the uuid from the superblock as the unique
775 * identifier for fsid. We can not use the uuid from the volume
776 * since a single partition filesystem is identical to a single
777 * partition volume/filesystem.
778 */
779 if ((mfsi_flags & XFS_MFSI_SECOND) == 0 &&
780 (mp->m_flags & XFS_MOUNT_NOUUID) == 0) {
781 if (xfs_uuid_mount(mp)) {
782 error = XFS_ERROR(EINVAL);
783 goto error1;
784 }
785 uuid_mounted=1;
786 ret64 = uuid_hash64(&sbp->sb_uuid);
787 memcpy(&vfsp->vfs_fsid, &ret64, sizeof(ret64));
788 }
789
790 /*
791 * Set the default minimum read and write sizes unless
792 * already specified in a mount option.
793 * We use smaller I/O sizes when the file system
794 * is being used for NFS service (wsync mount option).
795 */
796 if (!(mp->m_flags & XFS_MOUNT_DFLT_IOSIZE)) {
797 if (mp->m_flags & XFS_MOUNT_WSYNC) {
798 readio_log = XFS_WSYNC_READIO_LOG;
799 writeio_log = XFS_WSYNC_WRITEIO_LOG;
800 } else {
801 readio_log = XFS_READIO_LOG_LARGE;
802 writeio_log = XFS_WRITEIO_LOG_LARGE;
803 }
804 } else {
805 readio_log = mp->m_readio_log;
806 writeio_log = mp->m_writeio_log;
807 }
808
809 /*
810 * Set the number of readahead buffers to use based on
811 * physical memory size.
812 */
813 if (xfs_physmem <= 4096) /* <= 16MB */
814 mp->m_nreadaheads = XFS_RW_NREADAHEAD_16MB;
815 else if (xfs_physmem <= 8192) /* <= 32MB */
816 mp->m_nreadaheads = XFS_RW_NREADAHEAD_32MB;
817 else
818 mp->m_nreadaheads = XFS_RW_NREADAHEAD_K32;
819 if (sbp->sb_blocklog > readio_log) {
820 mp->m_readio_log = sbp->sb_blocklog;
821 } else {
822 mp->m_readio_log = readio_log;
823 }
824 mp->m_readio_blocks = 1 << (mp->m_readio_log - sbp->sb_blocklog);
825 if (sbp->sb_blocklog > writeio_log) {
826 mp->m_writeio_log = sbp->sb_blocklog;
827 } else {
828 mp->m_writeio_log = writeio_log;
829 }
830 mp->m_writeio_blocks = 1 << (mp->m_writeio_log - sbp->sb_blocklog);
831
832 /*
833 * Set the inode cluster size based on the physical memory
834 * size. This may still be overridden by the file system
835 * block size if it is larger than the chosen cluster size.
836 */
837 if (xfs_physmem <= btoc(32 * 1024 * 1024)) { /* <= 32 MB */
838 mp->m_inode_cluster_size = XFS_INODE_SMALL_CLUSTER_SIZE;
839 } else {
840 mp->m_inode_cluster_size = XFS_INODE_BIG_CLUSTER_SIZE;
841 }
842 /*
843 * Set whether we're using inode alignment.
844 */
845 if (XFS_SB_VERSION_HASALIGN(&mp->m_sb) &&
846 mp->m_sb.sb_inoalignmt >=
847 XFS_B_TO_FSBT(mp, mp->m_inode_cluster_size))
848 mp->m_inoalign_mask = mp->m_sb.sb_inoalignmt - 1;
849 else
850 mp->m_inoalign_mask = 0;
851 /*
852 * If we are using stripe alignment, check whether
853 * the stripe unit is a multiple of the inode alignment
854 */
855 if (mp->m_dalign && mp->m_inoalign_mask &&
856 !(mp->m_dalign & mp->m_inoalign_mask))
857 mp->m_sinoalign = mp->m_dalign;
858 else
859 mp->m_sinoalign = 0;
860 /*
861 * Check that the data (and log if separate) are an ok size.
862 */
863 d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks);
864 if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_dblocks) {
865 cmn_err(CE_WARN, "XFS: size check 1 failed");
866 error = XFS_ERROR(E2BIG);
867 goto error1;
868 }
869 error = xfs_read_buf(mp, mp->m_ddev_targp,
870 d - XFS_FSS_TO_BB(mp, 1),
871 XFS_FSS_TO_BB(mp, 1), 0, &bp);
872 if (!error) {
873 xfs_buf_relse(bp);
874 } else {
875 cmn_err(CE_WARN, "XFS: size check 2 failed");
876 if (error == ENOSPC) {
877 error = XFS_ERROR(E2BIG);
878 }
879 goto error1;
880 }
881
882 if (((mfsi_flags & XFS_MFSI_CLIENT) == 0) &&
883 mp->m_logdev_targp != mp->m_ddev_targp) {
884 d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_logblocks);
885 if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_logblocks) {
886 cmn_err(CE_WARN, "XFS: size check 3 failed");
887 error = XFS_ERROR(E2BIG);
888 goto error1;
889 }
890 error = xfs_read_buf(mp, mp->m_logdev_targp,
891 d - XFS_FSB_TO_BB(mp, 1),
892 XFS_FSB_TO_BB(mp, 1), 0, &bp);
893 if (!error) {
894 xfs_buf_relse(bp);
895 } else {
896 cmn_err(CE_WARN, "XFS: size check 3 failed");
897 if (error == ENOSPC) {
898 error = XFS_ERROR(E2BIG);
899 }
900 goto error1;
901 }
902 }
903
904 /*
905 * Initialize realtime fields in the mount structure
906 */
907 if ((error = xfs_rtmount_init(mp))) {
908 cmn_err(CE_WARN, "XFS: RT mount failed");
909 goto error1;
910 }
911
912 /*
913 * For client case we are done now
914 */
915 if (mfsi_flags & XFS_MFSI_CLIENT) {
Jesper Juhl014c2542006-01-15 02:37:08 +0100916 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 }
918
919 /*
920 * Copies the low order bits of the timestamp and the randomly
921 * set "sequence" number out of a UUID.
922 */
923 uuid_getnodeuniq(&sbp->sb_uuid, mp->m_fixedfsid);
924
925 /*
926 * The vfs structure needs to have a file system independent
927 * way of checking for the invariant file system ID. Since it
928 * can't look at mount structures it has a pointer to the data
929 * in the mount structure.
930 *
931 * File systems that don't support user level file handles (i.e.
932 * all of them except for XFS) will leave vfs_altfsid as NULL.
933 */
934 vfsp->vfs_altfsid = (xfs_fsid_t *)mp->m_fixedfsid;
935 mp->m_dmevmask = 0; /* not persistent; set after each mount */
936
937 /*
938 * Select the right directory manager.
939 */
940 mp->m_dirops =
941 XFS_SB_VERSION_HASDIRV2(&mp->m_sb) ?
942 xfsv2_dirops :
943 xfsv1_dirops;
944
945 /*
946 * Initialize directory manager's entries.
947 */
948 XFS_DIR_MOUNT(mp);
949
950 /*
951 * Initialize the attribute manager's entries.
952 */
953 mp->m_attr_magicpct = (mp->m_sb.sb_blocksize * 37) / 100;
954
955 /*
956 * Initialize the precomputed transaction reservations values.
957 */
958 xfs_trans_init(mp);
959
960 /*
961 * Allocate and initialize the inode hash table for this
962 * file system.
963 */
964 xfs_ihash_init(mp);
965 xfs_chash_init(mp);
966
967 /*
968 * Allocate and initialize the per-ag data.
969 */
970 init_rwsem(&mp->m_peraglock);
971 mp->m_perag =
972 kmem_zalloc(sbp->sb_agcount * sizeof(xfs_perag_t), KM_SLEEP);
973
Nathan Scottc11e2c32005-11-02 15:11:45 +1100974 mp->m_maxagi = xfs_initialize_perag(vfsp, mp, sbp->sb_agcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
976 /*
977 * log's mount-time initialization. Perform 1st part recovery if needed
978 */
979 if (likely(sbp->sb_logblocks > 0)) { /* check for volume case */
980 error = xfs_log_mount(mp, mp->m_logdev_targp,
981 XFS_FSB_TO_DADDR(mp, sbp->sb_logstart),
982 XFS_FSB_TO_BB(mp, sbp->sb_logblocks));
983 if (error) {
984 cmn_err(CE_WARN, "XFS: log mount failed");
985 goto error2;
986 }
987 } else { /* No log has been defined */
988 cmn_err(CE_WARN, "XFS: no log defined");
989 XFS_ERROR_REPORT("xfs_mountfs_int(1)", XFS_ERRLEVEL_LOW, mp);
990 error = XFS_ERROR(EFSCORRUPTED);
991 goto error2;
992 }
993
994 /*
995 * Get and sanity-check the root inode.
996 * Save the pointer to it in the mount structure.
997 */
998 error = xfs_iget(mp, NULL, sbp->sb_rootino, 0, XFS_ILOCK_EXCL, &rip, 0);
999 if (error) {
1000 cmn_err(CE_WARN, "XFS: failed to read root inode");
1001 goto error3;
1002 }
1003
1004 ASSERT(rip != NULL);
1005 rvp = XFS_ITOV(rip);
1006
1007 if (unlikely((rip->i_d.di_mode & S_IFMT) != S_IFDIR)) {
1008 cmn_err(CE_WARN, "XFS: corrupted root inode");
Nathan Scottb6574522006-06-09 15:29:40 +10001009 cmn_err(CE_WARN, "Device %s - root %llu is not a directory",
1010 XFS_BUFTARG_NAME(mp->m_ddev_targp),
1011 (unsigned long long)rip->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 xfs_iunlock(rip, XFS_ILOCK_EXCL);
1013 XFS_ERROR_REPORT("xfs_mountfs_int(2)", XFS_ERRLEVEL_LOW,
1014 mp);
1015 error = XFS_ERROR(EFSCORRUPTED);
1016 goto error4;
1017 }
1018 mp->m_rootip = rip; /* save it */
1019
1020 xfs_iunlock(rip, XFS_ILOCK_EXCL);
1021
1022 /*
1023 * Initialize realtime inode pointers in the mount structure
1024 */
1025 if ((error = xfs_rtmount_inodes(mp))) {
1026 /*
1027 * Free up the root inode.
1028 */
1029 cmn_err(CE_WARN, "XFS: failed to read RT inodes");
1030 goto error4;
1031 }
1032
1033 /*
1034 * If fs is not mounted readonly, then update the superblock
1035 * unit and width changes.
1036 */
1037 if (update_flags && !(vfsp->vfs_flag & VFS_RDONLY))
1038 xfs_mount_log_sbunit(mp, update_flags);
1039
1040 /*
1041 * Initialise the XFS quota management subsystem for this mount
1042 */
1043 if ((error = XFS_QM_INIT(mp, &quotamount, &quotaflags)))
1044 goto error4;
1045
1046 /*
1047 * Finish recovering the file system. This part needed to be
1048 * delayed until after the root and real-time bitmap inodes
1049 * were consistently read in.
1050 */
1051 error = xfs_log_mount_finish(mp, mfsi_flags);
1052 if (error) {
1053 cmn_err(CE_WARN, "XFS: log mount finish failed");
1054 goto error4;
1055 }
1056
1057 /*
1058 * Complete the quota initialisation, post-log-replay component.
1059 */
1060 if ((error = XFS_QM_MOUNT(mp, quotamount, quotaflags, mfsi_flags)))
1061 goto error4;
1062
1063 return 0;
1064
1065 error4:
1066 /*
1067 * Free up the root inode.
1068 */
1069 VN_RELE(rvp);
1070 error3:
1071 xfs_log_unmount_dealloc(mp);
1072 error2:
1073 xfs_ihash_free(mp);
1074 xfs_chash_free(mp);
1075 for (agno = 0; agno < sbp->sb_agcount; agno++)
1076 if (mp->m_perag[agno].pagb_list)
1077 kmem_free(mp->m_perag[agno].pagb_list,
1078 sizeof(xfs_perag_busy_t) * XFS_PAGB_NUM_SLOTS);
1079 kmem_free(mp->m_perag, sbp->sb_agcount * sizeof(xfs_perag_t));
1080 mp->m_perag = NULL;
1081 /* FALLTHROUGH */
1082 error1:
1083 if (uuid_mounted)
1084 xfs_uuid_unmount(mp);
1085 xfs_freesb(mp);
1086 return error;
1087}
1088
1089/*
1090 * xfs_unmountfs
1091 *
1092 * This flushes out the inodes,dquots and the superblock, unmounts the
1093 * log and makes sure that incore structures are freed.
1094 */
1095int
1096xfs_unmountfs(xfs_mount_t *mp, struct cred *cr)
1097{
1098 struct vfs *vfsp = XFS_MTOVFS(mp);
1099#if defined(DEBUG) || defined(INDUCE_IO_ERROR)
1100 int64_t fsid;
1101#endif
1102
Christoph Hellwigefa80272005-06-21 15:37:17 +10001103 xfs_iflush_all(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
Nathan Scottee2a4f72006-01-11 15:33:36 +11001105 XFS_QM_DQPURGEALL(mp, XFS_QMOPT_QUOTALL | XFS_QMOPT_UMOUNTING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
1107 /*
1108 * Flush out the log synchronously so that we know for sure
1109 * that nothing is pinned. This is important because bflush()
1110 * will skip pinned buffers.
1111 */
1112 xfs_log_force(mp, (xfs_lsn_t)0, XFS_LOG_FORCE | XFS_LOG_SYNC);
1113
1114 xfs_binval(mp->m_ddev_targp);
1115 if (mp->m_rtdev_targp) {
1116 xfs_binval(mp->m_rtdev_targp);
1117 }
1118
1119 xfs_unmountfs_writesb(mp);
1120
1121 xfs_unmountfs_wait(mp); /* wait for async bufs */
1122
1123 xfs_log_unmount(mp); /* Done! No more fs ops. */
1124
1125 xfs_freesb(mp);
1126
1127 /*
1128 * All inodes from this mount point should be freed.
1129 */
1130 ASSERT(mp->m_inodes == NULL);
1131
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 xfs_unmountfs_close(mp, cr);
1133 if ((mp->m_flags & XFS_MOUNT_NOUUID) == 0)
1134 xfs_uuid_unmount(mp);
1135
1136#if defined(DEBUG) || defined(INDUCE_IO_ERROR)
1137 /*
1138 * clear all error tags on this filesystem
1139 */
1140 memcpy(&fsid, &vfsp->vfs_fsid, sizeof(int64_t));
1141 xfs_errortag_clearall_umount(fsid, mp->m_fsname, 0);
1142#endif
1143 XFS_IODONE(vfsp);
1144 xfs_mount_free(mp, 1);
1145 return 0;
1146}
1147
1148void
1149xfs_unmountfs_close(xfs_mount_t *mp, struct cred *cr)
1150{
1151 if (mp->m_logdev_targp != mp->m_ddev_targp)
1152 xfs_free_buftarg(mp->m_logdev_targp, 1);
1153 if (mp->m_rtdev_targp)
1154 xfs_free_buftarg(mp->m_rtdev_targp, 1);
1155 xfs_free_buftarg(mp->m_ddev_targp, 0);
1156}
1157
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10001158STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159xfs_unmountfs_wait(xfs_mount_t *mp)
1160{
1161 if (mp->m_logdev_targp != mp->m_ddev_targp)
1162 xfs_wait_buftarg(mp->m_logdev_targp);
1163 if (mp->m_rtdev_targp)
1164 xfs_wait_buftarg(mp->m_rtdev_targp);
1165 xfs_wait_buftarg(mp->m_ddev_targp);
1166}
1167
1168int
1169xfs_unmountfs_writesb(xfs_mount_t *mp)
1170{
1171 xfs_buf_t *sbp;
1172 xfs_sb_t *sb;
1173 int error = 0;
1174
1175 /*
1176 * skip superblock write if fs is read-only, or
1177 * if we are doing a forced umount.
1178 */
1179 sbp = xfs_getsb(mp, 0);
1180 if (!(XFS_MTOVFS(mp)->vfs_flag & VFS_RDONLY ||
1181 XFS_FORCED_SHUTDOWN(mp))) {
David Chinner8d280b92006-03-14 13:13:09 +11001182
1183 xfs_icsb_sync_counters(mp);
1184
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 /*
1186 * mark shared-readonly if desired
1187 */
1188 sb = XFS_BUF_TO_SBP(sbp);
1189 if (mp->m_mk_sharedro) {
1190 if (!(sb->sb_flags & XFS_SBF_READONLY))
1191 sb->sb_flags |= XFS_SBF_READONLY;
1192 if (!XFS_SB_VERSION_HASSHARED(sb))
1193 XFS_SB_VERSION_ADDSHARED(sb);
1194 xfs_fs_cmn_err(CE_NOTE, mp,
1195 "Unmounting, marking shared read-only");
1196 }
1197 XFS_BUF_UNDONE(sbp);
1198 XFS_BUF_UNREAD(sbp);
1199 XFS_BUF_UNDELAYWRITE(sbp);
1200 XFS_BUF_WRITE(sbp);
1201 XFS_BUF_UNASYNC(sbp);
1202 ASSERT(XFS_BUF_TARGET(sbp) == mp->m_ddev_targp);
1203 xfsbdstrat(mp, sbp);
1204 /* Nevermind errors we might get here. */
1205 error = xfs_iowait(sbp);
1206 if (error)
1207 xfs_ioerror_alert("xfs_unmountfs_writesb",
1208 mp, sbp, XFS_BUF_ADDR(sbp));
1209 if (error && mp->m_mk_sharedro)
1210 xfs_fs_cmn_err(CE_ALERT, mp, "Superblock write error detected while unmounting. Filesystem may not be marked shared readonly");
1211 }
1212 xfs_buf_relse(sbp);
Jesper Juhl014c2542006-01-15 02:37:08 +01001213 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214}
1215
1216/*
1217 * xfs_mod_sb() can be used to copy arbitrary changes to the
1218 * in-core superblock into the superblock buffer to be logged.
1219 * It does not provide the higher level of locking that is
1220 * needed to protect the in-core superblock from concurrent
1221 * access.
1222 */
1223void
1224xfs_mod_sb(xfs_trans_t *tp, __int64_t fields)
1225{
1226 xfs_buf_t *bp;
1227 int first;
1228 int last;
1229 xfs_mount_t *mp;
1230 xfs_sb_t *sbp;
1231 xfs_sb_field_t f;
1232
1233 ASSERT(fields);
1234 if (!fields)
1235 return;
1236 mp = tp->t_mountp;
1237 bp = xfs_trans_getsb(tp, mp, 0);
1238 sbp = XFS_BUF_TO_SBP(bp);
1239 first = sizeof(xfs_sb_t);
1240 last = 0;
1241
1242 /* translate/copy */
1243
1244 xfs_xlatesb(XFS_BUF_PTR(bp), &(mp->m_sb), -1, fields);
1245
1246 /* find modified range */
1247
1248 f = (xfs_sb_field_t)xfs_lowbit64((__uint64_t)fields);
1249 ASSERT((1LL << f) & XFS_SB_MOD_BITS);
1250 first = xfs_sb_info[f].offset;
1251
1252 f = (xfs_sb_field_t)xfs_highbit64((__uint64_t)fields);
1253 ASSERT((1LL << f) & XFS_SB_MOD_BITS);
1254 last = xfs_sb_info[f + 1].offset - 1;
1255
1256 xfs_trans_log_buf(tp, bp, first, last);
1257}
Yingping Lud210a282006-06-09 14:55:18 +10001258
1259/*
1260 * In order to avoid ENOSPC-related deadlock caused by
1261 * out-of-order locking of AGF buffer (PV 947395), we place
1262 * constraints on the relationship among actual allocations for
1263 * data blocks, freelist blocks, and potential file data bmap
1264 * btree blocks. However, these restrictions may result in no
1265 * actual space allocated for a delayed extent, for example, a data
1266 * block in a certain AG is allocated but there is no additional
1267 * block for the additional bmap btree block due to a split of the
1268 * bmap btree of the file. The result of this may lead to an
1269 * infinite loop in xfssyncd when the file gets flushed to disk and
1270 * all delayed extents need to be actually allocated. To get around
1271 * this, we explicitly set aside a few blocks which will not be
1272 * reserved in delayed allocation. Considering the minimum number of
1273 * needed freelist blocks is 4 fsbs, a potential split of file's bmap
1274 * btree requires 1 fsb, so we set the number of set-aside blocks to 8.
1275*/
1276#define SET_ASIDE_BLOCKS 8
1277
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278/*
1279 * xfs_mod_incore_sb_unlocked() is a utility routine common used to apply
1280 * a delta to a specified field in the in-core superblock. Simply
1281 * switch on the field indicated and apply the delta to that field.
1282 * Fields are not allowed to dip below zero, so if the delta would
1283 * do this do not apply it and return EINVAL.
1284 *
1285 * The SB_LOCK must be held when this routine is called.
1286 */
David Chinner8d280b92006-03-14 13:13:09 +11001287int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288xfs_mod_incore_sb_unlocked(xfs_mount_t *mp, xfs_sb_field_t field,
1289 int delta, int rsvd)
1290{
1291 int scounter; /* short counter for 32 bit fields */
1292 long long lcounter; /* long counter for 64 bit fields */
1293 long long res_used, rem;
1294
1295 /*
1296 * With the in-core superblock spin lock held, switch
1297 * on the indicated field. Apply the delta to the
1298 * proper field. If the fields value would dip below
1299 * 0, then do not apply the delta and return EINVAL.
1300 */
1301 switch (field) {
1302 case XFS_SBS_ICOUNT:
1303 lcounter = (long long)mp->m_sb.sb_icount;
1304 lcounter += delta;
1305 if (lcounter < 0) {
1306 ASSERT(0);
Jesper Juhl014c2542006-01-15 02:37:08 +01001307 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 }
1309 mp->m_sb.sb_icount = lcounter;
Jesper Juhl014c2542006-01-15 02:37:08 +01001310 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 case XFS_SBS_IFREE:
1312 lcounter = (long long)mp->m_sb.sb_ifree;
1313 lcounter += delta;
1314 if (lcounter < 0) {
1315 ASSERT(0);
Jesper Juhl014c2542006-01-15 02:37:08 +01001316 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 }
1318 mp->m_sb.sb_ifree = lcounter;
Jesper Juhl014c2542006-01-15 02:37:08 +01001319 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 case XFS_SBS_FDBLOCKS:
1321
Yingping Lud210a282006-06-09 14:55:18 +10001322 lcounter = (long long)mp->m_sb.sb_fdblocks - SET_ASIDE_BLOCKS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 res_used = (long long)(mp->m_resblks - mp->m_resblks_avail);
1324
1325 if (delta > 0) { /* Putting blocks back */
1326 if (res_used > delta) {
1327 mp->m_resblks_avail += delta;
1328 } else {
1329 rem = delta - res_used;
1330 mp->m_resblks_avail = mp->m_resblks;
1331 lcounter += rem;
1332 }
1333 } else { /* Taking blocks away */
1334
1335 lcounter += delta;
1336
1337 /*
1338 * If were out of blocks, use any available reserved blocks if
1339 * were allowed to.
1340 */
1341
1342 if (lcounter < 0) {
1343 if (rsvd) {
1344 lcounter = (long long)mp->m_resblks_avail + delta;
1345 if (lcounter < 0) {
Jesper Juhl014c2542006-01-15 02:37:08 +01001346 return XFS_ERROR(ENOSPC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 }
1348 mp->m_resblks_avail = lcounter;
Jesper Juhl014c2542006-01-15 02:37:08 +01001349 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 } else { /* not reserved */
Jesper Juhl014c2542006-01-15 02:37:08 +01001351 return XFS_ERROR(ENOSPC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 }
1353 }
1354 }
1355
Yingping Lud210a282006-06-09 14:55:18 +10001356 mp->m_sb.sb_fdblocks = lcounter + SET_ASIDE_BLOCKS;
Jesper Juhl014c2542006-01-15 02:37:08 +01001357 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 case XFS_SBS_FREXTENTS:
1359 lcounter = (long long)mp->m_sb.sb_frextents;
1360 lcounter += delta;
1361 if (lcounter < 0) {
Jesper Juhl014c2542006-01-15 02:37:08 +01001362 return XFS_ERROR(ENOSPC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 }
1364 mp->m_sb.sb_frextents = lcounter;
Jesper Juhl014c2542006-01-15 02:37:08 +01001365 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 case XFS_SBS_DBLOCKS:
1367 lcounter = (long long)mp->m_sb.sb_dblocks;
1368 lcounter += delta;
1369 if (lcounter < 0) {
1370 ASSERT(0);
Jesper Juhl014c2542006-01-15 02:37:08 +01001371 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 }
1373 mp->m_sb.sb_dblocks = lcounter;
Jesper Juhl014c2542006-01-15 02:37:08 +01001374 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 case XFS_SBS_AGCOUNT:
1376 scounter = mp->m_sb.sb_agcount;
1377 scounter += delta;
1378 if (scounter < 0) {
1379 ASSERT(0);
Jesper Juhl014c2542006-01-15 02:37:08 +01001380 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 }
1382 mp->m_sb.sb_agcount = scounter;
Jesper Juhl014c2542006-01-15 02:37:08 +01001383 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 case XFS_SBS_IMAX_PCT:
1385 scounter = mp->m_sb.sb_imax_pct;
1386 scounter += delta;
1387 if (scounter < 0) {
1388 ASSERT(0);
Jesper Juhl014c2542006-01-15 02:37:08 +01001389 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 }
1391 mp->m_sb.sb_imax_pct = scounter;
Jesper Juhl014c2542006-01-15 02:37:08 +01001392 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 case XFS_SBS_REXTSIZE:
1394 scounter = mp->m_sb.sb_rextsize;
1395 scounter += delta;
1396 if (scounter < 0) {
1397 ASSERT(0);
Jesper Juhl014c2542006-01-15 02:37:08 +01001398 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 }
1400 mp->m_sb.sb_rextsize = scounter;
Jesper Juhl014c2542006-01-15 02:37:08 +01001401 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 case XFS_SBS_RBMBLOCKS:
1403 scounter = mp->m_sb.sb_rbmblocks;
1404 scounter += delta;
1405 if (scounter < 0) {
1406 ASSERT(0);
Jesper Juhl014c2542006-01-15 02:37:08 +01001407 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 }
1409 mp->m_sb.sb_rbmblocks = scounter;
Jesper Juhl014c2542006-01-15 02:37:08 +01001410 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 case XFS_SBS_RBLOCKS:
1412 lcounter = (long long)mp->m_sb.sb_rblocks;
1413 lcounter += delta;
1414 if (lcounter < 0) {
1415 ASSERT(0);
Jesper Juhl014c2542006-01-15 02:37:08 +01001416 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 }
1418 mp->m_sb.sb_rblocks = lcounter;
Jesper Juhl014c2542006-01-15 02:37:08 +01001419 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 case XFS_SBS_REXTENTS:
1421 lcounter = (long long)mp->m_sb.sb_rextents;
1422 lcounter += delta;
1423 if (lcounter < 0) {
1424 ASSERT(0);
Jesper Juhl014c2542006-01-15 02:37:08 +01001425 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 }
1427 mp->m_sb.sb_rextents = lcounter;
Jesper Juhl014c2542006-01-15 02:37:08 +01001428 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 case XFS_SBS_REXTSLOG:
1430 scounter = mp->m_sb.sb_rextslog;
1431 scounter += delta;
1432 if (scounter < 0) {
1433 ASSERT(0);
Jesper Juhl014c2542006-01-15 02:37:08 +01001434 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 }
1436 mp->m_sb.sb_rextslog = scounter;
Jesper Juhl014c2542006-01-15 02:37:08 +01001437 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 default:
1439 ASSERT(0);
Jesper Juhl014c2542006-01-15 02:37:08 +01001440 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 }
1442}
1443
1444/*
1445 * xfs_mod_incore_sb() is used to change a field in the in-core
1446 * superblock structure by the specified delta. This modification
1447 * is protected by the SB_LOCK. Just use the xfs_mod_incore_sb_unlocked()
1448 * routine to do the work.
1449 */
1450int
1451xfs_mod_incore_sb(xfs_mount_t *mp, xfs_sb_field_t field, int delta, int rsvd)
1452{
1453 unsigned long s;
1454 int status;
1455
David Chinner8d280b92006-03-14 13:13:09 +11001456 /* check for per-cpu counters */
1457 switch (field) {
1458#ifdef HAVE_PERCPU_SB
1459 case XFS_SBS_ICOUNT:
1460 case XFS_SBS_IFREE:
1461 case XFS_SBS_FDBLOCKS:
1462 if (!(mp->m_flags & XFS_MOUNT_NO_PERCPU_SB)) {
1463 status = xfs_icsb_modify_counters(mp, field,
1464 delta, rsvd);
1465 break;
1466 }
1467 /* FALLTHROUGH */
1468#endif
1469 default:
1470 s = XFS_SB_LOCK(mp);
1471 status = xfs_mod_incore_sb_unlocked(mp, field, delta, rsvd);
1472 XFS_SB_UNLOCK(mp, s);
1473 break;
1474 }
1475
Jesper Juhl014c2542006-01-15 02:37:08 +01001476 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477}
1478
1479/*
1480 * xfs_mod_incore_sb_batch() is used to change more than one field
1481 * in the in-core superblock structure at a time. This modification
1482 * is protected by a lock internal to this module. The fields and
1483 * changes to those fields are specified in the array of xfs_mod_sb
1484 * structures passed in.
1485 *
1486 * Either all of the specified deltas will be applied or none of
1487 * them will. If any modified field dips below 0, then all modifications
1488 * will be backed out and EINVAL will be returned.
1489 */
1490int
1491xfs_mod_incore_sb_batch(xfs_mount_t *mp, xfs_mod_sb_t *msb, uint nmsb, int rsvd)
1492{
1493 unsigned long s;
1494 int status=0;
1495 xfs_mod_sb_t *msbp;
1496
1497 /*
1498 * Loop through the array of mod structures and apply each
1499 * individually. If any fail, then back out all those
1500 * which have already been applied. Do all of this within
1501 * the scope of the SB_LOCK so that all of the changes will
1502 * be atomic.
1503 */
1504 s = XFS_SB_LOCK(mp);
1505 msbp = &msb[0];
1506 for (msbp = &msbp[0]; msbp < (msb + nmsb); msbp++) {
1507 /*
1508 * Apply the delta at index n. If it fails, break
1509 * from the loop so we'll fall into the undo loop
1510 * below.
1511 */
David Chinner8d280b92006-03-14 13:13:09 +11001512 switch (msbp->msb_field) {
1513#ifdef HAVE_PERCPU_SB
1514 case XFS_SBS_ICOUNT:
1515 case XFS_SBS_IFREE:
1516 case XFS_SBS_FDBLOCKS:
1517 if (!(mp->m_flags & XFS_MOUNT_NO_PERCPU_SB)) {
1518 status = xfs_icsb_modify_counters_locked(mp,
1519 msbp->msb_field,
1520 msbp->msb_delta, rsvd);
1521 break;
1522 }
1523 /* FALLTHROUGH */
1524#endif
1525 default:
1526 status = xfs_mod_incore_sb_unlocked(mp,
1527 msbp->msb_field,
1528 msbp->msb_delta, rsvd);
1529 break;
1530 }
1531
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 if (status != 0) {
1533 break;
1534 }
1535 }
1536
1537 /*
1538 * If we didn't complete the loop above, then back out
1539 * any changes made to the superblock. If you add code
1540 * between the loop above and here, make sure that you
1541 * preserve the value of status. Loop back until
1542 * we step below the beginning of the array. Make sure
1543 * we don't touch anything back there.
1544 */
1545 if (status != 0) {
1546 msbp--;
1547 while (msbp >= msb) {
David Chinner8d280b92006-03-14 13:13:09 +11001548 switch (msbp->msb_field) {
1549#ifdef HAVE_PERCPU_SB
1550 case XFS_SBS_ICOUNT:
1551 case XFS_SBS_IFREE:
1552 case XFS_SBS_FDBLOCKS:
1553 if (!(mp->m_flags & XFS_MOUNT_NO_PERCPU_SB)) {
1554 status =
1555 xfs_icsb_modify_counters_locked(mp,
1556 msbp->msb_field,
1557 -(msbp->msb_delta),
1558 rsvd);
1559 break;
1560 }
1561 /* FALLTHROUGH */
1562#endif
1563 default:
1564 status = xfs_mod_incore_sb_unlocked(mp,
1565 msbp->msb_field,
1566 -(msbp->msb_delta),
1567 rsvd);
1568 break;
1569 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 ASSERT(status == 0);
1571 msbp--;
1572 }
1573 }
1574 XFS_SB_UNLOCK(mp, s);
Jesper Juhl014c2542006-01-15 02:37:08 +01001575 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576}
1577
1578/*
1579 * xfs_getsb() is called to obtain the buffer for the superblock.
1580 * The buffer is returned locked and read in from disk.
1581 * The buffer should be released with a call to xfs_brelse().
1582 *
1583 * If the flags parameter is BUF_TRYLOCK, then we'll only return
1584 * the superblock buffer if it can be locked without sleeping.
1585 * If it can't then we'll return NULL.
1586 */
1587xfs_buf_t *
1588xfs_getsb(
1589 xfs_mount_t *mp,
1590 int flags)
1591{
1592 xfs_buf_t *bp;
1593
1594 ASSERT(mp->m_sb_bp != NULL);
1595 bp = mp->m_sb_bp;
1596 if (flags & XFS_BUF_TRYLOCK) {
1597 if (!XFS_BUF_CPSEMA(bp)) {
1598 return NULL;
1599 }
1600 } else {
1601 XFS_BUF_PSEMA(bp, PRIBIO);
1602 }
1603 XFS_BUF_HOLD(bp);
1604 ASSERT(XFS_BUF_ISDONE(bp));
Jesper Juhl014c2542006-01-15 02:37:08 +01001605 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606}
1607
1608/*
1609 * Used to free the superblock along various error paths.
1610 */
1611void
1612xfs_freesb(
1613 xfs_mount_t *mp)
1614{
1615 xfs_buf_t *bp;
1616
1617 /*
1618 * Use xfs_getsb() so that the buffer will be locked
1619 * when we call xfs_buf_relse().
1620 */
1621 bp = xfs_getsb(mp, 0);
1622 XFS_BUF_UNMANAGE(bp);
1623 xfs_buf_relse(bp);
1624 mp->m_sb_bp = NULL;
1625}
1626
1627/*
1628 * See if the UUID is unique among mounted XFS filesystems.
1629 * Mount fails if UUID is nil or a FS with the same UUID is already mounted.
1630 */
1631STATIC int
1632xfs_uuid_mount(
1633 xfs_mount_t *mp)
1634{
1635 if (uuid_is_nil(&mp->m_sb.sb_uuid)) {
1636 cmn_err(CE_WARN,
1637 "XFS: Filesystem %s has nil UUID - can't mount",
1638 mp->m_fsname);
1639 return -1;
1640 }
1641 if (!uuid_table_insert(&mp->m_sb.sb_uuid)) {
1642 cmn_err(CE_WARN,
1643 "XFS: Filesystem %s has duplicate UUID - can't mount",
1644 mp->m_fsname);
1645 return -1;
1646 }
1647 return 0;
1648}
1649
1650/*
1651 * Remove filesystem from the UUID table.
1652 */
1653STATIC void
1654xfs_uuid_unmount(
1655 xfs_mount_t *mp)
1656{
1657 uuid_table_remove(&mp->m_sb.sb_uuid);
1658}
1659
1660/*
1661 * Used to log changes to the superblock unit and width fields which could
1662 * be altered by the mount options. Only the first superblock is updated.
1663 */
1664STATIC void
1665xfs_mount_log_sbunit(
1666 xfs_mount_t *mp,
1667 __int64_t fields)
1668{
1669 xfs_trans_t *tp;
1670
1671 ASSERT(fields & (XFS_SB_UNIT|XFS_SB_WIDTH|XFS_SB_UUID));
1672
1673 tp = xfs_trans_alloc(mp, XFS_TRANS_SB_UNIT);
1674 if (xfs_trans_reserve(tp, 0, mp->m_sb.sb_sectsize + 128, 0, 0,
1675 XFS_DEFAULT_LOG_COUNT)) {
1676 xfs_trans_cancel(tp, 0);
1677 return;
1678 }
1679 xfs_mod_sb(tp, fields);
1680 xfs_trans_commit(tp, 0, NULL);
1681}
David Chinner8d280b92006-03-14 13:13:09 +11001682
1683
1684#ifdef HAVE_PERCPU_SB
1685/*
1686 * Per-cpu incore superblock counters
1687 *
1688 * Simple concept, difficult implementation
1689 *
1690 * Basically, replace the incore superblock counters with a distributed per cpu
1691 * counter for contended fields (e.g. free block count).
1692 *
1693 * Difficulties arise in that the incore sb is used for ENOSPC checking, and
1694 * hence needs to be accurately read when we are running low on space. Hence
1695 * there is a method to enable and disable the per-cpu counters based on how
1696 * much "stuff" is available in them.
1697 *
1698 * Basically, a counter is enabled if there is enough free resource to justify
1699 * running a per-cpu fast-path. If the per-cpu counter runs out (i.e. a local
1700 * ENOSPC), then we disable the counters to synchronise all callers and
1701 * re-distribute the available resources.
1702 *
1703 * If, once we redistributed the available resources, we still get a failure,
1704 * we disable the per-cpu counter and go through the slow path.
1705 *
1706 * The slow path is the current xfs_mod_incore_sb() function. This means that
1707 * when we disable a per-cpu counter, we need to drain it's resources back to
1708 * the global superblock. We do this after disabling the counter to prevent
1709 * more threads from queueing up on the counter.
1710 *
1711 * Essentially, this means that we still need a lock in the fast path to enable
1712 * synchronisation between the global counters and the per-cpu counters. This
1713 * is not a problem because the lock will be local to a CPU almost all the time
1714 * and have little contention except when we get to ENOSPC conditions.
1715 *
1716 * Basically, this lock becomes a barrier that enables us to lock out the fast
1717 * path while we do things like enabling and disabling counters and
1718 * synchronising the counters.
1719 *
1720 * Locking rules:
1721 *
1722 * 1. XFS_SB_LOCK() before picking up per-cpu locks
1723 * 2. per-cpu locks always picked up via for_each_online_cpu() order
1724 * 3. accurate counter sync requires XFS_SB_LOCK + per cpu locks
1725 * 4. modifying per-cpu counters requires holding per-cpu lock
1726 * 5. modifying global counters requires holding XFS_SB_LOCK
1727 * 6. enabling or disabling a counter requires holding the XFS_SB_LOCK
1728 * and _none_ of the per-cpu locks.
1729 *
1730 * Disabled counters are only ever re-enabled by a balance operation
1731 * that results in more free resources per CPU than a given threshold.
1732 * To ensure counters don't remain disabled, they are rebalanced when
1733 * the global resource goes above a higher threshold (i.e. some hysteresis
1734 * is present to prevent thrashing).
David Chinner8d280b92006-03-14 13:13:09 +11001735 */
David Chinnere8234a62006-03-14 13:23:52 +11001736
1737/*
1738 * hot-plug CPU notifier support.
1739 *
1740 * We cannot use the hotcpu_register() function because it does
1741 * not allow notifier instances. We need a notifier per filesystem
1742 * as we need to be able to identify the filesystem to balance
Nathan Scottc41564b2006-03-29 08:55:14 +10001743 * the counters out. This is achieved by having a notifier block
David Chinnere8234a62006-03-14 13:23:52 +11001744 * embedded in the xfs_mount_t and doing pointer magic to get the
1745 * mount pointer from the notifier block address.
1746 */
1747STATIC int
1748xfs_icsb_cpu_notify(
1749 struct notifier_block *nfb,
1750 unsigned long action,
1751 void *hcpu)
1752{
1753 xfs_icsb_cnts_t *cntp;
1754 xfs_mount_t *mp;
1755 int s;
1756
1757 mp = (xfs_mount_t *)container_of(nfb, xfs_mount_t, m_icsb_notifier);
1758 cntp = (xfs_icsb_cnts_t *)
1759 per_cpu_ptr(mp->m_sb_cnts, (unsigned long)hcpu);
1760 switch (action) {
1761 case CPU_UP_PREPARE:
1762 /* Easy Case - initialize the area and locks, and
1763 * then rebalance when online does everything else for us. */
David Chinner01e1b692006-03-14 13:29:16 +11001764 memset(cntp, 0, sizeof(xfs_icsb_cnts_t));
David Chinnere8234a62006-03-14 13:23:52 +11001765 break;
1766 case CPU_ONLINE:
1767 xfs_icsb_balance_counter(mp, XFS_SBS_ICOUNT, 0);
1768 xfs_icsb_balance_counter(mp, XFS_SBS_IFREE, 0);
1769 xfs_icsb_balance_counter(mp, XFS_SBS_FDBLOCKS, 0);
1770 break;
1771 case CPU_DEAD:
1772 /* Disable all the counters, then fold the dead cpu's
1773 * count into the total on the global superblock and
1774 * re-enable the counters. */
1775 s = XFS_SB_LOCK(mp);
1776 xfs_icsb_disable_counter(mp, XFS_SBS_ICOUNT);
1777 xfs_icsb_disable_counter(mp, XFS_SBS_IFREE);
1778 xfs_icsb_disable_counter(mp, XFS_SBS_FDBLOCKS);
1779
1780 mp->m_sb.sb_icount += cntp->icsb_icount;
1781 mp->m_sb.sb_ifree += cntp->icsb_ifree;
1782 mp->m_sb.sb_fdblocks += cntp->icsb_fdblocks;
1783
David Chinner01e1b692006-03-14 13:29:16 +11001784 memset(cntp, 0, sizeof(xfs_icsb_cnts_t));
David Chinnere8234a62006-03-14 13:23:52 +11001785
1786 xfs_icsb_balance_counter(mp, XFS_SBS_ICOUNT, XFS_ICSB_SB_LOCKED);
1787 xfs_icsb_balance_counter(mp, XFS_SBS_IFREE, XFS_ICSB_SB_LOCKED);
1788 xfs_icsb_balance_counter(mp, XFS_SBS_FDBLOCKS, XFS_ICSB_SB_LOCKED);
1789 XFS_SB_UNLOCK(mp, s);
1790 break;
1791 }
1792
1793 return NOTIFY_OK;
1794}
1795
David Chinner8d280b92006-03-14 13:13:09 +11001796int
1797xfs_icsb_init_counters(
1798 xfs_mount_t *mp)
1799{
1800 xfs_icsb_cnts_t *cntp;
1801 int i;
1802
1803 mp->m_sb_cnts = alloc_percpu(xfs_icsb_cnts_t);
1804 if (mp->m_sb_cnts == NULL)
1805 return -ENOMEM;
1806
David Chinnere8234a62006-03-14 13:23:52 +11001807 mp->m_icsb_notifier.notifier_call = xfs_icsb_cpu_notify;
1808 mp->m_icsb_notifier.priority = 0;
1809 register_cpu_notifier(&mp->m_icsb_notifier);
1810
David Chinner8d280b92006-03-14 13:13:09 +11001811 for_each_online_cpu(i) {
1812 cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i);
David Chinner01e1b692006-03-14 13:29:16 +11001813 memset(cntp, 0, sizeof(xfs_icsb_cnts_t));
David Chinner8d280b92006-03-14 13:13:09 +11001814 }
1815 /*
1816 * start with all counters disabled so that the
1817 * initial balance kicks us off correctly
1818 */
1819 mp->m_icsb_counters = -1;
1820 return 0;
1821}
1822
1823STATIC void
1824xfs_icsb_destroy_counters(
1825 xfs_mount_t *mp)
1826{
David Chinnere8234a62006-03-14 13:23:52 +11001827 if (mp->m_sb_cnts) {
1828 unregister_cpu_notifier(&mp->m_icsb_notifier);
David Chinner8d280b92006-03-14 13:13:09 +11001829 free_percpu(mp->m_sb_cnts);
David Chinnere8234a62006-03-14 13:23:52 +11001830 }
David Chinner8d280b92006-03-14 13:13:09 +11001831}
1832
David Chinner01e1b692006-03-14 13:29:16 +11001833STATIC inline void
1834xfs_icsb_lock_cntr(
1835 xfs_icsb_cnts_t *icsbp)
1836{
1837 while (test_and_set_bit(XFS_ICSB_FLAG_LOCK, &icsbp->icsb_flags)) {
1838 ndelay(1000);
1839 }
1840}
1841
1842STATIC inline void
1843xfs_icsb_unlock_cntr(
1844 xfs_icsb_cnts_t *icsbp)
1845{
1846 clear_bit(XFS_ICSB_FLAG_LOCK, &icsbp->icsb_flags);
1847}
1848
David Chinner8d280b92006-03-14 13:13:09 +11001849
1850STATIC inline void
1851xfs_icsb_lock_all_counters(
1852 xfs_mount_t *mp)
1853{
1854 xfs_icsb_cnts_t *cntp;
1855 int i;
1856
1857 for_each_online_cpu(i) {
1858 cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i);
David Chinner01e1b692006-03-14 13:29:16 +11001859 xfs_icsb_lock_cntr(cntp);
David Chinner8d280b92006-03-14 13:13:09 +11001860 }
1861}
1862
1863STATIC inline void
1864xfs_icsb_unlock_all_counters(
1865 xfs_mount_t *mp)
1866{
1867 xfs_icsb_cnts_t *cntp;
1868 int i;
1869
1870 for_each_online_cpu(i) {
1871 cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i);
David Chinner01e1b692006-03-14 13:29:16 +11001872 xfs_icsb_unlock_cntr(cntp);
David Chinner8d280b92006-03-14 13:13:09 +11001873 }
1874}
1875
1876STATIC void
1877xfs_icsb_count(
1878 xfs_mount_t *mp,
1879 xfs_icsb_cnts_t *cnt,
1880 int flags)
1881{
1882 xfs_icsb_cnts_t *cntp;
1883 int i;
1884
1885 memset(cnt, 0, sizeof(xfs_icsb_cnts_t));
1886
1887 if (!(flags & XFS_ICSB_LAZY_COUNT))
1888 xfs_icsb_lock_all_counters(mp);
1889
1890 for_each_online_cpu(i) {
1891 cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i);
1892 cnt->icsb_icount += cntp->icsb_icount;
1893 cnt->icsb_ifree += cntp->icsb_ifree;
1894 cnt->icsb_fdblocks += cntp->icsb_fdblocks;
1895 }
1896
1897 if (!(flags & XFS_ICSB_LAZY_COUNT))
1898 xfs_icsb_unlock_all_counters(mp);
1899}
1900
1901STATIC int
1902xfs_icsb_counter_disabled(
1903 xfs_mount_t *mp,
1904 xfs_sb_field_t field)
1905{
1906 ASSERT((field >= XFS_SBS_ICOUNT) && (field <= XFS_SBS_FDBLOCKS));
1907 return test_bit(field, &mp->m_icsb_counters);
1908}
1909
1910STATIC int
1911xfs_icsb_disable_counter(
1912 xfs_mount_t *mp,
1913 xfs_sb_field_t field)
1914{
1915 xfs_icsb_cnts_t cnt;
1916
1917 ASSERT((field >= XFS_SBS_ICOUNT) && (field <= XFS_SBS_FDBLOCKS));
1918
1919 xfs_icsb_lock_all_counters(mp);
1920 if (!test_and_set_bit(field, &mp->m_icsb_counters)) {
1921 /* drain back to superblock */
1922
1923 xfs_icsb_count(mp, &cnt, XFS_ICSB_SB_LOCKED|XFS_ICSB_LAZY_COUNT);
1924 switch(field) {
1925 case XFS_SBS_ICOUNT:
1926 mp->m_sb.sb_icount = cnt.icsb_icount;
1927 break;
1928 case XFS_SBS_IFREE:
1929 mp->m_sb.sb_ifree = cnt.icsb_ifree;
1930 break;
1931 case XFS_SBS_FDBLOCKS:
1932 mp->m_sb.sb_fdblocks = cnt.icsb_fdblocks;
1933 break;
1934 default:
1935 BUG();
1936 }
1937 }
1938
1939 xfs_icsb_unlock_all_counters(mp);
1940
1941 return 0;
1942}
1943
1944STATIC void
1945xfs_icsb_enable_counter(
1946 xfs_mount_t *mp,
1947 xfs_sb_field_t field,
1948 uint64_t count,
1949 uint64_t resid)
1950{
1951 xfs_icsb_cnts_t *cntp;
1952 int i;
1953
1954 ASSERT((field >= XFS_SBS_ICOUNT) && (field <= XFS_SBS_FDBLOCKS));
1955
1956 xfs_icsb_lock_all_counters(mp);
1957 for_each_online_cpu(i) {
1958 cntp = per_cpu_ptr(mp->m_sb_cnts, i);
1959 switch (field) {
1960 case XFS_SBS_ICOUNT:
1961 cntp->icsb_icount = count + resid;
1962 break;
1963 case XFS_SBS_IFREE:
1964 cntp->icsb_ifree = count + resid;
1965 break;
1966 case XFS_SBS_FDBLOCKS:
1967 cntp->icsb_fdblocks = count + resid;
1968 break;
1969 default:
1970 BUG();
1971 break;
1972 }
1973 resid = 0;
1974 }
1975 clear_bit(field, &mp->m_icsb_counters);
1976 xfs_icsb_unlock_all_counters(mp);
1977}
1978
1979STATIC void
1980xfs_icsb_sync_counters_int(
1981 xfs_mount_t *mp,
1982 int flags)
1983{
1984 xfs_icsb_cnts_t cnt;
1985 int s;
1986
1987 /* Pass 1: lock all counters */
1988 if ((flags & XFS_ICSB_SB_LOCKED) == 0)
1989 s = XFS_SB_LOCK(mp);
1990
1991 xfs_icsb_count(mp, &cnt, flags);
1992
1993 /* Step 3: update mp->m_sb fields */
1994 if (!xfs_icsb_counter_disabled(mp, XFS_SBS_ICOUNT))
1995 mp->m_sb.sb_icount = cnt.icsb_icount;
1996 if (!xfs_icsb_counter_disabled(mp, XFS_SBS_IFREE))
1997 mp->m_sb.sb_ifree = cnt.icsb_ifree;
1998 if (!xfs_icsb_counter_disabled(mp, XFS_SBS_FDBLOCKS))
1999 mp->m_sb.sb_fdblocks = cnt.icsb_fdblocks;
2000
2001 if ((flags & XFS_ICSB_SB_LOCKED) == 0)
2002 XFS_SB_UNLOCK(mp, s);
2003}
2004
2005/*
2006 * Accurate update of per-cpu counters to incore superblock
2007 */
2008STATIC void
2009xfs_icsb_sync_counters(
2010 xfs_mount_t *mp)
2011{
2012 xfs_icsb_sync_counters_int(mp, 0);
2013}
2014
2015/*
2016 * lazy addition used for things like df, background sb syncs, etc
2017 */
2018void
2019xfs_icsb_sync_counters_lazy(
2020 xfs_mount_t *mp)
2021{
2022 xfs_icsb_sync_counters_int(mp, XFS_ICSB_LAZY_COUNT);
2023}
2024
2025/*
2026 * Balance and enable/disable counters as necessary.
2027 *
2028 * Thresholds for re-enabling counters are somewhat magic.
2029 * inode counts are chosen to be the same number as single
2030 * on disk allocation chunk per CPU, and free blocks is
2031 * something far enough zero that we aren't going thrash
2032 * when we get near ENOSPC.
2033 */
2034#define XFS_ICSB_INO_CNTR_REENABLE 64
2035#define XFS_ICSB_FDBLK_CNTR_REENABLE 512
2036STATIC void
2037xfs_icsb_balance_counter(
2038 xfs_mount_t *mp,
2039 xfs_sb_field_t field,
2040 int flags)
2041{
2042 uint64_t count, resid = 0;
2043 int weight = num_online_cpus();
2044 int s;
2045
2046 if (!(flags & XFS_ICSB_SB_LOCKED))
2047 s = XFS_SB_LOCK(mp);
2048
2049 /* disable counter and sync counter */
2050 xfs_icsb_disable_counter(mp, field);
2051
2052 /* update counters - first CPU gets residual*/
2053 switch (field) {
2054 case XFS_SBS_ICOUNT:
2055 count = mp->m_sb.sb_icount;
2056 resid = do_div(count, weight);
2057 if (count < XFS_ICSB_INO_CNTR_REENABLE)
2058 goto out;
2059 break;
2060 case XFS_SBS_IFREE:
2061 count = mp->m_sb.sb_ifree;
2062 resid = do_div(count, weight);
2063 if (count < XFS_ICSB_INO_CNTR_REENABLE)
2064 goto out;
2065 break;
2066 case XFS_SBS_FDBLOCKS:
2067 count = mp->m_sb.sb_fdblocks;
2068 resid = do_div(count, weight);
2069 if (count < XFS_ICSB_FDBLK_CNTR_REENABLE)
2070 goto out;
2071 break;
2072 default:
2073 BUG();
2074 break;
2075 }
2076
2077 xfs_icsb_enable_counter(mp, field, count, resid);
2078out:
2079 if (!(flags & XFS_ICSB_SB_LOCKED))
2080 XFS_SB_UNLOCK(mp, s);
2081}
2082
2083STATIC int
2084xfs_icsb_modify_counters_int(
2085 xfs_mount_t *mp,
2086 xfs_sb_field_t field,
2087 int delta,
2088 int rsvd,
2089 int flags)
2090{
2091 xfs_icsb_cnts_t *icsbp;
2092 long long lcounter; /* long counter for 64 bit fields */
2093 int cpu, s, locked = 0;
2094 int ret = 0, balance_done = 0;
2095
2096again:
2097 cpu = get_cpu();
2098 icsbp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, cpu),
David Chinner01e1b692006-03-14 13:29:16 +11002099 xfs_icsb_lock_cntr(icsbp);
David Chinner8d280b92006-03-14 13:13:09 +11002100 if (unlikely(xfs_icsb_counter_disabled(mp, field)))
2101 goto slow_path;
2102
2103 switch (field) {
2104 case XFS_SBS_ICOUNT:
2105 lcounter = icsbp->icsb_icount;
2106 lcounter += delta;
2107 if (unlikely(lcounter < 0))
2108 goto slow_path;
2109 icsbp->icsb_icount = lcounter;
2110 break;
2111
2112 case XFS_SBS_IFREE:
2113 lcounter = icsbp->icsb_ifree;
2114 lcounter += delta;
2115 if (unlikely(lcounter < 0))
2116 goto slow_path;
2117 icsbp->icsb_ifree = lcounter;
2118 break;
2119
2120 case XFS_SBS_FDBLOCKS:
2121 BUG_ON((mp->m_resblks - mp->m_resblks_avail) != 0);
2122
2123 lcounter = icsbp->icsb_fdblocks;
2124 lcounter += delta;
2125 if (unlikely(lcounter < 0))
2126 goto slow_path;
2127 icsbp->icsb_fdblocks = lcounter;
2128 break;
2129 default:
2130 BUG();
2131 break;
2132 }
David Chinner01e1b692006-03-14 13:29:16 +11002133 xfs_icsb_unlock_cntr(icsbp);
David Chinner8d280b92006-03-14 13:13:09 +11002134 put_cpu();
2135 if (locked)
2136 XFS_SB_UNLOCK(mp, s);
2137 return 0;
2138
2139 /*
2140 * The slow path needs to be run with the SBLOCK
2141 * held so that we prevent other threads from
2142 * attempting to run this path at the same time.
2143 * this provides exclusion for the balancing code,
2144 * and exclusive fallback if the balance does not
2145 * provide enough resources to continue in an unlocked
2146 * manner.
2147 */
2148slow_path:
David Chinner01e1b692006-03-14 13:29:16 +11002149 xfs_icsb_unlock_cntr(icsbp);
David Chinner8d280b92006-03-14 13:13:09 +11002150 put_cpu();
2151
2152 /* need to hold superblock incase we need
2153 * to disable a counter */
2154 if (!(flags & XFS_ICSB_SB_LOCKED)) {
2155 s = XFS_SB_LOCK(mp);
2156 locked = 1;
2157 flags |= XFS_ICSB_SB_LOCKED;
2158 }
2159 if (!balance_done) {
2160 xfs_icsb_balance_counter(mp, field, flags);
2161 balance_done = 1;
2162 goto again;
2163 } else {
2164 /*
2165 * we might not have enough on this local
2166 * cpu to allocate for a bulk request.
2167 * We need to drain this field from all CPUs
2168 * and disable the counter fastpath
2169 */
2170 xfs_icsb_disable_counter(mp, field);
2171 }
2172
2173 ret = xfs_mod_incore_sb_unlocked(mp, field, delta, rsvd);
2174
2175 if (locked)
2176 XFS_SB_UNLOCK(mp, s);
2177 return ret;
2178}
2179
2180STATIC int
2181xfs_icsb_modify_counters(
2182 xfs_mount_t *mp,
2183 xfs_sb_field_t field,
2184 int delta,
2185 int rsvd)
2186{
2187 return xfs_icsb_modify_counters_int(mp, field, delta, rsvd, 0);
2188}
2189
2190/*
2191 * Called when superblock is already locked
2192 */
2193STATIC int
2194xfs_icsb_modify_counters_locked(
2195 xfs_mount_t *mp,
2196 xfs_sb_field_t field,
2197 int delta,
2198 int rsvd)
2199{
2200 return xfs_icsb_modify_counters_int(mp, field, delta,
2201 rsvd, XFS_ICSB_SB_LOCKED);
2202}
2203#endif