[XFS] remove unused struct xfs_ail_ticket
[deliverable/linux.git] / fs / xfs / xfs_mount.c
CommitLineData
1da177e4 1/*
d8cc890d 2 * Copyright (c) 2000-2005 Silicon Graphics, Inc. All Rights Reserved.
1da177e4
LT
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11 *
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like. Any license provided herein, whether implied or
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc., 59
21 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
22 *
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
25 *
26 * http://www.sgi.com
27 *
28 * For further information regarding this notice, see:
29 *
30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
31 */
32
33#include "xfs.h"
34#include "xfs_macros.h"
35#include "xfs_types.h"
36#include "xfs_inum.h"
37#include "xfs_log.h"
38#include "xfs_trans.h"
39#include "xfs_sb.h"
40#include "xfs_ag.h"
41#include "xfs_dir.h"
42#include "xfs_dir2.h"
43#include "xfs_dmapi.h"
44#include "xfs_mount.h"
45#include "xfs_alloc_btree.h"
46#include "xfs_bmap_btree.h"
47#include "xfs_ialloc_btree.h"
48#include "xfs_btree.h"
49#include "xfs_ialloc.h"
50#include "xfs_attr_sf.h"
51#include "xfs_dir_sf.h"
52#include "xfs_dir2_sf.h"
53#include "xfs_dinode.h"
54#include "xfs_inode.h"
55#include "xfs_alloc.h"
56#include "xfs_rtalloc.h"
57#include "xfs_bmap.h"
58#include "xfs_error.h"
59#include "xfs_bit.h"
60#include "xfs_rw.h"
61#include "xfs_quota.h"
62#include "xfs_fsops.h"
63
64STATIC void xfs_mount_log_sbunit(xfs_mount_t *, __int64_t);
65STATIC int xfs_uuid_mount(xfs_mount_t *);
66STATIC void xfs_uuid_unmount(xfs_mount_t *mp);
ba0f32d4 67STATIC void xfs_unmountfs_wait(xfs_mount_t *);
1da177e4
LT
68
69static struct {
70 short offset;
71 short type; /* 0 = integer
72 * 1 = binary / string (no translation)
73 */
74} xfs_sb_info[] = {
75 { offsetof(xfs_sb_t, sb_magicnum), 0 },
76 { offsetof(xfs_sb_t, sb_blocksize), 0 },
77 { offsetof(xfs_sb_t, sb_dblocks), 0 },
78 { offsetof(xfs_sb_t, sb_rblocks), 0 },
79 { offsetof(xfs_sb_t, sb_rextents), 0 },
80 { offsetof(xfs_sb_t, sb_uuid), 1 },
81 { offsetof(xfs_sb_t, sb_logstart), 0 },
82 { offsetof(xfs_sb_t, sb_rootino), 0 },
83 { offsetof(xfs_sb_t, sb_rbmino), 0 },
84 { offsetof(xfs_sb_t, sb_rsumino), 0 },
85 { offsetof(xfs_sb_t, sb_rextsize), 0 },
86 { offsetof(xfs_sb_t, sb_agblocks), 0 },
87 { offsetof(xfs_sb_t, sb_agcount), 0 },
88 { offsetof(xfs_sb_t, sb_rbmblocks), 0 },
89 { offsetof(xfs_sb_t, sb_logblocks), 0 },
90 { offsetof(xfs_sb_t, sb_versionnum), 0 },
91 { offsetof(xfs_sb_t, sb_sectsize), 0 },
92 { offsetof(xfs_sb_t, sb_inodesize), 0 },
93 { offsetof(xfs_sb_t, sb_inopblock), 0 },
94 { offsetof(xfs_sb_t, sb_fname[0]), 1 },
95 { offsetof(xfs_sb_t, sb_blocklog), 0 },
96 { offsetof(xfs_sb_t, sb_sectlog), 0 },
97 { offsetof(xfs_sb_t, sb_inodelog), 0 },
98 { offsetof(xfs_sb_t, sb_inopblog), 0 },
99 { offsetof(xfs_sb_t, sb_agblklog), 0 },
100 { offsetof(xfs_sb_t, sb_rextslog), 0 },
101 { offsetof(xfs_sb_t, sb_inprogress), 0 },
102 { offsetof(xfs_sb_t, sb_imax_pct), 0 },
103 { offsetof(xfs_sb_t, sb_icount), 0 },
104 { offsetof(xfs_sb_t, sb_ifree), 0 },
105 { offsetof(xfs_sb_t, sb_fdblocks), 0 },
106 { offsetof(xfs_sb_t, sb_frextents), 0 },
107 { offsetof(xfs_sb_t, sb_uquotino), 0 },
108 { offsetof(xfs_sb_t, sb_gquotino), 0 },
109 { offsetof(xfs_sb_t, sb_qflags), 0 },
110 { offsetof(xfs_sb_t, sb_flags), 0 },
111 { offsetof(xfs_sb_t, sb_shared_vn), 0 },
112 { offsetof(xfs_sb_t, sb_inoalignmt), 0 },
113 { offsetof(xfs_sb_t, sb_unit), 0 },
114 { offsetof(xfs_sb_t, sb_width), 0 },
115 { offsetof(xfs_sb_t, sb_dirblklog), 0 },
116 { offsetof(xfs_sb_t, sb_logsectlog), 0 },
117 { offsetof(xfs_sb_t, sb_logsectsize),0 },
118 { offsetof(xfs_sb_t, sb_logsunit), 0 },
119 { offsetof(xfs_sb_t, sb_features2), 0 },
120 { sizeof(xfs_sb_t), 0 }
121};
122
123/*
124 * Return a pointer to an initialized xfs_mount structure.
125 */
126xfs_mount_t *
127xfs_mount_init(void)
128{
129 xfs_mount_t *mp;
130
131 mp = kmem_zalloc(sizeof(*mp), KM_SLEEP);
132
133 AIL_LOCKINIT(&mp->m_ail_lock, "xfs_ail");
134 spinlock_init(&mp->m_sb_lock, "xfs_sb");
135 mutex_init(&mp->m_ilock, MUTEX_DEFAULT, "xfs_ilock");
136 initnsema(&mp->m_growlock, 1, "xfs_grow");
137 /*
138 * Initialize the AIL.
139 */
140 xfs_trans_ail_init(mp);
141
142 atomic_set(&mp->m_active_trans, 0);
143
144 return mp;
145}
146
147/*
148 * Free up the resources associated with a mount structure. Assume that
149 * the structure was initially zeroed, so we can tell which fields got
150 * initialized.
151 */
152void
153xfs_mount_free(
154 xfs_mount_t *mp,
155 int remove_bhv)
156{
157 if (mp->m_ihash)
158 xfs_ihash_free(mp);
159 if (mp->m_chash)
160 xfs_chash_free(mp);
161
162 if (mp->m_perag) {
163 int agno;
164
165 for (agno = 0; agno < mp->m_maxagi; agno++)
166 if (mp->m_perag[agno].pagb_list)
167 kmem_free(mp->m_perag[agno].pagb_list,
168 sizeof(xfs_perag_busy_t) *
169 XFS_PAGB_NUM_SLOTS);
170 kmem_free(mp->m_perag,
171 sizeof(xfs_perag_t) * mp->m_sb.sb_agcount);
172 }
173
174 AIL_LOCK_DESTROY(&mp->m_ail_lock);
175 spinlock_destroy(&mp->m_sb_lock);
176 mutex_destroy(&mp->m_ilock);
177 freesema(&mp->m_growlock);
178 if (mp->m_quotainfo)
179 XFS_QM_DONE(mp);
180
181 if (mp->m_fsname != NULL)
182 kmem_free(mp->m_fsname, mp->m_fsname_len);
fc1f8c1c
NS
183 if (mp->m_rtname != NULL)
184 kmem_free(mp->m_rtname, strlen(mp->m_rtname) + 1);
185 if (mp->m_logname != NULL)
186 kmem_free(mp->m_logname, strlen(mp->m_logname) + 1);
1da177e4
LT
187
188 if (remove_bhv) {
189 struct vfs *vfsp = XFS_MTOVFS(mp);
190
191 bhv_remove_all_vfsops(vfsp, 0);
192 VFS_REMOVEBHV(vfsp, &mp->m_bhv);
193 }
194
195 kmem_free(mp, sizeof(xfs_mount_t));
196}
197
198
199/*
200 * Check the validity of the SB found.
201 */
202STATIC int
203xfs_mount_validate_sb(
204 xfs_mount_t *mp,
205 xfs_sb_t *sbp)
206{
207 /*
208 * If the log device and data device have the
209 * same device number, the log is internal.
210 * Consequently, the sb_logstart should be non-zero. If
211 * we have a zero sb_logstart in this case, we may be trying to mount
212 * a volume filesystem in a non-volume manner.
213 */
214 if (sbp->sb_magicnum != XFS_SB_MAGIC) {
215 cmn_err(CE_WARN, "XFS: bad magic number");
216 return XFS_ERROR(EWRONGFS);
217 }
218
219 if (!XFS_SB_GOOD_VERSION(sbp)) {
220 cmn_err(CE_WARN, "XFS: bad version");
221 return XFS_ERROR(EWRONGFS);
222 }
223
224 if (unlikely(
225 sbp->sb_logstart == 0 && mp->m_logdev_targp == mp->m_ddev_targp)) {
226 cmn_err(CE_WARN,
227 "XFS: filesystem is marked as having an external log; "
228 "specify logdev on the\nmount command line.");
229 XFS_CORRUPTION_ERROR("xfs_mount_validate_sb(1)",
230 XFS_ERRLEVEL_HIGH, mp, sbp);
231 return XFS_ERROR(EFSCORRUPTED);
232 }
233
234 if (unlikely(
235 sbp->sb_logstart != 0 && mp->m_logdev_targp != mp->m_ddev_targp)) {
236 cmn_err(CE_WARN,
237 "XFS: filesystem is marked as having an internal log; "
238 "don't specify logdev on\nthe mount command line.");
239 XFS_CORRUPTION_ERROR("xfs_mount_validate_sb(2)",
240 XFS_ERRLEVEL_HIGH, mp, sbp);
241 return XFS_ERROR(EFSCORRUPTED);
242 }
243
244 /*
245 * More sanity checking. These were stolen directly from
246 * xfs_repair.
247 */
248 if (unlikely(
249 sbp->sb_agcount <= 0 ||
250 sbp->sb_sectsize < XFS_MIN_SECTORSIZE ||
251 sbp->sb_sectsize > XFS_MAX_SECTORSIZE ||
252 sbp->sb_sectlog < XFS_MIN_SECTORSIZE_LOG ||
253 sbp->sb_sectlog > XFS_MAX_SECTORSIZE_LOG ||
254 sbp->sb_blocksize < XFS_MIN_BLOCKSIZE ||
255 sbp->sb_blocksize > XFS_MAX_BLOCKSIZE ||
256 sbp->sb_blocklog < XFS_MIN_BLOCKSIZE_LOG ||
257 sbp->sb_blocklog > XFS_MAX_BLOCKSIZE_LOG ||
258 sbp->sb_inodesize < XFS_DINODE_MIN_SIZE ||
259 sbp->sb_inodesize > XFS_DINODE_MAX_SIZE ||
260 (sbp->sb_rextsize * sbp->sb_blocksize > XFS_MAX_RTEXTSIZE) ||
261 (sbp->sb_rextsize * sbp->sb_blocksize < XFS_MIN_RTEXTSIZE) ||
262 sbp->sb_imax_pct > 100)) {
263 cmn_err(CE_WARN, "XFS: SB sanity check 1 failed");
264 XFS_CORRUPTION_ERROR("xfs_mount_validate_sb(3)",
265 XFS_ERRLEVEL_LOW, mp, sbp);
266 return XFS_ERROR(EFSCORRUPTED);
267 }
268
269 /*
270 * Sanity check AG count, size fields against data size field
271 */
272 if (unlikely(
273 sbp->sb_dblocks == 0 ||
274 sbp->sb_dblocks >
275 (xfs_drfsbno_t)sbp->sb_agcount * sbp->sb_agblocks ||
276 sbp->sb_dblocks < (xfs_drfsbno_t)(sbp->sb_agcount - 1) *
277 sbp->sb_agblocks + XFS_MIN_AG_BLOCKS)) {
278 cmn_err(CE_WARN, "XFS: SB sanity check 2 failed");
279 XFS_ERROR_REPORT("xfs_mount_validate_sb(4)",
280 XFS_ERRLEVEL_LOW, mp);
281 return XFS_ERROR(EFSCORRUPTED);
282 }
283
284 ASSERT(PAGE_SHIFT >= sbp->sb_blocklog);
285 ASSERT(sbp->sb_blocklog >= BBSHIFT);
286
287#if XFS_BIG_BLKNOS /* Limited by ULONG_MAX of page cache index */
288 if (unlikely(
289 (sbp->sb_dblocks >> (PAGE_SHIFT - sbp->sb_blocklog)) > ULONG_MAX ||
290 (sbp->sb_rblocks >> (PAGE_SHIFT - sbp->sb_blocklog)) > ULONG_MAX)) {
291#else /* Limited by UINT_MAX of sectors */
292 if (unlikely(
293 (sbp->sb_dblocks << (sbp->sb_blocklog - BBSHIFT)) > UINT_MAX ||
294 (sbp->sb_rblocks << (sbp->sb_blocklog - BBSHIFT)) > UINT_MAX)) {
295#endif
296 cmn_err(CE_WARN,
297 "XFS: File system is too large to be mounted on this system.");
298 return XFS_ERROR(E2BIG);
299 }
300
301 if (unlikely(sbp->sb_inprogress)) {
302 cmn_err(CE_WARN, "XFS: file system busy");
303 XFS_ERROR_REPORT("xfs_mount_validate_sb(5)",
304 XFS_ERRLEVEL_LOW, mp);
305 return XFS_ERROR(EFSCORRUPTED);
306 }
307
de20614b
NS
308 /*
309 * Version 1 directory format has never worked on Linux.
310 */
311 if (unlikely(!XFS_SB_VERSION_HASDIRV2(sbp))) {
312 cmn_err(CE_WARN,
313 "XFS: Attempted to mount file system using version 1 directory format");
314 return XFS_ERROR(ENOSYS);
315 }
316
1da177e4
LT
317 /*
318 * Until this is fixed only page-sized or smaller data blocks work.
319 */
320 if (unlikely(sbp->sb_blocksize > PAGE_SIZE)) {
321 cmn_err(CE_WARN,
322 "XFS: Attempted to mount file system with blocksize %d bytes",
323 sbp->sb_blocksize);
324 cmn_err(CE_WARN,
da1650a5 325 "XFS: Only page-sized (%ld) or less blocksizes currently work.",
1da177e4
LT
326 PAGE_SIZE);
327 return XFS_ERROR(ENOSYS);
328 }
329
330 return 0;
331}
332
333xfs_agnumber_t
334xfs_initialize_perag(xfs_mount_t *mp, xfs_agnumber_t agcount)
335{
336 xfs_agnumber_t index, max_metadata;
337 xfs_perag_t *pag;
338 xfs_agino_t agino;
339 xfs_ino_t ino;
340 xfs_sb_t *sbp = &mp->m_sb;
341 xfs_ino_t max_inum = XFS_MAXINUMBER_32;
342
343 /* Check to see if the filesystem can overflow 32 bit inodes */
344 agino = XFS_OFFBNO_TO_AGINO(mp, sbp->sb_agblocks - 1, 0);
345 ino = XFS_AGINO_TO_INO(mp, agcount - 1, agino);
346
347 /* Clear the mount flag if no inode can overflow 32 bits
348 * on this filesystem, or if specifically requested..
349 */
350 if ((mp->m_flags & XFS_MOUNT_32BITINOOPT) && ino > max_inum) {
351 mp->m_flags |= XFS_MOUNT_32BITINODES;
352 } else {
353 mp->m_flags &= ~XFS_MOUNT_32BITINODES;
354 }
355
356 /* If we can overflow then setup the ag headers accordingly */
357 if (mp->m_flags & XFS_MOUNT_32BITINODES) {
358 /* Calculate how much should be reserved for inodes to
359 * meet the max inode percentage.
360 */
361 if (mp->m_maxicount) {
362 __uint64_t icount;
363
364 icount = sbp->sb_dblocks * sbp->sb_imax_pct;
365 do_div(icount, 100);
366 icount += sbp->sb_agblocks - 1;
367 do_div(icount, mp->m_ialloc_blks);
368 max_metadata = icount;
369 } else {
370 max_metadata = agcount;
371 }
372 for (index = 0; index < agcount; index++) {
373 ino = XFS_AGINO_TO_INO(mp, index, agino);
374 if (ino > max_inum) {
375 index++;
376 break;
377 }
378
379 /* This ag is prefered for inodes */
380 pag = &mp->m_perag[index];
381 pag->pagi_inodeok = 1;
382 if (index < max_metadata)
383 pag->pagf_metadata = 1;
384 }
385 } else {
386 /* Setup default behavior for smaller filesystems */
387 for (index = 0; index < agcount; index++) {
388 pag = &mp->m_perag[index];
389 pag->pagi_inodeok = 1;
390 }
391 }
392 return index;
393}
394
395/*
396 * xfs_xlatesb
397 *
398 * data - on disk version of sb
399 * sb - a superblock
400 * dir - conversion direction: <0 - convert sb to buf
401 * >0 - convert buf to sb
402 * fields - which fields to copy (bitmask)
403 */
404void
405xfs_xlatesb(
406 void *data,
407 xfs_sb_t *sb,
408 int dir,
409 __int64_t fields)
410{
411 xfs_caddr_t buf_ptr;
412 xfs_caddr_t mem_ptr;
413 xfs_sb_field_t f;
414 int first;
415 int size;
416
417 ASSERT(dir);
418 ASSERT(fields);
419
420 if (!fields)
421 return;
422
423 buf_ptr = (xfs_caddr_t)data;
424 mem_ptr = (xfs_caddr_t)sb;
425
426 while (fields) {
427 f = (xfs_sb_field_t)xfs_lowbit64((__uint64_t)fields);
428 first = xfs_sb_info[f].offset;
429 size = xfs_sb_info[f + 1].offset - first;
430
431 ASSERT(xfs_sb_info[f].type == 0 || xfs_sb_info[f].type == 1);
432
433 if (size == 1 || xfs_sb_info[f].type == 1) {
434 if (dir > 0) {
435 memcpy(mem_ptr + first, buf_ptr + first, size);
436 } else {
437 memcpy(buf_ptr + first, mem_ptr + first, size);
438 }
439 } else {
440 switch (size) {
441 case 2:
442 INT_XLATE(*(__uint16_t*)(buf_ptr+first),
443 *(__uint16_t*)(mem_ptr+first),
444 dir, ARCH_CONVERT);
445 break;
446 case 4:
447 INT_XLATE(*(__uint32_t*)(buf_ptr+first),
448 *(__uint32_t*)(mem_ptr+first),
449 dir, ARCH_CONVERT);
450 break;
451 case 8:
452 INT_XLATE(*(__uint64_t*)(buf_ptr+first),
453 *(__uint64_t*)(mem_ptr+first), dir, ARCH_CONVERT);
454 break;
455 default:
456 ASSERT(0);
457 }
458 }
459
460 fields &= ~(1LL << f);
461 }
462}
463
464/*
465 * xfs_readsb
466 *
467 * Does the initial read of the superblock.
468 */
469int
470xfs_readsb(xfs_mount_t *mp)
471{
472 unsigned int sector_size;
473 unsigned int extra_flags;
474 xfs_buf_t *bp;
475 xfs_sb_t *sbp;
476 int error;
477
478 ASSERT(mp->m_sb_bp == NULL);
479 ASSERT(mp->m_ddev_targp != NULL);
480
481 /*
482 * Allocate a (locked) buffer to hold the superblock.
483 * This will be kept around at all times to optimize
484 * access to the superblock.
485 */
486 sector_size = xfs_getsize_buftarg(mp->m_ddev_targp);
487 extra_flags = XFS_BUF_LOCK | XFS_BUF_MANAGE | XFS_BUF_MAPPED;
488
489 bp = xfs_buf_read_flags(mp->m_ddev_targp, XFS_SB_DADDR,
490 BTOBB(sector_size), extra_flags);
491 if (!bp || XFS_BUF_ISERROR(bp)) {
492 cmn_err(CE_WARN, "XFS: SB read failed");
493 error = bp ? XFS_BUF_GETERROR(bp) : ENOMEM;
494 goto fail;
495 }
496 ASSERT(XFS_BUF_ISBUSY(bp));
497 ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
498
499 /*
500 * Initialize the mount structure from the superblock.
501 * But first do some basic consistency checking.
502 */
503 sbp = XFS_BUF_TO_SBP(bp);
504 xfs_xlatesb(XFS_BUF_PTR(bp), &(mp->m_sb), 1, XFS_SB_ALL_BITS);
505
506 error = xfs_mount_validate_sb(mp, &(mp->m_sb));
507 if (error) {
508 cmn_err(CE_WARN, "XFS: SB validate failed");
509 goto fail;
510 }
511
512 /*
513 * We must be able to do sector-sized and sector-aligned IO.
514 */
515 if (sector_size > mp->m_sb.sb_sectsize) {
516 cmn_err(CE_WARN,
517 "XFS: device supports only %u byte sectors (not %u)",
518 sector_size, mp->m_sb.sb_sectsize);
519 error = ENOSYS;
520 goto fail;
521 }
522
523 /*
524 * If device sector size is smaller than the superblock size,
525 * re-read the superblock so the buffer is correctly sized.
526 */
527 if (sector_size < mp->m_sb.sb_sectsize) {
528 XFS_BUF_UNMANAGE(bp);
529 xfs_buf_relse(bp);
530 sector_size = mp->m_sb.sb_sectsize;
531 bp = xfs_buf_read_flags(mp->m_ddev_targp, XFS_SB_DADDR,
532 BTOBB(sector_size), extra_flags);
533 if (!bp || XFS_BUF_ISERROR(bp)) {
534 cmn_err(CE_WARN, "XFS: SB re-read failed");
535 error = bp ? XFS_BUF_GETERROR(bp) : ENOMEM;
536 goto fail;
537 }
538 ASSERT(XFS_BUF_ISBUSY(bp));
539 ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
540 }
541
542 mp->m_sb_bp = bp;
543 xfs_buf_relse(bp);
544 ASSERT(XFS_BUF_VALUSEMA(bp) > 0);
545 return 0;
546
547 fail:
548 if (bp) {
549 XFS_BUF_UNMANAGE(bp);
550 xfs_buf_relse(bp);
551 }
552 return error;
553}
554
555
556/*
557 * xfs_mount_common
558 *
559 * Mount initialization code establishing various mount
560 * fields from the superblock associated with the given
561 * mount structure
562 */
ba0f32d4 563STATIC void
1da177e4
LT
564xfs_mount_common(xfs_mount_t *mp, xfs_sb_t *sbp)
565{
566 int i;
567
568 mp->m_agfrotor = mp->m_agirotor = 0;
569 spinlock_init(&mp->m_agirotor_lock, "m_agirotor_lock");
570 mp->m_maxagi = mp->m_sb.sb_agcount;
571 mp->m_blkbit_log = sbp->sb_blocklog + XFS_NBBYLOG;
572 mp->m_blkbb_log = sbp->sb_blocklog - BBSHIFT;
573 mp->m_sectbb_log = sbp->sb_sectlog - BBSHIFT;
574 mp->m_agno_log = xfs_highbit32(sbp->sb_agcount - 1) + 1;
575 mp->m_agino_log = sbp->sb_inopblog + sbp->sb_agblklog;
576 mp->m_litino = sbp->sb_inodesize -
577 ((uint)sizeof(xfs_dinode_core_t) + (uint)sizeof(xfs_agino_t));
578 mp->m_blockmask = sbp->sb_blocksize - 1;
579 mp->m_blockwsize = sbp->sb_blocksize >> XFS_WORDLOG;
580 mp->m_blockwmask = mp->m_blockwsize - 1;
581 INIT_LIST_HEAD(&mp->m_del_inodes);
582
583 /*
584 * Setup for attributes, in case they get created.
585 * This value is for inodes getting attributes for the first time,
586 * the per-inode value is for old attribute values.
587 */
588 ASSERT(sbp->sb_inodesize >= 256 && sbp->sb_inodesize <= 2048);
589 switch (sbp->sb_inodesize) {
590 case 256:
d8cc890d
NS
591 mp->m_attroffset = XFS_LITINO(mp) -
592 XFS_BMDR_SPACE_CALC(MINABTPTRS);
1da177e4
LT
593 break;
594 case 512:
595 case 1024:
596 case 2048:
d8cc890d 597 mp->m_attroffset = XFS_BMDR_SPACE_CALC(6 * MINABTPTRS);
1da177e4
LT
598 break;
599 default:
600 ASSERT(0);
601 }
602 ASSERT(mp->m_attroffset < XFS_LITINO(mp));
603
604 for (i = 0; i < 2; i++) {
605 mp->m_alloc_mxr[i] = XFS_BTREE_BLOCK_MAXRECS(sbp->sb_blocksize,
606 xfs_alloc, i == 0);
607 mp->m_alloc_mnr[i] = XFS_BTREE_BLOCK_MINRECS(sbp->sb_blocksize,
608 xfs_alloc, i == 0);
609 }
610 for (i = 0; i < 2; i++) {
611 mp->m_bmap_dmxr[i] = XFS_BTREE_BLOCK_MAXRECS(sbp->sb_blocksize,
612 xfs_bmbt, i == 0);
613 mp->m_bmap_dmnr[i] = XFS_BTREE_BLOCK_MINRECS(sbp->sb_blocksize,
614 xfs_bmbt, i == 0);
615 }
616 for (i = 0; i < 2; i++) {
617 mp->m_inobt_mxr[i] = XFS_BTREE_BLOCK_MAXRECS(sbp->sb_blocksize,
618 xfs_inobt, i == 0);
619 mp->m_inobt_mnr[i] = XFS_BTREE_BLOCK_MINRECS(sbp->sb_blocksize,
620 xfs_inobt, i == 0);
621 }
622
623 mp->m_bsize = XFS_FSB_TO_BB(mp, 1);
624 mp->m_ialloc_inos = (int)MAX((__uint16_t)XFS_INODES_PER_CHUNK,
625 sbp->sb_inopblock);
626 mp->m_ialloc_blks = mp->m_ialloc_inos >> sbp->sb_inopblog;
627}
628/*
629 * xfs_mountfs
630 *
631 * This function does the following on an initial mount of a file system:
632 * - reads the superblock from disk and init the mount struct
633 * - if we're a 32-bit kernel, do a size check on the superblock
634 * so we don't mount terabyte filesystems
635 * - init mount struct realtime fields
636 * - allocate inode hash table for fs
637 * - init directory manager
638 * - perform recovery and init the log manager
639 */
640int
641xfs_mountfs(
642 vfs_t *vfsp,
643 xfs_mount_t *mp,
644 int mfsi_flags)
645{
646 xfs_buf_t *bp;
647 xfs_sb_t *sbp = &(mp->m_sb);
648 xfs_inode_t *rip;
649 vnode_t *rvp = NULL;
650 int readio_log, writeio_log;
651 xfs_daddr_t d;
652 __uint64_t ret64;
653 __int64_t update_flags;
654 uint quotamount, quotaflags;
655 int agno;
656 int uuid_mounted = 0;
657 int error = 0;
658
659 if (mp->m_sb_bp == NULL) {
660 if ((error = xfs_readsb(mp))) {
661 return (error);
662 }
663 }
664 xfs_mount_common(mp, sbp);
665
666 /*
667 * Check if sb_agblocks is aligned at stripe boundary
668 * If sb_agblocks is NOT aligned turn off m_dalign since
669 * allocator alignment is within an ag, therefore ag has
670 * to be aligned at stripe boundary.
671 */
672 update_flags = 0LL;
673 if (mp->m_dalign && !(mfsi_flags & XFS_MFSI_SECOND)) {
674 /*
675 * If stripe unit and stripe width are not multiples
676 * of the fs blocksize turn off alignment.
677 */
678 if ((BBTOB(mp->m_dalign) & mp->m_blockmask) ||
679 (BBTOB(mp->m_swidth) & mp->m_blockmask)) {
680 if (mp->m_flags & XFS_MOUNT_RETERR) {
681 cmn_err(CE_WARN,
682 "XFS: alignment check 1 failed");
683 error = XFS_ERROR(EINVAL);
684 goto error1;
685 }
686 mp->m_dalign = mp->m_swidth = 0;
687 } else {
688 /*
689 * Convert the stripe unit and width to FSBs.
690 */
691 mp->m_dalign = XFS_BB_TO_FSBT(mp, mp->m_dalign);
692 if (mp->m_dalign && (sbp->sb_agblocks % mp->m_dalign)) {
693 if (mp->m_flags & XFS_MOUNT_RETERR) {
694 error = XFS_ERROR(EINVAL);
695 goto error1;
696 }
697 xfs_fs_cmn_err(CE_WARN, mp,
698"stripe alignment turned off: sunit(%d)/swidth(%d) incompatible with agsize(%d)",
699 mp->m_dalign, mp->m_swidth,
700 sbp->sb_agblocks);
701
702 mp->m_dalign = 0;
703 mp->m_swidth = 0;
704 } else if (mp->m_dalign) {
705 mp->m_swidth = XFS_BB_TO_FSBT(mp, mp->m_swidth);
706 } else {
707 if (mp->m_flags & XFS_MOUNT_RETERR) {
708 xfs_fs_cmn_err(CE_WARN, mp,
709"stripe alignment turned off: sunit(%d) less than bsize(%d)",
710 mp->m_dalign,
711 mp->m_blockmask +1);
712 error = XFS_ERROR(EINVAL);
713 goto error1;
714 }
715 mp->m_swidth = 0;
716 }
717 }
718
719 /*
720 * Update superblock with new values
721 * and log changes
722 */
723 if (XFS_SB_VERSION_HASDALIGN(sbp)) {
724 if (sbp->sb_unit != mp->m_dalign) {
725 sbp->sb_unit = mp->m_dalign;
726 update_flags |= XFS_SB_UNIT;
727 }
728 if (sbp->sb_width != mp->m_swidth) {
729 sbp->sb_width = mp->m_swidth;
730 update_flags |= XFS_SB_WIDTH;
731 }
732 }
733 } else if ((mp->m_flags & XFS_MOUNT_NOALIGN) != XFS_MOUNT_NOALIGN &&
734 XFS_SB_VERSION_HASDALIGN(&mp->m_sb)) {
735 mp->m_dalign = sbp->sb_unit;
736 mp->m_swidth = sbp->sb_width;
737 }
738
739 xfs_alloc_compute_maxlevels(mp);
740 xfs_bmap_compute_maxlevels(mp, XFS_DATA_FORK);
741 xfs_bmap_compute_maxlevels(mp, XFS_ATTR_FORK);
742 xfs_ialloc_compute_maxlevels(mp);
743
744 if (sbp->sb_imax_pct) {
745 __uint64_t icount;
746
747 /* Make sure the maximum inode count is a multiple of the
748 * units we allocate inodes in.
749 */
750
751 icount = sbp->sb_dblocks * sbp->sb_imax_pct;
752 do_div(icount, 100);
753 do_div(icount, mp->m_ialloc_blks);
754 mp->m_maxicount = (icount * mp->m_ialloc_blks) <<
755 sbp->sb_inopblog;
756 } else
757 mp->m_maxicount = 0;
758
759 mp->m_maxioffset = xfs_max_file_offset(sbp->sb_blocklog);
760
761 /*
762 * XFS uses the uuid from the superblock as the unique
763 * identifier for fsid. We can not use the uuid from the volume
764 * since a single partition filesystem is identical to a single
765 * partition volume/filesystem.
766 */
767 if ((mfsi_flags & XFS_MFSI_SECOND) == 0 &&
768 (mp->m_flags & XFS_MOUNT_NOUUID) == 0) {
769 if (xfs_uuid_mount(mp)) {
770 error = XFS_ERROR(EINVAL);
771 goto error1;
772 }
773 uuid_mounted=1;
774 ret64 = uuid_hash64(&sbp->sb_uuid);
775 memcpy(&vfsp->vfs_fsid, &ret64, sizeof(ret64));
776 }
777
778 /*
779 * Set the default minimum read and write sizes unless
780 * already specified in a mount option.
781 * We use smaller I/O sizes when the file system
782 * is being used for NFS service (wsync mount option).
783 */
784 if (!(mp->m_flags & XFS_MOUNT_DFLT_IOSIZE)) {
785 if (mp->m_flags & XFS_MOUNT_WSYNC) {
786 readio_log = XFS_WSYNC_READIO_LOG;
787 writeio_log = XFS_WSYNC_WRITEIO_LOG;
788 } else {
789 readio_log = XFS_READIO_LOG_LARGE;
790 writeio_log = XFS_WRITEIO_LOG_LARGE;
791 }
792 } else {
793 readio_log = mp->m_readio_log;
794 writeio_log = mp->m_writeio_log;
795 }
796
797 /*
798 * Set the number of readahead buffers to use based on
799 * physical memory size.
800 */
801 if (xfs_physmem <= 4096) /* <= 16MB */
802 mp->m_nreadaheads = XFS_RW_NREADAHEAD_16MB;
803 else if (xfs_physmem <= 8192) /* <= 32MB */
804 mp->m_nreadaheads = XFS_RW_NREADAHEAD_32MB;
805 else
806 mp->m_nreadaheads = XFS_RW_NREADAHEAD_K32;
807 if (sbp->sb_blocklog > readio_log) {
808 mp->m_readio_log = sbp->sb_blocklog;
809 } else {
810 mp->m_readio_log = readio_log;
811 }
812 mp->m_readio_blocks = 1 << (mp->m_readio_log - sbp->sb_blocklog);
813 if (sbp->sb_blocklog > writeio_log) {
814 mp->m_writeio_log = sbp->sb_blocklog;
815 } else {
816 mp->m_writeio_log = writeio_log;
817 }
818 mp->m_writeio_blocks = 1 << (mp->m_writeio_log - sbp->sb_blocklog);
819
820 /*
821 * Set the inode cluster size based on the physical memory
822 * size. This may still be overridden by the file system
823 * block size if it is larger than the chosen cluster size.
824 */
825 if (xfs_physmem <= btoc(32 * 1024 * 1024)) { /* <= 32 MB */
826 mp->m_inode_cluster_size = XFS_INODE_SMALL_CLUSTER_SIZE;
827 } else {
828 mp->m_inode_cluster_size = XFS_INODE_BIG_CLUSTER_SIZE;
829 }
830 /*
831 * Set whether we're using inode alignment.
832 */
833 if (XFS_SB_VERSION_HASALIGN(&mp->m_sb) &&
834 mp->m_sb.sb_inoalignmt >=
835 XFS_B_TO_FSBT(mp, mp->m_inode_cluster_size))
836 mp->m_inoalign_mask = mp->m_sb.sb_inoalignmt - 1;
837 else
838 mp->m_inoalign_mask = 0;
839 /*
840 * If we are using stripe alignment, check whether
841 * the stripe unit is a multiple of the inode alignment
842 */
843 if (mp->m_dalign && mp->m_inoalign_mask &&
844 !(mp->m_dalign & mp->m_inoalign_mask))
845 mp->m_sinoalign = mp->m_dalign;
846 else
847 mp->m_sinoalign = 0;
848 /*
849 * Check that the data (and log if separate) are an ok size.
850 */
851 d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks);
852 if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_dblocks) {
853 cmn_err(CE_WARN, "XFS: size check 1 failed");
854 error = XFS_ERROR(E2BIG);
855 goto error1;
856 }
857 error = xfs_read_buf(mp, mp->m_ddev_targp,
858 d - XFS_FSS_TO_BB(mp, 1),
859 XFS_FSS_TO_BB(mp, 1), 0, &bp);
860 if (!error) {
861 xfs_buf_relse(bp);
862 } else {
863 cmn_err(CE_WARN, "XFS: size check 2 failed");
864 if (error == ENOSPC) {
865 error = XFS_ERROR(E2BIG);
866 }
867 goto error1;
868 }
869
870 if (((mfsi_flags & XFS_MFSI_CLIENT) == 0) &&
871 mp->m_logdev_targp != mp->m_ddev_targp) {
872 d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_logblocks);
873 if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_logblocks) {
874 cmn_err(CE_WARN, "XFS: size check 3 failed");
875 error = XFS_ERROR(E2BIG);
876 goto error1;
877 }
878 error = xfs_read_buf(mp, mp->m_logdev_targp,
879 d - XFS_FSB_TO_BB(mp, 1),
880 XFS_FSB_TO_BB(mp, 1), 0, &bp);
881 if (!error) {
882 xfs_buf_relse(bp);
883 } else {
884 cmn_err(CE_WARN, "XFS: size check 3 failed");
885 if (error == ENOSPC) {
886 error = XFS_ERROR(E2BIG);
887 }
888 goto error1;
889 }
890 }
891
892 /*
893 * Initialize realtime fields in the mount structure
894 */
895 if ((error = xfs_rtmount_init(mp))) {
896 cmn_err(CE_WARN, "XFS: RT mount failed");
897 goto error1;
898 }
899
900 /*
901 * For client case we are done now
902 */
903 if (mfsi_flags & XFS_MFSI_CLIENT) {
904 return(0);
905 }
906
907 /*
908 * Copies the low order bits of the timestamp and the randomly
909 * set "sequence" number out of a UUID.
910 */
911 uuid_getnodeuniq(&sbp->sb_uuid, mp->m_fixedfsid);
912
913 /*
914 * The vfs structure needs to have a file system independent
915 * way of checking for the invariant file system ID. Since it
916 * can't look at mount structures it has a pointer to the data
917 * in the mount structure.
918 *
919 * File systems that don't support user level file handles (i.e.
920 * all of them except for XFS) will leave vfs_altfsid as NULL.
921 */
922 vfsp->vfs_altfsid = (xfs_fsid_t *)mp->m_fixedfsid;
923 mp->m_dmevmask = 0; /* not persistent; set after each mount */
924
925 /*
926 * Select the right directory manager.
927 */
928 mp->m_dirops =
929 XFS_SB_VERSION_HASDIRV2(&mp->m_sb) ?
930 xfsv2_dirops :
931 xfsv1_dirops;
932
933 /*
934 * Initialize directory manager's entries.
935 */
936 XFS_DIR_MOUNT(mp);
937
938 /*
939 * Initialize the attribute manager's entries.
940 */
941 mp->m_attr_magicpct = (mp->m_sb.sb_blocksize * 37) / 100;
942
943 /*
944 * Initialize the precomputed transaction reservations values.
945 */
946 xfs_trans_init(mp);
947
948 /*
949 * Allocate and initialize the inode hash table for this
950 * file system.
951 */
952 xfs_ihash_init(mp);
953 xfs_chash_init(mp);
954
955 /*
956 * Allocate and initialize the per-ag data.
957 */
958 init_rwsem(&mp->m_peraglock);
959 mp->m_perag =
960 kmem_zalloc(sbp->sb_agcount * sizeof(xfs_perag_t), KM_SLEEP);
961
962 mp->m_maxagi = xfs_initialize_perag(mp, sbp->sb_agcount);
963
964 /*
965 * log's mount-time initialization. Perform 1st part recovery if needed
966 */
967 if (likely(sbp->sb_logblocks > 0)) { /* check for volume case */
968 error = xfs_log_mount(mp, mp->m_logdev_targp,
969 XFS_FSB_TO_DADDR(mp, sbp->sb_logstart),
970 XFS_FSB_TO_BB(mp, sbp->sb_logblocks));
971 if (error) {
972 cmn_err(CE_WARN, "XFS: log mount failed");
973 goto error2;
974 }
975 } else { /* No log has been defined */
976 cmn_err(CE_WARN, "XFS: no log defined");
977 XFS_ERROR_REPORT("xfs_mountfs_int(1)", XFS_ERRLEVEL_LOW, mp);
978 error = XFS_ERROR(EFSCORRUPTED);
979 goto error2;
980 }
981
982 /*
983 * Get and sanity-check the root inode.
984 * Save the pointer to it in the mount structure.
985 */
986 error = xfs_iget(mp, NULL, sbp->sb_rootino, 0, XFS_ILOCK_EXCL, &rip, 0);
987 if (error) {
988 cmn_err(CE_WARN, "XFS: failed to read root inode");
989 goto error3;
990 }
991
992 ASSERT(rip != NULL);
993 rvp = XFS_ITOV(rip);
994
995 if (unlikely((rip->i_d.di_mode & S_IFMT) != S_IFDIR)) {
996 cmn_err(CE_WARN, "XFS: corrupted root inode");
997 prdev("Root inode %llu is not a directory",
998 mp->m_ddev_targp, (unsigned long long)rip->i_ino);
999 xfs_iunlock(rip, XFS_ILOCK_EXCL);
1000 XFS_ERROR_REPORT("xfs_mountfs_int(2)", XFS_ERRLEVEL_LOW,
1001 mp);
1002 error = XFS_ERROR(EFSCORRUPTED);
1003 goto error4;
1004 }
1005 mp->m_rootip = rip; /* save it */
1006
1007 xfs_iunlock(rip, XFS_ILOCK_EXCL);
1008
1009 /*
1010 * Initialize realtime inode pointers in the mount structure
1011 */
1012 if ((error = xfs_rtmount_inodes(mp))) {
1013 /*
1014 * Free up the root inode.
1015 */
1016 cmn_err(CE_WARN, "XFS: failed to read RT inodes");
1017 goto error4;
1018 }
1019
1020 /*
1021 * If fs is not mounted readonly, then update the superblock
1022 * unit and width changes.
1023 */
1024 if (update_flags && !(vfsp->vfs_flag & VFS_RDONLY))
1025 xfs_mount_log_sbunit(mp, update_flags);
1026
1027 /*
1028 * Initialise the XFS quota management subsystem for this mount
1029 */
1030 if ((error = XFS_QM_INIT(mp, &quotamount, &quotaflags)))
1031 goto error4;
1032
1033 /*
1034 * Finish recovering the file system. This part needed to be
1035 * delayed until after the root and real-time bitmap inodes
1036 * were consistently read in.
1037 */
1038 error = xfs_log_mount_finish(mp, mfsi_flags);
1039 if (error) {
1040 cmn_err(CE_WARN, "XFS: log mount finish failed");
1041 goto error4;
1042 }
1043
1044 /*
1045 * Complete the quota initialisation, post-log-replay component.
1046 */
1047 if ((error = XFS_QM_MOUNT(mp, quotamount, quotaflags, mfsi_flags)))
1048 goto error4;
1049
1050 return 0;
1051
1052 error4:
1053 /*
1054 * Free up the root inode.
1055 */
1056 VN_RELE(rvp);
1057 error3:
1058 xfs_log_unmount_dealloc(mp);
1059 error2:
1060 xfs_ihash_free(mp);
1061 xfs_chash_free(mp);
1062 for (agno = 0; agno < sbp->sb_agcount; agno++)
1063 if (mp->m_perag[agno].pagb_list)
1064 kmem_free(mp->m_perag[agno].pagb_list,
1065 sizeof(xfs_perag_busy_t) * XFS_PAGB_NUM_SLOTS);
1066 kmem_free(mp->m_perag, sbp->sb_agcount * sizeof(xfs_perag_t));
1067 mp->m_perag = NULL;
1068 /* FALLTHROUGH */
1069 error1:
1070 if (uuid_mounted)
1071 xfs_uuid_unmount(mp);
1072 xfs_freesb(mp);
1073 return error;
1074}
1075
1076/*
1077 * xfs_unmountfs
1078 *
1079 * This flushes out the inodes,dquots and the superblock, unmounts the
1080 * log and makes sure that incore structures are freed.
1081 */
1082int
1083xfs_unmountfs(xfs_mount_t *mp, struct cred *cr)
1084{
1085 struct vfs *vfsp = XFS_MTOVFS(mp);
1086#if defined(DEBUG) || defined(INDUCE_IO_ERROR)
1087 int64_t fsid;
1088#endif
1089
efa80278 1090 xfs_iflush_all(mp);
1da177e4
LT
1091
1092 XFS_QM_DQPURGEALL(mp,
1093 XFS_QMOPT_UQUOTA | XFS_QMOPT_GQUOTA | XFS_QMOPT_UMOUNTING);
1094
1095 /*
1096 * Flush out the log synchronously so that we know for sure
1097 * that nothing is pinned. This is important because bflush()
1098 * will skip pinned buffers.
1099 */
1100 xfs_log_force(mp, (xfs_lsn_t)0, XFS_LOG_FORCE | XFS_LOG_SYNC);
1101
1102 xfs_binval(mp->m_ddev_targp);
1103 if (mp->m_rtdev_targp) {
1104 xfs_binval(mp->m_rtdev_targp);
1105 }
1106
1107 xfs_unmountfs_writesb(mp);
1108
1109 xfs_unmountfs_wait(mp); /* wait for async bufs */
1110
1111 xfs_log_unmount(mp); /* Done! No more fs ops. */
1112
1113 xfs_freesb(mp);
1114
1115 /*
1116 * All inodes from this mount point should be freed.
1117 */
1118 ASSERT(mp->m_inodes == NULL);
1119
1da177e4
LT
1120 xfs_unmountfs_close(mp, cr);
1121 if ((mp->m_flags & XFS_MOUNT_NOUUID) == 0)
1122 xfs_uuid_unmount(mp);
1123
1124#if defined(DEBUG) || defined(INDUCE_IO_ERROR)
1125 /*
1126 * clear all error tags on this filesystem
1127 */
1128 memcpy(&fsid, &vfsp->vfs_fsid, sizeof(int64_t));
1129 xfs_errortag_clearall_umount(fsid, mp->m_fsname, 0);
1130#endif
1131 XFS_IODONE(vfsp);
1132 xfs_mount_free(mp, 1);
1133 return 0;
1134}
1135
1136void
1137xfs_unmountfs_close(xfs_mount_t *mp, struct cred *cr)
1138{
1139 if (mp->m_logdev_targp != mp->m_ddev_targp)
1140 xfs_free_buftarg(mp->m_logdev_targp, 1);
1141 if (mp->m_rtdev_targp)
1142 xfs_free_buftarg(mp->m_rtdev_targp, 1);
1143 xfs_free_buftarg(mp->m_ddev_targp, 0);
1144}
1145
ba0f32d4 1146STATIC void
1da177e4
LT
1147xfs_unmountfs_wait(xfs_mount_t *mp)
1148{
1149 if (mp->m_logdev_targp != mp->m_ddev_targp)
1150 xfs_wait_buftarg(mp->m_logdev_targp);
1151 if (mp->m_rtdev_targp)
1152 xfs_wait_buftarg(mp->m_rtdev_targp);
1153 xfs_wait_buftarg(mp->m_ddev_targp);
1154}
1155
1156int
1157xfs_unmountfs_writesb(xfs_mount_t *mp)
1158{
1159 xfs_buf_t *sbp;
1160 xfs_sb_t *sb;
1161 int error = 0;
1162
1163 /*
1164 * skip superblock write if fs is read-only, or
1165 * if we are doing a forced umount.
1166 */
1167 sbp = xfs_getsb(mp, 0);
1168 if (!(XFS_MTOVFS(mp)->vfs_flag & VFS_RDONLY ||
1169 XFS_FORCED_SHUTDOWN(mp))) {
1170 /*
1171 * mark shared-readonly if desired
1172 */
1173 sb = XFS_BUF_TO_SBP(sbp);
1174 if (mp->m_mk_sharedro) {
1175 if (!(sb->sb_flags & XFS_SBF_READONLY))
1176 sb->sb_flags |= XFS_SBF_READONLY;
1177 if (!XFS_SB_VERSION_HASSHARED(sb))
1178 XFS_SB_VERSION_ADDSHARED(sb);
1179 xfs_fs_cmn_err(CE_NOTE, mp,
1180 "Unmounting, marking shared read-only");
1181 }
1182 XFS_BUF_UNDONE(sbp);
1183 XFS_BUF_UNREAD(sbp);
1184 XFS_BUF_UNDELAYWRITE(sbp);
1185 XFS_BUF_WRITE(sbp);
1186 XFS_BUF_UNASYNC(sbp);
1187 ASSERT(XFS_BUF_TARGET(sbp) == mp->m_ddev_targp);
1188 xfsbdstrat(mp, sbp);
1189 /* Nevermind errors we might get here. */
1190 error = xfs_iowait(sbp);
1191 if (error)
1192 xfs_ioerror_alert("xfs_unmountfs_writesb",
1193 mp, sbp, XFS_BUF_ADDR(sbp));
1194 if (error && mp->m_mk_sharedro)
1195 xfs_fs_cmn_err(CE_ALERT, mp, "Superblock write error detected while unmounting. Filesystem may not be marked shared readonly");
1196 }
1197 xfs_buf_relse(sbp);
1198 return (error);
1199}
1200
1201/*
1202 * xfs_mod_sb() can be used to copy arbitrary changes to the
1203 * in-core superblock into the superblock buffer to be logged.
1204 * It does not provide the higher level of locking that is
1205 * needed to protect the in-core superblock from concurrent
1206 * access.
1207 */
1208void
1209xfs_mod_sb(xfs_trans_t *tp, __int64_t fields)
1210{
1211 xfs_buf_t *bp;
1212 int first;
1213 int last;
1214 xfs_mount_t *mp;
1215 xfs_sb_t *sbp;
1216 xfs_sb_field_t f;
1217
1218 ASSERT(fields);
1219 if (!fields)
1220 return;
1221 mp = tp->t_mountp;
1222 bp = xfs_trans_getsb(tp, mp, 0);
1223 sbp = XFS_BUF_TO_SBP(bp);
1224 first = sizeof(xfs_sb_t);
1225 last = 0;
1226
1227 /* translate/copy */
1228
1229 xfs_xlatesb(XFS_BUF_PTR(bp), &(mp->m_sb), -1, fields);
1230
1231 /* find modified range */
1232
1233 f = (xfs_sb_field_t)xfs_lowbit64((__uint64_t)fields);
1234 ASSERT((1LL << f) & XFS_SB_MOD_BITS);
1235 first = xfs_sb_info[f].offset;
1236
1237 f = (xfs_sb_field_t)xfs_highbit64((__uint64_t)fields);
1238 ASSERT((1LL << f) & XFS_SB_MOD_BITS);
1239 last = xfs_sb_info[f + 1].offset - 1;
1240
1241 xfs_trans_log_buf(tp, bp, first, last);
1242}
1243
1244/*
1245 * xfs_mod_incore_sb_unlocked() is a utility routine common used to apply
1246 * a delta to a specified field in the in-core superblock. Simply
1247 * switch on the field indicated and apply the delta to that field.
1248 * Fields are not allowed to dip below zero, so if the delta would
1249 * do this do not apply it and return EINVAL.
1250 *
1251 * The SB_LOCK must be held when this routine is called.
1252 */
1253STATIC int
1254xfs_mod_incore_sb_unlocked(xfs_mount_t *mp, xfs_sb_field_t field,
1255 int delta, int rsvd)
1256{
1257 int scounter; /* short counter for 32 bit fields */
1258 long long lcounter; /* long counter for 64 bit fields */
1259 long long res_used, rem;
1260
1261 /*
1262 * With the in-core superblock spin lock held, switch
1263 * on the indicated field. Apply the delta to the
1264 * proper field. If the fields value would dip below
1265 * 0, then do not apply the delta and return EINVAL.
1266 */
1267 switch (field) {
1268 case XFS_SBS_ICOUNT:
1269 lcounter = (long long)mp->m_sb.sb_icount;
1270 lcounter += delta;
1271 if (lcounter < 0) {
1272 ASSERT(0);
1273 return (XFS_ERROR(EINVAL));
1274 }
1275 mp->m_sb.sb_icount = lcounter;
1276 return (0);
1277 case XFS_SBS_IFREE:
1278 lcounter = (long long)mp->m_sb.sb_ifree;
1279 lcounter += delta;
1280 if (lcounter < 0) {
1281 ASSERT(0);
1282 return (XFS_ERROR(EINVAL));
1283 }
1284 mp->m_sb.sb_ifree = lcounter;
1285 return (0);
1286 case XFS_SBS_FDBLOCKS:
1287
1288 lcounter = (long long)mp->m_sb.sb_fdblocks;
1289 res_used = (long long)(mp->m_resblks - mp->m_resblks_avail);
1290
1291 if (delta > 0) { /* Putting blocks back */
1292 if (res_used > delta) {
1293 mp->m_resblks_avail += delta;
1294 } else {
1295 rem = delta - res_used;
1296 mp->m_resblks_avail = mp->m_resblks;
1297 lcounter += rem;
1298 }
1299 } else { /* Taking blocks away */
1300
1301 lcounter += delta;
1302
1303 /*
1304 * If were out of blocks, use any available reserved blocks if
1305 * were allowed to.
1306 */
1307
1308 if (lcounter < 0) {
1309 if (rsvd) {
1310 lcounter = (long long)mp->m_resblks_avail + delta;
1311 if (lcounter < 0) {
1312 return (XFS_ERROR(ENOSPC));
1313 }
1314 mp->m_resblks_avail = lcounter;
1315 return (0);
1316 } else { /* not reserved */
1317 return (XFS_ERROR(ENOSPC));
1318 }
1319 }
1320 }
1321
1322 mp->m_sb.sb_fdblocks = lcounter;
1323 return (0);
1324 case XFS_SBS_FREXTENTS:
1325 lcounter = (long long)mp->m_sb.sb_frextents;
1326 lcounter += delta;
1327 if (lcounter < 0) {
1328 return (XFS_ERROR(ENOSPC));
1329 }
1330 mp->m_sb.sb_frextents = lcounter;
1331 return (0);
1332 case XFS_SBS_DBLOCKS:
1333 lcounter = (long long)mp->m_sb.sb_dblocks;
1334 lcounter += delta;
1335 if (lcounter < 0) {
1336 ASSERT(0);
1337 return (XFS_ERROR(EINVAL));
1338 }
1339 mp->m_sb.sb_dblocks = lcounter;
1340 return (0);
1341 case XFS_SBS_AGCOUNT:
1342 scounter = mp->m_sb.sb_agcount;
1343 scounter += delta;
1344 if (scounter < 0) {
1345 ASSERT(0);
1346 return (XFS_ERROR(EINVAL));
1347 }
1348 mp->m_sb.sb_agcount = scounter;
1349 return (0);
1350 case XFS_SBS_IMAX_PCT:
1351 scounter = mp->m_sb.sb_imax_pct;
1352 scounter += delta;
1353 if (scounter < 0) {
1354 ASSERT(0);
1355 return (XFS_ERROR(EINVAL));
1356 }
1357 mp->m_sb.sb_imax_pct = scounter;
1358 return (0);
1359 case XFS_SBS_REXTSIZE:
1360 scounter = mp->m_sb.sb_rextsize;
1361 scounter += delta;
1362 if (scounter < 0) {
1363 ASSERT(0);
1364 return (XFS_ERROR(EINVAL));
1365 }
1366 mp->m_sb.sb_rextsize = scounter;
1367 return (0);
1368 case XFS_SBS_RBMBLOCKS:
1369 scounter = mp->m_sb.sb_rbmblocks;
1370 scounter += delta;
1371 if (scounter < 0) {
1372 ASSERT(0);
1373 return (XFS_ERROR(EINVAL));
1374 }
1375 mp->m_sb.sb_rbmblocks = scounter;
1376 return (0);
1377 case XFS_SBS_RBLOCKS:
1378 lcounter = (long long)mp->m_sb.sb_rblocks;
1379 lcounter += delta;
1380 if (lcounter < 0) {
1381 ASSERT(0);
1382 return (XFS_ERROR(EINVAL));
1383 }
1384 mp->m_sb.sb_rblocks = lcounter;
1385 return (0);
1386 case XFS_SBS_REXTENTS:
1387 lcounter = (long long)mp->m_sb.sb_rextents;
1388 lcounter += delta;
1389 if (lcounter < 0) {
1390 ASSERT(0);
1391 return (XFS_ERROR(EINVAL));
1392 }
1393 mp->m_sb.sb_rextents = lcounter;
1394 return (0);
1395 case XFS_SBS_REXTSLOG:
1396 scounter = mp->m_sb.sb_rextslog;
1397 scounter += delta;
1398 if (scounter < 0) {
1399 ASSERT(0);
1400 return (XFS_ERROR(EINVAL));
1401 }
1402 mp->m_sb.sb_rextslog = scounter;
1403 return (0);
1404 default:
1405 ASSERT(0);
1406 return (XFS_ERROR(EINVAL));
1407 }
1408}
1409
1410/*
1411 * xfs_mod_incore_sb() is used to change a field in the in-core
1412 * superblock structure by the specified delta. This modification
1413 * is protected by the SB_LOCK. Just use the xfs_mod_incore_sb_unlocked()
1414 * routine to do the work.
1415 */
1416int
1417xfs_mod_incore_sb(xfs_mount_t *mp, xfs_sb_field_t field, int delta, int rsvd)
1418{
1419 unsigned long s;
1420 int status;
1421
1422 s = XFS_SB_LOCK(mp);
1423 status = xfs_mod_incore_sb_unlocked(mp, field, delta, rsvd);
1424 XFS_SB_UNLOCK(mp, s);
1425 return (status);
1426}
1427
1428/*
1429 * xfs_mod_incore_sb_batch() is used to change more than one field
1430 * in the in-core superblock structure at a time. This modification
1431 * is protected by a lock internal to this module. The fields and
1432 * changes to those fields are specified in the array of xfs_mod_sb
1433 * structures passed in.
1434 *
1435 * Either all of the specified deltas will be applied or none of
1436 * them will. If any modified field dips below 0, then all modifications
1437 * will be backed out and EINVAL will be returned.
1438 */
1439int
1440xfs_mod_incore_sb_batch(xfs_mount_t *mp, xfs_mod_sb_t *msb, uint nmsb, int rsvd)
1441{
1442 unsigned long s;
1443 int status=0;
1444 xfs_mod_sb_t *msbp;
1445
1446 /*
1447 * Loop through the array of mod structures and apply each
1448 * individually. If any fail, then back out all those
1449 * which have already been applied. Do all of this within
1450 * the scope of the SB_LOCK so that all of the changes will
1451 * be atomic.
1452 */
1453 s = XFS_SB_LOCK(mp);
1454 msbp = &msb[0];
1455 for (msbp = &msbp[0]; msbp < (msb + nmsb); msbp++) {
1456 /*
1457 * Apply the delta at index n. If it fails, break
1458 * from the loop so we'll fall into the undo loop
1459 * below.
1460 */
1461 status = xfs_mod_incore_sb_unlocked(mp, msbp->msb_field,
1462 msbp->msb_delta, rsvd);
1463 if (status != 0) {
1464 break;
1465 }
1466 }
1467
1468 /*
1469 * If we didn't complete the loop above, then back out
1470 * any changes made to the superblock. If you add code
1471 * between the loop above and here, make sure that you
1472 * preserve the value of status. Loop back until
1473 * we step below the beginning of the array. Make sure
1474 * we don't touch anything back there.
1475 */
1476 if (status != 0) {
1477 msbp--;
1478 while (msbp >= msb) {
1479 status = xfs_mod_incore_sb_unlocked(mp,
1480 msbp->msb_field, -(msbp->msb_delta), rsvd);
1481 ASSERT(status == 0);
1482 msbp--;
1483 }
1484 }
1485 XFS_SB_UNLOCK(mp, s);
1486 return (status);
1487}
1488
1489/*
1490 * xfs_getsb() is called to obtain the buffer for the superblock.
1491 * The buffer is returned locked and read in from disk.
1492 * The buffer should be released with a call to xfs_brelse().
1493 *
1494 * If the flags parameter is BUF_TRYLOCK, then we'll only return
1495 * the superblock buffer if it can be locked without sleeping.
1496 * If it can't then we'll return NULL.
1497 */
1498xfs_buf_t *
1499xfs_getsb(
1500 xfs_mount_t *mp,
1501 int flags)
1502{
1503 xfs_buf_t *bp;
1504
1505 ASSERT(mp->m_sb_bp != NULL);
1506 bp = mp->m_sb_bp;
1507 if (flags & XFS_BUF_TRYLOCK) {
1508 if (!XFS_BUF_CPSEMA(bp)) {
1509 return NULL;
1510 }
1511 } else {
1512 XFS_BUF_PSEMA(bp, PRIBIO);
1513 }
1514 XFS_BUF_HOLD(bp);
1515 ASSERT(XFS_BUF_ISDONE(bp));
1516 return (bp);
1517}
1518
1519/*
1520 * Used to free the superblock along various error paths.
1521 */
1522void
1523xfs_freesb(
1524 xfs_mount_t *mp)
1525{
1526 xfs_buf_t *bp;
1527
1528 /*
1529 * Use xfs_getsb() so that the buffer will be locked
1530 * when we call xfs_buf_relse().
1531 */
1532 bp = xfs_getsb(mp, 0);
1533 XFS_BUF_UNMANAGE(bp);
1534 xfs_buf_relse(bp);
1535 mp->m_sb_bp = NULL;
1536}
1537
1538/*
1539 * See if the UUID is unique among mounted XFS filesystems.
1540 * Mount fails if UUID is nil or a FS with the same UUID is already mounted.
1541 */
1542STATIC int
1543xfs_uuid_mount(
1544 xfs_mount_t *mp)
1545{
1546 if (uuid_is_nil(&mp->m_sb.sb_uuid)) {
1547 cmn_err(CE_WARN,
1548 "XFS: Filesystem %s has nil UUID - can't mount",
1549 mp->m_fsname);
1550 return -1;
1551 }
1552 if (!uuid_table_insert(&mp->m_sb.sb_uuid)) {
1553 cmn_err(CE_WARN,
1554 "XFS: Filesystem %s has duplicate UUID - can't mount",
1555 mp->m_fsname);
1556 return -1;
1557 }
1558 return 0;
1559}
1560
1561/*
1562 * Remove filesystem from the UUID table.
1563 */
1564STATIC void
1565xfs_uuid_unmount(
1566 xfs_mount_t *mp)
1567{
1568 uuid_table_remove(&mp->m_sb.sb_uuid);
1569}
1570
1571/*
1572 * Used to log changes to the superblock unit and width fields which could
1573 * be altered by the mount options. Only the first superblock is updated.
1574 */
1575STATIC void
1576xfs_mount_log_sbunit(
1577 xfs_mount_t *mp,
1578 __int64_t fields)
1579{
1580 xfs_trans_t *tp;
1581
1582 ASSERT(fields & (XFS_SB_UNIT|XFS_SB_WIDTH|XFS_SB_UUID));
1583
1584 tp = xfs_trans_alloc(mp, XFS_TRANS_SB_UNIT);
1585 if (xfs_trans_reserve(tp, 0, mp->m_sb.sb_sectsize + 128, 0, 0,
1586 XFS_DEFAULT_LOG_COUNT)) {
1587 xfs_trans_cancel(tp, 0);
1588 return;
1589 }
1590 xfs_mod_sb(tp, fields);
1591 xfs_trans_commit(tp, 0, NULL);
1592}
This page took 0.129544 seconds and 5 git commands to generate.