blob: 4c132a87d437ef591d61a581e2b7f92921f3cd34 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110019#include "xfs_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "xfs_types.h"
Nathan Scotta844f452005-11-02 14:38:42 +110021#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "xfs_log.h"
Nathan Scotta844f452005-11-02 14:38:42 +110023#include "xfs_inum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "xfs_trans.h"
25#include "xfs_sb.h"
Nathan Scotta844f452005-11-02 14:38:42 +110026#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "xfs_dir2.h"
28#include "xfs_dmapi.h"
29#include "xfs_mount.h"
Nathan Scotta844f452005-11-02 14:38:42 +110030#include "xfs_da_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include "xfs_bmap_btree.h"
32#include "xfs_ialloc_btree.h"
33#include "xfs_alloc_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110034#include "xfs_dir2_sf.h"
35#include "xfs_attr_sf.h"
36#include "xfs_dinode.h"
37#include "xfs_inode.h"
38#include "xfs_inode_item.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include "xfs_btree.h"
40#include "xfs_alloc.h"
41#include "xfs_ialloc.h"
Nathan Scotta844f452005-11-02 14:38:42 +110042#include "xfs_quota.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include "xfs_error.h"
44#include "xfs_bmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include "xfs_rw.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include "xfs_buf_item.h"
Nathan Scotta844f452005-11-02 14:38:42 +110047#include "xfs_log_priv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include "xfs_dir2_trace.h"
Nathan Scotta844f452005-11-02 14:38:42 +110049#include "xfs_extfree_item.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include "xfs_acl.h"
51#include "xfs_attr.h"
52#include "xfs_clnt.h"
David Chinner2a82b8b2007-07-11 11:09:12 +100053#include "xfs_mru_cache.h"
54#include "xfs_filestream.h"
Christoph Hellwige13a73f2006-01-11 15:30:08 +110055#include "xfs_fsops.h"
Christoph Hellwig739bfb22007-08-29 10:58:01 +100056#include "xfs_vnodeops.h"
Christoph Hellwig48c872a9f2007-08-30 17:20:31 +100057#include "xfs_vfsops.h"
Christoph Hellwig739bfb22007-08-29 10:58:01 +100058
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Lachlan McIlroyde2eeea2008-02-06 13:37:56 +110060int __init
Linus Torvalds1da177e2005-04-16 15:20:36 -070061xfs_init(void)
62{
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#ifdef XFS_DABUF_DEBUG
Eric Sandeen703e1f02007-10-11 17:41:21 +100064 extern spinlock_t xfs_dabuf_global_lock;
Eric Sandeen007c61c2007-10-11 17:43:56 +100065 spin_lock_init(&xfs_dabuf_global_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066#endif
67
68 /*
69 * Initialize all of the zone allocators we use.
70 */
71 xfs_bmap_free_item_zone = kmem_zone_init(sizeof(xfs_bmap_free_item_t),
72 "xfs_bmap_free_item");
73 xfs_btree_cur_zone = kmem_zone_init(sizeof(xfs_btree_cur_t),
74 "xfs_btree_cur");
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 xfs_trans_zone = kmem_zone_init(sizeof(xfs_trans_t), "xfs_trans");
76 xfs_da_state_zone =
77 kmem_zone_init(sizeof(xfs_da_state_t), "xfs_da_state");
78 xfs_dabuf_zone = kmem_zone_init(sizeof(xfs_dabuf_t), "xfs_dabuf");
Nathan Scott87582802006-03-14 13:18:19 +110079 xfs_ifork_zone = kmem_zone_init(sizeof(xfs_ifork_t), "xfs_ifork");
80 xfs_acl_zone_init(xfs_acl_zone, "xfs_acl");
David Chinner2a82b8b2007-07-11 11:09:12 +100081 xfs_mru_cache_init();
82 xfs_filestream_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
84 /*
85 * The size of the zone allocated buf log item is the maximum
86 * size possible under XFS. This wastes a little bit of memory,
87 * but it is much faster.
88 */
89 xfs_buf_item_zone =
90 kmem_zone_init((sizeof(xfs_buf_log_item_t) +
91 (((XFS_MAX_BLOCKSIZE / XFS_BLI_CHUNK) /
92 NBWORD) * sizeof(int))),
93 "xfs_buf_item");
Nathan Scott87582802006-03-14 13:18:19 +110094 xfs_efd_zone =
95 kmem_zone_init((sizeof(xfs_efd_log_item_t) +
96 ((XFS_EFD_MAX_FAST_EXTENTS - 1) *
97 sizeof(xfs_extent_t))),
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 "xfs_efd_item");
Nathan Scott87582802006-03-14 13:18:19 +110099 xfs_efi_zone =
100 kmem_zone_init((sizeof(xfs_efi_log_item_t) +
101 ((XFS_EFI_MAX_FAST_EXTENTS - 1) *
102 sizeof(xfs_extent_t))),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 "xfs_efi_item");
Nathan Scott87582802006-03-14 13:18:19 +1100104
105 /*
106 * These zones warrant special memory allocator hints
107 */
108 xfs_inode_zone =
109 kmem_zone_init_flags(sizeof(xfs_inode_t), "xfs_inode",
110 KM_ZONE_HWALIGN | KM_ZONE_RECLAIM |
111 KM_ZONE_SPREAD, NULL);
112 xfs_ili_zone =
113 kmem_zone_init_flags(sizeof(xfs_inode_log_item_t), "xfs_ili",
114 KM_ZONE_SPREAD, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116 /*
117 * Allocate global trace buffers.
118 */
119#ifdef XFS_ALLOC_TRACE
120 xfs_alloc_trace_buf = ktrace_alloc(XFS_ALLOC_TRACE_SIZE, KM_SLEEP);
121#endif
122#ifdef XFS_BMAP_TRACE
123 xfs_bmap_trace_buf = ktrace_alloc(XFS_BMAP_TRACE_SIZE, KM_SLEEP);
124#endif
125#ifdef XFS_BMBT_TRACE
126 xfs_bmbt_trace_buf = ktrace_alloc(XFS_BMBT_TRACE_SIZE, KM_SLEEP);
127#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128#ifdef XFS_ATTR_TRACE
129 xfs_attr_trace_buf = ktrace_alloc(XFS_ATTR_TRACE_SIZE, KM_SLEEP);
130#endif
131#ifdef XFS_DIR2_TRACE
132 xfs_dir2_trace_buf = ktrace_alloc(XFS_DIR2_GTRACE_SIZE, KM_SLEEP);
133#endif
134
135 xfs_dir_startup();
136
137#if (defined(DEBUG) || defined(INDUCE_IO_ERROR))
138 xfs_error_test_init();
139#endif /* DEBUG || INDUCE_IO_ERROR */
140
141 xfs_init_procfs();
142 xfs_sysctl_register();
143 return 0;
144}
145
Lachlan McIlroyde2eeea2008-02-06 13:37:56 +1100146void __exit
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147xfs_cleanup(void)
148{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 extern kmem_zone_t *xfs_inode_zone;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 extern kmem_zone_t *xfs_efd_zone;
151 extern kmem_zone_t *xfs_efi_zone;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
153 xfs_cleanup_procfs();
154 xfs_sysctl_unregister();
David Chinner2a82b8b2007-07-11 11:09:12 +1000155 xfs_filestream_uninit();
156 xfs_mru_cache_uninit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 xfs_acl_zone_destroy(xfs_acl_zone);
158
159#ifdef XFS_DIR2_TRACE
160 ktrace_free(xfs_dir2_trace_buf);
161#endif
162#ifdef XFS_ATTR_TRACE
163 ktrace_free(xfs_attr_trace_buf);
164#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165#ifdef XFS_BMBT_TRACE
166 ktrace_free(xfs_bmbt_trace_buf);
167#endif
168#ifdef XFS_BMAP_TRACE
169 ktrace_free(xfs_bmap_trace_buf);
170#endif
171#ifdef XFS_ALLOC_TRACE
172 ktrace_free(xfs_alloc_trace_buf);
173#endif
174
Nathan Scott3758dee2006-03-22 12:47:28 +1100175 kmem_zone_destroy(xfs_bmap_free_item_zone);
176 kmem_zone_destroy(xfs_btree_cur_zone);
177 kmem_zone_destroy(xfs_inode_zone);
178 kmem_zone_destroy(xfs_trans_zone);
179 kmem_zone_destroy(xfs_da_state_zone);
180 kmem_zone_destroy(xfs_dabuf_zone);
181 kmem_zone_destroy(xfs_buf_item_zone);
182 kmem_zone_destroy(xfs_efd_zone);
183 kmem_zone_destroy(xfs_efi_zone);
184 kmem_zone_destroy(xfs_ifork_zone);
185 kmem_zone_destroy(xfs_ili_zone);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186}
187
188/*
189 * xfs_start_flags
190 *
191 * This function fills in xfs_mount_t fields based on mount args.
192 * Note: the superblock has _not_ yet been read in.
193 */
194STATIC int
195xfs_start_flags(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 struct xfs_mount_args *ap,
197 struct xfs_mount *mp)
198{
199 /* Values are in BBs */
200 if ((ap->flags & XFSMNT_NOALIGN) != XFSMNT_NOALIGN) {
201 /*
202 * At this point the superblock has not been read
203 * in, therefore we do not know the block size.
204 * Before the mount call ends we will convert
205 * these to FSBs.
206 */
207 mp->m_dalign = ap->sunit;
208 mp->m_swidth = ap->swidth;
209 }
210
211 if (ap->logbufs != -1 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 ap->logbufs != 0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 (ap->logbufs < XLOG_MIN_ICLOGS ||
214 ap->logbufs > XLOG_MAX_ICLOGS)) {
215 cmn_err(CE_WARN,
216 "XFS: invalid logbufs value: %d [not %d-%d]",
217 ap->logbufs, XLOG_MIN_ICLOGS, XLOG_MAX_ICLOGS);
218 return XFS_ERROR(EINVAL);
219 }
220 mp->m_logbufs = ap->logbufs;
221 if (ap->logbufsize != -1 &&
Nathan Scottcfcbbbd2005-11-02 15:12:04 +1100222 ap->logbufsize != 0 &&
Eric Sandeendcb3b832007-08-16 16:25:33 +1000223 (ap->logbufsize < XLOG_MIN_RECORD_BSIZE ||
224 ap->logbufsize > XLOG_MAX_RECORD_BSIZE ||
225 !is_power_of_2(ap->logbufsize))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 cmn_err(CE_WARN,
227 "XFS: invalid logbufsize: %d [not 16k,32k,64k,128k or 256k]",
228 ap->logbufsize);
229 return XFS_ERROR(EINVAL);
230 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 mp->m_logbsize = ap->logbufsize;
232 mp->m_fsname_len = strlen(ap->fsname) + 1;
233 mp->m_fsname = kmem_alloc(mp->m_fsname_len, KM_SLEEP);
234 strcpy(mp->m_fsname, ap->fsname);
Nathan Scottfc1f8c12005-11-02 11:44:33 +1100235 if (ap->rtname[0]) {
236 mp->m_rtname = kmem_alloc(strlen(ap->rtname) + 1, KM_SLEEP);
237 strcpy(mp->m_rtname, ap->rtname);
238 }
239 if (ap->logname[0]) {
240 mp->m_logname = kmem_alloc(strlen(ap->logname) + 1, KM_SLEEP);
241 strcpy(mp->m_logname, ap->logname);
242 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
244 if (ap->flags & XFSMNT_WSYNC)
245 mp->m_flags |= XFS_MOUNT_WSYNC;
246#if XFS_BIG_INUMS
247 if (ap->flags & XFSMNT_INO64) {
248 mp->m_flags |= XFS_MOUNT_INO64;
249 mp->m_inoadd = XFS_INO64_OFFSET;
250 }
251#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 if (ap->flags & XFSMNT_RETERR)
253 mp->m_flags |= XFS_MOUNT_RETERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 if (ap->flags & XFSMNT_NOALIGN)
255 mp->m_flags |= XFS_MOUNT_NOALIGN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 if (ap->flags & XFSMNT_SWALLOC)
257 mp->m_flags |= XFS_MOUNT_SWALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 if (ap->flags & XFSMNT_OSYNCISOSYNC)
259 mp->m_flags |= XFS_MOUNT_OSYNCISOSYNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 if (ap->flags & XFSMNT_32BITINODES)
Nathan Scottc11e2c32005-11-02 15:11:45 +1100261 mp->m_flags |= XFS_MOUNT_32BITINODES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
263 if (ap->flags & XFSMNT_IOSIZE) {
264 if (ap->iosizelog > XFS_MAX_IO_LOG ||
265 ap->iosizelog < XFS_MIN_IO_LOG) {
266 cmn_err(CE_WARN,
267 "XFS: invalid log iosize: %d [not %d-%d]",
268 ap->iosizelog, XFS_MIN_IO_LOG,
269 XFS_MAX_IO_LOG);
270 return XFS_ERROR(EINVAL);
271 }
272
273 mp->m_flags |= XFS_MOUNT_DFLT_IOSIZE;
274 mp->m_readio_log = mp->m_writeio_log = ap->iosizelog;
275 }
276
Josef Jeff Sipek1bd960e2008-02-29 13:58:40 +1100277 if (ap->flags & XFSMNT_IKEEP)
278 mp->m_flags |= XFS_MOUNT_IKEEP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 if (ap->flags & XFSMNT_DIRSYNC)
280 mp->m_flags |= XFS_MOUNT_DIRSYNC;
Nathan Scott13059ff2006-01-11 15:32:01 +1100281 if (ap->flags & XFSMNT_ATTR2)
282 mp->m_flags |= XFS_MOUNT_ATTR2;
David Chinnere8c8b3a2005-11-02 10:33:05 +1100283
Nathan Scotte718eeb42005-11-02 15:09:22 +1100284 if (ap->flags2 & XFSMNT2_COMPAT_IOSIZE)
285 mp->m_flags |= XFS_MOUNT_COMPAT_IOSIZE;
286
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 /*
288 * no recovery flag requires a read-only mount
289 */
290 if (ap->flags & XFSMNT_NORECOVERY) {
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000291 if (!(mp->m_flags & XFS_MOUNT_RDONLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 cmn_err(CE_WARN,
293 "XFS: tried to mount a FS read-write without recovery!");
294 return XFS_ERROR(EINVAL);
295 }
296 mp->m_flags |= XFS_MOUNT_NORECOVERY;
297 }
298
299 if (ap->flags & XFSMNT_NOUUID)
300 mp->m_flags |= XFS_MOUNT_NOUUID;
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100301 if (ap->flags & XFSMNT_BARRIER)
302 mp->m_flags |= XFS_MOUNT_BARRIER;
Christoph Hellwig4ef19dd2006-01-11 15:27:18 +1100303 else
304 mp->m_flags &= ~XFS_MOUNT_BARRIER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
David Chinner2a82b8b2007-07-11 11:09:12 +1000306 if (ap->flags2 & XFSMNT2_FILESTREAMS)
307 mp->m_flags |= XFS_MOUNT_FILESTREAMS;
308
Christoph Hellwig293688e2007-08-29 11:59:36 +1000309 if (ap->flags & XFSMNT_DMAPI)
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000310 mp->m_flags |= XFS_MOUNT_DMAPI;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 return 0;
312}
313
314/*
315 * This function fills in xfs_mount_t fields based on mount args.
316 * Note: the superblock _has_ now been read in.
317 */
318STATIC int
319xfs_finish_flags(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 struct xfs_mount_args *ap,
321 struct xfs_mount *mp)
322{
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000323 int ronly = (mp->m_flags & XFS_MOUNT_RDONLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
325 /* Fail a mount where the logbuf is smaller then the log stripe */
Eric Sandeen62118702008-03-06 13:44:28 +1100326 if (xfs_sb_version_haslogv2(&mp->m_sb)) {
Nathan Scottc2cd2552006-01-11 15:35:32 +1100327 if ((ap->logbufsize <= 0) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 (mp->m_sb.sb_logsunit > XLOG_BIG_RECORD_BSIZE)) {
329 mp->m_logbsize = mp->m_sb.sb_logsunit;
Nathan Scottc2cd2552006-01-11 15:35:32 +1100330 } else if (ap->logbufsize > 0 &&
331 ap->logbufsize < mp->m_sb.sb_logsunit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 cmn_err(CE_WARN,
333 "XFS: logbuf size must be greater than or equal to log stripe size");
334 return XFS_ERROR(EINVAL);
335 }
336 } else {
337 /* Fail a mount if the logbuf is larger than 32K */
338 if (ap->logbufsize > XLOG_BIG_RECORD_BSIZE) {
339 cmn_err(CE_WARN,
340 "XFS: logbuf size for version 1 logs must be 16K or 32K");
341 return XFS_ERROR(EINVAL);
342 }
343 }
344
Eric Sandeen62118702008-03-06 13:44:28 +1100345 if (xfs_sb_version_hasattr2(&mp->m_sb))
Nathan Scott13059ff2006-01-11 15:32:01 +1100346 mp->m_flags |= XFS_MOUNT_ATTR2;
Nathan Scott13059ff2006-01-11 15:32:01 +1100347
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 /*
349 * prohibit r/w mounts of read-only filesystems
350 */
351 if ((mp->m_sb.sb_flags & XFS_SBF_READONLY) && !ronly) {
352 cmn_err(CE_WARN,
353 "XFS: cannot mount a read-only filesystem as read-write");
354 return XFS_ERROR(EROFS);
355 }
356
357 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 * check for shared mount.
359 */
360 if (ap->flags & XFSMNT_SHARED) {
Eric Sandeen62118702008-03-06 13:44:28 +1100361 if (!xfs_sb_version_hasshared(&mp->m_sb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 return XFS_ERROR(EINVAL);
363
364 /*
365 * For IRIX 6.5, shared mounts must have the shared
366 * version bit set, have the persistent readonly
367 * field set, must be version 0 and can only be mounted
368 * read-only.
369 */
370 if (!ronly || !(mp->m_sb.sb_flags & XFS_SBF_READONLY) ||
371 (mp->m_sb.sb_shared_vn != 0))
372 return XFS_ERROR(EINVAL);
373
374 mp->m_flags |= XFS_MOUNT_SHARED;
375
376 /*
377 * Shared XFS V0 can't deal with DMI. Return EINVAL.
378 */
379 if (mp->m_sb.sb_shared_vn == 0 && (ap->flags & XFSMNT_DMAPI))
380 return XFS_ERROR(EINVAL);
381 }
382
Christoph Hellwigb09cc772007-08-30 17:19:57 +1000383 if (ap->flags & XFSMNT_UQUOTA) {
384 mp->m_qflags |= (XFS_UQUOTA_ACCT | XFS_UQUOTA_ACTIVE);
385 if (ap->flags & XFSMNT_UQUOTAENF)
386 mp->m_qflags |= XFS_UQUOTA_ENFD;
387 }
388
389 if (ap->flags & XFSMNT_GQUOTA) {
390 mp->m_qflags |= (XFS_GQUOTA_ACCT | XFS_GQUOTA_ACTIVE);
391 if (ap->flags & XFSMNT_GQUOTAENF)
392 mp->m_qflags |= XFS_OQUOTA_ENFD;
393 } else if (ap->flags & XFSMNT_PQUOTA) {
394 mp->m_qflags |= (XFS_PQUOTA_ACCT | XFS_PQUOTA_ACTIVE);
395 if (ap->flags & XFSMNT_PQUOTAENF)
396 mp->m_qflags |= XFS_OQUOTA_ENFD;
397 }
398
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 return 0;
400}
401
402/*
403 * xfs_mount
404 *
405 * The file system configurations are:
406 * (1) device (partition) with data and internal log
407 * (2) logical volume with data and log subvolumes.
408 * (3) logical volume with data, log, and realtime subvolumes.
409 *
410 * We only have to handle opening the log and realtime volumes here if
411 * they are present. The data subvolume has already been opened by
412 * get_sb_bdev() and is stored in vfsp->vfs_super->s_bdev.
413 */
Christoph Hellwig48c872a9f2007-08-30 17:20:31 +1000414int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415xfs_mount(
Christoph Hellwig48c872a9f2007-08-30 17:20:31 +1000416 struct xfs_mount *mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 struct xfs_mount_args *args,
418 cred_t *credp)
419{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 struct block_device *ddev, *logdev, *rtdev;
421 int flags = 0, error;
422
Christoph Hellwigb267ce92007-08-30 17:21:30 +1000423 ddev = mp->m_super->s_bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 logdev = rtdev = NULL;
425
Christoph Hellwig293688e2007-08-29 11:59:36 +1000426 error = xfs_dmops_get(mp, args);
427 if (error)
428 return error;
Christoph Hellwigb09cc772007-08-30 17:19:57 +1000429 error = xfs_qmops_get(mp, args);
430 if (error)
431 return error;
Christoph Hellwig293688e2007-08-29 11:59:36 +1000432
Nathan Scott764d1f82006-03-31 13:04:17 +1000433 if (args->flags & XFSMNT_QUIET)
434 flags |= XFS_MFSI_QUIET;
435
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 /*
437 * Open real time and log devices - order is important.
438 */
439 if (args->logname[0]) {
440 error = xfs_blkdev_get(mp, args->logname, &logdev);
441 if (error)
442 return error;
443 }
444 if (args->rtname[0]) {
445 error = xfs_blkdev_get(mp, args->rtname, &rtdev);
446 if (error) {
447 xfs_blkdev_put(logdev);
448 return error;
449 }
450
451 if (rtdev == ddev || rtdev == logdev) {
452 cmn_err(CE_WARN,
453 "XFS: Cannot mount filesystem with identical rtdev and ddev/logdev.");
454 xfs_blkdev_put(logdev);
455 xfs_blkdev_put(rtdev);
456 return EINVAL;
457 }
458 }
459
460 /*
461 * Setup xfs_mount buffer target pointers
462 */
463 error = ENOMEM;
464 mp->m_ddev_targp = xfs_alloc_buftarg(ddev, 0);
465 if (!mp->m_ddev_targp) {
466 xfs_blkdev_put(logdev);
467 xfs_blkdev_put(rtdev);
468 return error;
469 }
470 if (rtdev) {
471 mp->m_rtdev_targp = xfs_alloc_buftarg(rtdev, 1);
Jesper Juhl49ee6c92007-08-16 16:25:42 +1000472 if (!mp->m_rtdev_targp) {
473 xfs_blkdev_put(logdev);
474 xfs_blkdev_put(rtdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 goto error0;
Jesper Juhl49ee6c92007-08-16 16:25:42 +1000476 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 }
478 mp->m_logdev_targp = (logdev && logdev != ddev) ?
479 xfs_alloc_buftarg(logdev, 1) : mp->m_ddev_targp;
Jesper Juhl49ee6c92007-08-16 16:25:42 +1000480 if (!mp->m_logdev_targp) {
481 xfs_blkdev_put(logdev);
482 xfs_blkdev_put(rtdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 goto error0;
Jesper Juhl49ee6c92007-08-16 16:25:42 +1000484 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
486 /*
487 * Setup flags based on mount(2) options and then the superblock
488 */
Christoph Hellwigb267ce92007-08-30 17:21:30 +1000489 error = xfs_start_flags(args, mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 if (error)
491 goto error1;
Nathan Scott764d1f82006-03-31 13:04:17 +1000492 error = xfs_readsb(mp, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 if (error)
494 goto error1;
Christoph Hellwigb267ce92007-08-30 17:21:30 +1000495 error = xfs_finish_flags(args, mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 if (error)
497 goto error2;
498
499 /*
500 * Setup xfs_mount buffer target pointers based on superblock
501 */
502 error = xfs_setsize_buftarg(mp->m_ddev_targp, mp->m_sb.sb_blocksize,
503 mp->m_sb.sb_sectsize);
504 if (!error && logdev && logdev != ddev) {
505 unsigned int log_sector_size = BBSIZE;
506
Eric Sandeen62118702008-03-06 13:44:28 +1100507 if (xfs_sb_version_hassector(&mp->m_sb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 log_sector_size = mp->m_sb.sb_logsectsize;
509 error = xfs_setsize_buftarg(mp->m_logdev_targp,
510 mp->m_sb.sb_blocksize,
511 log_sector_size);
512 }
513 if (!error && rtdev)
514 error = xfs_setsize_buftarg(mp->m_rtdev_targp,
515 mp->m_sb.sb_blocksize,
516 mp->m_sb.sb_sectsize);
517 if (error)
518 goto error2;
519
Nathan Scottb2ea4012006-07-28 17:05:13 +1000520 if (mp->m_flags & XFS_MOUNT_BARRIER)
Christoph Hellwigc7d437d2006-01-11 15:28:56 +1100521 xfs_mountfs_check_barriers(mp);
522
David Chinner2a82b8b2007-07-11 11:09:12 +1000523 if ((error = xfs_filestream_mount(mp)))
524 goto error2;
525
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000526 error = xfs_mountfs(mp, flags);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100527 if (error)
528 goto error2;
529
Christoph Hellwig293688e2007-08-29 11:59:36 +1000530 XFS_SEND_MOUNT(mp, DM_RIGHT_NULL, args->mtpt, args->fsname);
531
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100532 return 0;
533
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534error2:
535 if (mp->m_sb_bp)
536 xfs_freesb(mp);
537error1:
538 xfs_binval(mp->m_ddev_targp);
539 if (logdev && logdev != ddev)
540 xfs_binval(mp->m_logdev_targp);
541 if (rtdev)
542 xfs_binval(mp->m_rtdev_targp);
543error0:
544 xfs_unmountfs_close(mp, credp);
Christoph Hellwigb09cc772007-08-30 17:19:57 +1000545 xfs_qmops_put(mp);
Christoph Hellwig293688e2007-08-29 11:59:36 +1000546 xfs_dmops_put(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 return error;
548}
549
Christoph Hellwig48c872a9f2007-08-30 17:20:31 +1000550int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551xfs_unmount(
Christoph Hellwig48c872a9f2007-08-30 17:20:31 +1000552 xfs_mount_t *mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 int flags,
554 cred_t *credp)
555{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 xfs_inode_t *rip;
Nathan Scott67fcaa72006-06-09 17:00:52 +1000557 bhv_vnode_t *rvp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 int unmount_event_wanted = 0;
559 int unmount_event_flags = 0;
560 int xfs_unmountfs_needed = 0;
561 int error;
562
563 rip = mp->m_rootip;
564 rvp = XFS_ITOV(rip);
565
Eric Sandeen948c6d4f2007-08-23 16:19:57 +1000566#ifdef HAVE_DMAPI
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000567 if (mp->m_flags & XFS_MOUNT_DMAPI) {
Christoph Hellwigb267ce92007-08-30 17:21:30 +1000568 error = XFS_SEND_PREUNMOUNT(mp,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +1100569 rip, DM_RIGHT_NULL, rip, DM_RIGHT_NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 NULL, NULL, 0, 0,
571 (mp->m_dmevmask & (1<<DM_EVENT_PREUNMOUNT))?
572 0:DM_FLAGS_UNWANTED);
573 if (error)
574 return XFS_ERROR(error);
575 unmount_event_wanted = 1;
576 unmount_event_flags = (mp->m_dmevmask & (1<<DM_EVENT_UNMOUNT))?
577 0 : DM_FLAGS_UNWANTED;
578 }
Eric Sandeen948c6d4f2007-08-23 16:19:57 +1000579#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
David Chinner2a82b8b2007-07-11 11:09:12 +1000581 /*
582 * Blow away any referenced inode in the filestreams cache.
583 * This can and will cause log traffic as inodes go inactive
584 * here.
585 */
586 xfs_filestream_unmount(mp);
587
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 XFS_bflush(mp->m_ddev_targp);
589 error = xfs_unmount_flush(mp, 0);
590 if (error)
591 goto out;
592
593 ASSERT(vn_count(rvp) == 1);
594
595 /*
596 * Drop the reference count
597 */
598 VN_RELE(rvp);
599
600 /*
601 * If we're forcing a shutdown, typically because of a media error,
602 * we want to make sure we invalidate dirty pages that belong to
603 * referenced vnodes as well.
604 */
605 if (XFS_FORCED_SHUTDOWN(mp)) {
Christoph Hellwig48c872a9f2007-08-30 17:20:31 +1000606 error = xfs_sync(mp, SYNC_WAIT | SYNC_CLOSE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 ASSERT(error != EFSCORRUPTED);
608 }
609 xfs_unmountfs_needed = 1;
610
611out:
612 /* Send DMAPI event, if required.
613 * Then do xfs_unmountfs() if needed.
614 * Then return error (or zero).
615 */
616 if (unmount_event_wanted) {
617 /* Note: mp structure must still exist for
618 * XFS_SEND_UNMOUNT() call.
619 */
Christoph Hellwigbc4ac742008-03-06 13:45:58 +1100620 XFS_SEND_UNMOUNT(mp, error == 0 ? rip : NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 DM_RIGHT_NULL, 0, error, unmount_event_flags);
622 }
623 if (xfs_unmountfs_needed) {
624 /*
625 * Call common unmount function to flush to disk
626 * and free the super block buffer & mount structures.
627 */
628 xfs_unmountfs(mp, credp);
Christoph Hellwigb09cc772007-08-30 17:19:57 +1000629 xfs_qmops_put(mp);
Christoph Hellwig293688e2007-08-29 11:59:36 +1000630 xfs_dmops_put(mp);
Christoph Hellwigf541d272007-08-29 11:53:22 +1000631 kmem_free(mp, sizeof(xfs_mount_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 }
633
634 return XFS_ERROR(error);
635}
636
Christoph Hellwigf898d6c2005-06-21 15:40:48 +1000637STATIC int
638xfs_quiesce_fs(
639 xfs_mount_t *mp)
640{
641 int count = 0, pincount;
Nathan Scott3758dee2006-03-22 12:47:28 +1100642
Christoph Hellwigf898d6c2005-06-21 15:40:48 +1000643 xfs_flush_buftarg(mp->m_ddev_targp, 0);
644 xfs_finish_reclaim_all(mp, 0);
645
646 /* This loop must run at least twice.
647 * The first instance of the loop will flush
648 * most meta data but that will generate more
649 * meta data (typically directory updates).
650 * Which then must be flushed and logged before
651 * we can write the unmount record.
Nathan Scott3758dee2006-03-22 12:47:28 +1100652 */
Christoph Hellwigf898d6c2005-06-21 15:40:48 +1000653 do {
David Chinner516b2e72007-06-18 16:50:48 +1000654 xfs_syncsub(mp, SYNC_INODE_QUIESCE, NULL);
Christoph Hellwigf898d6c2005-06-21 15:40:48 +1000655 pincount = xfs_flush_buftarg(mp->m_ddev_targp, 1);
656 if (!pincount) {
657 delay(50);
658 count++;
659 }
660 } while (count < 2);
661
662 return 0;
663}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
David Chinner516b2e72007-06-18 16:50:48 +1000665/*
666 * Second stage of a quiesce. The data is already synced, now we have to take
667 * care of the metadata. New transactions are already blocked, so we need to
668 * wait for any remaining transactions to drain out before proceding.
669 */
Christoph Hellwig9909c4a2007-10-11 18:11:14 +1000670void
David Chinner516b2e72007-06-18 16:50:48 +1000671xfs_attr_quiesce(
672 xfs_mount_t *mp)
673{
674 /* wait for all modifications to complete */
675 while (atomic_read(&mp->m_active_trans) > 0)
676 delay(100);
677
678 /* flush inodes and push all remaining buffers out to disk */
679 xfs_quiesce_fs(mp);
680
681 ASSERT_ALWAYS(atomic_read(&mp->m_active_trans) == 0);
682
683 /* Push the superblock and write an unmount record */
684 xfs_log_sbcount(mp, 1);
685 xfs_log_unmount_write(mp);
686 xfs_unmountfs_writesb(mp);
687}
688
Christoph Hellwig48c872a9f2007-08-30 17:20:31 +1000689int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690xfs_mntupdate(
Christoph Hellwig48c872a9f2007-08-30 17:20:31 +1000691 struct xfs_mount *mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 int *flags,
693 struct xfs_mount_args *args)
694{
Nathan Scottd08d3892006-05-08 19:51:28 +1000695 if (!(*flags & MS_RDONLY)) { /* rw/ro -> rw */
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000696 if (mp->m_flags & XFS_MOUNT_RDONLY)
697 mp->m_flags &= ~XFS_MOUNT_RDONLY;
Nathan Scottd08d3892006-05-08 19:51:28 +1000698 if (args->flags & XFSMNT_BARRIER) {
699 mp->m_flags |= XFS_MOUNT_BARRIER;
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100700 xfs_mountfs_check_barriers(mp);
Nathan Scottd08d3892006-05-08 19:51:28 +1000701 } else {
702 mp->m_flags &= ~XFS_MOUNT_BARRIER;
703 }
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000704 } else if (!(mp->m_flags & XFS_MOUNT_RDONLY)) { /* rw -> ro */
David Chinner2a82b8b2007-07-11 11:09:12 +1000705 xfs_filestream_flush(mp);
Christoph Hellwig745f6912007-08-30 17:20:39 +1000706 xfs_sync(mp, SYNC_DATA_QUIESCE);
David Chinner516b2e72007-06-18 16:50:48 +1000707 xfs_attr_quiesce(mp);
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000708 mp->m_flags |= XFS_MOUNT_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 return 0;
711}
712
713/*
714 * xfs_unmount_flush implements a set of flush operation on special
715 * inodes, which are needed as a separate set of operations so that
716 * they can be called as part of relocation process.
717 */
718int
719xfs_unmount_flush(
720 xfs_mount_t *mp, /* Mount structure we are getting
721 rid of. */
722 int relocation) /* Called from vfs relocation. */
723{
724 xfs_inode_t *rip = mp->m_rootip;
725 xfs_inode_t *rbmip;
726 xfs_inode_t *rsumip = NULL;
Nathan Scott67fcaa72006-06-09 17:00:52 +1000727 bhv_vnode_t *rvp = XFS_ITOV(rip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 int error;
729
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +1000730 xfs_ilock(rip, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 xfs_iflock(rip);
732
733 /*
734 * Flush out the real time inodes.
735 */
736 if ((rbmip = mp->m_rbmip) != NULL) {
737 xfs_ilock(rbmip, XFS_ILOCK_EXCL);
738 xfs_iflock(rbmip);
739 error = xfs_iflush(rbmip, XFS_IFLUSH_SYNC);
740 xfs_iunlock(rbmip, XFS_ILOCK_EXCL);
741
742 if (error == EFSCORRUPTED)
743 goto fscorrupt_out;
744
745 ASSERT(vn_count(XFS_ITOV(rbmip)) == 1);
746
747 rsumip = mp->m_rsumip;
748 xfs_ilock(rsumip, XFS_ILOCK_EXCL);
749 xfs_iflock(rsumip);
750 error = xfs_iflush(rsumip, XFS_IFLUSH_SYNC);
751 xfs_iunlock(rsumip, XFS_ILOCK_EXCL);
752
753 if (error == EFSCORRUPTED)
754 goto fscorrupt_out;
755
756 ASSERT(vn_count(XFS_ITOV(rsumip)) == 1);
757 }
758
759 /*
760 * Synchronously flush root inode to disk
761 */
762 error = xfs_iflush(rip, XFS_IFLUSH_SYNC);
763 if (error == EFSCORRUPTED)
764 goto fscorrupt_out2;
765
766 if (vn_count(rvp) != 1 && !relocation) {
767 xfs_iunlock(rip, XFS_ILOCK_EXCL);
768 return XFS_ERROR(EBUSY);
769 }
770
771 /*
772 * Release dquot that rootinode, rbmino and rsumino might be holding,
773 * flush and purge the quota inodes.
774 */
775 error = XFS_QM_UNMOUNT(mp);
776 if (error == EFSCORRUPTED)
777 goto fscorrupt_out2;
778
779 if (rbmip) {
780 VN_RELE(XFS_ITOV(rbmip));
781 VN_RELE(XFS_ITOV(rsumip));
782 }
783
784 xfs_iunlock(rip, XFS_ILOCK_EXCL);
785 return 0;
786
787fscorrupt_out:
788 xfs_ifunlock(rip);
789
790fscorrupt_out2:
791 xfs_iunlock(rip, XFS_ILOCK_EXCL);
792
793 return XFS_ERROR(EFSCORRUPTED);
794}
795
796/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 * xfs_sync flushes any pending I/O to file system vfsp.
798 *
799 * This routine is called by vfs_sync() to make sure that things make it
800 * out to disk eventually, on sync() system calls to flush out everything,
801 * and when the file system is unmounted. For the vfs_sync() case, all
802 * we really need to do is sync out the log to make all of our meta-data
803 * updates permanent (except for timestamps). For calls from pflushd(),
804 * dirty pages are kept moving by calling pdflush() on the inodes
805 * containing them. We also flush the inodes that we can lock without
806 * sleeping and the superblock if we can lock it without sleeping from
807 * vfs_sync() so that items at the tail of the log are always moving out.
808 *
809 * Flags:
810 * SYNC_BDFLUSH - We're being called from vfs_sync() so we don't want
811 * to sleep if we can help it. All we really need
812 * to do is ensure that the log is synced at least
813 * periodically. We also push the inodes and
814 * superblock if we can lock them without sleeping
815 * and they are not pinned.
816 * SYNC_ATTR - We need to flush the inodes. If SYNC_BDFLUSH is not
817 * set, then we really want to lock each inode and flush
818 * it.
819 * SYNC_WAIT - All the flushes that take place in this call should
820 * be synchronous.
821 * SYNC_DELWRI - This tells us to push dirty pages associated with
822 * inodes. SYNC_WAIT and SYNC_BDFLUSH are used to
823 * determine if they should be flushed sync, async, or
824 * delwri.
825 * SYNC_CLOSE - This flag is passed when the system is being
Nathan Scottc41564b2006-03-29 08:55:14 +1000826 * unmounted. We should sync and invalidate everything.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 * SYNC_FSDATA - This indicates that the caller would like to make
828 * sure the superblock is safe on disk. We can ensure
Nathan Scottc41564b2006-03-29 08:55:14 +1000829 * this by simply making sure the log gets flushed
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 * if SYNC_BDFLUSH is set, and by actually writing it
831 * out otherwise.
David Chinner3c0dc772007-02-10 18:37:22 +1100832 * SYNC_IOWAIT - The caller wants us to wait for all data I/O to complete
833 * before we return (including direct I/O). Forms the drain
834 * side of the write barrier needed to safely quiesce the
835 * filesystem.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 *
837 */
Christoph Hellwig48c872a9f2007-08-30 17:20:31 +1000838int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839xfs_sync(
Christoph Hellwig48c872a9f2007-08-30 17:20:31 +1000840 xfs_mount_t *mp,
841 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842{
Christoph Hellwigb09cc772007-08-30 17:19:57 +1000843 int error;
844
845 /*
846 * Get the Quota Manager to flush the dquots.
847 *
848 * If XFS quota support is not enabled or this filesystem
849 * instance does not use quotas XFS_QM_DQSYNC will always
850 * return zero.
851 */
852 error = XFS_QM_DQSYNC(mp, flags);
853 if (error) {
854 /*
855 * If we got an IO error, we will be shutting down.
856 * So, there's nothing more for us to do here.
857 */
858 ASSERT(error != EIO || XFS_FORCED_SHUTDOWN(mp));
859 if (XFS_FORCED_SHUTDOWN(mp))
860 return XFS_ERROR(error);
861 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
David Chinner2a82b8b2007-07-11 11:09:12 +1000863 if (flags & SYNC_IOWAIT)
864 xfs_filestream_flush(mp);
865
David Chinner28239452007-02-10 18:36:40 +1100866 return xfs_syncsub(mp, flags, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867}
868
869/*
870 * xfs sync routine for internal use
871 *
Nathan Scottb83bd132006-06-09 16:48:30 +1000872 * This routine supports all of the flags defined for the generic vfs_sync
Lachlan McIlroy1f9b3b62007-02-10 18:35:33 +1100873 * interface as explained above under xfs_sync.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 *
875 */
Nathan Scottee348072005-11-02 10:32:38 +1100876int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877xfs_sync_inodes(
878 xfs_mount_t *mp,
879 int flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 int *bypassed)
881{
882 xfs_inode_t *ip = NULL;
Nathan Scott67fcaa72006-06-09 17:00:52 +1000883 bhv_vnode_t *vp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 int error;
885 int last_error;
886 uint64_t fflag;
887 uint lock_flags;
888 uint base_lock_flags;
889 boolean_t mount_locked;
890 boolean_t vnode_refed;
891 int preempt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 xfs_iptr_t *ipointer;
893#ifdef DEBUG
894 boolean_t ipointer_in = B_FALSE;
895
896#define IPOINTER_SET ipointer_in = B_TRUE
897#define IPOINTER_CLR ipointer_in = B_FALSE
898#else
899#define IPOINTER_SET
900#define IPOINTER_CLR
901#endif
902
903
904/* Insert a marker record into the inode list after inode ip. The list
905 * must be locked when this is called. After the call the list will no
906 * longer be locked.
907 */
908#define IPOINTER_INSERT(ip, mp) { \
909 ASSERT(ipointer_in == B_FALSE); \
910 ipointer->ip_mnext = ip->i_mnext; \
911 ipointer->ip_mprev = ip; \
912 ip->i_mnext = (xfs_inode_t *)ipointer; \
913 ipointer->ip_mnext->i_mprev = (xfs_inode_t *)ipointer; \
914 preempt = 0; \
915 XFS_MOUNT_IUNLOCK(mp); \
916 mount_locked = B_FALSE; \
917 IPOINTER_SET; \
918 }
919
920/* Remove the marker from the inode list. If the marker was the only item
921 * in the list then there are no remaining inodes and we should zero out
922 * the whole list. If we are the current head of the list then move the head
923 * past us.
924 */
925#define IPOINTER_REMOVE(ip, mp) { \
926 ASSERT(ipointer_in == B_TRUE); \
927 if (ipointer->ip_mnext != (xfs_inode_t *)ipointer) { \
928 ip = ipointer->ip_mnext; \
929 ip->i_mprev = ipointer->ip_mprev; \
930 ipointer->ip_mprev->i_mnext = ip; \
931 if (mp->m_inodes == (xfs_inode_t *)ipointer) { \
932 mp->m_inodes = ip; \
933 } \
934 } else { \
935 ASSERT(mp->m_inodes == (xfs_inode_t *)ipointer); \
936 mp->m_inodes = NULL; \
937 ip = NULL; \
938 } \
939 IPOINTER_CLR; \
940 }
941
942#define XFS_PREEMPT_MASK 0x7f
943
Lachlan McIlroy44866d32007-09-14 15:21:08 +1000944 ASSERT(!(flags & SYNC_BDFLUSH));
945
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 if (bypassed)
947 *bypassed = 0;
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000948 if (mp->m_flags & XFS_MOUNT_RDONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 return 0;
950 error = 0;
951 last_error = 0;
952 preempt = 0;
953
954 /* Allocate a reference marker */
955 ipointer = (xfs_iptr_t *)kmem_zalloc(sizeof(xfs_iptr_t), KM_SLEEP);
956
957 fflag = XFS_B_ASYNC; /* default is don't wait */
Lachlan McIlroy44866d32007-09-14 15:21:08 +1000958 if (flags & SYNC_DELWRI)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 fflag = XFS_B_DELWRI;
960 if (flags & SYNC_WAIT)
961 fflag = 0; /* synchronous overrides all */
962
963 base_lock_flags = XFS_ILOCK_SHARED;
964 if (flags & (SYNC_DELWRI | SYNC_CLOSE)) {
965 /*
966 * We need the I/O lock if we're going to call any of
967 * the flush/inval routines.
968 */
969 base_lock_flags |= XFS_IOLOCK_SHARED;
970 }
971
972 XFS_MOUNT_ILOCK(mp);
973
974 ip = mp->m_inodes;
975
976 mount_locked = B_TRUE;
977 vnode_refed = B_FALSE;
978
979 IPOINTER_CLR;
980
981 do {
982 ASSERT(ipointer_in == B_FALSE);
983 ASSERT(vnode_refed == B_FALSE);
984
985 lock_flags = base_lock_flags;
986
987 /*
988 * There were no inodes in the list, just break out
989 * of the loop.
990 */
991 if (ip == NULL) {
992 break;
993 }
994
995 /*
996 * We found another sync thread marker - skip it
997 */
998 if (ip->i_mount == NULL) {
999 ip = ip->i_mnext;
1000 continue;
1001 }
1002
1003 vp = XFS_ITOV_NULL(ip);
1004
1005 /*
1006 * If the vnode is gone then this is being torn down,
1007 * call reclaim if it is flushed, else let regular flush
1008 * code deal with it later in the loop.
1009 */
1010
1011 if (vp == NULL) {
1012 /* Skip ones already in reclaim */
1013 if (ip->i_flags & XFS_IRECLAIM) {
1014 ip = ip->i_mnext;
1015 continue;
1016 }
1017 if (xfs_ilock_nowait(ip, XFS_ILOCK_EXCL) == 0) {
1018 ip = ip->i_mnext;
1019 } else if ((xfs_ipincount(ip) == 0) &&
1020 xfs_iflock_nowait(ip)) {
1021 IPOINTER_INSERT(ip, mp);
1022
1023 xfs_finish_reclaim(ip, 1,
1024 XFS_IFLUSH_DELWRI_ELSE_ASYNC);
1025
1026 XFS_MOUNT_ILOCK(mp);
1027 mount_locked = B_TRUE;
1028 IPOINTER_REMOVE(ip, mp);
1029 } else {
1030 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1031 ip = ip->i_mnext;
1032 }
1033 continue;
1034 }
1035
1036 if (VN_BAD(vp)) {
1037 ip = ip->i_mnext;
1038 continue;
1039 }
1040
1041 if (XFS_FORCED_SHUTDOWN(mp) && !(flags & SYNC_CLOSE)) {
1042 XFS_MOUNT_IUNLOCK(mp);
1043 kmem_free(ipointer, sizeof(xfs_iptr_t));
1044 return 0;
1045 }
1046
1047 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 * Try to lock without sleeping. We're out of order with
1049 * the inode list lock here, so if we fail we need to drop
1050 * the mount lock and try again. If we're called from
1051 * bdflush() here, then don't bother.
1052 *
1053 * The inode lock here actually coordinates with the
1054 * almost spurious inode lock in xfs_ireclaim() to prevent
1055 * the vnode we handle here without a reference from
1056 * being freed while we reference it. If we lock the inode
1057 * while it's on the mount list here, then the spurious inode
1058 * lock in xfs_ireclaim() after the inode is pulled from
1059 * the mount list will sleep until we release it here.
1060 * This keeps the vnode from being freed while we reference
Christoph Hellwigcdb62682005-09-02 16:24:19 +10001061 * it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 */
1063 if (xfs_ilock_nowait(ip, lock_flags) == 0) {
Lachlan McIlroy44866d32007-09-14 15:21:08 +10001064 if (vp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 ip = ip->i_mnext;
1066 continue;
1067 }
1068
Christoph Hellwigcdb62682005-09-02 16:24:19 +10001069 vp = vn_grab(vp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 if (vp == NULL) {
Christoph Hellwigcdb62682005-09-02 16:24:19 +10001071 ip = ip->i_mnext;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 continue;
1073 }
1074
Christoph Hellwigcdb62682005-09-02 16:24:19 +10001075 IPOINTER_INSERT(ip, mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 xfs_ilock(ip, lock_flags);
1077
1078 ASSERT(vp == XFS_ITOV(ip));
1079 ASSERT(ip->i_mount == mp);
1080
1081 vnode_refed = B_TRUE;
1082 }
1083
1084 /* From here on in the loop we may have a marker record
1085 * in the inode list.
1086 */
1087
David Chinner40095b62007-05-14 18:24:09 +10001088 /*
1089 * If we have to flush data or wait for I/O completion
1090 * we need to drop the ilock that we currently hold.
1091 * If we need to drop the lock, insert a marker if we
1092 * have not already done so.
1093 */
1094 if ((flags & (SYNC_CLOSE|SYNC_IOWAIT)) ||
1095 ((flags & SYNC_DELWRI) && VN_DIRTY(vp))) {
1096 if (mount_locked) {
1097 IPOINTER_INSERT(ip, mp);
1098 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 xfs_iunlock(ip, XFS_ILOCK_SHARED);
1100
David Chinner40095b62007-05-14 18:24:09 +10001101 if (flags & SYNC_CLOSE) {
1102 /* Shutdown case. Flush and invalidate. */
1103 if (XFS_FORCED_SHUTDOWN(mp))
Christoph Hellwig739bfb22007-08-29 10:58:01 +10001104 xfs_tosspages(ip, 0, -1,
1105 FI_REMAPF);
David Chinner40095b62007-05-14 18:24:09 +10001106 else
Christoph Hellwig739bfb22007-08-29 10:58:01 +10001107 error = xfs_flushinval_pages(ip,
1108 0, -1, FI_REMAPF);
David Chinner40095b62007-05-14 18:24:09 +10001109 } else if ((flags & SYNC_DELWRI) && VN_DIRTY(vp)) {
Christoph Hellwig739bfb22007-08-29 10:58:01 +10001110 error = xfs_flush_pages(ip, 0,
Nathan Scott67fcaa72006-06-09 17:00:52 +10001111 -1, fflag, FI_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 }
1113
David Chinner40095b62007-05-14 18:24:09 +10001114 /*
1115 * When freezing, we need to wait ensure all I/O (including direct
1116 * I/O) is complete to ensure no further data modification can take
1117 * place after this point
1118 */
1119 if (flags & SYNC_IOWAIT)
Christoph Hellwigb677c212007-08-29 11:46:28 +10001120 vn_iowait(ip);
David Chinner40095b62007-05-14 18:24:09 +10001121
1122 xfs_ilock(ip, XFS_ILOCK_SHARED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 }
1124
Lachlan McIlroy44866d32007-09-14 15:21:08 +10001125 if ((flags & SYNC_ATTR) &&
1126 (ip->i_update_core ||
1127 (ip->i_itemp && ip->i_itemp->ili_format.ilf_fields))) {
1128 if (mount_locked)
1129 IPOINTER_INSERT(ip, mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130
Lachlan McIlroy44866d32007-09-14 15:21:08 +10001131 if (flags & SYNC_WAIT) {
1132 xfs_iflock(ip);
1133 error = xfs_iflush(ip, XFS_IFLUSH_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
Lachlan McIlroy44866d32007-09-14 15:21:08 +10001135 /*
1136 * If we can't acquire the flush lock, then the inode
1137 * is already being flushed so don't bother waiting.
1138 *
1139 * If we can lock it then do a delwri flush so we can
1140 * combine multiple inode flushes in each disk write.
1141 */
1142 } else if (xfs_iflock_nowait(ip)) {
1143 error = xfs_iflush(ip, XFS_IFLUSH_DELWRI);
1144 } else if (bypassed) {
1145 (*bypassed)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 }
1147 }
1148
1149 if (lock_flags != 0) {
1150 xfs_iunlock(ip, lock_flags);
1151 }
1152
1153 if (vnode_refed) {
1154 /*
1155 * If we had to take a reference on the vnode
1156 * above, then wait until after we've unlocked
1157 * the inode to release the reference. This is
1158 * because we can be already holding the inode
1159 * lock when VN_RELE() calls xfs_inactive().
1160 *
1161 * Make sure to drop the mount lock before calling
1162 * VN_RELE() so that we don't trip over ourselves if
1163 * we have to go for the mount lock again in the
1164 * inactive code.
1165 */
1166 if (mount_locked) {
1167 IPOINTER_INSERT(ip, mp);
1168 }
1169
1170 VN_RELE(vp);
1171
1172 vnode_refed = B_FALSE;
1173 }
1174
1175 if (error) {
1176 last_error = error;
1177 }
1178
1179 /*
1180 * bail out if the filesystem is corrupted.
1181 */
1182 if (error == EFSCORRUPTED) {
1183 if (!mount_locked) {
1184 XFS_MOUNT_ILOCK(mp);
1185 IPOINTER_REMOVE(ip, mp);
1186 }
1187 XFS_MOUNT_IUNLOCK(mp);
1188 ASSERT(ipointer_in == B_FALSE);
1189 kmem_free(ipointer, sizeof(xfs_iptr_t));
1190 return XFS_ERROR(error);
1191 }
1192
1193 /* Let other threads have a chance at the mount lock
1194 * if we have looped many times without dropping the
1195 * lock.
1196 */
1197 if ((++preempt & XFS_PREEMPT_MASK) == 0) {
1198 if (mount_locked) {
1199 IPOINTER_INSERT(ip, mp);
1200 }
1201 }
1202
1203 if (mount_locked == B_FALSE) {
1204 XFS_MOUNT_ILOCK(mp);
1205 mount_locked = B_TRUE;
1206 IPOINTER_REMOVE(ip, mp);
1207 continue;
1208 }
1209
1210 ASSERT(ipointer_in == B_FALSE);
1211 ip = ip->i_mnext;
1212
1213 } while (ip != mp->m_inodes);
1214
1215 XFS_MOUNT_IUNLOCK(mp);
1216
1217 ASSERT(ipointer_in == B_FALSE);
1218
1219 kmem_free(ipointer, sizeof(xfs_iptr_t));
1220 return XFS_ERROR(last_error);
1221}
1222
1223/*
1224 * xfs sync routine for internal use
1225 *
Nathan Scottb83bd132006-06-09 16:48:30 +10001226 * This routine supports all of the flags defined for the generic vfs_sync
Lachlan McIlroy1f9b3b62007-02-10 18:35:33 +11001227 * interface as explained above under xfs_sync.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 *
1229 */
1230int
1231xfs_syncsub(
1232 xfs_mount_t *mp,
1233 int flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 int *bypassed)
1235{
1236 int error = 0;
1237 int last_error = 0;
1238 uint log_flags = XFS_LOG_FORCE;
1239 xfs_buf_t *bp;
1240 xfs_buf_log_item_t *bip;
1241
1242 /*
1243 * Sync out the log. This ensures that the log is periodically
1244 * flushed even if there is not enough activity to fill it up.
1245 */
1246 if (flags & SYNC_WAIT)
1247 log_flags |= XFS_LOG_SYNC;
1248
1249 xfs_log_force(mp, (xfs_lsn_t)0, log_flags);
1250
1251 if (flags & (SYNC_ATTR|SYNC_DELWRI)) {
1252 if (flags & SYNC_BDFLUSH)
1253 xfs_finish_reclaim_all(mp, 1);
1254 else
Lachlan McIlroy1f9b3b62007-02-10 18:35:33 +11001255 error = xfs_sync_inodes(mp, flags, bypassed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 }
1257
1258 /*
1259 * Flushing out dirty data above probably generated more
1260 * log activity, so if this isn't vfs_sync() then flush
1261 * the log again.
1262 */
1263 if (flags & SYNC_DELWRI) {
1264 xfs_log_force(mp, (xfs_lsn_t)0, log_flags);
1265 }
1266
1267 if (flags & SYNC_FSDATA) {
1268 /*
1269 * If this is vfs_sync() then only sync the superblock
1270 * if we can lock it without sleeping and it is not pinned.
1271 */
1272 if (flags & SYNC_BDFLUSH) {
1273 bp = xfs_getsb(mp, XFS_BUF_TRYLOCK);
1274 if (bp != NULL) {
1275 bip = XFS_BUF_FSPRIVATE(bp,xfs_buf_log_item_t*);
1276 if ((bip != NULL) &&
1277 xfs_buf_item_dirty(bip)) {
1278 if (!(XFS_BUF_ISPINNED(bp))) {
1279 XFS_BUF_ASYNC(bp);
1280 error = xfs_bwrite(mp, bp);
1281 } else {
1282 xfs_buf_relse(bp);
1283 }
1284 } else {
1285 xfs_buf_relse(bp);
1286 }
1287 }
1288 } else {
1289 bp = xfs_getsb(mp, 0);
1290 /*
1291 * If the buffer is pinned then push on the log so
1292 * we won't get stuck waiting in the write for
1293 * someone, maybe ourselves, to flush the log.
1294 * Even though we just pushed the log above, we
1295 * did not have the superblock buffer locked at
1296 * that point so it can become pinned in between
1297 * there and here.
1298 */
1299 if (XFS_BUF_ISPINNED(bp))
1300 xfs_log_force(mp, (xfs_lsn_t)0, XFS_LOG_FORCE);
1301 if (flags & SYNC_WAIT)
1302 XFS_BUF_UNASYNC(bp);
1303 else
1304 XFS_BUF_ASYNC(bp);
1305 error = xfs_bwrite(mp, bp);
1306 }
1307 if (error) {
1308 last_error = error;
1309 }
1310 }
1311
1312 /*
David Chinner92821e22007-05-24 15:26:31 +10001313 * If asked, update the disk superblock with incore counter values if we
1314 * are using non-persistent counters so that they don't get too far out
1315 * of sync if we crash or get a forced shutdown. We don't want to force
1316 * this to disk, just get a transaction into the iclogs....
1317 */
1318 if (flags & SYNC_SUPER)
1319 xfs_log_sbcount(mp, 0);
1320
1321 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 * Now check to see if the log needs a "dummy" transaction.
1323 */
1324
1325 if (!(flags & SYNC_REMOUNT) && xfs_log_need_covered(mp)) {
1326 xfs_trans_t *tp;
1327 xfs_inode_t *ip;
1328
1329 /*
1330 * Put a dummy transaction in the log to tell
1331 * recovery that all others are OK.
1332 */
1333 tp = xfs_trans_alloc(mp, XFS_TRANS_DUMMY1);
1334 if ((error = xfs_trans_reserve(tp, 0,
1335 XFS_ICHANGE_LOG_RES(mp),
1336 0, 0, 0))) {
1337 xfs_trans_cancel(tp, 0);
1338 return error;
1339 }
1340
1341 ip = mp->m_rootip;
1342 xfs_ilock(ip, XFS_ILOCK_EXCL);
1343
1344 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
1345 xfs_trans_ihold(tp, ip);
1346 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Eric Sandeen1c72bf92007-05-08 13:48:42 +10001347 error = xfs_trans_commit(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1349 xfs_log_force(mp, (xfs_lsn_t)0, log_flags);
1350 }
1351
1352 /*
1353 * When shutting down, we need to insure that the AIL is pushed
1354 * to disk or the filesystem can appear corrupt from the PROM.
1355 */
1356 if ((flags & (SYNC_CLOSE|SYNC_WAIT)) == (SYNC_CLOSE|SYNC_WAIT)) {
1357 XFS_bflush(mp->m_ddev_targp);
1358 if (mp->m_rtdev_targp) {
1359 XFS_bflush(mp->m_rtdev_targp);
1360 }
1361 }
1362
1363 return XFS_ERROR(last_error);
1364}