blob: 7094caff13cff35acb33f8f2359632ed3f6c3a35 [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"
46#include "xfs_refcache.h"
47#include "xfs_buf_item.h"
Nathan Scotta844f452005-11-02 14:38:42 +110048#include "xfs_log_priv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include "xfs_dir2_trace.h"
Nathan Scotta844f452005-11-02 14:38:42 +110050#include "xfs_extfree_item.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include "xfs_acl.h"
52#include "xfs_attr.h"
53#include "xfs_clnt.h"
David Chinner2a82b8b2007-07-11 11:09:12 +100054#include "xfs_mru_cache.h"
55#include "xfs_filestream.h"
Christoph Hellwige13a73f2006-01-11 15:30:08 +110056#include "xfs_fsops.h"
Christoph Hellwig739bfb22007-08-29 10:58:01 +100057#include "xfs_vnodeops.h"
Christoph Hellwig48c872a9f2007-08-30 17:20:31 +100058#include "xfs_vfsops.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 */
72 xfs_bmap_free_item_zone = kmem_zone_init(sizeof(xfs_bmap_free_item_t),
73 "xfs_bmap_free_item");
74 xfs_btree_cur_zone = kmem_zone_init(sizeof(xfs_btree_cur_t),
75 "xfs_btree_cur");
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 xfs_trans_zone = kmem_zone_init(sizeof(xfs_trans_t), "xfs_trans");
77 xfs_da_state_zone =
78 kmem_zone_init(sizeof(xfs_da_state_t), "xfs_da_state");
79 xfs_dabuf_zone = kmem_zone_init(sizeof(xfs_dabuf_t), "xfs_dabuf");
Nathan Scott87582802006-03-14 13:18:19 +110080 xfs_ifork_zone = kmem_zone_init(sizeof(xfs_ifork_t), "xfs_ifork");
81 xfs_acl_zone_init(xfs_acl_zone, "xfs_acl");
David Chinner2a82b8b2007-07-11 11:09:12 +100082 xfs_mru_cache_init();
83 xfs_filestream_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85 /*
86 * The size of the zone allocated buf log item is the maximum
87 * size possible under XFS. This wastes a little bit of memory,
88 * but it is much faster.
89 */
90 xfs_buf_item_zone =
91 kmem_zone_init((sizeof(xfs_buf_log_item_t) +
92 (((XFS_MAX_BLOCKSIZE / XFS_BLI_CHUNK) /
93 NBWORD) * sizeof(int))),
94 "xfs_buf_item");
Nathan Scott87582802006-03-14 13:18:19 +110095 xfs_efd_zone =
96 kmem_zone_init((sizeof(xfs_efd_log_item_t) +
97 ((XFS_EFD_MAX_FAST_EXTENTS - 1) *
98 sizeof(xfs_extent_t))),
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 "xfs_efd_item");
Nathan Scott87582802006-03-14 13:18:19 +1100100 xfs_efi_zone =
101 kmem_zone_init((sizeof(xfs_efi_log_item_t) +
102 ((XFS_EFI_MAX_FAST_EXTENTS - 1) *
103 sizeof(xfs_extent_t))),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 "xfs_efi_item");
Nathan Scott87582802006-03-14 13:18:19 +1100105
106 /*
107 * These zones warrant special memory allocator hints
108 */
109 xfs_inode_zone =
110 kmem_zone_init_flags(sizeof(xfs_inode_t), "xfs_inode",
111 KM_ZONE_HWALIGN | KM_ZONE_RECLAIM |
112 KM_ZONE_SPREAD, NULL);
113 xfs_ili_zone =
114 kmem_zone_init_flags(sizeof(xfs_inode_log_item_t), "xfs_ili",
115 KM_ZONE_SPREAD, NULL);
David Chinnerda353b02007-08-28 14:00:13 +1000116 xfs_icluster_zone =
117 kmem_zone_init_flags(sizeof(xfs_icluster_t), "xfs_icluster",
Nathan Scott87582802006-03-14 13:18:19 +1100118 KM_ZONE_SPREAD, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
120 /*
121 * Allocate global trace buffers.
122 */
123#ifdef XFS_ALLOC_TRACE
124 xfs_alloc_trace_buf = ktrace_alloc(XFS_ALLOC_TRACE_SIZE, KM_SLEEP);
125#endif
126#ifdef XFS_BMAP_TRACE
127 xfs_bmap_trace_buf = ktrace_alloc(XFS_BMAP_TRACE_SIZE, KM_SLEEP);
128#endif
129#ifdef XFS_BMBT_TRACE
130 xfs_bmbt_trace_buf = ktrace_alloc(XFS_BMBT_TRACE_SIZE, KM_SLEEP);
131#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132#ifdef XFS_ATTR_TRACE
133 xfs_attr_trace_buf = ktrace_alloc(XFS_ATTR_TRACE_SIZE, KM_SLEEP);
134#endif
135#ifdef XFS_DIR2_TRACE
136 xfs_dir2_trace_buf = ktrace_alloc(XFS_DIR2_GTRACE_SIZE, KM_SLEEP);
137#endif
138
139 xfs_dir_startup();
140
141#if (defined(DEBUG) || defined(INDUCE_IO_ERROR))
142 xfs_error_test_init();
143#endif /* DEBUG || INDUCE_IO_ERROR */
144
145 xfs_init_procfs();
146 xfs_sysctl_register();
147 return 0;
148}
149
Lachlan McIlroyde2eeea2008-02-06 13:37:56 +1100150void __exit
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151xfs_cleanup(void)
152{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 extern kmem_zone_t *xfs_inode_zone;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 extern kmem_zone_t *xfs_efd_zone;
155 extern kmem_zone_t *xfs_efi_zone;
David Chinnerda353b02007-08-28 14:00:13 +1000156 extern kmem_zone_t *xfs_icluster_zone;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
158 xfs_cleanup_procfs();
159 xfs_sysctl_unregister();
160 xfs_refcache_destroy();
David Chinner2a82b8b2007-07-11 11:09:12 +1000161 xfs_filestream_uninit();
162 xfs_mru_cache_uninit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 xfs_acl_zone_destroy(xfs_acl_zone);
164
165#ifdef XFS_DIR2_TRACE
166 ktrace_free(xfs_dir2_trace_buf);
167#endif
168#ifdef XFS_ATTR_TRACE
169 ktrace_free(xfs_attr_trace_buf);
170#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171#ifdef XFS_BMBT_TRACE
172 ktrace_free(xfs_bmbt_trace_buf);
173#endif
174#ifdef XFS_BMAP_TRACE
175 ktrace_free(xfs_bmap_trace_buf);
176#endif
177#ifdef XFS_ALLOC_TRACE
178 ktrace_free(xfs_alloc_trace_buf);
179#endif
180
Nathan Scott3758dee2006-03-22 12:47:28 +1100181 kmem_zone_destroy(xfs_bmap_free_item_zone);
182 kmem_zone_destroy(xfs_btree_cur_zone);
183 kmem_zone_destroy(xfs_inode_zone);
184 kmem_zone_destroy(xfs_trans_zone);
185 kmem_zone_destroy(xfs_da_state_zone);
186 kmem_zone_destroy(xfs_dabuf_zone);
187 kmem_zone_destroy(xfs_buf_item_zone);
188 kmem_zone_destroy(xfs_efd_zone);
189 kmem_zone_destroy(xfs_efi_zone);
190 kmem_zone_destroy(xfs_ifork_zone);
191 kmem_zone_destroy(xfs_ili_zone);
David Chinnerda353b02007-08-28 14:00:13 +1000192 kmem_zone_destroy(xfs_icluster_zone);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193}
194
195/*
196 * xfs_start_flags
197 *
198 * This function fills in xfs_mount_t fields based on mount args.
199 * Note: the superblock has _not_ yet been read in.
200 */
201STATIC int
202xfs_start_flags(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 struct xfs_mount_args *ap,
204 struct xfs_mount *mp)
205{
206 /* Values are in BBs */
207 if ((ap->flags & XFSMNT_NOALIGN) != XFSMNT_NOALIGN) {
208 /*
209 * At this point the superblock has not been read
210 * in, therefore we do not know the block size.
211 * Before the mount call ends we will convert
212 * these to FSBs.
213 */
214 mp->m_dalign = ap->sunit;
215 mp->m_swidth = ap->swidth;
216 }
217
218 if (ap->logbufs != -1 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 ap->logbufs != 0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 (ap->logbufs < XLOG_MIN_ICLOGS ||
221 ap->logbufs > XLOG_MAX_ICLOGS)) {
222 cmn_err(CE_WARN,
223 "XFS: invalid logbufs value: %d [not %d-%d]",
224 ap->logbufs, XLOG_MIN_ICLOGS, XLOG_MAX_ICLOGS);
225 return XFS_ERROR(EINVAL);
226 }
227 mp->m_logbufs = ap->logbufs;
228 if (ap->logbufsize != -1 &&
Nathan Scottcfcbbbd2005-11-02 15:12:04 +1100229 ap->logbufsize != 0 &&
Eric Sandeendcb3b832007-08-16 16:25:33 +1000230 (ap->logbufsize < XLOG_MIN_RECORD_BSIZE ||
231 ap->logbufsize > XLOG_MAX_RECORD_BSIZE ||
232 !is_power_of_2(ap->logbufsize))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 cmn_err(CE_WARN,
234 "XFS: invalid logbufsize: %d [not 16k,32k,64k,128k or 256k]",
235 ap->logbufsize);
236 return XFS_ERROR(EINVAL);
237 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 mp->m_logbsize = ap->logbufsize;
239 mp->m_fsname_len = strlen(ap->fsname) + 1;
240 mp->m_fsname = kmem_alloc(mp->m_fsname_len, KM_SLEEP);
241 strcpy(mp->m_fsname, ap->fsname);
Nathan Scottfc1f8c12005-11-02 11:44:33 +1100242 if (ap->rtname[0]) {
243 mp->m_rtname = kmem_alloc(strlen(ap->rtname) + 1, KM_SLEEP);
244 strcpy(mp->m_rtname, ap->rtname);
245 }
246 if (ap->logname[0]) {
247 mp->m_logname = kmem_alloc(strlen(ap->logname) + 1, KM_SLEEP);
248 strcpy(mp->m_logname, ap->logname);
249 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
251 if (ap->flags & XFSMNT_WSYNC)
252 mp->m_flags |= XFS_MOUNT_WSYNC;
253#if XFS_BIG_INUMS
254 if (ap->flags & XFSMNT_INO64) {
255 mp->m_flags |= XFS_MOUNT_INO64;
256 mp->m_inoadd = XFS_INO64_OFFSET;
257 }
258#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 if (ap->flags & XFSMNT_RETERR)
260 mp->m_flags |= XFS_MOUNT_RETERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 if (ap->flags & XFSMNT_NOALIGN)
262 mp->m_flags |= XFS_MOUNT_NOALIGN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 if (ap->flags & XFSMNT_SWALLOC)
264 mp->m_flags |= XFS_MOUNT_SWALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 if (ap->flags & XFSMNT_OSYNCISOSYNC)
266 mp->m_flags |= XFS_MOUNT_OSYNCISOSYNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 if (ap->flags & XFSMNT_32BITINODES)
Nathan Scottc11e2c32005-11-02 15:11:45 +1100268 mp->m_flags |= XFS_MOUNT_32BITINODES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
270 if (ap->flags & XFSMNT_IOSIZE) {
271 if (ap->iosizelog > XFS_MAX_IO_LOG ||
272 ap->iosizelog < XFS_MIN_IO_LOG) {
273 cmn_err(CE_WARN,
274 "XFS: invalid log iosize: %d [not %d-%d]",
275 ap->iosizelog, XFS_MIN_IO_LOG,
276 XFS_MAX_IO_LOG);
277 return XFS_ERROR(EINVAL);
278 }
279
280 mp->m_flags |= XFS_MOUNT_DFLT_IOSIZE;
281 mp->m_readio_log = mp->m_writeio_log = ap->iosizelog;
282 }
283
Josef Jeff Sipek1bd960e2008-02-29 13:58:40 +1100284 if (ap->flags & XFSMNT_IKEEP)
285 mp->m_flags |= XFS_MOUNT_IKEEP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 if (ap->flags & XFSMNT_DIRSYNC)
287 mp->m_flags |= XFS_MOUNT_DIRSYNC;
Nathan Scott13059ff2006-01-11 15:32:01 +1100288 if (ap->flags & XFSMNT_ATTR2)
289 mp->m_flags |= XFS_MOUNT_ATTR2;
David Chinnere8c8b3a2005-11-02 10:33:05 +1100290
Nathan Scotte718eeb42005-11-02 15:09:22 +1100291 if (ap->flags2 & XFSMNT2_COMPAT_IOSIZE)
292 mp->m_flags |= XFS_MOUNT_COMPAT_IOSIZE;
293
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 /*
295 * no recovery flag requires a read-only mount
296 */
297 if (ap->flags & XFSMNT_NORECOVERY) {
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000298 if (!(mp->m_flags & XFS_MOUNT_RDONLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 cmn_err(CE_WARN,
300 "XFS: tried to mount a FS read-write without recovery!");
301 return XFS_ERROR(EINVAL);
302 }
303 mp->m_flags |= XFS_MOUNT_NORECOVERY;
304 }
305
306 if (ap->flags & XFSMNT_NOUUID)
307 mp->m_flags |= XFS_MOUNT_NOUUID;
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100308 if (ap->flags & XFSMNT_BARRIER)
309 mp->m_flags |= XFS_MOUNT_BARRIER;
Christoph Hellwig4ef19dd2006-01-11 15:27:18 +1100310 else
311 mp->m_flags &= ~XFS_MOUNT_BARRIER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
David Chinner2a82b8b2007-07-11 11:09:12 +1000313 if (ap->flags2 & XFSMNT2_FILESTREAMS)
314 mp->m_flags |= XFS_MOUNT_FILESTREAMS;
315
Christoph Hellwig293688e2007-08-29 11:59:36 +1000316 if (ap->flags & XFSMNT_DMAPI)
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000317 mp->m_flags |= XFS_MOUNT_DMAPI;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 return 0;
319}
320
321/*
322 * This function fills in xfs_mount_t fields based on mount args.
323 * Note: the superblock _has_ now been read in.
324 */
325STATIC int
326xfs_finish_flags(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 struct xfs_mount_args *ap,
328 struct xfs_mount *mp)
329{
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000330 int ronly = (mp->m_flags & XFS_MOUNT_RDONLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
332 /* Fail a mount where the logbuf is smaller then the log stripe */
Eric Sandeen62118702008-03-06 13:44:28 +1100333 if (xfs_sb_version_haslogv2(&mp->m_sb)) {
Nathan Scottc2cd2552006-01-11 15:35:32 +1100334 if ((ap->logbufsize <= 0) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 (mp->m_sb.sb_logsunit > XLOG_BIG_RECORD_BSIZE)) {
336 mp->m_logbsize = mp->m_sb.sb_logsunit;
Nathan Scottc2cd2552006-01-11 15:35:32 +1100337 } else if (ap->logbufsize > 0 &&
338 ap->logbufsize < mp->m_sb.sb_logsunit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 cmn_err(CE_WARN,
340 "XFS: logbuf size must be greater than or equal to log stripe size");
341 return XFS_ERROR(EINVAL);
342 }
343 } else {
344 /* Fail a mount if the logbuf is larger than 32K */
345 if (ap->logbufsize > XLOG_BIG_RECORD_BSIZE) {
346 cmn_err(CE_WARN,
347 "XFS: logbuf size for version 1 logs must be 16K or 32K");
348 return XFS_ERROR(EINVAL);
349 }
350 }
351
Eric Sandeen62118702008-03-06 13:44:28 +1100352 if (xfs_sb_version_hasattr2(&mp->m_sb))
Nathan Scott13059ff2006-01-11 15:32:01 +1100353 mp->m_flags |= XFS_MOUNT_ATTR2;
Nathan Scott13059ff2006-01-11 15:32:01 +1100354
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 /*
356 * prohibit r/w mounts of read-only filesystems
357 */
358 if ((mp->m_sb.sb_flags & XFS_SBF_READONLY) && !ronly) {
359 cmn_err(CE_WARN,
360 "XFS: cannot mount a read-only filesystem as read-write");
361 return XFS_ERROR(EROFS);
362 }
363
364 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 * check for shared mount.
366 */
367 if (ap->flags & XFSMNT_SHARED) {
Eric Sandeen62118702008-03-06 13:44:28 +1100368 if (!xfs_sb_version_hasshared(&mp->m_sb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 return XFS_ERROR(EINVAL);
370
371 /*
372 * For IRIX 6.5, shared mounts must have the shared
373 * version bit set, have the persistent readonly
374 * field set, must be version 0 and can only be mounted
375 * read-only.
376 */
377 if (!ronly || !(mp->m_sb.sb_flags & XFS_SBF_READONLY) ||
378 (mp->m_sb.sb_shared_vn != 0))
379 return XFS_ERROR(EINVAL);
380
381 mp->m_flags |= XFS_MOUNT_SHARED;
382
383 /*
384 * Shared XFS V0 can't deal with DMI. Return EINVAL.
385 */
386 if (mp->m_sb.sb_shared_vn == 0 && (ap->flags & XFSMNT_DMAPI))
387 return XFS_ERROR(EINVAL);
388 }
389
Christoph Hellwigb09cc772007-08-30 17:19:57 +1000390 if (ap->flags & XFSMNT_UQUOTA) {
391 mp->m_qflags |= (XFS_UQUOTA_ACCT | XFS_UQUOTA_ACTIVE);
392 if (ap->flags & XFSMNT_UQUOTAENF)
393 mp->m_qflags |= XFS_UQUOTA_ENFD;
394 }
395
396 if (ap->flags & XFSMNT_GQUOTA) {
397 mp->m_qflags |= (XFS_GQUOTA_ACCT | XFS_GQUOTA_ACTIVE);
398 if (ap->flags & XFSMNT_GQUOTAENF)
399 mp->m_qflags |= XFS_OQUOTA_ENFD;
400 } else if (ap->flags & XFSMNT_PQUOTA) {
401 mp->m_qflags |= (XFS_PQUOTA_ACCT | XFS_PQUOTA_ACTIVE);
402 if (ap->flags & XFSMNT_PQUOTAENF)
403 mp->m_qflags |= XFS_OQUOTA_ENFD;
404 }
405
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 return 0;
407}
408
409/*
410 * xfs_mount
411 *
412 * The file system configurations are:
413 * (1) device (partition) with data and internal log
414 * (2) logical volume with data and log subvolumes.
415 * (3) logical volume with data, log, and realtime subvolumes.
416 *
417 * We only have to handle opening the log and realtime volumes here if
418 * they are present. The data subvolume has already been opened by
419 * get_sb_bdev() and is stored in vfsp->vfs_super->s_bdev.
420 */
Christoph Hellwig48c872a9f2007-08-30 17:20:31 +1000421int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422xfs_mount(
Christoph Hellwig48c872a9f2007-08-30 17:20:31 +1000423 struct xfs_mount *mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 struct xfs_mount_args *args,
425 cred_t *credp)
426{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 struct block_device *ddev, *logdev, *rtdev;
428 int flags = 0, error;
429
Christoph Hellwigb267ce92007-08-30 17:21:30 +1000430 ddev = mp->m_super->s_bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 logdev = rtdev = NULL;
432
Christoph Hellwig293688e2007-08-29 11:59:36 +1000433 error = xfs_dmops_get(mp, args);
434 if (error)
435 return error;
Christoph Hellwigb09cc772007-08-30 17:19:57 +1000436 error = xfs_qmops_get(mp, args);
437 if (error)
438 return error;
Christoph Hellwig293688e2007-08-29 11:59:36 +1000439
Nathan Scott764d1f82006-03-31 13:04:17 +1000440 if (args->flags & XFSMNT_QUIET)
441 flags |= XFS_MFSI_QUIET;
442
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 /*
444 * Open real time and log devices - order is important.
445 */
446 if (args->logname[0]) {
447 error = xfs_blkdev_get(mp, args->logname, &logdev);
448 if (error)
449 return error;
450 }
451 if (args->rtname[0]) {
452 error = xfs_blkdev_get(mp, args->rtname, &rtdev);
453 if (error) {
454 xfs_blkdev_put(logdev);
455 return error;
456 }
457
458 if (rtdev == ddev || rtdev == logdev) {
459 cmn_err(CE_WARN,
460 "XFS: Cannot mount filesystem with identical rtdev and ddev/logdev.");
461 xfs_blkdev_put(logdev);
462 xfs_blkdev_put(rtdev);
463 return EINVAL;
464 }
465 }
466
467 /*
468 * Setup xfs_mount buffer target pointers
469 */
470 error = ENOMEM;
471 mp->m_ddev_targp = xfs_alloc_buftarg(ddev, 0);
472 if (!mp->m_ddev_targp) {
473 xfs_blkdev_put(logdev);
474 xfs_blkdev_put(rtdev);
475 return error;
476 }
477 if (rtdev) {
478 mp->m_rtdev_targp = xfs_alloc_buftarg(rtdev, 1);
Jesper Juhl49ee6c92007-08-16 16:25:42 +1000479 if (!mp->m_rtdev_targp) {
480 xfs_blkdev_put(logdev);
481 xfs_blkdev_put(rtdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 goto error0;
Jesper Juhl49ee6c92007-08-16 16:25:42 +1000483 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 }
485 mp->m_logdev_targp = (logdev && logdev != ddev) ?
486 xfs_alloc_buftarg(logdev, 1) : mp->m_ddev_targp;
Jesper Juhl49ee6c92007-08-16 16:25:42 +1000487 if (!mp->m_logdev_targp) {
488 xfs_blkdev_put(logdev);
489 xfs_blkdev_put(rtdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 goto error0;
Jesper Juhl49ee6c92007-08-16 16:25:42 +1000491 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
493 /*
494 * Setup flags based on mount(2) options and then the superblock
495 */
Christoph Hellwigb267ce92007-08-30 17:21:30 +1000496 error = xfs_start_flags(args, mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 if (error)
498 goto error1;
Nathan Scott764d1f82006-03-31 13:04:17 +1000499 error = xfs_readsb(mp, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 if (error)
501 goto error1;
Christoph Hellwigb267ce92007-08-30 17:21:30 +1000502 error = xfs_finish_flags(args, mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 if (error)
504 goto error2;
505
506 /*
507 * Setup xfs_mount buffer target pointers based on superblock
508 */
509 error = xfs_setsize_buftarg(mp->m_ddev_targp, mp->m_sb.sb_blocksize,
510 mp->m_sb.sb_sectsize);
511 if (!error && logdev && logdev != ddev) {
512 unsigned int log_sector_size = BBSIZE;
513
Eric Sandeen62118702008-03-06 13:44:28 +1100514 if (xfs_sb_version_hassector(&mp->m_sb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 log_sector_size = mp->m_sb.sb_logsectsize;
516 error = xfs_setsize_buftarg(mp->m_logdev_targp,
517 mp->m_sb.sb_blocksize,
518 log_sector_size);
519 }
520 if (!error && rtdev)
521 error = xfs_setsize_buftarg(mp->m_rtdev_targp,
522 mp->m_sb.sb_blocksize,
523 mp->m_sb.sb_sectsize);
524 if (error)
525 goto error2;
526
Nathan Scottb2ea4012006-07-28 17:05:13 +1000527 if (mp->m_flags & XFS_MOUNT_BARRIER)
Christoph Hellwigc7d437d2006-01-11 15:28:56 +1100528 xfs_mountfs_check_barriers(mp);
529
David Chinner2a82b8b2007-07-11 11:09:12 +1000530 if ((error = xfs_filestream_mount(mp)))
531 goto error2;
532
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000533 error = xfs_mountfs(mp, flags);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100534 if (error)
535 goto error2;
536
Christoph Hellwig293688e2007-08-29 11:59:36 +1000537 XFS_SEND_MOUNT(mp, DM_RIGHT_NULL, args->mtpt, args->fsname);
538
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100539 return 0;
540
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541error2:
542 if (mp->m_sb_bp)
543 xfs_freesb(mp);
544error1:
545 xfs_binval(mp->m_ddev_targp);
546 if (logdev && logdev != ddev)
547 xfs_binval(mp->m_logdev_targp);
548 if (rtdev)
549 xfs_binval(mp->m_rtdev_targp);
550error0:
551 xfs_unmountfs_close(mp, credp);
Christoph Hellwigb09cc772007-08-30 17:19:57 +1000552 xfs_qmops_put(mp);
Christoph Hellwig293688e2007-08-29 11:59:36 +1000553 xfs_dmops_put(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 return error;
555}
556
Christoph Hellwig48c872a9f2007-08-30 17:20:31 +1000557int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558xfs_unmount(
Christoph Hellwig48c872a9f2007-08-30 17:20:31 +1000559 xfs_mount_t *mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 int flags,
561 cred_t *credp)
562{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 xfs_inode_t *rip;
Nathan Scott67fcaa72006-06-09 17:00:52 +1000564 bhv_vnode_t *rvp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 int unmount_event_wanted = 0;
566 int unmount_event_flags = 0;
567 int xfs_unmountfs_needed = 0;
568 int error;
569
570 rip = mp->m_rootip;
571 rvp = XFS_ITOV(rip);
572
Eric Sandeen948c6d4f2007-08-23 16:19:57 +1000573#ifdef HAVE_DMAPI
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000574 if (mp->m_flags & XFS_MOUNT_DMAPI) {
Christoph Hellwigb267ce92007-08-30 17:21:30 +1000575 error = XFS_SEND_PREUNMOUNT(mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 rvp, DM_RIGHT_NULL, rvp, DM_RIGHT_NULL,
577 NULL, NULL, 0, 0,
578 (mp->m_dmevmask & (1<<DM_EVENT_PREUNMOUNT))?
579 0:DM_FLAGS_UNWANTED);
580 if (error)
581 return XFS_ERROR(error);
582 unmount_event_wanted = 1;
583 unmount_event_flags = (mp->m_dmevmask & (1<<DM_EVENT_UNMOUNT))?
584 0 : DM_FLAGS_UNWANTED;
585 }
Eric Sandeen948c6d4f2007-08-23 16:19:57 +1000586#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 /*
588 * First blow any referenced inode from this file system
589 * out of the reference cache, and delete the timer.
590 */
591 xfs_refcache_purge_mp(mp);
592
David Chinner2a82b8b2007-07-11 11:09:12 +1000593 /*
594 * Blow away any referenced inode in the filestreams cache.
595 * This can and will cause log traffic as inodes go inactive
596 * here.
597 */
598 xfs_filestream_unmount(mp);
599
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 XFS_bflush(mp->m_ddev_targp);
601 error = xfs_unmount_flush(mp, 0);
602 if (error)
603 goto out;
604
605 ASSERT(vn_count(rvp) == 1);
606
607 /*
608 * Drop the reference count
609 */
610 VN_RELE(rvp);
611
612 /*
613 * If we're forcing a shutdown, typically because of a media error,
614 * we want to make sure we invalidate dirty pages that belong to
615 * referenced vnodes as well.
616 */
617 if (XFS_FORCED_SHUTDOWN(mp)) {
Christoph Hellwig48c872a9f2007-08-30 17:20:31 +1000618 error = xfs_sync(mp, SYNC_WAIT | SYNC_CLOSE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 ASSERT(error != EFSCORRUPTED);
620 }
621 xfs_unmountfs_needed = 1;
622
623out:
624 /* Send DMAPI event, if required.
625 * Then do xfs_unmountfs() if needed.
626 * Then return error (or zero).
627 */
628 if (unmount_event_wanted) {
629 /* Note: mp structure must still exist for
630 * XFS_SEND_UNMOUNT() call.
631 */
Christoph Hellwigb267ce92007-08-30 17:21:30 +1000632 XFS_SEND_UNMOUNT(mp, error == 0 ? rvp : NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 DM_RIGHT_NULL, 0, error, unmount_event_flags);
634 }
635 if (xfs_unmountfs_needed) {
636 /*
637 * Call common unmount function to flush to disk
638 * and free the super block buffer & mount structures.
639 */
640 xfs_unmountfs(mp, credp);
Christoph Hellwigb09cc772007-08-30 17:19:57 +1000641 xfs_qmops_put(mp);
Christoph Hellwig293688e2007-08-29 11:59:36 +1000642 xfs_dmops_put(mp);
Christoph Hellwigf541d272007-08-29 11:53:22 +1000643 kmem_free(mp, sizeof(xfs_mount_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 }
645
646 return XFS_ERROR(error);
647}
648
Christoph Hellwigf898d6c2005-06-21 15:40:48 +1000649STATIC int
650xfs_quiesce_fs(
651 xfs_mount_t *mp)
652{
653 int count = 0, pincount;
Nathan Scott3758dee2006-03-22 12:47:28 +1100654
Christoph Hellwigf898d6c2005-06-21 15:40:48 +1000655 xfs_refcache_purge_mp(mp);
656 xfs_flush_buftarg(mp->m_ddev_targp, 0);
657 xfs_finish_reclaim_all(mp, 0);
658
659 /* This loop must run at least twice.
660 * The first instance of the loop will flush
661 * most meta data but that will generate more
662 * meta data (typically directory updates).
663 * Which then must be flushed and logged before
664 * we can write the unmount record.
Nathan Scott3758dee2006-03-22 12:47:28 +1100665 */
Christoph Hellwigf898d6c2005-06-21 15:40:48 +1000666 do {
David Chinner516b2e72007-06-18 16:50:48 +1000667 xfs_syncsub(mp, SYNC_INODE_QUIESCE, NULL);
Christoph Hellwigf898d6c2005-06-21 15:40:48 +1000668 pincount = xfs_flush_buftarg(mp->m_ddev_targp, 1);
669 if (!pincount) {
670 delay(50);
671 count++;
672 }
673 } while (count < 2);
674
675 return 0;
676}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
David Chinner516b2e72007-06-18 16:50:48 +1000678/*
679 * Second stage of a quiesce. The data is already synced, now we have to take
680 * care of the metadata. New transactions are already blocked, so we need to
681 * wait for any remaining transactions to drain out before proceding.
682 */
Christoph Hellwig9909c4a2007-10-11 18:11:14 +1000683void
David Chinner516b2e72007-06-18 16:50:48 +1000684xfs_attr_quiesce(
685 xfs_mount_t *mp)
686{
687 /* wait for all modifications to complete */
688 while (atomic_read(&mp->m_active_trans) > 0)
689 delay(100);
690
691 /* flush inodes and push all remaining buffers out to disk */
692 xfs_quiesce_fs(mp);
693
694 ASSERT_ALWAYS(atomic_read(&mp->m_active_trans) == 0);
695
696 /* Push the superblock and write an unmount record */
697 xfs_log_sbcount(mp, 1);
698 xfs_log_unmount_write(mp);
699 xfs_unmountfs_writesb(mp);
700}
701
Christoph Hellwig48c872a9f2007-08-30 17:20:31 +1000702int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703xfs_mntupdate(
Christoph Hellwig48c872a9f2007-08-30 17:20:31 +1000704 struct xfs_mount *mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 int *flags,
706 struct xfs_mount_args *args)
707{
Nathan Scottd08d3892006-05-08 19:51:28 +1000708 if (!(*flags & MS_RDONLY)) { /* rw/ro -> rw */
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000709 if (mp->m_flags & XFS_MOUNT_RDONLY)
710 mp->m_flags &= ~XFS_MOUNT_RDONLY;
Nathan Scottd08d3892006-05-08 19:51:28 +1000711 if (args->flags & XFSMNT_BARRIER) {
712 mp->m_flags |= XFS_MOUNT_BARRIER;
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100713 xfs_mountfs_check_barriers(mp);
Nathan Scottd08d3892006-05-08 19:51:28 +1000714 } else {
715 mp->m_flags &= ~XFS_MOUNT_BARRIER;
716 }
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000717 } else if (!(mp->m_flags & XFS_MOUNT_RDONLY)) { /* rw -> ro */
David Chinner2a82b8b2007-07-11 11:09:12 +1000718 xfs_filestream_flush(mp);
Christoph Hellwig745f6912007-08-30 17:20:39 +1000719 xfs_sync(mp, SYNC_DATA_QUIESCE);
David Chinner516b2e72007-06-18 16:50:48 +1000720 xfs_attr_quiesce(mp);
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000721 mp->m_flags |= XFS_MOUNT_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 return 0;
724}
725
726/*
727 * xfs_unmount_flush implements a set of flush operation on special
728 * inodes, which are needed as a separate set of operations so that
729 * they can be called as part of relocation process.
730 */
731int
732xfs_unmount_flush(
733 xfs_mount_t *mp, /* Mount structure we are getting
734 rid of. */
735 int relocation) /* Called from vfs relocation. */
736{
737 xfs_inode_t *rip = mp->m_rootip;
738 xfs_inode_t *rbmip;
739 xfs_inode_t *rsumip = NULL;
Nathan Scott67fcaa72006-06-09 17:00:52 +1000740 bhv_vnode_t *rvp = XFS_ITOV(rip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 int error;
742
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +1000743 xfs_ilock(rip, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 xfs_iflock(rip);
745
746 /*
747 * Flush out the real time inodes.
748 */
749 if ((rbmip = mp->m_rbmip) != NULL) {
750 xfs_ilock(rbmip, XFS_ILOCK_EXCL);
751 xfs_iflock(rbmip);
752 error = xfs_iflush(rbmip, XFS_IFLUSH_SYNC);
753 xfs_iunlock(rbmip, XFS_ILOCK_EXCL);
754
755 if (error == EFSCORRUPTED)
756 goto fscorrupt_out;
757
758 ASSERT(vn_count(XFS_ITOV(rbmip)) == 1);
759
760 rsumip = mp->m_rsumip;
761 xfs_ilock(rsumip, XFS_ILOCK_EXCL);
762 xfs_iflock(rsumip);
763 error = xfs_iflush(rsumip, XFS_IFLUSH_SYNC);
764 xfs_iunlock(rsumip, XFS_ILOCK_EXCL);
765
766 if (error == EFSCORRUPTED)
767 goto fscorrupt_out;
768
769 ASSERT(vn_count(XFS_ITOV(rsumip)) == 1);
770 }
771
772 /*
773 * Synchronously flush root inode to disk
774 */
775 error = xfs_iflush(rip, XFS_IFLUSH_SYNC);
776 if (error == EFSCORRUPTED)
777 goto fscorrupt_out2;
778
779 if (vn_count(rvp) != 1 && !relocation) {
780 xfs_iunlock(rip, XFS_ILOCK_EXCL);
781 return XFS_ERROR(EBUSY);
782 }
783
784 /*
785 * Release dquot that rootinode, rbmino and rsumino might be holding,
786 * flush and purge the quota inodes.
787 */
788 error = XFS_QM_UNMOUNT(mp);
789 if (error == EFSCORRUPTED)
790 goto fscorrupt_out2;
791
792 if (rbmip) {
793 VN_RELE(XFS_ITOV(rbmip));
794 VN_RELE(XFS_ITOV(rsumip));
795 }
796
797 xfs_iunlock(rip, XFS_ILOCK_EXCL);
798 return 0;
799
800fscorrupt_out:
801 xfs_ifunlock(rip);
802
803fscorrupt_out2:
804 xfs_iunlock(rip, XFS_ILOCK_EXCL);
805
806 return XFS_ERROR(EFSCORRUPTED);
807}
808
809/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 * xfs_sync flushes any pending I/O to file system vfsp.
811 *
812 * This routine is called by vfs_sync() to make sure that things make it
813 * out to disk eventually, on sync() system calls to flush out everything,
814 * and when the file system is unmounted. For the vfs_sync() case, all
815 * we really need to do is sync out the log to make all of our meta-data
816 * updates permanent (except for timestamps). For calls from pflushd(),
817 * dirty pages are kept moving by calling pdflush() on the inodes
818 * containing them. We also flush the inodes that we can lock without
819 * sleeping and the superblock if we can lock it without sleeping from
820 * vfs_sync() so that items at the tail of the log are always moving out.
821 *
822 * Flags:
823 * SYNC_BDFLUSH - We're being called from vfs_sync() so we don't want
824 * to sleep if we can help it. All we really need
825 * to do is ensure that the log is synced at least
826 * periodically. We also push the inodes and
827 * superblock if we can lock them without sleeping
828 * and they are not pinned.
829 * SYNC_ATTR - We need to flush the inodes. If SYNC_BDFLUSH is not
830 * set, then we really want to lock each inode and flush
831 * it.
832 * SYNC_WAIT - All the flushes that take place in this call should
833 * be synchronous.
834 * SYNC_DELWRI - This tells us to push dirty pages associated with
835 * inodes. SYNC_WAIT and SYNC_BDFLUSH are used to
836 * determine if they should be flushed sync, async, or
837 * delwri.
838 * SYNC_CLOSE - This flag is passed when the system is being
Nathan Scottc41564b2006-03-29 08:55:14 +1000839 * unmounted. We should sync and invalidate everything.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 * SYNC_FSDATA - This indicates that the caller would like to make
841 * sure the superblock is safe on disk. We can ensure
Nathan Scottc41564b2006-03-29 08:55:14 +1000842 * this by simply making sure the log gets flushed
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 * if SYNC_BDFLUSH is set, and by actually writing it
844 * out otherwise.
David Chinner3c0dc772007-02-10 18:37:22 +1100845 * SYNC_IOWAIT - The caller wants us to wait for all data I/O to complete
846 * before we return (including direct I/O). Forms the drain
847 * side of the write barrier needed to safely quiesce the
848 * filesystem.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 *
850 */
Christoph Hellwig48c872a9f2007-08-30 17:20:31 +1000851int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852xfs_sync(
Christoph Hellwig48c872a9f2007-08-30 17:20:31 +1000853 xfs_mount_t *mp,
854 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855{
Christoph Hellwigb09cc772007-08-30 17:19:57 +1000856 int error;
857
858 /*
859 * Get the Quota Manager to flush the dquots.
860 *
861 * If XFS quota support is not enabled or this filesystem
862 * instance does not use quotas XFS_QM_DQSYNC will always
863 * return zero.
864 */
865 error = XFS_QM_DQSYNC(mp, flags);
866 if (error) {
867 /*
868 * If we got an IO error, we will be shutting down.
869 * So, there's nothing more for us to do here.
870 */
871 ASSERT(error != EIO || XFS_FORCED_SHUTDOWN(mp));
872 if (XFS_FORCED_SHUTDOWN(mp))
873 return XFS_ERROR(error);
874 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
David Chinner2a82b8b2007-07-11 11:09:12 +1000876 if (flags & SYNC_IOWAIT)
877 xfs_filestream_flush(mp);
878
David Chinner28239452007-02-10 18:36:40 +1100879 return xfs_syncsub(mp, flags, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880}
881
882/*
883 * xfs sync routine for internal use
884 *
Nathan Scottb83bd132006-06-09 16:48:30 +1000885 * This routine supports all of the flags defined for the generic vfs_sync
Lachlan McIlroy1f9b3b62007-02-10 18:35:33 +1100886 * interface as explained above under xfs_sync.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 *
888 */
Nathan Scottee348072005-11-02 10:32:38 +1100889int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890xfs_sync_inodes(
891 xfs_mount_t *mp,
892 int flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 int *bypassed)
894{
895 xfs_inode_t *ip = NULL;
Nathan Scott67fcaa72006-06-09 17:00:52 +1000896 bhv_vnode_t *vp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 int error;
898 int last_error;
899 uint64_t fflag;
900 uint lock_flags;
901 uint base_lock_flags;
902 boolean_t mount_locked;
903 boolean_t vnode_refed;
904 int preempt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 xfs_iptr_t *ipointer;
906#ifdef DEBUG
907 boolean_t ipointer_in = B_FALSE;
908
909#define IPOINTER_SET ipointer_in = B_TRUE
910#define IPOINTER_CLR ipointer_in = B_FALSE
911#else
912#define IPOINTER_SET
913#define IPOINTER_CLR
914#endif
915
916
917/* Insert a marker record into the inode list after inode ip. The list
918 * must be locked when this is called. After the call the list will no
919 * longer be locked.
920 */
921#define IPOINTER_INSERT(ip, mp) { \
922 ASSERT(ipointer_in == B_FALSE); \
923 ipointer->ip_mnext = ip->i_mnext; \
924 ipointer->ip_mprev = ip; \
925 ip->i_mnext = (xfs_inode_t *)ipointer; \
926 ipointer->ip_mnext->i_mprev = (xfs_inode_t *)ipointer; \
927 preempt = 0; \
928 XFS_MOUNT_IUNLOCK(mp); \
929 mount_locked = B_FALSE; \
930 IPOINTER_SET; \
931 }
932
933/* Remove the marker from the inode list. If the marker was the only item
934 * in the list then there are no remaining inodes and we should zero out
935 * the whole list. If we are the current head of the list then move the head
936 * past us.
937 */
938#define IPOINTER_REMOVE(ip, mp) { \
939 ASSERT(ipointer_in == B_TRUE); \
940 if (ipointer->ip_mnext != (xfs_inode_t *)ipointer) { \
941 ip = ipointer->ip_mnext; \
942 ip->i_mprev = ipointer->ip_mprev; \
943 ipointer->ip_mprev->i_mnext = ip; \
944 if (mp->m_inodes == (xfs_inode_t *)ipointer) { \
945 mp->m_inodes = ip; \
946 } \
947 } else { \
948 ASSERT(mp->m_inodes == (xfs_inode_t *)ipointer); \
949 mp->m_inodes = NULL; \
950 ip = NULL; \
951 } \
952 IPOINTER_CLR; \
953 }
954
955#define XFS_PREEMPT_MASK 0x7f
956
Lachlan McIlroy44866d32007-09-14 15:21:08 +1000957 ASSERT(!(flags & SYNC_BDFLUSH));
958
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 if (bypassed)
960 *bypassed = 0;
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000961 if (mp->m_flags & XFS_MOUNT_RDONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 return 0;
963 error = 0;
964 last_error = 0;
965 preempt = 0;
966
967 /* Allocate a reference marker */
968 ipointer = (xfs_iptr_t *)kmem_zalloc(sizeof(xfs_iptr_t), KM_SLEEP);
969
970 fflag = XFS_B_ASYNC; /* default is don't wait */
Lachlan McIlroy44866d32007-09-14 15:21:08 +1000971 if (flags & SYNC_DELWRI)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 fflag = XFS_B_DELWRI;
973 if (flags & SYNC_WAIT)
974 fflag = 0; /* synchronous overrides all */
975
976 base_lock_flags = XFS_ILOCK_SHARED;
977 if (flags & (SYNC_DELWRI | SYNC_CLOSE)) {
978 /*
979 * We need the I/O lock if we're going to call any of
980 * the flush/inval routines.
981 */
982 base_lock_flags |= XFS_IOLOCK_SHARED;
983 }
984
985 XFS_MOUNT_ILOCK(mp);
986
987 ip = mp->m_inodes;
988
989 mount_locked = B_TRUE;
990 vnode_refed = B_FALSE;
991
992 IPOINTER_CLR;
993
994 do {
995 ASSERT(ipointer_in == B_FALSE);
996 ASSERT(vnode_refed == B_FALSE);
997
998 lock_flags = base_lock_flags;
999
1000 /*
1001 * There were no inodes in the list, just break out
1002 * of the loop.
1003 */
1004 if (ip == NULL) {
1005 break;
1006 }
1007
1008 /*
1009 * We found another sync thread marker - skip it
1010 */
1011 if (ip->i_mount == NULL) {
1012 ip = ip->i_mnext;
1013 continue;
1014 }
1015
1016 vp = XFS_ITOV_NULL(ip);
1017
1018 /*
1019 * If the vnode is gone then this is being torn down,
1020 * call reclaim if it is flushed, else let regular flush
1021 * code deal with it later in the loop.
1022 */
1023
1024 if (vp == NULL) {
1025 /* Skip ones already in reclaim */
1026 if (ip->i_flags & XFS_IRECLAIM) {
1027 ip = ip->i_mnext;
1028 continue;
1029 }
1030 if (xfs_ilock_nowait(ip, XFS_ILOCK_EXCL) == 0) {
1031 ip = ip->i_mnext;
1032 } else if ((xfs_ipincount(ip) == 0) &&
1033 xfs_iflock_nowait(ip)) {
1034 IPOINTER_INSERT(ip, mp);
1035
1036 xfs_finish_reclaim(ip, 1,
1037 XFS_IFLUSH_DELWRI_ELSE_ASYNC);
1038
1039 XFS_MOUNT_ILOCK(mp);
1040 mount_locked = B_TRUE;
1041 IPOINTER_REMOVE(ip, mp);
1042 } else {
1043 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1044 ip = ip->i_mnext;
1045 }
1046 continue;
1047 }
1048
1049 if (VN_BAD(vp)) {
1050 ip = ip->i_mnext;
1051 continue;
1052 }
1053
1054 if (XFS_FORCED_SHUTDOWN(mp) && !(flags & SYNC_CLOSE)) {
1055 XFS_MOUNT_IUNLOCK(mp);
1056 kmem_free(ipointer, sizeof(xfs_iptr_t));
1057 return 0;
1058 }
1059
1060 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 * Try to lock without sleeping. We're out of order with
1062 * the inode list lock here, so if we fail we need to drop
1063 * the mount lock and try again. If we're called from
1064 * bdflush() here, then don't bother.
1065 *
1066 * The inode lock here actually coordinates with the
1067 * almost spurious inode lock in xfs_ireclaim() to prevent
1068 * the vnode we handle here without a reference from
1069 * being freed while we reference it. If we lock the inode
1070 * while it's on the mount list here, then the spurious inode
1071 * lock in xfs_ireclaim() after the inode is pulled from
1072 * the mount list will sleep until we release it here.
1073 * This keeps the vnode from being freed while we reference
Christoph Hellwigcdb62682005-09-02 16:24:19 +10001074 * it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 */
1076 if (xfs_ilock_nowait(ip, lock_flags) == 0) {
Lachlan McIlroy44866d32007-09-14 15:21:08 +10001077 if (vp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 ip = ip->i_mnext;
1079 continue;
1080 }
1081
Christoph Hellwigcdb62682005-09-02 16:24:19 +10001082 vp = vn_grab(vp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 if (vp == NULL) {
Christoph Hellwigcdb62682005-09-02 16:24:19 +10001084 ip = ip->i_mnext;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 continue;
1086 }
1087
Christoph Hellwigcdb62682005-09-02 16:24:19 +10001088 IPOINTER_INSERT(ip, mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 xfs_ilock(ip, lock_flags);
1090
1091 ASSERT(vp == XFS_ITOV(ip));
1092 ASSERT(ip->i_mount == mp);
1093
1094 vnode_refed = B_TRUE;
1095 }
1096
1097 /* From here on in the loop we may have a marker record
1098 * in the inode list.
1099 */
1100
David Chinner40095b62007-05-14 18:24:09 +10001101 /*
1102 * If we have to flush data or wait for I/O completion
1103 * we need to drop the ilock that we currently hold.
1104 * If we need to drop the lock, insert a marker if we
1105 * have not already done so.
1106 */
1107 if ((flags & (SYNC_CLOSE|SYNC_IOWAIT)) ||
1108 ((flags & SYNC_DELWRI) && VN_DIRTY(vp))) {
1109 if (mount_locked) {
1110 IPOINTER_INSERT(ip, mp);
1111 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 xfs_iunlock(ip, XFS_ILOCK_SHARED);
1113
David Chinner40095b62007-05-14 18:24:09 +10001114 if (flags & SYNC_CLOSE) {
1115 /* Shutdown case. Flush and invalidate. */
1116 if (XFS_FORCED_SHUTDOWN(mp))
Christoph Hellwig739bfb22007-08-29 10:58:01 +10001117 xfs_tosspages(ip, 0, -1,
1118 FI_REMAPF);
David Chinner40095b62007-05-14 18:24:09 +10001119 else
Christoph Hellwig739bfb22007-08-29 10:58:01 +10001120 error = xfs_flushinval_pages(ip,
1121 0, -1, FI_REMAPF);
David Chinner40095b62007-05-14 18:24:09 +10001122 } else if ((flags & SYNC_DELWRI) && VN_DIRTY(vp)) {
Christoph Hellwig739bfb22007-08-29 10:58:01 +10001123 error = xfs_flush_pages(ip, 0,
Nathan Scott67fcaa72006-06-09 17:00:52 +10001124 -1, fflag, FI_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 }
1126
David Chinner40095b62007-05-14 18:24:09 +10001127 /*
1128 * When freezing, we need to wait ensure all I/O (including direct
1129 * I/O) is complete to ensure no further data modification can take
1130 * place after this point
1131 */
1132 if (flags & SYNC_IOWAIT)
Christoph Hellwigb677c212007-08-29 11:46:28 +10001133 vn_iowait(ip);
David Chinner40095b62007-05-14 18:24:09 +10001134
1135 xfs_ilock(ip, XFS_ILOCK_SHARED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 }
1137
Lachlan McIlroy44866d32007-09-14 15:21:08 +10001138 if ((flags & SYNC_ATTR) &&
1139 (ip->i_update_core ||
1140 (ip->i_itemp && ip->i_itemp->ili_format.ilf_fields))) {
1141 if (mount_locked)
1142 IPOINTER_INSERT(ip, mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
Lachlan McIlroy44866d32007-09-14 15:21:08 +10001144 if (flags & SYNC_WAIT) {
1145 xfs_iflock(ip);
1146 error = xfs_iflush(ip, XFS_IFLUSH_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
Lachlan McIlroy44866d32007-09-14 15:21:08 +10001148 /*
1149 * If we can't acquire the flush lock, then the inode
1150 * is already being flushed so don't bother waiting.
1151 *
1152 * If we can lock it then do a delwri flush so we can
1153 * combine multiple inode flushes in each disk write.
1154 */
1155 } else if (xfs_iflock_nowait(ip)) {
1156 error = xfs_iflush(ip, XFS_IFLUSH_DELWRI);
1157 } else if (bypassed) {
1158 (*bypassed)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 }
1160 }
1161
1162 if (lock_flags != 0) {
1163 xfs_iunlock(ip, lock_flags);
1164 }
1165
1166 if (vnode_refed) {
1167 /*
1168 * If we had to take a reference on the vnode
1169 * above, then wait until after we've unlocked
1170 * the inode to release the reference. This is
1171 * because we can be already holding the inode
1172 * lock when VN_RELE() calls xfs_inactive().
1173 *
1174 * Make sure to drop the mount lock before calling
1175 * VN_RELE() so that we don't trip over ourselves if
1176 * we have to go for the mount lock again in the
1177 * inactive code.
1178 */
1179 if (mount_locked) {
1180 IPOINTER_INSERT(ip, mp);
1181 }
1182
1183 VN_RELE(vp);
1184
1185 vnode_refed = B_FALSE;
1186 }
1187
1188 if (error) {
1189 last_error = error;
1190 }
1191
1192 /*
1193 * bail out if the filesystem is corrupted.
1194 */
1195 if (error == EFSCORRUPTED) {
1196 if (!mount_locked) {
1197 XFS_MOUNT_ILOCK(mp);
1198 IPOINTER_REMOVE(ip, mp);
1199 }
1200 XFS_MOUNT_IUNLOCK(mp);
1201 ASSERT(ipointer_in == B_FALSE);
1202 kmem_free(ipointer, sizeof(xfs_iptr_t));
1203 return XFS_ERROR(error);
1204 }
1205
1206 /* Let other threads have a chance at the mount lock
1207 * if we have looped many times without dropping the
1208 * lock.
1209 */
1210 if ((++preempt & XFS_PREEMPT_MASK) == 0) {
1211 if (mount_locked) {
1212 IPOINTER_INSERT(ip, mp);
1213 }
1214 }
1215
1216 if (mount_locked == B_FALSE) {
1217 XFS_MOUNT_ILOCK(mp);
1218 mount_locked = B_TRUE;
1219 IPOINTER_REMOVE(ip, mp);
1220 continue;
1221 }
1222
1223 ASSERT(ipointer_in == B_FALSE);
1224 ip = ip->i_mnext;
1225
1226 } while (ip != mp->m_inodes);
1227
1228 XFS_MOUNT_IUNLOCK(mp);
1229
1230 ASSERT(ipointer_in == B_FALSE);
1231
1232 kmem_free(ipointer, sizeof(xfs_iptr_t));
1233 return XFS_ERROR(last_error);
1234}
1235
1236/*
1237 * xfs sync routine for internal use
1238 *
Nathan Scottb83bd132006-06-09 16:48:30 +10001239 * This routine supports all of the flags defined for the generic vfs_sync
Lachlan McIlroy1f9b3b62007-02-10 18:35:33 +11001240 * interface as explained above under xfs_sync.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 *
1242 */
1243int
1244xfs_syncsub(
1245 xfs_mount_t *mp,
1246 int flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 int *bypassed)
1248{
1249 int error = 0;
1250 int last_error = 0;
1251 uint log_flags = XFS_LOG_FORCE;
1252 xfs_buf_t *bp;
1253 xfs_buf_log_item_t *bip;
1254
1255 /*
1256 * Sync out the log. This ensures that the log is periodically
1257 * flushed even if there is not enough activity to fill it up.
1258 */
1259 if (flags & SYNC_WAIT)
1260 log_flags |= XFS_LOG_SYNC;
1261
1262 xfs_log_force(mp, (xfs_lsn_t)0, log_flags);
1263
1264 if (flags & (SYNC_ATTR|SYNC_DELWRI)) {
1265 if (flags & SYNC_BDFLUSH)
1266 xfs_finish_reclaim_all(mp, 1);
1267 else
Lachlan McIlroy1f9b3b62007-02-10 18:35:33 +11001268 error = xfs_sync_inodes(mp, flags, bypassed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 }
1270
1271 /*
1272 * Flushing out dirty data above probably generated more
1273 * log activity, so if this isn't vfs_sync() then flush
1274 * the log again.
1275 */
1276 if (flags & SYNC_DELWRI) {
1277 xfs_log_force(mp, (xfs_lsn_t)0, log_flags);
1278 }
1279
1280 if (flags & SYNC_FSDATA) {
1281 /*
1282 * If this is vfs_sync() then only sync the superblock
1283 * if we can lock it without sleeping and it is not pinned.
1284 */
1285 if (flags & SYNC_BDFLUSH) {
1286 bp = xfs_getsb(mp, XFS_BUF_TRYLOCK);
1287 if (bp != NULL) {
1288 bip = XFS_BUF_FSPRIVATE(bp,xfs_buf_log_item_t*);
1289 if ((bip != NULL) &&
1290 xfs_buf_item_dirty(bip)) {
1291 if (!(XFS_BUF_ISPINNED(bp))) {
1292 XFS_BUF_ASYNC(bp);
1293 error = xfs_bwrite(mp, bp);
1294 } else {
1295 xfs_buf_relse(bp);
1296 }
1297 } else {
1298 xfs_buf_relse(bp);
1299 }
1300 }
1301 } else {
1302 bp = xfs_getsb(mp, 0);
1303 /*
1304 * If the buffer is pinned then push on the log so
1305 * we won't get stuck waiting in the write for
1306 * someone, maybe ourselves, to flush the log.
1307 * Even though we just pushed the log above, we
1308 * did not have the superblock buffer locked at
1309 * that point so it can become pinned in between
1310 * there and here.
1311 */
1312 if (XFS_BUF_ISPINNED(bp))
1313 xfs_log_force(mp, (xfs_lsn_t)0, XFS_LOG_FORCE);
1314 if (flags & SYNC_WAIT)
1315 XFS_BUF_UNASYNC(bp);
1316 else
1317 XFS_BUF_ASYNC(bp);
1318 error = xfs_bwrite(mp, bp);
1319 }
1320 if (error) {
1321 last_error = error;
1322 }
1323 }
1324
1325 /*
1326 * If this is the periodic sync, then kick some entries out of
1327 * the reference cache. This ensures that idle entries are
1328 * eventually kicked out of the cache.
1329 */
1330 if (flags & SYNC_REFCACHE) {
Nathan Scottcde410a2005-09-05 11:47:01 +10001331 if (flags & SYNC_WAIT)
1332 xfs_refcache_purge_mp(mp);
1333 else
1334 xfs_refcache_purge_some(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 }
1336
1337 /*
David Chinner92821e22007-05-24 15:26:31 +10001338 * If asked, update the disk superblock with incore counter values if we
1339 * are using non-persistent counters so that they don't get too far out
1340 * of sync if we crash or get a forced shutdown. We don't want to force
1341 * this to disk, just get a transaction into the iclogs....
1342 */
1343 if (flags & SYNC_SUPER)
1344 xfs_log_sbcount(mp, 0);
1345
1346 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 * Now check to see if the log needs a "dummy" transaction.
1348 */
1349
1350 if (!(flags & SYNC_REMOUNT) && xfs_log_need_covered(mp)) {
1351 xfs_trans_t *tp;
1352 xfs_inode_t *ip;
1353
1354 /*
1355 * Put a dummy transaction in the log to tell
1356 * recovery that all others are OK.
1357 */
1358 tp = xfs_trans_alloc(mp, XFS_TRANS_DUMMY1);
1359 if ((error = xfs_trans_reserve(tp, 0,
1360 XFS_ICHANGE_LOG_RES(mp),
1361 0, 0, 0))) {
1362 xfs_trans_cancel(tp, 0);
1363 return error;
1364 }
1365
1366 ip = mp->m_rootip;
1367 xfs_ilock(ip, XFS_ILOCK_EXCL);
1368
1369 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
1370 xfs_trans_ihold(tp, ip);
1371 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Eric Sandeen1c72bf92007-05-08 13:48:42 +10001372 error = xfs_trans_commit(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1374 xfs_log_force(mp, (xfs_lsn_t)0, log_flags);
1375 }
1376
1377 /*
1378 * When shutting down, we need to insure that the AIL is pushed
1379 * to disk or the filesystem can appear corrupt from the PROM.
1380 */
1381 if ((flags & (SYNC_CLOSE|SYNC_WAIT)) == (SYNC_CLOSE|SYNC_WAIT)) {
1382 XFS_bflush(mp->m_ddev_targp);
1383 if (mp->m_rtdev_targp) {
1384 XFS_bflush(mp->m_rtdev_targp);
1385 }
1386 }
1387
1388 return XFS_ERROR(last_error);
1389}