blob: 6351efb569c7350d78ab00de811f16d6a776d40b [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 Hellwig43355092008-03-27 18:01:08 +110058#include "xfs_utils.h"
Christoph Hellwig739bfb22007-08-29 10:58:01 +100059
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Lachlan McIlroyde2eeea2008-02-06 13:37:56 +110061int __init
Linus Torvalds1da177e2005-04-16 15:20:36 -070062xfs_init(void)
63{
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#ifdef XFS_DABUF_DEBUG
Eric Sandeen703e1f02007-10-11 17:41:21 +100065 extern spinlock_t xfs_dabuf_global_lock;
Eric Sandeen007c61c2007-10-11 17:43:56 +100066 spin_lock_init(&xfs_dabuf_global_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#endif
68
69 /*
70 * Initialize all of the zone allocators we use.
71 */
David Chinnereb01c9c2008-04-10 12:18:46 +100072 xfs_log_ticket_zone = kmem_zone_init(sizeof(xlog_ticket_t),
73 "xfs_log_ticket");
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 xfs_bmap_free_item_zone = kmem_zone_init(sizeof(xfs_bmap_free_item_t),
David Chinnereb01c9c2008-04-10 12:18:46 +100075 "xfs_bmap_free_item");
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 xfs_btree_cur_zone = kmem_zone_init(sizeof(xfs_btree_cur_t),
David Chinnereb01c9c2008-04-10 12:18:46 +100077 "xfs_btree_cur");
78 xfs_da_state_zone = kmem_zone_init(sizeof(xfs_da_state_t),
79 "xfs_da_state");
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 xfs_dabuf_zone = kmem_zone_init(sizeof(xfs_dabuf_t), "xfs_dabuf");
Nathan Scott87582802006-03-14 13:18:19 +110081 xfs_ifork_zone = kmem_zone_init(sizeof(xfs_ifork_t), "xfs_ifork");
David Chinnereb01c9c2008-04-10 12:18:46 +100082 xfs_trans_zone = kmem_zone_init(sizeof(xfs_trans_t), "xfs_trans");
Nathan Scott87582802006-03-14 13:18:19 +110083 xfs_acl_zone_init(xfs_acl_zone, "xfs_acl");
David Chinner2a82b8b2007-07-11 11:09:12 +100084 xfs_mru_cache_init();
85 xfs_filestream_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87 /*
88 * The size of the zone allocated buf log item is the maximum
89 * size possible under XFS. This wastes a little bit of memory,
90 * but it is much faster.
91 */
92 xfs_buf_item_zone =
93 kmem_zone_init((sizeof(xfs_buf_log_item_t) +
94 (((XFS_MAX_BLOCKSIZE / XFS_BLI_CHUNK) /
95 NBWORD) * sizeof(int))),
96 "xfs_buf_item");
Nathan Scott87582802006-03-14 13:18:19 +110097 xfs_efd_zone =
98 kmem_zone_init((sizeof(xfs_efd_log_item_t) +
99 ((XFS_EFD_MAX_FAST_EXTENTS - 1) *
100 sizeof(xfs_extent_t))),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 "xfs_efd_item");
Nathan Scott87582802006-03-14 13:18:19 +1100102 xfs_efi_zone =
103 kmem_zone_init((sizeof(xfs_efi_log_item_t) +
104 ((XFS_EFI_MAX_FAST_EXTENTS - 1) *
105 sizeof(xfs_extent_t))),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 "xfs_efi_item");
Nathan Scott87582802006-03-14 13:18:19 +1100107
108 /*
109 * These zones warrant special memory allocator hints
110 */
111 xfs_inode_zone =
112 kmem_zone_init_flags(sizeof(xfs_inode_t), "xfs_inode",
113 KM_ZONE_HWALIGN | KM_ZONE_RECLAIM |
114 KM_ZONE_SPREAD, NULL);
115 xfs_ili_zone =
116 kmem_zone_init_flags(sizeof(xfs_inode_log_item_t), "xfs_ili",
117 KM_ZONE_SPREAD, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
119 /*
120 * Allocate global trace buffers.
121 */
122#ifdef XFS_ALLOC_TRACE
123 xfs_alloc_trace_buf = ktrace_alloc(XFS_ALLOC_TRACE_SIZE, KM_SLEEP);
124#endif
125#ifdef XFS_BMAP_TRACE
126 xfs_bmap_trace_buf = ktrace_alloc(XFS_BMAP_TRACE_SIZE, KM_SLEEP);
127#endif
128#ifdef XFS_BMBT_TRACE
129 xfs_bmbt_trace_buf = ktrace_alloc(XFS_BMBT_TRACE_SIZE, KM_SLEEP);
130#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131#ifdef XFS_ATTR_TRACE
132 xfs_attr_trace_buf = ktrace_alloc(XFS_ATTR_TRACE_SIZE, KM_SLEEP);
133#endif
134#ifdef XFS_DIR2_TRACE
135 xfs_dir2_trace_buf = ktrace_alloc(XFS_DIR2_GTRACE_SIZE, KM_SLEEP);
136#endif
137
138 xfs_dir_startup();
139
140#if (defined(DEBUG) || defined(INDUCE_IO_ERROR))
141 xfs_error_test_init();
142#endif /* DEBUG || INDUCE_IO_ERROR */
143
144 xfs_init_procfs();
145 xfs_sysctl_register();
146 return 0;
147}
148
Lachlan McIlroyde2eeea2008-02-06 13:37:56 +1100149void __exit
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150xfs_cleanup(void)
151{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 extern kmem_zone_t *xfs_inode_zone;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 extern kmem_zone_t *xfs_efd_zone;
154 extern kmem_zone_t *xfs_efi_zone;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
156 xfs_cleanup_procfs();
157 xfs_sysctl_unregister();
David Chinner2a82b8b2007-07-11 11:09:12 +1000158 xfs_filestream_uninit();
159 xfs_mru_cache_uninit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 xfs_acl_zone_destroy(xfs_acl_zone);
161
162#ifdef XFS_DIR2_TRACE
163 ktrace_free(xfs_dir2_trace_buf);
164#endif
165#ifdef XFS_ATTR_TRACE
166 ktrace_free(xfs_attr_trace_buf);
167#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168#ifdef XFS_BMBT_TRACE
169 ktrace_free(xfs_bmbt_trace_buf);
170#endif
171#ifdef XFS_BMAP_TRACE
172 ktrace_free(xfs_bmap_trace_buf);
173#endif
174#ifdef XFS_ALLOC_TRACE
175 ktrace_free(xfs_alloc_trace_buf);
176#endif
177
Nathan Scott3758dee2006-03-22 12:47:28 +1100178 kmem_zone_destroy(xfs_bmap_free_item_zone);
179 kmem_zone_destroy(xfs_btree_cur_zone);
180 kmem_zone_destroy(xfs_inode_zone);
181 kmem_zone_destroy(xfs_trans_zone);
182 kmem_zone_destroy(xfs_da_state_zone);
183 kmem_zone_destroy(xfs_dabuf_zone);
184 kmem_zone_destroy(xfs_buf_item_zone);
185 kmem_zone_destroy(xfs_efd_zone);
186 kmem_zone_destroy(xfs_efi_zone);
187 kmem_zone_destroy(xfs_ifork_zone);
188 kmem_zone_destroy(xfs_ili_zone);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189}
190
191/*
192 * xfs_start_flags
193 *
194 * This function fills in xfs_mount_t fields based on mount args.
195 * Note: the superblock has _not_ yet been read in.
196 */
197STATIC int
198xfs_start_flags(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 struct xfs_mount_args *ap,
200 struct xfs_mount *mp)
201{
202 /* Values are in BBs */
203 if ((ap->flags & XFSMNT_NOALIGN) != XFSMNT_NOALIGN) {
204 /*
205 * At this point the superblock has not been read
206 * in, therefore we do not know the block size.
207 * Before the mount call ends we will convert
208 * these to FSBs.
209 */
210 mp->m_dalign = ap->sunit;
211 mp->m_swidth = ap->swidth;
212 }
213
214 if (ap->logbufs != -1 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 ap->logbufs != 0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 (ap->logbufs < XLOG_MIN_ICLOGS ||
217 ap->logbufs > XLOG_MAX_ICLOGS)) {
218 cmn_err(CE_WARN,
219 "XFS: invalid logbufs value: %d [not %d-%d]",
220 ap->logbufs, XLOG_MIN_ICLOGS, XLOG_MAX_ICLOGS);
221 return XFS_ERROR(EINVAL);
222 }
223 mp->m_logbufs = ap->logbufs;
224 if (ap->logbufsize != -1 &&
Nathan Scottcfcbbbd2005-11-02 15:12:04 +1100225 ap->logbufsize != 0 &&
Eric Sandeendcb3b832007-08-16 16:25:33 +1000226 (ap->logbufsize < XLOG_MIN_RECORD_BSIZE ||
227 ap->logbufsize > XLOG_MAX_RECORD_BSIZE ||
228 !is_power_of_2(ap->logbufsize))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 cmn_err(CE_WARN,
230 "XFS: invalid logbufsize: %d [not 16k,32k,64k,128k or 256k]",
231 ap->logbufsize);
232 return XFS_ERROR(EINVAL);
233 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 mp->m_logbsize = ap->logbufsize;
235 mp->m_fsname_len = strlen(ap->fsname) + 1;
236 mp->m_fsname = kmem_alloc(mp->m_fsname_len, KM_SLEEP);
237 strcpy(mp->m_fsname, ap->fsname);
Nathan Scottfc1f8c12005-11-02 11:44:33 +1100238 if (ap->rtname[0]) {
239 mp->m_rtname = kmem_alloc(strlen(ap->rtname) + 1, KM_SLEEP);
240 strcpy(mp->m_rtname, ap->rtname);
241 }
242 if (ap->logname[0]) {
243 mp->m_logname = kmem_alloc(strlen(ap->logname) + 1, KM_SLEEP);
244 strcpy(mp->m_logname, ap->logname);
245 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
247 if (ap->flags & XFSMNT_WSYNC)
248 mp->m_flags |= XFS_MOUNT_WSYNC;
249#if XFS_BIG_INUMS
250 if (ap->flags & XFSMNT_INO64) {
251 mp->m_flags |= XFS_MOUNT_INO64;
252 mp->m_inoadd = XFS_INO64_OFFSET;
253 }
254#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 if (ap->flags & XFSMNT_RETERR)
256 mp->m_flags |= XFS_MOUNT_RETERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 if (ap->flags & XFSMNT_NOALIGN)
258 mp->m_flags |= XFS_MOUNT_NOALIGN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 if (ap->flags & XFSMNT_SWALLOC)
260 mp->m_flags |= XFS_MOUNT_SWALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 if (ap->flags & XFSMNT_OSYNCISOSYNC)
262 mp->m_flags |= XFS_MOUNT_OSYNCISOSYNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 if (ap->flags & XFSMNT_32BITINODES)
Nathan Scottc11e2c32005-11-02 15:11:45 +1100264 mp->m_flags |= XFS_MOUNT_32BITINODES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
266 if (ap->flags & XFSMNT_IOSIZE) {
267 if (ap->iosizelog > XFS_MAX_IO_LOG ||
268 ap->iosizelog < XFS_MIN_IO_LOG) {
269 cmn_err(CE_WARN,
270 "XFS: invalid log iosize: %d [not %d-%d]",
271 ap->iosizelog, XFS_MIN_IO_LOG,
272 XFS_MAX_IO_LOG);
273 return XFS_ERROR(EINVAL);
274 }
275
276 mp->m_flags |= XFS_MOUNT_DFLT_IOSIZE;
277 mp->m_readio_log = mp->m_writeio_log = ap->iosizelog;
278 }
279
Josef Jeff Sipek1bd960e2008-02-29 13:58:40 +1100280 if (ap->flags & XFSMNT_IKEEP)
281 mp->m_flags |= XFS_MOUNT_IKEEP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 if (ap->flags & XFSMNT_DIRSYNC)
283 mp->m_flags |= XFS_MOUNT_DIRSYNC;
Nathan Scott13059ff2006-01-11 15:32:01 +1100284 if (ap->flags & XFSMNT_ATTR2)
285 mp->m_flags |= XFS_MOUNT_ATTR2;
David Chinnere8c8b3a2005-11-02 10:33:05 +1100286
Nathan Scotte718eeb2005-11-02 15:09:22 +1100287 if (ap->flags2 & XFSMNT2_COMPAT_IOSIZE)
288 mp->m_flags |= XFS_MOUNT_COMPAT_IOSIZE;
289
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 /*
291 * no recovery flag requires a read-only mount
292 */
293 if (ap->flags & XFSMNT_NORECOVERY) {
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000294 if (!(mp->m_flags & XFS_MOUNT_RDONLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 cmn_err(CE_WARN,
296 "XFS: tried to mount a FS read-write without recovery!");
297 return XFS_ERROR(EINVAL);
298 }
299 mp->m_flags |= XFS_MOUNT_NORECOVERY;
300 }
301
302 if (ap->flags & XFSMNT_NOUUID)
303 mp->m_flags |= XFS_MOUNT_NOUUID;
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100304 if (ap->flags & XFSMNT_BARRIER)
305 mp->m_flags |= XFS_MOUNT_BARRIER;
Christoph Hellwig4ef19dd2006-01-11 15:27:18 +1100306 else
307 mp->m_flags &= ~XFS_MOUNT_BARRIER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
David Chinner2a82b8b2007-07-11 11:09:12 +1000309 if (ap->flags2 & XFSMNT2_FILESTREAMS)
310 mp->m_flags |= XFS_MOUNT_FILESTREAMS;
311
Christoph Hellwig293688e2007-08-29 11:59:36 +1000312 if (ap->flags & XFSMNT_DMAPI)
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000313 mp->m_flags |= XFS_MOUNT_DMAPI;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 return 0;
315}
316
317/*
318 * This function fills in xfs_mount_t fields based on mount args.
319 * Note: the superblock _has_ now been read in.
320 */
321STATIC int
322xfs_finish_flags(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 struct xfs_mount_args *ap,
324 struct xfs_mount *mp)
325{
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000326 int ronly = (mp->m_flags & XFS_MOUNT_RDONLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
328 /* Fail a mount where the logbuf is smaller then the log stripe */
Eric Sandeen62118702008-03-06 13:44:28 +1100329 if (xfs_sb_version_haslogv2(&mp->m_sb)) {
Nathan Scottc2cd2552006-01-11 15:35:32 +1100330 if ((ap->logbufsize <= 0) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 (mp->m_sb.sb_logsunit > XLOG_BIG_RECORD_BSIZE)) {
332 mp->m_logbsize = mp->m_sb.sb_logsunit;
Nathan Scottc2cd2552006-01-11 15:35:32 +1100333 } else if (ap->logbufsize > 0 &&
334 ap->logbufsize < mp->m_sb.sb_logsunit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 cmn_err(CE_WARN,
336 "XFS: logbuf size must be greater than or equal to log stripe size");
337 return XFS_ERROR(EINVAL);
338 }
339 } else {
340 /* Fail a mount if the logbuf is larger than 32K */
341 if (ap->logbufsize > XLOG_BIG_RECORD_BSIZE) {
342 cmn_err(CE_WARN,
343 "XFS: logbuf size for version 1 logs must be 16K or 32K");
344 return XFS_ERROR(EINVAL);
345 }
346 }
347
Eric Sandeen62118702008-03-06 13:44:28 +1100348 if (xfs_sb_version_hasattr2(&mp->m_sb))
Nathan Scott13059ff2006-01-11 15:32:01 +1100349 mp->m_flags |= XFS_MOUNT_ATTR2;
Nathan Scott13059ff2006-01-11 15:32:01 +1100350
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 /*
352 * prohibit r/w mounts of read-only filesystems
353 */
354 if ((mp->m_sb.sb_flags & XFS_SBF_READONLY) && !ronly) {
355 cmn_err(CE_WARN,
356 "XFS: cannot mount a read-only filesystem as read-write");
357 return XFS_ERROR(EROFS);
358 }
359
360 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 * check for shared mount.
362 */
363 if (ap->flags & XFSMNT_SHARED) {
Eric Sandeen62118702008-03-06 13:44:28 +1100364 if (!xfs_sb_version_hasshared(&mp->m_sb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 return XFS_ERROR(EINVAL);
366
367 /*
368 * For IRIX 6.5, shared mounts must have the shared
369 * version bit set, have the persistent readonly
370 * field set, must be version 0 and can only be mounted
371 * read-only.
372 */
373 if (!ronly || !(mp->m_sb.sb_flags & XFS_SBF_READONLY) ||
374 (mp->m_sb.sb_shared_vn != 0))
375 return XFS_ERROR(EINVAL);
376
377 mp->m_flags |= XFS_MOUNT_SHARED;
378
379 /*
380 * Shared XFS V0 can't deal with DMI. Return EINVAL.
381 */
382 if (mp->m_sb.sb_shared_vn == 0 && (ap->flags & XFSMNT_DMAPI))
383 return XFS_ERROR(EINVAL);
384 }
385
Christoph Hellwigb09cc772007-08-30 17:19:57 +1000386 if (ap->flags & XFSMNT_UQUOTA) {
387 mp->m_qflags |= (XFS_UQUOTA_ACCT | XFS_UQUOTA_ACTIVE);
388 if (ap->flags & XFSMNT_UQUOTAENF)
389 mp->m_qflags |= XFS_UQUOTA_ENFD;
390 }
391
392 if (ap->flags & XFSMNT_GQUOTA) {
393 mp->m_qflags |= (XFS_GQUOTA_ACCT | XFS_GQUOTA_ACTIVE);
394 if (ap->flags & XFSMNT_GQUOTAENF)
395 mp->m_qflags |= XFS_OQUOTA_ENFD;
396 } else if (ap->flags & XFSMNT_PQUOTA) {
397 mp->m_qflags |= (XFS_PQUOTA_ACCT | XFS_PQUOTA_ACTIVE);
398 if (ap->flags & XFSMNT_PQUOTAENF)
399 mp->m_qflags |= XFS_OQUOTA_ENFD;
400 }
401
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 return 0;
403}
404
405/*
406 * xfs_mount
407 *
408 * The file system configurations are:
409 * (1) device (partition) with data and internal log
410 * (2) logical volume with data and log subvolumes.
411 * (3) logical volume with data, log, and realtime subvolumes.
412 *
413 * We only have to handle opening the log and realtime volumes here if
414 * they are present. The data subvolume has already been opened by
415 * get_sb_bdev() and is stored in vfsp->vfs_super->s_bdev.
416 */
Christoph Hellwig48c872a9f2007-08-30 17:20:31 +1000417int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418xfs_mount(
Christoph Hellwig48c872a9f2007-08-30 17:20:31 +1000419 struct xfs_mount *mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 struct xfs_mount_args *args,
421 cred_t *credp)
422{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 struct block_device *ddev, *logdev, *rtdev;
424 int flags = 0, error;
425
Christoph Hellwigb267ce92007-08-30 17:21:30 +1000426 ddev = mp->m_super->s_bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 logdev = rtdev = NULL;
428
Christoph Hellwig293688e2007-08-29 11:59:36 +1000429 error = xfs_dmops_get(mp, args);
430 if (error)
431 return error;
Christoph Hellwigb09cc772007-08-30 17:19:57 +1000432 error = xfs_qmops_get(mp, args);
433 if (error)
434 return error;
Christoph Hellwig293688e2007-08-29 11:59:36 +1000435
Nathan Scott764d1f82006-03-31 13:04:17 +1000436 if (args->flags & XFSMNT_QUIET)
437 flags |= XFS_MFSI_QUIET;
438
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 /*
440 * Open real time and log devices - order is important.
441 */
442 if (args->logname[0]) {
443 error = xfs_blkdev_get(mp, args->logname, &logdev);
444 if (error)
445 return error;
446 }
447 if (args->rtname[0]) {
448 error = xfs_blkdev_get(mp, args->rtname, &rtdev);
449 if (error) {
450 xfs_blkdev_put(logdev);
451 return error;
452 }
453
454 if (rtdev == ddev || rtdev == logdev) {
455 cmn_err(CE_WARN,
456 "XFS: Cannot mount filesystem with identical rtdev and ddev/logdev.");
457 xfs_blkdev_put(logdev);
458 xfs_blkdev_put(rtdev);
459 return EINVAL;
460 }
461 }
462
463 /*
464 * Setup xfs_mount buffer target pointers
465 */
466 error = ENOMEM;
467 mp->m_ddev_targp = xfs_alloc_buftarg(ddev, 0);
468 if (!mp->m_ddev_targp) {
469 xfs_blkdev_put(logdev);
470 xfs_blkdev_put(rtdev);
471 return error;
472 }
473 if (rtdev) {
474 mp->m_rtdev_targp = xfs_alloc_buftarg(rtdev, 1);
Jesper Juhl49ee6c92007-08-16 16:25:42 +1000475 if (!mp->m_rtdev_targp) {
476 xfs_blkdev_put(logdev);
477 xfs_blkdev_put(rtdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 goto error0;
Jesper Juhl49ee6c92007-08-16 16:25:42 +1000479 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 }
481 mp->m_logdev_targp = (logdev && logdev != ddev) ?
482 xfs_alloc_buftarg(logdev, 1) : mp->m_ddev_targp;
Jesper Juhl49ee6c92007-08-16 16:25:42 +1000483 if (!mp->m_logdev_targp) {
484 xfs_blkdev_put(logdev);
485 xfs_blkdev_put(rtdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 goto error0;
Jesper Juhl49ee6c92007-08-16 16:25:42 +1000487 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
489 /*
490 * Setup flags based on mount(2) options and then the superblock
491 */
Christoph Hellwigb267ce92007-08-30 17:21:30 +1000492 error = xfs_start_flags(args, mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 if (error)
494 goto error1;
Nathan Scott764d1f82006-03-31 13:04:17 +1000495 error = xfs_readsb(mp, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 if (error)
497 goto error1;
Christoph Hellwigb267ce92007-08-30 17:21:30 +1000498 error = xfs_finish_flags(args, mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 if (error)
500 goto error2;
501
502 /*
503 * Setup xfs_mount buffer target pointers based on superblock
504 */
505 error = xfs_setsize_buftarg(mp->m_ddev_targp, mp->m_sb.sb_blocksize,
506 mp->m_sb.sb_sectsize);
507 if (!error && logdev && logdev != ddev) {
508 unsigned int log_sector_size = BBSIZE;
509
Eric Sandeen62118702008-03-06 13:44:28 +1100510 if (xfs_sb_version_hassector(&mp->m_sb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 log_sector_size = mp->m_sb.sb_logsectsize;
512 error = xfs_setsize_buftarg(mp->m_logdev_targp,
513 mp->m_sb.sb_blocksize,
514 log_sector_size);
515 }
516 if (!error && rtdev)
517 error = xfs_setsize_buftarg(mp->m_rtdev_targp,
518 mp->m_sb.sb_blocksize,
519 mp->m_sb.sb_sectsize);
520 if (error)
521 goto error2;
522
Nathan Scottb2ea4012006-07-28 17:05:13 +1000523 if (mp->m_flags & XFS_MOUNT_BARRIER)
Christoph Hellwigc7d437d2006-01-11 15:28:56 +1100524 xfs_mountfs_check_barriers(mp);
525
David Chinner2a82b8b2007-07-11 11:09:12 +1000526 if ((error = xfs_filestream_mount(mp)))
527 goto error2;
528
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000529 error = xfs_mountfs(mp, flags);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100530 if (error)
531 goto error2;
532
Christoph Hellwig293688e2007-08-29 11:59:36 +1000533 XFS_SEND_MOUNT(mp, DM_RIGHT_NULL, args->mtpt, args->fsname);
534
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100535 return 0;
536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537error2:
538 if (mp->m_sb_bp)
539 xfs_freesb(mp);
540error1:
541 xfs_binval(mp->m_ddev_targp);
542 if (logdev && logdev != ddev)
543 xfs_binval(mp->m_logdev_targp);
544 if (rtdev)
545 xfs_binval(mp->m_rtdev_targp);
546error0:
547 xfs_unmountfs_close(mp, credp);
Christoph Hellwigb09cc772007-08-30 17:19:57 +1000548 xfs_qmops_put(mp);
Christoph Hellwig293688e2007-08-29 11:59:36 +1000549 xfs_dmops_put(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 return error;
551}
552
Christoph Hellwig48c872a9f2007-08-30 17:20:31 +1000553int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554xfs_unmount(
Christoph Hellwig48c872a9f2007-08-30 17:20:31 +1000555 xfs_mount_t *mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 int flags,
557 cred_t *credp)
558{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 xfs_inode_t *rip;
Nathan Scott67fcaa72006-06-09 17:00:52 +1000560 bhv_vnode_t *rvp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 int unmount_event_wanted = 0;
562 int unmount_event_flags = 0;
563 int xfs_unmountfs_needed = 0;
564 int error;
565
566 rip = mp->m_rootip;
567 rvp = XFS_ITOV(rip);
568
Eric Sandeen948c6d4f2007-08-23 16:19:57 +1000569#ifdef HAVE_DMAPI
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000570 if (mp->m_flags & XFS_MOUNT_DMAPI) {
Christoph Hellwigb267ce92007-08-30 17:21:30 +1000571 error = XFS_SEND_PREUNMOUNT(mp,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +1100572 rip, DM_RIGHT_NULL, rip, DM_RIGHT_NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 NULL, NULL, 0, 0,
574 (mp->m_dmevmask & (1<<DM_EVENT_PREUNMOUNT))?
575 0:DM_FLAGS_UNWANTED);
576 if (error)
577 return XFS_ERROR(error);
578 unmount_event_wanted = 1;
579 unmount_event_flags = (mp->m_dmevmask & (1<<DM_EVENT_UNMOUNT))?
580 0 : DM_FLAGS_UNWANTED;
581 }
Eric Sandeen948c6d4f2007-08-23 16:19:57 +1000582#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
David Chinner2a82b8b2007-07-11 11:09:12 +1000584 /*
585 * Blow away any referenced inode in the filestreams cache.
586 * This can and will cause log traffic as inodes go inactive
587 * here.
588 */
589 xfs_filestream_unmount(mp);
590
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 XFS_bflush(mp->m_ddev_targp);
592 error = xfs_unmount_flush(mp, 0);
593 if (error)
594 goto out;
595
596 ASSERT(vn_count(rvp) == 1);
597
598 /*
599 * Drop the reference count
600 */
Christoph Hellwig43355092008-03-27 18:01:08 +1100601 IRELE(rip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
603 /*
604 * If we're forcing a shutdown, typically because of a media error,
605 * we want to make sure we invalidate dirty pages that belong to
606 * referenced vnodes as well.
607 */
608 if (XFS_FORCED_SHUTDOWN(mp)) {
Christoph Hellwig48c872a9f2007-08-30 17:20:31 +1000609 error = xfs_sync(mp, SYNC_WAIT | SYNC_CLOSE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 ASSERT(error != EFSCORRUPTED);
611 }
612 xfs_unmountfs_needed = 1;
613
614out:
615 /* Send DMAPI event, if required.
616 * Then do xfs_unmountfs() if needed.
617 * Then return error (or zero).
618 */
619 if (unmount_event_wanted) {
620 /* Note: mp structure must still exist for
621 * XFS_SEND_UNMOUNT() call.
622 */
Christoph Hellwigbc4ac742008-03-06 13:45:58 +1100623 XFS_SEND_UNMOUNT(mp, error == 0 ? rip : NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 DM_RIGHT_NULL, 0, error, unmount_event_flags);
625 }
626 if (xfs_unmountfs_needed) {
627 /*
628 * Call common unmount function to flush to disk
629 * and free the super block buffer & mount structures.
630 */
631 xfs_unmountfs(mp, credp);
Christoph Hellwigb09cc772007-08-30 17:19:57 +1000632 xfs_qmops_put(mp);
Christoph Hellwig293688e2007-08-29 11:59:36 +1000633 xfs_dmops_put(mp);
Christoph Hellwigf541d272007-08-29 11:53:22 +1000634 kmem_free(mp, sizeof(xfs_mount_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 }
636
637 return XFS_ERROR(error);
638}
639
David Chinner3c85c362008-04-10 12:19:40 +1000640STATIC void
Christoph Hellwigf898d6c2005-06-21 15:40:48 +1000641xfs_quiesce_fs(
642 xfs_mount_t *mp)
643{
644 int count = 0, pincount;
Nathan Scott3758dee2006-03-22 12:47:28 +1100645
Christoph Hellwigf898d6c2005-06-21 15:40:48 +1000646 xfs_flush_buftarg(mp->m_ddev_targp, 0);
647 xfs_finish_reclaim_all(mp, 0);
648
649 /* This loop must run at least twice.
650 * The first instance of the loop will flush
651 * most meta data but that will generate more
652 * meta data (typically directory updates).
653 * Which then must be flushed and logged before
654 * we can write the unmount record.
Nathan Scott3758dee2006-03-22 12:47:28 +1100655 */
Christoph Hellwigf898d6c2005-06-21 15:40:48 +1000656 do {
David Chinner516b2e72007-06-18 16:50:48 +1000657 xfs_syncsub(mp, SYNC_INODE_QUIESCE, NULL);
Christoph Hellwigf898d6c2005-06-21 15:40:48 +1000658 pincount = xfs_flush_buftarg(mp->m_ddev_targp, 1);
659 if (!pincount) {
660 delay(50);
661 count++;
662 }
663 } while (count < 2);
Christoph Hellwigf898d6c2005-06-21 15:40:48 +1000664}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
David Chinner516b2e72007-06-18 16:50:48 +1000666/*
667 * Second stage of a quiesce. The data is already synced, now we have to take
668 * care of the metadata. New transactions are already blocked, so we need to
669 * wait for any remaining transactions to drain out before proceding.
670 */
Christoph Hellwig9909c4a2007-10-11 18:11:14 +1000671void
David Chinner516b2e72007-06-18 16:50:48 +1000672xfs_attr_quiesce(
673 xfs_mount_t *mp)
674{
675 /* wait for all modifications to complete */
676 while (atomic_read(&mp->m_active_trans) > 0)
677 delay(100);
678
679 /* flush inodes and push all remaining buffers out to disk */
680 xfs_quiesce_fs(mp);
681
682 ASSERT_ALWAYS(atomic_read(&mp->m_active_trans) == 0);
683
684 /* Push the superblock and write an unmount record */
685 xfs_log_sbcount(mp, 1);
686 xfs_log_unmount_write(mp);
687 xfs_unmountfs_writesb(mp);
688}
689
Christoph Hellwig48c872a9f2007-08-30 17:20:31 +1000690int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691xfs_mntupdate(
Christoph Hellwig48c872a9f2007-08-30 17:20:31 +1000692 struct xfs_mount *mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 int *flags,
694 struct xfs_mount_args *args)
695{
Nathan Scottd08d3892006-05-08 19:51:28 +1000696 if (!(*flags & MS_RDONLY)) { /* rw/ro -> rw */
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000697 if (mp->m_flags & XFS_MOUNT_RDONLY)
698 mp->m_flags &= ~XFS_MOUNT_RDONLY;
Nathan Scottd08d3892006-05-08 19:51:28 +1000699 if (args->flags & XFSMNT_BARRIER) {
700 mp->m_flags |= XFS_MOUNT_BARRIER;
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100701 xfs_mountfs_check_barriers(mp);
Nathan Scottd08d3892006-05-08 19:51:28 +1000702 } else {
703 mp->m_flags &= ~XFS_MOUNT_BARRIER;
704 }
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000705 } else if (!(mp->m_flags & XFS_MOUNT_RDONLY)) { /* rw -> ro */
David Chinner2a82b8b2007-07-11 11:09:12 +1000706 xfs_filestream_flush(mp);
Christoph Hellwig745f6912007-08-30 17:20:39 +1000707 xfs_sync(mp, SYNC_DATA_QUIESCE);
David Chinner516b2e72007-06-18 16:50:48 +1000708 xfs_attr_quiesce(mp);
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000709 mp->m_flags |= XFS_MOUNT_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 return 0;
712}
713
714/*
715 * xfs_unmount_flush implements a set of flush operation on special
716 * inodes, which are needed as a separate set of operations so that
717 * they can be called as part of relocation process.
718 */
719int
720xfs_unmount_flush(
721 xfs_mount_t *mp, /* Mount structure we are getting
722 rid of. */
723 int relocation) /* Called from vfs relocation. */
724{
725 xfs_inode_t *rip = mp->m_rootip;
726 xfs_inode_t *rbmip;
727 xfs_inode_t *rsumip = NULL;
Nathan Scott67fcaa72006-06-09 17:00:52 +1000728 bhv_vnode_t *rvp = XFS_ITOV(rip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 int error;
730
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +1000731 xfs_ilock(rip, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 xfs_iflock(rip);
733
734 /*
735 * Flush out the real time inodes.
736 */
737 if ((rbmip = mp->m_rbmip) != NULL) {
738 xfs_ilock(rbmip, XFS_ILOCK_EXCL);
739 xfs_iflock(rbmip);
740 error = xfs_iflush(rbmip, XFS_IFLUSH_SYNC);
741 xfs_iunlock(rbmip, XFS_ILOCK_EXCL);
742
743 if (error == EFSCORRUPTED)
744 goto fscorrupt_out;
745
746 ASSERT(vn_count(XFS_ITOV(rbmip)) == 1);
747
748 rsumip = mp->m_rsumip;
749 xfs_ilock(rsumip, XFS_ILOCK_EXCL);
750 xfs_iflock(rsumip);
751 error = xfs_iflush(rsumip, XFS_IFLUSH_SYNC);
752 xfs_iunlock(rsumip, XFS_ILOCK_EXCL);
753
754 if (error == EFSCORRUPTED)
755 goto fscorrupt_out;
756
757 ASSERT(vn_count(XFS_ITOV(rsumip)) == 1);
758 }
759
760 /*
761 * Synchronously flush root inode to disk
762 */
763 error = xfs_iflush(rip, XFS_IFLUSH_SYNC);
764 if (error == EFSCORRUPTED)
765 goto fscorrupt_out2;
766
767 if (vn_count(rvp) != 1 && !relocation) {
768 xfs_iunlock(rip, XFS_ILOCK_EXCL);
769 return XFS_ERROR(EBUSY);
770 }
771
772 /*
773 * Release dquot that rootinode, rbmino and rsumino might be holding,
774 * flush and purge the quota inodes.
775 */
776 error = XFS_QM_UNMOUNT(mp);
777 if (error == EFSCORRUPTED)
778 goto fscorrupt_out2;
779
780 if (rbmip) {
Christoph Hellwig43355092008-03-27 18:01:08 +1100781 IRELE(rbmip);
782 IRELE(rsumip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 }
784
785 xfs_iunlock(rip, XFS_ILOCK_EXCL);
786 return 0;
787
788fscorrupt_out:
789 xfs_ifunlock(rip);
790
791fscorrupt_out2:
792 xfs_iunlock(rip, XFS_ILOCK_EXCL);
793
794 return XFS_ERROR(EFSCORRUPTED);
795}
796
797/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 * xfs_sync flushes any pending I/O to file system vfsp.
799 *
800 * This routine is called by vfs_sync() to make sure that things make it
801 * out to disk eventually, on sync() system calls to flush out everything,
802 * and when the file system is unmounted. For the vfs_sync() case, all
803 * we really need to do is sync out the log to make all of our meta-data
804 * updates permanent (except for timestamps). For calls from pflushd(),
805 * dirty pages are kept moving by calling pdflush() on the inodes
806 * containing them. We also flush the inodes that we can lock without
807 * sleeping and the superblock if we can lock it without sleeping from
808 * vfs_sync() so that items at the tail of the log are always moving out.
809 *
810 * Flags:
811 * SYNC_BDFLUSH - We're being called from vfs_sync() so we don't want
812 * to sleep if we can help it. All we really need
813 * to do is ensure that the log is synced at least
814 * periodically. We also push the inodes and
815 * superblock if we can lock them without sleeping
816 * and they are not pinned.
817 * SYNC_ATTR - We need to flush the inodes. If SYNC_BDFLUSH is not
818 * set, then we really want to lock each inode and flush
819 * it.
820 * SYNC_WAIT - All the flushes that take place in this call should
821 * be synchronous.
822 * SYNC_DELWRI - This tells us to push dirty pages associated with
823 * inodes. SYNC_WAIT and SYNC_BDFLUSH are used to
824 * determine if they should be flushed sync, async, or
825 * delwri.
826 * SYNC_CLOSE - This flag is passed when the system is being
Nathan Scottc41564b2006-03-29 08:55:14 +1000827 * unmounted. We should sync and invalidate everything.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 * SYNC_FSDATA - This indicates that the caller would like to make
829 * sure the superblock is safe on disk. We can ensure
Nathan Scottc41564b2006-03-29 08:55:14 +1000830 * this by simply making sure the log gets flushed
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 * if SYNC_BDFLUSH is set, and by actually writing it
832 * out otherwise.
David Chinner3c0dc772007-02-10 18:37:22 +1100833 * SYNC_IOWAIT - The caller wants us to wait for all data I/O to complete
834 * before we return (including direct I/O). Forms the drain
835 * side of the write barrier needed to safely quiesce the
836 * filesystem.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 *
838 */
Christoph Hellwig48c872a9f2007-08-30 17:20:31 +1000839int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840xfs_sync(
Christoph Hellwig48c872a9f2007-08-30 17:20:31 +1000841 xfs_mount_t *mp,
842 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843{
Christoph Hellwigb09cc772007-08-30 17:19:57 +1000844 int error;
845
846 /*
847 * Get the Quota Manager to flush the dquots.
848 *
849 * If XFS quota support is not enabled or this filesystem
850 * instance does not use quotas XFS_QM_DQSYNC will always
851 * return zero.
852 */
853 error = XFS_QM_DQSYNC(mp, flags);
854 if (error) {
855 /*
856 * If we got an IO error, we will be shutting down.
857 * So, there's nothing more for us to do here.
858 */
859 ASSERT(error != EIO || XFS_FORCED_SHUTDOWN(mp));
860 if (XFS_FORCED_SHUTDOWN(mp))
861 return XFS_ERROR(error);
862 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
David Chinner2a82b8b2007-07-11 11:09:12 +1000864 if (flags & SYNC_IOWAIT)
865 xfs_filestream_flush(mp);
866
David Chinner28239452007-02-10 18:36:40 +1100867 return xfs_syncsub(mp, flags, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868}
869
870/*
871 * xfs sync routine for internal use
872 *
Nathan Scottb83bd132006-06-09 16:48:30 +1000873 * This routine supports all of the flags defined for the generic vfs_sync
Lachlan McIlroy1f9b3b62007-02-10 18:35:33 +1100874 * interface as explained above under xfs_sync.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 *
876 */
Nathan Scottee348072005-11-02 10:32:38 +1100877int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878xfs_sync_inodes(
879 xfs_mount_t *mp,
880 int flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 int *bypassed)
882{
883 xfs_inode_t *ip = NULL;
Nathan Scott67fcaa72006-06-09 17:00:52 +1000884 bhv_vnode_t *vp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 int error;
886 int last_error;
887 uint64_t fflag;
888 uint lock_flags;
889 uint base_lock_flags;
890 boolean_t mount_locked;
891 boolean_t vnode_refed;
892 int preempt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 xfs_iptr_t *ipointer;
894#ifdef DEBUG
895 boolean_t ipointer_in = B_FALSE;
896
897#define IPOINTER_SET ipointer_in = B_TRUE
898#define IPOINTER_CLR ipointer_in = B_FALSE
899#else
900#define IPOINTER_SET
901#define IPOINTER_CLR
902#endif
903
904
905/* Insert a marker record into the inode list after inode ip. The list
906 * must be locked when this is called. After the call the list will no
907 * longer be locked.
908 */
909#define IPOINTER_INSERT(ip, mp) { \
910 ASSERT(ipointer_in == B_FALSE); \
911 ipointer->ip_mnext = ip->i_mnext; \
912 ipointer->ip_mprev = ip; \
913 ip->i_mnext = (xfs_inode_t *)ipointer; \
914 ipointer->ip_mnext->i_mprev = (xfs_inode_t *)ipointer; \
915 preempt = 0; \
916 XFS_MOUNT_IUNLOCK(mp); \
917 mount_locked = B_FALSE; \
918 IPOINTER_SET; \
919 }
920
921/* Remove the marker from the inode list. If the marker was the only item
922 * in the list then there are no remaining inodes and we should zero out
923 * the whole list. If we are the current head of the list then move the head
924 * past us.
925 */
926#define IPOINTER_REMOVE(ip, mp) { \
927 ASSERT(ipointer_in == B_TRUE); \
928 if (ipointer->ip_mnext != (xfs_inode_t *)ipointer) { \
929 ip = ipointer->ip_mnext; \
930 ip->i_mprev = ipointer->ip_mprev; \
931 ipointer->ip_mprev->i_mnext = ip; \
932 if (mp->m_inodes == (xfs_inode_t *)ipointer) { \
933 mp->m_inodes = ip; \
934 } \
935 } else { \
936 ASSERT(mp->m_inodes == (xfs_inode_t *)ipointer); \
937 mp->m_inodes = NULL; \
938 ip = NULL; \
939 } \
940 IPOINTER_CLR; \
941 }
942
943#define XFS_PREEMPT_MASK 0x7f
944
Lachlan McIlroy44866d32007-09-14 15:21:08 +1000945 ASSERT(!(flags & SYNC_BDFLUSH));
946
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 if (bypassed)
948 *bypassed = 0;
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000949 if (mp->m_flags & XFS_MOUNT_RDONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 return 0;
951 error = 0;
952 last_error = 0;
953 preempt = 0;
954
955 /* Allocate a reference marker */
956 ipointer = (xfs_iptr_t *)kmem_zalloc(sizeof(xfs_iptr_t), KM_SLEEP);
957
958 fflag = XFS_B_ASYNC; /* default is don't wait */
Lachlan McIlroy44866d32007-09-14 15:21:08 +1000959 if (flags & SYNC_DELWRI)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 fflag = XFS_B_DELWRI;
961 if (flags & SYNC_WAIT)
962 fflag = 0; /* synchronous overrides all */
963
964 base_lock_flags = XFS_ILOCK_SHARED;
965 if (flags & (SYNC_DELWRI | SYNC_CLOSE)) {
966 /*
967 * We need the I/O lock if we're going to call any of
968 * the flush/inval routines.
969 */
970 base_lock_flags |= XFS_IOLOCK_SHARED;
971 }
972
973 XFS_MOUNT_ILOCK(mp);
974
975 ip = mp->m_inodes;
976
977 mount_locked = B_TRUE;
978 vnode_refed = B_FALSE;
979
980 IPOINTER_CLR;
981
982 do {
983 ASSERT(ipointer_in == B_FALSE);
984 ASSERT(vnode_refed == B_FALSE);
985
986 lock_flags = base_lock_flags;
987
988 /*
989 * There were no inodes in the list, just break out
990 * of the loop.
991 */
992 if (ip == NULL) {
993 break;
994 }
995
996 /*
997 * We found another sync thread marker - skip it
998 */
999 if (ip->i_mount == NULL) {
1000 ip = ip->i_mnext;
1001 continue;
1002 }
1003
1004 vp = XFS_ITOV_NULL(ip);
1005
1006 /*
1007 * If the vnode is gone then this is being torn down,
1008 * call reclaim if it is flushed, else let regular flush
1009 * code deal with it later in the loop.
1010 */
1011
1012 if (vp == NULL) {
1013 /* Skip ones already in reclaim */
1014 if (ip->i_flags & XFS_IRECLAIM) {
1015 ip = ip->i_mnext;
1016 continue;
1017 }
1018 if (xfs_ilock_nowait(ip, XFS_ILOCK_EXCL) == 0) {
1019 ip = ip->i_mnext;
1020 } else if ((xfs_ipincount(ip) == 0) &&
1021 xfs_iflock_nowait(ip)) {
1022 IPOINTER_INSERT(ip, mp);
1023
1024 xfs_finish_reclaim(ip, 1,
1025 XFS_IFLUSH_DELWRI_ELSE_ASYNC);
1026
1027 XFS_MOUNT_ILOCK(mp);
1028 mount_locked = B_TRUE;
1029 IPOINTER_REMOVE(ip, mp);
1030 } else {
1031 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1032 ip = ip->i_mnext;
1033 }
1034 continue;
1035 }
1036
1037 if (VN_BAD(vp)) {
1038 ip = ip->i_mnext;
1039 continue;
1040 }
1041
1042 if (XFS_FORCED_SHUTDOWN(mp) && !(flags & SYNC_CLOSE)) {
1043 XFS_MOUNT_IUNLOCK(mp);
1044 kmem_free(ipointer, sizeof(xfs_iptr_t));
1045 return 0;
1046 }
1047
1048 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 * Try to lock without sleeping. We're out of order with
1050 * the inode list lock here, so if we fail we need to drop
1051 * the mount lock and try again. If we're called from
1052 * bdflush() here, then don't bother.
1053 *
1054 * The inode lock here actually coordinates with the
1055 * almost spurious inode lock in xfs_ireclaim() to prevent
1056 * the vnode we handle here without a reference from
1057 * being freed while we reference it. If we lock the inode
1058 * while it's on the mount list here, then the spurious inode
1059 * lock in xfs_ireclaim() after the inode is pulled from
1060 * the mount list will sleep until we release it here.
1061 * This keeps the vnode from being freed while we reference
Christoph Hellwigcdb62682005-09-02 16:24:19 +10001062 * it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 */
1064 if (xfs_ilock_nowait(ip, lock_flags) == 0) {
Lachlan McIlroy44866d32007-09-14 15:21:08 +10001065 if (vp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 ip = ip->i_mnext;
1067 continue;
1068 }
1069
Christoph Hellwigcdb62682005-09-02 16:24:19 +10001070 vp = vn_grab(vp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 if (vp == NULL) {
Christoph Hellwigcdb62682005-09-02 16:24:19 +10001072 ip = ip->i_mnext;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 continue;
1074 }
1075
Christoph Hellwigcdb62682005-09-02 16:24:19 +10001076 IPOINTER_INSERT(ip, mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 xfs_ilock(ip, lock_flags);
1078
1079 ASSERT(vp == XFS_ITOV(ip));
1080 ASSERT(ip->i_mount == mp);
1081
1082 vnode_refed = B_TRUE;
1083 }
1084
1085 /* From here on in the loop we may have a marker record
1086 * in the inode list.
1087 */
1088
David Chinner40095b62007-05-14 18:24:09 +10001089 /*
1090 * If we have to flush data or wait for I/O completion
1091 * we need to drop the ilock that we currently hold.
1092 * If we need to drop the lock, insert a marker if we
1093 * have not already done so.
1094 */
1095 if ((flags & (SYNC_CLOSE|SYNC_IOWAIT)) ||
1096 ((flags & SYNC_DELWRI) && VN_DIRTY(vp))) {
1097 if (mount_locked) {
1098 IPOINTER_INSERT(ip, mp);
1099 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 xfs_iunlock(ip, XFS_ILOCK_SHARED);
1101
David Chinner40095b62007-05-14 18:24:09 +10001102 if (flags & SYNC_CLOSE) {
1103 /* Shutdown case. Flush and invalidate. */
1104 if (XFS_FORCED_SHUTDOWN(mp))
Christoph Hellwig739bfb22007-08-29 10:58:01 +10001105 xfs_tosspages(ip, 0, -1,
1106 FI_REMAPF);
David Chinner40095b62007-05-14 18:24:09 +10001107 else
Christoph Hellwig739bfb22007-08-29 10:58:01 +10001108 error = xfs_flushinval_pages(ip,
1109 0, -1, FI_REMAPF);
David Chinner40095b62007-05-14 18:24:09 +10001110 } else if ((flags & SYNC_DELWRI) && VN_DIRTY(vp)) {
Christoph Hellwig739bfb22007-08-29 10:58:01 +10001111 error = xfs_flush_pages(ip, 0,
Nathan Scott67fcaa72006-06-09 17:00:52 +10001112 -1, fflag, FI_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 }
1114
David Chinner40095b62007-05-14 18:24:09 +10001115 /*
1116 * When freezing, we need to wait ensure all I/O (including direct
1117 * I/O) is complete to ensure no further data modification can take
1118 * place after this point
1119 */
1120 if (flags & SYNC_IOWAIT)
Christoph Hellwigb677c212007-08-29 11:46:28 +10001121 vn_iowait(ip);
David Chinner40095b62007-05-14 18:24:09 +10001122
1123 xfs_ilock(ip, XFS_ILOCK_SHARED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 }
1125
Lachlan McIlroy44866d32007-09-14 15:21:08 +10001126 if ((flags & SYNC_ATTR) &&
1127 (ip->i_update_core ||
1128 (ip->i_itemp && ip->i_itemp->ili_format.ilf_fields))) {
1129 if (mount_locked)
1130 IPOINTER_INSERT(ip, mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
Lachlan McIlroy44866d32007-09-14 15:21:08 +10001132 if (flags & SYNC_WAIT) {
1133 xfs_iflock(ip);
1134 error = xfs_iflush(ip, XFS_IFLUSH_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
Lachlan McIlroy44866d32007-09-14 15:21:08 +10001136 /*
1137 * If we can't acquire the flush lock, then the inode
1138 * is already being flushed so don't bother waiting.
1139 *
1140 * If we can lock it then do a delwri flush so we can
1141 * combine multiple inode flushes in each disk write.
1142 */
1143 } else if (xfs_iflock_nowait(ip)) {
1144 error = xfs_iflush(ip, XFS_IFLUSH_DELWRI);
1145 } else if (bypassed) {
1146 (*bypassed)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 }
1148 }
1149
1150 if (lock_flags != 0) {
1151 xfs_iunlock(ip, lock_flags);
1152 }
1153
1154 if (vnode_refed) {
1155 /*
1156 * If we had to take a reference on the vnode
1157 * above, then wait until after we've unlocked
1158 * the inode to release the reference. This is
1159 * because we can be already holding the inode
Christoph Hellwig43355092008-03-27 18:01:08 +11001160 * lock when IRELE() calls xfs_inactive().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 *
1162 * Make sure to drop the mount lock before calling
Christoph Hellwig43355092008-03-27 18:01:08 +11001163 * IRELE() so that we don't trip over ourselves if
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 * we have to go for the mount lock again in the
1165 * inactive code.
1166 */
1167 if (mount_locked) {
1168 IPOINTER_INSERT(ip, mp);
1169 }
1170
Christoph Hellwig43355092008-03-27 18:01:08 +11001171 IRELE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
1173 vnode_refed = B_FALSE;
1174 }
1175
1176 if (error) {
1177 last_error = error;
1178 }
1179
1180 /*
1181 * bail out if the filesystem is corrupted.
1182 */
1183 if (error == EFSCORRUPTED) {
1184 if (!mount_locked) {
1185 XFS_MOUNT_ILOCK(mp);
1186 IPOINTER_REMOVE(ip, mp);
1187 }
1188 XFS_MOUNT_IUNLOCK(mp);
1189 ASSERT(ipointer_in == B_FALSE);
1190 kmem_free(ipointer, sizeof(xfs_iptr_t));
1191 return XFS_ERROR(error);
1192 }
1193
1194 /* Let other threads have a chance at the mount lock
1195 * if we have looped many times without dropping the
1196 * lock.
1197 */
1198 if ((++preempt & XFS_PREEMPT_MASK) == 0) {
1199 if (mount_locked) {
1200 IPOINTER_INSERT(ip, mp);
1201 }
1202 }
1203
1204 if (mount_locked == B_FALSE) {
1205 XFS_MOUNT_ILOCK(mp);
1206 mount_locked = B_TRUE;
1207 IPOINTER_REMOVE(ip, mp);
1208 continue;
1209 }
1210
1211 ASSERT(ipointer_in == B_FALSE);
1212 ip = ip->i_mnext;
1213
1214 } while (ip != mp->m_inodes);
1215
1216 XFS_MOUNT_IUNLOCK(mp);
1217
1218 ASSERT(ipointer_in == B_FALSE);
1219
1220 kmem_free(ipointer, sizeof(xfs_iptr_t));
1221 return XFS_ERROR(last_error);
1222}
1223
1224/*
1225 * xfs sync routine for internal use
1226 *
Nathan Scottb83bd132006-06-09 16:48:30 +10001227 * This routine supports all of the flags defined for the generic vfs_sync
Lachlan McIlroy1f9b3b62007-02-10 18:35:33 +11001228 * interface as explained above under xfs_sync.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 *
1230 */
1231int
1232xfs_syncsub(
1233 xfs_mount_t *mp,
1234 int flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 int *bypassed)
1236{
1237 int error = 0;
1238 int last_error = 0;
1239 uint log_flags = XFS_LOG_FORCE;
1240 xfs_buf_t *bp;
1241 xfs_buf_log_item_t *bip;
1242
1243 /*
1244 * Sync out the log. This ensures that the log is periodically
1245 * flushed even if there is not enough activity to fill it up.
1246 */
1247 if (flags & SYNC_WAIT)
1248 log_flags |= XFS_LOG_SYNC;
1249
1250 xfs_log_force(mp, (xfs_lsn_t)0, log_flags);
1251
1252 if (flags & (SYNC_ATTR|SYNC_DELWRI)) {
1253 if (flags & SYNC_BDFLUSH)
1254 xfs_finish_reclaim_all(mp, 1);
1255 else
Lachlan McIlroy1f9b3b62007-02-10 18:35:33 +11001256 error = xfs_sync_inodes(mp, flags, bypassed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 }
1258
1259 /*
1260 * Flushing out dirty data above probably generated more
1261 * log activity, so if this isn't vfs_sync() then flush
1262 * the log again.
1263 */
1264 if (flags & SYNC_DELWRI) {
1265 xfs_log_force(mp, (xfs_lsn_t)0, log_flags);
1266 }
1267
1268 if (flags & SYNC_FSDATA) {
1269 /*
1270 * If this is vfs_sync() then only sync the superblock
1271 * if we can lock it without sleeping and it is not pinned.
1272 */
1273 if (flags & SYNC_BDFLUSH) {
1274 bp = xfs_getsb(mp, XFS_BUF_TRYLOCK);
1275 if (bp != NULL) {
1276 bip = XFS_BUF_FSPRIVATE(bp,xfs_buf_log_item_t*);
1277 if ((bip != NULL) &&
1278 xfs_buf_item_dirty(bip)) {
1279 if (!(XFS_BUF_ISPINNED(bp))) {
1280 XFS_BUF_ASYNC(bp);
1281 error = xfs_bwrite(mp, bp);
1282 } else {
1283 xfs_buf_relse(bp);
1284 }
1285 } else {
1286 xfs_buf_relse(bp);
1287 }
1288 }
1289 } else {
1290 bp = xfs_getsb(mp, 0);
1291 /*
1292 * If the buffer is pinned then push on the log so
1293 * we won't get stuck waiting in the write for
1294 * someone, maybe ourselves, to flush the log.
1295 * Even though we just pushed the log above, we
1296 * did not have the superblock buffer locked at
1297 * that point so it can become pinned in between
1298 * there and here.
1299 */
1300 if (XFS_BUF_ISPINNED(bp))
1301 xfs_log_force(mp, (xfs_lsn_t)0, XFS_LOG_FORCE);
1302 if (flags & SYNC_WAIT)
1303 XFS_BUF_UNASYNC(bp);
1304 else
1305 XFS_BUF_ASYNC(bp);
1306 error = xfs_bwrite(mp, bp);
1307 }
1308 if (error) {
1309 last_error = error;
1310 }
1311 }
1312
1313 /*
David Chinner92821e22007-05-24 15:26:31 +10001314 * If asked, update the disk superblock with incore counter values if we
1315 * are using non-persistent counters so that they don't get too far out
1316 * of sync if we crash or get a forced shutdown. We don't want to force
1317 * this to disk, just get a transaction into the iclogs....
1318 */
1319 if (flags & SYNC_SUPER)
1320 xfs_log_sbcount(mp, 0);
1321
1322 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 * Now check to see if the log needs a "dummy" transaction.
1324 */
1325
1326 if (!(flags & SYNC_REMOUNT) && xfs_log_need_covered(mp)) {
1327 xfs_trans_t *tp;
1328 xfs_inode_t *ip;
1329
1330 /*
1331 * Put a dummy transaction in the log to tell
1332 * recovery that all others are OK.
1333 */
1334 tp = xfs_trans_alloc(mp, XFS_TRANS_DUMMY1);
1335 if ((error = xfs_trans_reserve(tp, 0,
1336 XFS_ICHANGE_LOG_RES(mp),
1337 0, 0, 0))) {
1338 xfs_trans_cancel(tp, 0);
1339 return error;
1340 }
1341
1342 ip = mp->m_rootip;
1343 xfs_ilock(ip, XFS_ILOCK_EXCL);
1344
1345 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
1346 xfs_trans_ihold(tp, ip);
1347 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Eric Sandeen1c72bf92007-05-08 13:48:42 +10001348 error = xfs_trans_commit(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1350 xfs_log_force(mp, (xfs_lsn_t)0, log_flags);
1351 }
1352
1353 /*
1354 * When shutting down, we need to insure that the AIL is pushed
1355 * to disk or the filesystem can appear corrupt from the PROM.
1356 */
1357 if ((flags & (SYNC_CLOSE|SYNC_WAIT)) == (SYNC_CLOSE|SYNC_WAIT)) {
1358 XFS_bflush(mp->m_ddev_targp);
1359 if (mp->m_rtdev_targp) {
1360 XFS_bflush(mp->m_rtdev_targp);
1361 }
1362 }
1363
1364 return XFS_ERROR(last_error);
1365}