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