blob: a005cebf50412437c958d35a0caabd4a0337a24e [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);
Alexey Dobriyanfe0754f2008-04-29 12:53:08 +1000189 kmem_zone_destroy(xfs_log_ticket_zone);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190}
191
192/*
193 * xfs_start_flags
194 *
195 * This function fills in xfs_mount_t fields based on mount args.
196 * Note: the superblock has _not_ yet been read in.
197 */
198STATIC int
199xfs_start_flags(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 struct xfs_mount_args *ap,
201 struct xfs_mount *mp)
202{
203 /* Values are in BBs */
204 if ((ap->flags & XFSMNT_NOALIGN) != XFSMNT_NOALIGN) {
205 /*
206 * At this point the superblock has not been read
207 * in, therefore we do not know the block size.
208 * Before the mount call ends we will convert
209 * these to FSBs.
210 */
211 mp->m_dalign = ap->sunit;
212 mp->m_swidth = ap->swidth;
213 }
214
215 if (ap->logbufs != -1 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 ap->logbufs != 0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 (ap->logbufs < XLOG_MIN_ICLOGS ||
218 ap->logbufs > XLOG_MAX_ICLOGS)) {
219 cmn_err(CE_WARN,
220 "XFS: invalid logbufs value: %d [not %d-%d]",
221 ap->logbufs, XLOG_MIN_ICLOGS, XLOG_MAX_ICLOGS);
222 return XFS_ERROR(EINVAL);
223 }
224 mp->m_logbufs = ap->logbufs;
225 if (ap->logbufsize != -1 &&
Nathan Scottcfcbbbd2005-11-02 15:12:04 +1100226 ap->logbufsize != 0 &&
Eric Sandeendcb3b832007-08-16 16:25:33 +1000227 (ap->logbufsize < XLOG_MIN_RECORD_BSIZE ||
228 ap->logbufsize > XLOG_MAX_RECORD_BSIZE ||
229 !is_power_of_2(ap->logbufsize))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 cmn_err(CE_WARN,
231 "XFS: invalid logbufsize: %d [not 16k,32k,64k,128k or 256k]",
232 ap->logbufsize);
233 return XFS_ERROR(EINVAL);
234 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 mp->m_logbsize = ap->logbufsize;
236 mp->m_fsname_len = strlen(ap->fsname) + 1;
237 mp->m_fsname = kmem_alloc(mp->m_fsname_len, KM_SLEEP);
238 strcpy(mp->m_fsname, ap->fsname);
Nathan Scottfc1f8c12005-11-02 11:44:33 +1100239 if (ap->rtname[0]) {
240 mp->m_rtname = kmem_alloc(strlen(ap->rtname) + 1, KM_SLEEP);
241 strcpy(mp->m_rtname, ap->rtname);
242 }
243 if (ap->logname[0]) {
244 mp->m_logname = kmem_alloc(strlen(ap->logname) + 1, KM_SLEEP);
245 strcpy(mp->m_logname, ap->logname);
246 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
248 if (ap->flags & XFSMNT_WSYNC)
249 mp->m_flags |= XFS_MOUNT_WSYNC;
250#if XFS_BIG_INUMS
251 if (ap->flags & XFSMNT_INO64) {
252 mp->m_flags |= XFS_MOUNT_INO64;
253 mp->m_inoadd = XFS_INO64_OFFSET;
254 }
255#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 if (ap->flags & XFSMNT_RETERR)
257 mp->m_flags |= XFS_MOUNT_RETERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 if (ap->flags & XFSMNT_NOALIGN)
259 mp->m_flags |= XFS_MOUNT_NOALIGN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 if (ap->flags & XFSMNT_SWALLOC)
261 mp->m_flags |= XFS_MOUNT_SWALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 if (ap->flags & XFSMNT_OSYNCISOSYNC)
263 mp->m_flags |= XFS_MOUNT_OSYNCISOSYNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 if (ap->flags & XFSMNT_32BITINODES)
Nathan Scottc11e2c32005-11-02 15:11:45 +1100265 mp->m_flags |= XFS_MOUNT_32BITINODES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
267 if (ap->flags & XFSMNT_IOSIZE) {
268 if (ap->iosizelog > XFS_MAX_IO_LOG ||
269 ap->iosizelog < XFS_MIN_IO_LOG) {
270 cmn_err(CE_WARN,
271 "XFS: invalid log iosize: %d [not %d-%d]",
272 ap->iosizelog, XFS_MIN_IO_LOG,
273 XFS_MAX_IO_LOG);
274 return XFS_ERROR(EINVAL);
275 }
276
277 mp->m_flags |= XFS_MOUNT_DFLT_IOSIZE;
278 mp->m_readio_log = mp->m_writeio_log = ap->iosizelog;
279 }
280
Josef Jeff Sipek1bd960e2008-02-29 13:58:40 +1100281 if (ap->flags & XFSMNT_IKEEP)
282 mp->m_flags |= XFS_MOUNT_IKEEP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 if (ap->flags & XFSMNT_DIRSYNC)
284 mp->m_flags |= XFS_MOUNT_DIRSYNC;
Nathan Scott13059ff2006-01-11 15:32:01 +1100285 if (ap->flags & XFSMNT_ATTR2)
286 mp->m_flags |= XFS_MOUNT_ATTR2;
Tim Shimmin7c12f292008-04-30 18:15:28 +1000287 if (ap->flags & XFSMNT_NOATTR2)
288 mp->m_flags |= XFS_MOUNT_NOATTR2;
David Chinnere8c8b3a2005-11-02 10:33:05 +1100289
Nathan Scotte718eeb42005-11-02 15:09:22 +1100290 if (ap->flags2 & XFSMNT2_COMPAT_IOSIZE)
291 mp->m_flags |= XFS_MOUNT_COMPAT_IOSIZE;
292
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 /*
294 * no recovery flag requires a read-only mount
295 */
296 if (ap->flags & XFSMNT_NORECOVERY) {
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000297 if (!(mp->m_flags & XFS_MOUNT_RDONLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 cmn_err(CE_WARN,
299 "XFS: tried to mount a FS read-write without recovery!");
300 return XFS_ERROR(EINVAL);
301 }
302 mp->m_flags |= XFS_MOUNT_NORECOVERY;
303 }
304
305 if (ap->flags & XFSMNT_NOUUID)
306 mp->m_flags |= XFS_MOUNT_NOUUID;
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100307 if (ap->flags & XFSMNT_BARRIER)
308 mp->m_flags |= XFS_MOUNT_BARRIER;
Christoph Hellwig4ef19dd2006-01-11 15:27:18 +1100309 else
310 mp->m_flags &= ~XFS_MOUNT_BARRIER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
David Chinner2a82b8b2007-07-11 11:09:12 +1000312 if (ap->flags2 & XFSMNT2_FILESTREAMS)
313 mp->m_flags |= XFS_MOUNT_FILESTREAMS;
314
Christoph Hellwig293688e2007-08-29 11:59:36 +1000315 if (ap->flags & XFSMNT_DMAPI)
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000316 mp->m_flags |= XFS_MOUNT_DMAPI;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 return 0;
318}
319
320/*
321 * This function fills in xfs_mount_t fields based on mount args.
322 * Note: the superblock _has_ now been read in.
323 */
324STATIC int
325xfs_finish_flags(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 struct xfs_mount_args *ap,
327 struct xfs_mount *mp)
328{
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000329 int ronly = (mp->m_flags & XFS_MOUNT_RDONLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
331 /* Fail a mount where the logbuf is smaller then the log stripe */
Eric Sandeen62118702008-03-06 13:44:28 +1100332 if (xfs_sb_version_haslogv2(&mp->m_sb)) {
Nathan Scottc2cd2552006-01-11 15:35:32 +1100333 if ((ap->logbufsize <= 0) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 (mp->m_sb.sb_logsunit > XLOG_BIG_RECORD_BSIZE)) {
335 mp->m_logbsize = mp->m_sb.sb_logsunit;
Nathan Scottc2cd2552006-01-11 15:35:32 +1100336 } else if (ap->logbufsize > 0 &&
337 ap->logbufsize < mp->m_sb.sb_logsunit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 cmn_err(CE_WARN,
339 "XFS: logbuf size must be greater than or equal to log stripe size");
340 return XFS_ERROR(EINVAL);
341 }
342 } else {
343 /* Fail a mount if the logbuf is larger than 32K */
344 if (ap->logbufsize > XLOG_BIG_RECORD_BSIZE) {
345 cmn_err(CE_WARN,
346 "XFS: logbuf size for version 1 logs must be 16K or 32K");
347 return XFS_ERROR(EINVAL);
348 }
349 }
350
Tim Shimmin7c12f292008-04-30 18:15:28 +1000351 /*
352 * mkfs'ed attr2 will turn on attr2 mount unless explicitly
353 * told by noattr2 to turn it off
354 */
355 if (xfs_sb_version_hasattr2(&mp->m_sb) &&
356 !(ap->flags & XFSMNT_NOATTR2))
Nathan Scott13059ff2006-01-11 15:32:01 +1100357 mp->m_flags |= XFS_MOUNT_ATTR2;
Nathan Scott13059ff2006-01-11 15:32:01 +1100358
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 /*
360 * prohibit r/w mounts of read-only filesystems
361 */
362 if ((mp->m_sb.sb_flags & XFS_SBF_READONLY) && !ronly) {
363 cmn_err(CE_WARN,
364 "XFS: cannot mount a read-only filesystem as read-write");
365 return XFS_ERROR(EROFS);
366 }
367
368 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 * check for shared mount.
370 */
371 if (ap->flags & XFSMNT_SHARED) {
Eric Sandeen62118702008-03-06 13:44:28 +1100372 if (!xfs_sb_version_hasshared(&mp->m_sb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 return XFS_ERROR(EINVAL);
374
375 /*
376 * For IRIX 6.5, shared mounts must have the shared
377 * version bit set, have the persistent readonly
378 * field set, must be version 0 and can only be mounted
379 * read-only.
380 */
381 if (!ronly || !(mp->m_sb.sb_flags & XFS_SBF_READONLY) ||
382 (mp->m_sb.sb_shared_vn != 0))
383 return XFS_ERROR(EINVAL);
384
385 mp->m_flags |= XFS_MOUNT_SHARED;
386
387 /*
388 * Shared XFS V0 can't deal with DMI. Return EINVAL.
389 */
390 if (mp->m_sb.sb_shared_vn == 0 && (ap->flags & XFSMNT_DMAPI))
391 return XFS_ERROR(EINVAL);
392 }
393
Christoph Hellwigb09cc772007-08-30 17:19:57 +1000394 if (ap->flags & XFSMNT_UQUOTA) {
395 mp->m_qflags |= (XFS_UQUOTA_ACCT | XFS_UQUOTA_ACTIVE);
396 if (ap->flags & XFSMNT_UQUOTAENF)
397 mp->m_qflags |= XFS_UQUOTA_ENFD;
398 }
399
400 if (ap->flags & XFSMNT_GQUOTA) {
401 mp->m_qflags |= (XFS_GQUOTA_ACCT | XFS_GQUOTA_ACTIVE);
402 if (ap->flags & XFSMNT_GQUOTAENF)
403 mp->m_qflags |= XFS_OQUOTA_ENFD;
404 } else if (ap->flags & XFSMNT_PQUOTA) {
405 mp->m_qflags |= (XFS_PQUOTA_ACCT | XFS_PQUOTA_ACTIVE);
406 if (ap->flags & XFSMNT_PQUOTAENF)
407 mp->m_qflags |= XFS_OQUOTA_ENFD;
408 }
409
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 return 0;
411}
412
413/*
414 * xfs_mount
415 *
416 * The file system configurations are:
417 * (1) device (partition) with data and internal log
418 * (2) logical volume with data and log subvolumes.
419 * (3) logical volume with data, log, and realtime subvolumes.
420 *
421 * We only have to handle opening the log and realtime volumes here if
422 * they are present. The data subvolume has already been opened by
423 * get_sb_bdev() and is stored in vfsp->vfs_super->s_bdev.
424 */
Christoph Hellwig48c872a9f2007-08-30 17:20:31 +1000425int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426xfs_mount(
Christoph Hellwig48c872a9f2007-08-30 17:20:31 +1000427 struct xfs_mount *mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 struct xfs_mount_args *args,
429 cred_t *credp)
430{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 struct block_device *ddev, *logdev, *rtdev;
432 int flags = 0, error;
433
Christoph Hellwigb267ce92007-08-30 17:21:30 +1000434 ddev = mp->m_super->s_bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 logdev = rtdev = NULL;
436
Christoph Hellwig293688e2007-08-29 11:59:36 +1000437 error = xfs_dmops_get(mp, args);
438 if (error)
439 return error;
Christoph Hellwigb09cc772007-08-30 17:19:57 +1000440 error = xfs_qmops_get(mp, args);
441 if (error)
442 return error;
Christoph Hellwig293688e2007-08-29 11:59:36 +1000443
Nathan Scott764d1f82006-03-31 13:04:17 +1000444 if (args->flags & XFSMNT_QUIET)
445 flags |= XFS_MFSI_QUIET;
446
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 /*
448 * Open real time and log devices - order is important.
449 */
450 if (args->logname[0]) {
451 error = xfs_blkdev_get(mp, args->logname, &logdev);
452 if (error)
453 return error;
454 }
455 if (args->rtname[0]) {
456 error = xfs_blkdev_get(mp, args->rtname, &rtdev);
457 if (error) {
458 xfs_blkdev_put(logdev);
459 return error;
460 }
461
462 if (rtdev == ddev || rtdev == logdev) {
463 cmn_err(CE_WARN,
464 "XFS: Cannot mount filesystem with identical rtdev and ddev/logdev.");
465 xfs_blkdev_put(logdev);
466 xfs_blkdev_put(rtdev);
467 return EINVAL;
468 }
469 }
470
471 /*
472 * Setup xfs_mount buffer target pointers
473 */
474 error = ENOMEM;
475 mp->m_ddev_targp = xfs_alloc_buftarg(ddev, 0);
476 if (!mp->m_ddev_targp) {
477 xfs_blkdev_put(logdev);
478 xfs_blkdev_put(rtdev);
479 return error;
480 }
481 if (rtdev) {
482 mp->m_rtdev_targp = xfs_alloc_buftarg(rtdev, 1);
Jesper Juhl49ee6c92007-08-16 16:25:42 +1000483 if (!mp->m_rtdev_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 mp->m_logdev_targp = (logdev && logdev != ddev) ?
490 xfs_alloc_buftarg(logdev, 1) : mp->m_ddev_targp;
Jesper Juhl49ee6c92007-08-16 16:25:42 +1000491 if (!mp->m_logdev_targp) {
492 xfs_blkdev_put(logdev);
493 xfs_blkdev_put(rtdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 goto error0;
Jesper Juhl49ee6c92007-08-16 16:25:42 +1000495 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
497 /*
498 * Setup flags based on mount(2) options and then the superblock
499 */
Christoph Hellwigb267ce92007-08-30 17:21:30 +1000500 error = xfs_start_flags(args, mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 if (error)
502 goto error1;
Nathan Scott764d1f82006-03-31 13:04:17 +1000503 error = xfs_readsb(mp, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 if (error)
505 goto error1;
Christoph Hellwigb267ce92007-08-30 17:21:30 +1000506 error = xfs_finish_flags(args, mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 if (error)
508 goto error2;
509
510 /*
511 * Setup xfs_mount buffer target pointers based on superblock
512 */
513 error = xfs_setsize_buftarg(mp->m_ddev_targp, mp->m_sb.sb_blocksize,
514 mp->m_sb.sb_sectsize);
515 if (!error && logdev && logdev != ddev) {
516 unsigned int log_sector_size = BBSIZE;
517
Eric Sandeen62118702008-03-06 13:44:28 +1100518 if (xfs_sb_version_hassector(&mp->m_sb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 log_sector_size = mp->m_sb.sb_logsectsize;
520 error = xfs_setsize_buftarg(mp->m_logdev_targp,
521 mp->m_sb.sb_blocksize,
522 log_sector_size);
523 }
524 if (!error && rtdev)
525 error = xfs_setsize_buftarg(mp->m_rtdev_targp,
526 mp->m_sb.sb_blocksize,
527 mp->m_sb.sb_sectsize);
528 if (error)
529 goto error2;
530
Nathan Scottb2ea4012006-07-28 17:05:13 +1000531 if (mp->m_flags & XFS_MOUNT_BARRIER)
Christoph Hellwigc7d437d2006-01-11 15:28:56 +1100532 xfs_mountfs_check_barriers(mp);
533
David Chinner2a82b8b2007-07-11 11:09:12 +1000534 if ((error = xfs_filestream_mount(mp)))
535 goto error2;
536
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000537 error = xfs_mountfs(mp, flags);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100538 if (error)
539 goto error2;
540
Christoph Hellwig293688e2007-08-29 11:59:36 +1000541 XFS_SEND_MOUNT(mp, DM_RIGHT_NULL, args->mtpt, args->fsname);
542
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100543 return 0;
544
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545error2:
546 if (mp->m_sb_bp)
547 xfs_freesb(mp);
548error1:
549 xfs_binval(mp->m_ddev_targp);
550 if (logdev && logdev != ddev)
551 xfs_binval(mp->m_logdev_targp);
552 if (rtdev)
553 xfs_binval(mp->m_rtdev_targp);
554error0:
555 xfs_unmountfs_close(mp, credp);
Christoph Hellwigb09cc772007-08-30 17:19:57 +1000556 xfs_qmops_put(mp);
Christoph Hellwig293688e2007-08-29 11:59:36 +1000557 xfs_dmops_put(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 return error;
559}
560
Christoph Hellwig48c872a9f2007-08-30 17:20:31 +1000561int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562xfs_unmount(
Christoph Hellwig48c872a9f2007-08-30 17:20:31 +1000563 xfs_mount_t *mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 int flags,
565 cred_t *credp)
566{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 xfs_inode_t *rip;
Nathan Scott67fcaa72006-06-09 17:00:52 +1000568 bhv_vnode_t *rvp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 int unmount_event_wanted = 0;
570 int unmount_event_flags = 0;
571 int xfs_unmountfs_needed = 0;
572 int error;
573
574 rip = mp->m_rootip;
575 rvp = XFS_ITOV(rip);
576
Eric Sandeen948c6d4f2007-08-23 16:19:57 +1000577#ifdef HAVE_DMAPI
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000578 if (mp->m_flags & XFS_MOUNT_DMAPI) {
Christoph Hellwigb267ce92007-08-30 17:21:30 +1000579 error = XFS_SEND_PREUNMOUNT(mp,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +1100580 rip, DM_RIGHT_NULL, rip, DM_RIGHT_NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 NULL, NULL, 0, 0,
582 (mp->m_dmevmask & (1<<DM_EVENT_PREUNMOUNT))?
583 0:DM_FLAGS_UNWANTED);
584 if (error)
585 return XFS_ERROR(error);
586 unmount_event_wanted = 1;
587 unmount_event_flags = (mp->m_dmevmask & (1<<DM_EVENT_UNMOUNT))?
588 0 : DM_FLAGS_UNWANTED;
589 }
Eric Sandeen948c6d4f2007-08-23 16:19:57 +1000590#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
David Chinner2a82b8b2007-07-11 11:09:12 +1000592 /*
593 * Blow away any referenced inode in the filestreams cache.
594 * This can and will cause log traffic as inodes go inactive
595 * here.
596 */
597 xfs_filestream_unmount(mp);
598
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 XFS_bflush(mp->m_ddev_targp);
600 error = xfs_unmount_flush(mp, 0);
601 if (error)
602 goto out;
603
604 ASSERT(vn_count(rvp) == 1);
605
606 /*
607 * Drop the reference count
608 */
Christoph Hellwig43355092008-03-27 18:01:08 +1100609 IRELE(rip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
611 /*
612 * If we're forcing a shutdown, typically because of a media error,
613 * we want to make sure we invalidate dirty pages that belong to
614 * referenced vnodes as well.
615 */
616 if (XFS_FORCED_SHUTDOWN(mp)) {
Christoph Hellwig48c872a9f2007-08-30 17:20:31 +1000617 error = xfs_sync(mp, SYNC_WAIT | SYNC_CLOSE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 ASSERT(error != EFSCORRUPTED);
619 }
620 xfs_unmountfs_needed = 1;
621
622out:
623 /* Send DMAPI event, if required.
624 * Then do xfs_unmountfs() if needed.
625 * Then return error (or zero).
626 */
627 if (unmount_event_wanted) {
628 /* Note: mp structure must still exist for
629 * XFS_SEND_UNMOUNT() call.
630 */
Christoph Hellwigbc4ac742008-03-06 13:45:58 +1100631 XFS_SEND_UNMOUNT(mp, error == 0 ? rip : NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 DM_RIGHT_NULL, 0, error, unmount_event_flags);
633 }
634 if (xfs_unmountfs_needed) {
635 /*
636 * Call common unmount function to flush to disk
637 * and free the super block buffer & mount structures.
638 */
639 xfs_unmountfs(mp, credp);
Christoph Hellwigb09cc772007-08-30 17:19:57 +1000640 xfs_qmops_put(mp);
Christoph Hellwig293688e2007-08-29 11:59:36 +1000641 xfs_dmops_put(mp);
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000642 kmem_free(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 }
644
645 return XFS_ERROR(error);
646}
647
David Chinner3c85c362008-04-10 12:19:40 +1000648STATIC void
Christoph Hellwigf898d6c2005-06-21 15:40:48 +1000649xfs_quiesce_fs(
650 xfs_mount_t *mp)
651{
652 int count = 0, pincount;
Nathan Scott3758dee2006-03-22 12:47:28 +1100653
Christoph Hellwigf898d6c2005-06-21 15:40:48 +1000654 xfs_flush_buftarg(mp->m_ddev_targp, 0);
655 xfs_finish_reclaim_all(mp, 0);
656
657 /* This loop must run at least twice.
658 * The first instance of the loop will flush
659 * most meta data but that will generate more
660 * meta data (typically directory updates).
661 * Which then must be flushed and logged before
662 * we can write the unmount record.
Nathan Scott3758dee2006-03-22 12:47:28 +1100663 */
Christoph Hellwigf898d6c2005-06-21 15:40:48 +1000664 do {
David Chinner516b2e72007-06-18 16:50:48 +1000665 xfs_syncsub(mp, SYNC_INODE_QUIESCE, NULL);
Christoph Hellwigf898d6c2005-06-21 15:40:48 +1000666 pincount = xfs_flush_buftarg(mp->m_ddev_targp, 1);
667 if (!pincount) {
668 delay(50);
669 count++;
670 }
671 } while (count < 2);
Christoph Hellwigf898d6c2005-06-21 15:40:48 +1000672}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
David Chinner516b2e72007-06-18 16:50:48 +1000674/*
675 * Second stage of a quiesce. The data is already synced, now we have to take
676 * care of the metadata. New transactions are already blocked, so we need to
677 * wait for any remaining transactions to drain out before proceding.
678 */
Christoph Hellwig9909c4a2007-10-11 18:11:14 +1000679void
David Chinner516b2e72007-06-18 16:50:48 +1000680xfs_attr_quiesce(
681 xfs_mount_t *mp)
682{
David Chinnere5720ee2008-04-10 12:21:18 +1000683 int error = 0;
684
David Chinner516b2e72007-06-18 16:50:48 +1000685 /* wait for all modifications to complete */
686 while (atomic_read(&mp->m_active_trans) > 0)
687 delay(100);
688
689 /* flush inodes and push all remaining buffers out to disk */
690 xfs_quiesce_fs(mp);
691
692 ASSERT_ALWAYS(atomic_read(&mp->m_active_trans) == 0);
693
694 /* Push the superblock and write an unmount record */
David Chinnere5720ee2008-04-10 12:21:18 +1000695 error = xfs_log_sbcount(mp, 1);
696 if (error)
697 xfs_fs_cmn_err(CE_WARN, mp,
698 "xfs_attr_quiesce: failed to log sb changes. "
699 "Frozen image may not be consistent.");
David Chinner516b2e72007-06-18 16:50:48 +1000700 xfs_log_unmount_write(mp);
701 xfs_unmountfs_writesb(mp);
702}
703
Christoph Hellwig48c872a9f2007-08-30 17:20:31 +1000704int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705xfs_mntupdate(
Christoph Hellwig48c872a9f2007-08-30 17:20:31 +1000706 struct xfs_mount *mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 int *flags,
708 struct xfs_mount_args *args)
709{
Nathan Scottd08d3892006-05-08 19:51:28 +1000710 if (!(*flags & MS_RDONLY)) { /* rw/ro -> rw */
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000711 if (mp->m_flags & XFS_MOUNT_RDONLY)
712 mp->m_flags &= ~XFS_MOUNT_RDONLY;
Nathan Scottd08d3892006-05-08 19:51:28 +1000713 if (args->flags & XFSMNT_BARRIER) {
714 mp->m_flags |= XFS_MOUNT_BARRIER;
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100715 xfs_mountfs_check_barriers(mp);
Nathan Scottd08d3892006-05-08 19:51:28 +1000716 } else {
717 mp->m_flags &= ~XFS_MOUNT_BARRIER;
718 }
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000719 } else if (!(mp->m_flags & XFS_MOUNT_RDONLY)) { /* rw -> ro */
David Chinner2a82b8b2007-07-11 11:09:12 +1000720 xfs_filestream_flush(mp);
Christoph Hellwig745f6912007-08-30 17:20:39 +1000721 xfs_sync(mp, SYNC_DATA_QUIESCE);
David Chinner516b2e72007-06-18 16:50:48 +1000722 xfs_attr_quiesce(mp);
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000723 mp->m_flags |= XFS_MOUNT_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 return 0;
726}
727
728/*
729 * xfs_unmount_flush implements a set of flush operation on special
730 * inodes, which are needed as a separate set of operations so that
731 * they can be called as part of relocation process.
732 */
733int
734xfs_unmount_flush(
735 xfs_mount_t *mp, /* Mount structure we are getting
736 rid of. */
737 int relocation) /* Called from vfs relocation. */
738{
739 xfs_inode_t *rip = mp->m_rootip;
740 xfs_inode_t *rbmip;
741 xfs_inode_t *rsumip = NULL;
Nathan Scott67fcaa72006-06-09 17:00:52 +1000742 bhv_vnode_t *rvp = XFS_ITOV(rip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 int error;
744
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +1000745 xfs_ilock(rip, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 xfs_iflock(rip);
747
748 /*
749 * Flush out the real time inodes.
750 */
751 if ((rbmip = mp->m_rbmip) != NULL) {
752 xfs_ilock(rbmip, XFS_ILOCK_EXCL);
753 xfs_iflock(rbmip);
754 error = xfs_iflush(rbmip, XFS_IFLUSH_SYNC);
755 xfs_iunlock(rbmip, XFS_ILOCK_EXCL);
756
757 if (error == EFSCORRUPTED)
758 goto fscorrupt_out;
759
760 ASSERT(vn_count(XFS_ITOV(rbmip)) == 1);
761
762 rsumip = mp->m_rsumip;
763 xfs_ilock(rsumip, XFS_ILOCK_EXCL);
764 xfs_iflock(rsumip);
765 error = xfs_iflush(rsumip, XFS_IFLUSH_SYNC);
766 xfs_iunlock(rsumip, XFS_ILOCK_EXCL);
767
768 if (error == EFSCORRUPTED)
769 goto fscorrupt_out;
770
771 ASSERT(vn_count(XFS_ITOV(rsumip)) == 1);
772 }
773
774 /*
775 * Synchronously flush root inode to disk
776 */
777 error = xfs_iflush(rip, XFS_IFLUSH_SYNC);
778 if (error == EFSCORRUPTED)
779 goto fscorrupt_out2;
780
781 if (vn_count(rvp) != 1 && !relocation) {
782 xfs_iunlock(rip, XFS_ILOCK_EXCL);
783 return XFS_ERROR(EBUSY);
784 }
785
786 /*
787 * Release dquot that rootinode, rbmino and rsumino might be holding,
788 * flush and purge the quota inodes.
789 */
790 error = XFS_QM_UNMOUNT(mp);
791 if (error == EFSCORRUPTED)
792 goto fscorrupt_out2;
793
794 if (rbmip) {
Christoph Hellwig43355092008-03-27 18:01:08 +1100795 IRELE(rbmip);
796 IRELE(rsumip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 }
798
799 xfs_iunlock(rip, XFS_ILOCK_EXCL);
800 return 0;
801
802fscorrupt_out:
803 xfs_ifunlock(rip);
804
805fscorrupt_out2:
806 xfs_iunlock(rip, XFS_ILOCK_EXCL);
807
808 return XFS_ERROR(EFSCORRUPTED);
809}
810
811/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 * xfs_sync flushes any pending I/O to file system vfsp.
813 *
814 * This routine is called by vfs_sync() to make sure that things make it
815 * out to disk eventually, on sync() system calls to flush out everything,
816 * and when the file system is unmounted. For the vfs_sync() case, all
817 * we really need to do is sync out the log to make all of our meta-data
818 * updates permanent (except for timestamps). For calls from pflushd(),
819 * dirty pages are kept moving by calling pdflush() on the inodes
820 * containing them. We also flush the inodes that we can lock without
821 * sleeping and the superblock if we can lock it without sleeping from
822 * vfs_sync() so that items at the tail of the log are always moving out.
823 *
824 * Flags:
825 * SYNC_BDFLUSH - We're being called from vfs_sync() so we don't want
826 * to sleep if we can help it. All we really need
827 * to do is ensure that the log is synced at least
828 * periodically. We also push the inodes and
829 * superblock if we can lock them without sleeping
830 * and they are not pinned.
831 * SYNC_ATTR - We need to flush the inodes. If SYNC_BDFLUSH is not
832 * set, then we really want to lock each inode and flush
833 * it.
834 * SYNC_WAIT - All the flushes that take place in this call should
835 * be synchronous.
836 * SYNC_DELWRI - This tells us to push dirty pages associated with
837 * inodes. SYNC_WAIT and SYNC_BDFLUSH are used to
838 * determine if they should be flushed sync, async, or
839 * delwri.
840 * SYNC_CLOSE - This flag is passed when the system is being
Nathan Scottc41564b2006-03-29 08:55:14 +1000841 * unmounted. We should sync and invalidate everything.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 * SYNC_FSDATA - This indicates that the caller would like to make
843 * sure the superblock is safe on disk. We can ensure
Nathan Scottc41564b2006-03-29 08:55:14 +1000844 * this by simply making sure the log gets flushed
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 * if SYNC_BDFLUSH is set, and by actually writing it
846 * out otherwise.
David Chinner3c0dc772007-02-10 18:37:22 +1100847 * SYNC_IOWAIT - The caller wants us to wait for all data I/O to complete
848 * before we return (including direct I/O). Forms the drain
849 * side of the write barrier needed to safely quiesce the
850 * filesystem.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 *
852 */
Christoph Hellwig48c872a9f2007-08-30 17:20:31 +1000853int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854xfs_sync(
Christoph Hellwig48c872a9f2007-08-30 17:20:31 +1000855 xfs_mount_t *mp,
856 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857{
Christoph Hellwigb09cc772007-08-30 17:19:57 +1000858 int error;
859
860 /*
861 * Get the Quota Manager to flush the dquots.
862 *
863 * If XFS quota support is not enabled or this filesystem
864 * instance does not use quotas XFS_QM_DQSYNC will always
865 * return zero.
866 */
867 error = XFS_QM_DQSYNC(mp, flags);
868 if (error) {
869 /*
870 * If we got an IO error, we will be shutting down.
871 * So, there's nothing more for us to do here.
872 */
873 ASSERT(error != EIO || XFS_FORCED_SHUTDOWN(mp));
874 if (XFS_FORCED_SHUTDOWN(mp))
875 return XFS_ERROR(error);
876 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
David Chinner2a82b8b2007-07-11 11:09:12 +1000878 if (flags & SYNC_IOWAIT)
879 xfs_filestream_flush(mp);
880
David Chinner28239452007-02-10 18:36:40 +1100881 return xfs_syncsub(mp, flags, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882}
883
884/*
885 * xfs sync routine for internal use
886 *
Nathan Scottb83bd132006-06-09 16:48:30 +1000887 * This routine supports all of the flags defined for the generic vfs_sync
Lachlan McIlroy1f9b3b62007-02-10 18:35:33 +1100888 * interface as explained above under xfs_sync.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 *
890 */
Nathan Scottee348072005-11-02 10:32:38 +1100891int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892xfs_sync_inodes(
893 xfs_mount_t *mp,
894 int flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 int *bypassed)
896{
897 xfs_inode_t *ip = NULL;
Nathan Scott67fcaa72006-06-09 17:00:52 +1000898 bhv_vnode_t *vp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 int error;
900 int last_error;
901 uint64_t fflag;
902 uint lock_flags;
903 uint base_lock_flags;
904 boolean_t mount_locked;
905 boolean_t vnode_refed;
906 int preempt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 xfs_iptr_t *ipointer;
908#ifdef DEBUG
909 boolean_t ipointer_in = B_FALSE;
910
911#define IPOINTER_SET ipointer_in = B_TRUE
912#define IPOINTER_CLR ipointer_in = B_FALSE
913#else
914#define IPOINTER_SET
915#define IPOINTER_CLR
916#endif
917
918
919/* Insert a marker record into the inode list after inode ip. The list
920 * must be locked when this is called. After the call the list will no
921 * longer be locked.
922 */
923#define IPOINTER_INSERT(ip, mp) { \
924 ASSERT(ipointer_in == B_FALSE); \
925 ipointer->ip_mnext = ip->i_mnext; \
926 ipointer->ip_mprev = ip; \
927 ip->i_mnext = (xfs_inode_t *)ipointer; \
928 ipointer->ip_mnext->i_mprev = (xfs_inode_t *)ipointer; \
929 preempt = 0; \
930 XFS_MOUNT_IUNLOCK(mp); \
931 mount_locked = B_FALSE; \
932 IPOINTER_SET; \
933 }
934
935/* Remove the marker from the inode list. If the marker was the only item
936 * in the list then there are no remaining inodes and we should zero out
937 * the whole list. If we are the current head of the list then move the head
938 * past us.
939 */
940#define IPOINTER_REMOVE(ip, mp) { \
941 ASSERT(ipointer_in == B_TRUE); \
942 if (ipointer->ip_mnext != (xfs_inode_t *)ipointer) { \
943 ip = ipointer->ip_mnext; \
944 ip->i_mprev = ipointer->ip_mprev; \
945 ipointer->ip_mprev->i_mnext = ip; \
946 if (mp->m_inodes == (xfs_inode_t *)ipointer) { \
947 mp->m_inodes = ip; \
948 } \
949 } else { \
950 ASSERT(mp->m_inodes == (xfs_inode_t *)ipointer); \
951 mp->m_inodes = NULL; \
952 ip = NULL; \
953 } \
954 IPOINTER_CLR; \
955 }
956
957#define XFS_PREEMPT_MASK 0x7f
958
Lachlan McIlroy44866d32007-09-14 15:21:08 +1000959 ASSERT(!(flags & SYNC_BDFLUSH));
960
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 if (bypassed)
962 *bypassed = 0;
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000963 if (mp->m_flags & XFS_MOUNT_RDONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 return 0;
965 error = 0;
966 last_error = 0;
967 preempt = 0;
968
969 /* Allocate a reference marker */
970 ipointer = (xfs_iptr_t *)kmem_zalloc(sizeof(xfs_iptr_t), KM_SLEEP);
971
972 fflag = XFS_B_ASYNC; /* default is don't wait */
Lachlan McIlroy44866d32007-09-14 15:21:08 +1000973 if (flags & SYNC_DELWRI)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 fflag = XFS_B_DELWRI;
975 if (flags & SYNC_WAIT)
976 fflag = 0; /* synchronous overrides all */
977
978 base_lock_flags = XFS_ILOCK_SHARED;
979 if (flags & (SYNC_DELWRI | SYNC_CLOSE)) {
980 /*
981 * We need the I/O lock if we're going to call any of
982 * the flush/inval routines.
983 */
984 base_lock_flags |= XFS_IOLOCK_SHARED;
985 }
986
987 XFS_MOUNT_ILOCK(mp);
988
989 ip = mp->m_inodes;
990
991 mount_locked = B_TRUE;
992 vnode_refed = B_FALSE;
993
994 IPOINTER_CLR;
995
996 do {
997 ASSERT(ipointer_in == B_FALSE);
998 ASSERT(vnode_refed == B_FALSE);
999
1000 lock_flags = base_lock_flags;
1001
1002 /*
1003 * There were no inodes in the list, just break out
1004 * of the loop.
1005 */
1006 if (ip == NULL) {
1007 break;
1008 }
1009
1010 /*
1011 * We found another sync thread marker - skip it
1012 */
1013 if (ip->i_mount == NULL) {
1014 ip = ip->i_mnext;
1015 continue;
1016 }
1017
1018 vp = XFS_ITOV_NULL(ip);
1019
1020 /*
1021 * If the vnode is gone then this is being torn down,
1022 * call reclaim if it is flushed, else let regular flush
1023 * code deal with it later in the loop.
1024 */
1025
1026 if (vp == NULL) {
1027 /* Skip ones already in reclaim */
1028 if (ip->i_flags & XFS_IRECLAIM) {
1029 ip = ip->i_mnext;
1030 continue;
1031 }
1032 if (xfs_ilock_nowait(ip, XFS_ILOCK_EXCL) == 0) {
1033 ip = ip->i_mnext;
1034 } else if ((xfs_ipincount(ip) == 0) &&
1035 xfs_iflock_nowait(ip)) {
1036 IPOINTER_INSERT(ip, mp);
1037
1038 xfs_finish_reclaim(ip, 1,
1039 XFS_IFLUSH_DELWRI_ELSE_ASYNC);
1040
1041 XFS_MOUNT_ILOCK(mp);
1042 mount_locked = B_TRUE;
1043 IPOINTER_REMOVE(ip, mp);
1044 } else {
1045 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1046 ip = ip->i_mnext;
1047 }
1048 continue;
1049 }
1050
1051 if (VN_BAD(vp)) {
1052 ip = ip->i_mnext;
1053 continue;
1054 }
1055
1056 if (XFS_FORCED_SHUTDOWN(mp) && !(flags & SYNC_CLOSE)) {
1057 XFS_MOUNT_IUNLOCK(mp);
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001058 kmem_free(ipointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 return 0;
1060 }
1061
1062 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 * Try to lock without sleeping. We're out of order with
1064 * the inode list lock here, so if we fail we need to drop
1065 * the mount lock and try again. If we're called from
1066 * bdflush() here, then don't bother.
1067 *
1068 * The inode lock here actually coordinates with the
1069 * almost spurious inode lock in xfs_ireclaim() to prevent
1070 * the vnode we handle here without a reference from
1071 * being freed while we reference it. If we lock the inode
1072 * while it's on the mount list here, then the spurious inode
1073 * lock in xfs_ireclaim() after the inode is pulled from
1074 * the mount list will sleep until we release it here.
1075 * This keeps the vnode from being freed while we reference
Christoph Hellwigcdb62682005-09-02 16:24:19 +10001076 * it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 */
1078 if (xfs_ilock_nowait(ip, lock_flags) == 0) {
Lachlan McIlroy44866d32007-09-14 15:21:08 +10001079 if (vp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 ip = ip->i_mnext;
1081 continue;
1082 }
1083
Christoph Hellwigcdb62682005-09-02 16:24:19 +10001084 vp = vn_grab(vp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 if (vp == NULL) {
Christoph Hellwigcdb62682005-09-02 16:24:19 +10001086 ip = ip->i_mnext;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 continue;
1088 }
1089
Christoph Hellwigcdb62682005-09-02 16:24:19 +10001090 IPOINTER_INSERT(ip, mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 xfs_ilock(ip, lock_flags);
1092
1093 ASSERT(vp == XFS_ITOV(ip));
1094 ASSERT(ip->i_mount == mp);
1095
1096 vnode_refed = B_TRUE;
1097 }
1098
1099 /* From here on in the loop we may have a marker record
1100 * in the inode list.
1101 */
1102
David Chinner40095b62007-05-14 18:24:09 +10001103 /*
1104 * If we have to flush data or wait for I/O completion
1105 * we need to drop the ilock that we currently hold.
1106 * If we need to drop the lock, insert a marker if we
1107 * have not already done so.
1108 */
1109 if ((flags & (SYNC_CLOSE|SYNC_IOWAIT)) ||
1110 ((flags & SYNC_DELWRI) && VN_DIRTY(vp))) {
1111 if (mount_locked) {
1112 IPOINTER_INSERT(ip, mp);
1113 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 xfs_iunlock(ip, XFS_ILOCK_SHARED);
1115
David Chinner40095b62007-05-14 18:24:09 +10001116 if (flags & SYNC_CLOSE) {
1117 /* Shutdown case. Flush and invalidate. */
1118 if (XFS_FORCED_SHUTDOWN(mp))
Christoph Hellwig739bfb22007-08-29 10:58:01 +10001119 xfs_tosspages(ip, 0, -1,
1120 FI_REMAPF);
David Chinner40095b62007-05-14 18:24:09 +10001121 else
Christoph Hellwig739bfb22007-08-29 10:58:01 +10001122 error = xfs_flushinval_pages(ip,
1123 0, -1, FI_REMAPF);
David Chinner40095b62007-05-14 18:24:09 +10001124 } else if ((flags & SYNC_DELWRI) && VN_DIRTY(vp)) {
Christoph Hellwig739bfb22007-08-29 10:58:01 +10001125 error = xfs_flush_pages(ip, 0,
Nathan Scott67fcaa72006-06-09 17:00:52 +10001126 -1, fflag, FI_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 }
1128
David Chinner40095b62007-05-14 18:24:09 +10001129 /*
1130 * When freezing, we need to wait ensure all I/O (including direct
1131 * I/O) is complete to ensure no further data modification can take
1132 * place after this point
1133 */
1134 if (flags & SYNC_IOWAIT)
Christoph Hellwigb677c212007-08-29 11:46:28 +10001135 vn_iowait(ip);
David Chinner40095b62007-05-14 18:24:09 +10001136
1137 xfs_ilock(ip, XFS_ILOCK_SHARED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 }
1139
Lachlan McIlroy44866d32007-09-14 15:21:08 +10001140 if ((flags & SYNC_ATTR) &&
1141 (ip->i_update_core ||
1142 (ip->i_itemp && ip->i_itemp->ili_format.ilf_fields))) {
1143 if (mount_locked)
1144 IPOINTER_INSERT(ip, mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
Lachlan McIlroy44866d32007-09-14 15:21:08 +10001146 if (flags & SYNC_WAIT) {
1147 xfs_iflock(ip);
1148 error = xfs_iflush(ip, XFS_IFLUSH_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
Lachlan McIlroy44866d32007-09-14 15:21:08 +10001150 /*
1151 * If we can't acquire the flush lock, then the inode
1152 * is already being flushed so don't bother waiting.
1153 *
1154 * If we can lock it then do a delwri flush so we can
1155 * combine multiple inode flushes in each disk write.
1156 */
1157 } else if (xfs_iflock_nowait(ip)) {
1158 error = xfs_iflush(ip, XFS_IFLUSH_DELWRI);
1159 } else if (bypassed) {
1160 (*bypassed)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 }
1162 }
1163
1164 if (lock_flags != 0) {
1165 xfs_iunlock(ip, lock_flags);
1166 }
1167
1168 if (vnode_refed) {
1169 /*
1170 * If we had to take a reference on the vnode
1171 * above, then wait until after we've unlocked
1172 * the inode to release the reference. This is
1173 * because we can be already holding the inode
Christoph Hellwig43355092008-03-27 18:01:08 +11001174 * lock when IRELE() calls xfs_inactive().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 *
1176 * Make sure to drop the mount lock before calling
Christoph Hellwig43355092008-03-27 18:01:08 +11001177 * IRELE() so that we don't trip over ourselves if
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 * we have to go for the mount lock again in the
1179 * inactive code.
1180 */
1181 if (mount_locked) {
1182 IPOINTER_INSERT(ip, mp);
1183 }
1184
Christoph Hellwig43355092008-03-27 18:01:08 +11001185 IRELE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186
1187 vnode_refed = B_FALSE;
1188 }
1189
1190 if (error) {
1191 last_error = error;
1192 }
1193
1194 /*
1195 * bail out if the filesystem is corrupted.
1196 */
1197 if (error == EFSCORRUPTED) {
1198 if (!mount_locked) {
1199 XFS_MOUNT_ILOCK(mp);
1200 IPOINTER_REMOVE(ip, mp);
1201 }
1202 XFS_MOUNT_IUNLOCK(mp);
1203 ASSERT(ipointer_in == B_FALSE);
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001204 kmem_free(ipointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 return XFS_ERROR(error);
1206 }
1207
1208 /* Let other threads have a chance at the mount lock
1209 * if we have looped many times without dropping the
1210 * lock.
1211 */
1212 if ((++preempt & XFS_PREEMPT_MASK) == 0) {
1213 if (mount_locked) {
1214 IPOINTER_INSERT(ip, mp);
1215 }
1216 }
1217
1218 if (mount_locked == B_FALSE) {
1219 XFS_MOUNT_ILOCK(mp);
1220 mount_locked = B_TRUE;
1221 IPOINTER_REMOVE(ip, mp);
1222 continue;
1223 }
1224
1225 ASSERT(ipointer_in == B_FALSE);
1226 ip = ip->i_mnext;
1227
1228 } while (ip != mp->m_inodes);
1229
1230 XFS_MOUNT_IUNLOCK(mp);
1231
1232 ASSERT(ipointer_in == B_FALSE);
1233
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001234 kmem_free(ipointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 return XFS_ERROR(last_error);
1236}
1237
1238/*
1239 * xfs sync routine for internal use
1240 *
Nathan Scottb83bd132006-06-09 16:48:30 +10001241 * This routine supports all of the flags defined for the generic vfs_sync
Lachlan McIlroy1f9b3b62007-02-10 18:35:33 +11001242 * interface as explained above under xfs_sync.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 *
1244 */
1245int
1246xfs_syncsub(
1247 xfs_mount_t *mp,
1248 int flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 int *bypassed)
1250{
1251 int error = 0;
1252 int last_error = 0;
1253 uint log_flags = XFS_LOG_FORCE;
1254 xfs_buf_t *bp;
1255 xfs_buf_log_item_t *bip;
1256
1257 /*
1258 * Sync out the log. This ensures that the log is periodically
1259 * flushed even if there is not enough activity to fill it up.
1260 */
1261 if (flags & SYNC_WAIT)
1262 log_flags |= XFS_LOG_SYNC;
1263
1264 xfs_log_force(mp, (xfs_lsn_t)0, log_flags);
1265
1266 if (flags & (SYNC_ATTR|SYNC_DELWRI)) {
1267 if (flags & SYNC_BDFLUSH)
1268 xfs_finish_reclaim_all(mp, 1);
1269 else
Lachlan McIlroy1f9b3b62007-02-10 18:35:33 +11001270 error = xfs_sync_inodes(mp, flags, bypassed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 }
1272
1273 /*
1274 * Flushing out dirty data above probably generated more
1275 * log activity, so if this isn't vfs_sync() then flush
1276 * the log again.
1277 */
1278 if (flags & SYNC_DELWRI) {
1279 xfs_log_force(mp, (xfs_lsn_t)0, log_flags);
1280 }
1281
1282 if (flags & SYNC_FSDATA) {
1283 /*
1284 * If this is vfs_sync() then only sync the superblock
1285 * if we can lock it without sleeping and it is not pinned.
1286 */
1287 if (flags & SYNC_BDFLUSH) {
1288 bp = xfs_getsb(mp, XFS_BUF_TRYLOCK);
1289 if (bp != NULL) {
1290 bip = XFS_BUF_FSPRIVATE(bp,xfs_buf_log_item_t*);
1291 if ((bip != NULL) &&
1292 xfs_buf_item_dirty(bip)) {
1293 if (!(XFS_BUF_ISPINNED(bp))) {
1294 XFS_BUF_ASYNC(bp);
1295 error = xfs_bwrite(mp, bp);
1296 } else {
1297 xfs_buf_relse(bp);
1298 }
1299 } else {
1300 xfs_buf_relse(bp);
1301 }
1302 }
1303 } else {
1304 bp = xfs_getsb(mp, 0);
1305 /*
1306 * If the buffer is pinned then push on the log so
1307 * we won't get stuck waiting in the write for
1308 * someone, maybe ourselves, to flush the log.
1309 * Even though we just pushed the log above, we
1310 * did not have the superblock buffer locked at
1311 * that point so it can become pinned in between
1312 * there and here.
1313 */
1314 if (XFS_BUF_ISPINNED(bp))
1315 xfs_log_force(mp, (xfs_lsn_t)0, XFS_LOG_FORCE);
1316 if (flags & SYNC_WAIT)
1317 XFS_BUF_UNASYNC(bp);
1318 else
1319 XFS_BUF_ASYNC(bp);
1320 error = xfs_bwrite(mp, bp);
1321 }
1322 if (error) {
1323 last_error = error;
1324 }
1325 }
1326
1327 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 * Now check to see if the log needs a "dummy" transaction.
1329 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 if (!(flags & SYNC_REMOUNT) && xfs_log_need_covered(mp)) {
1331 xfs_trans_t *tp;
1332 xfs_inode_t *ip;
1333
1334 /*
1335 * Put a dummy transaction in the log to tell
1336 * recovery that all others are OK.
1337 */
1338 tp = xfs_trans_alloc(mp, XFS_TRANS_DUMMY1);
1339 if ((error = xfs_trans_reserve(tp, 0,
1340 XFS_ICHANGE_LOG_RES(mp),
1341 0, 0, 0))) {
1342 xfs_trans_cancel(tp, 0);
1343 return error;
1344 }
1345
1346 ip = mp->m_rootip;
1347 xfs_ilock(ip, XFS_ILOCK_EXCL);
1348
1349 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
1350 xfs_trans_ihold(tp, ip);
1351 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Eric Sandeen1c72bf92007-05-08 13:48:42 +10001352 error = xfs_trans_commit(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1354 xfs_log_force(mp, (xfs_lsn_t)0, log_flags);
1355 }
1356
1357 /*
1358 * When shutting down, we need to insure that the AIL is pushed
1359 * to disk or the filesystem can appear corrupt from the PROM.
1360 */
1361 if ((flags & (SYNC_CLOSE|SYNC_WAIT)) == (SYNC_CLOSE|SYNC_WAIT)) {
1362 XFS_bflush(mp->m_ddev_targp);
1363 if (mp->m_rtdev_targp) {
1364 XFS_bflush(mp->m_rtdev_targp);
1365 }
1366 }
1367
1368 return XFS_ERROR(last_error);
1369}