blob: 79bdfb3d6081ec060fe026da012ed5864cd3c3e6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110019#include "xfs_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "xfs_types.h"
Nathan Scotta844f452005-11-02 14:38:42 +110021#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "xfs_log.h"
Nathan Scotta844f452005-11-02 14:38:42 +110023#include "xfs_inum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "xfs_trans.h"
25#include "xfs_sb.h"
Nathan Scotta844f452005-11-02 14:38:42 +110026#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "xfs_dir2.h"
28#include "xfs_dmapi.h"
29#include "xfs_mount.h"
Nathan Scotta844f452005-11-02 14:38:42 +110030#include "xfs_da_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include "xfs_bmap_btree.h"
32#include "xfs_ialloc_btree.h"
33#include "xfs_alloc_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110034#include "xfs_dir2_sf.h"
35#include "xfs_attr_sf.h"
36#include "xfs_dinode.h"
37#include "xfs_inode.h"
38#include "xfs_inode_item.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include "xfs_btree.h"
40#include "xfs_alloc.h"
41#include "xfs_ialloc.h"
Nathan Scotta844f452005-11-02 14:38:42 +110042#include "xfs_quota.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include "xfs_error.h"
44#include "xfs_bmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include "xfs_rw.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include "xfs_buf_item.h"
Nathan Scotta844f452005-11-02 14:38:42 +110047#include "xfs_log_priv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include "xfs_dir2_trace.h"
Nathan Scotta844f452005-11-02 14:38:42 +110049#include "xfs_extfree_item.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include "xfs_acl.h"
51#include "xfs_attr.h"
52#include "xfs_clnt.h"
David Chinner2a82b8b2007-07-11 11:09:12 +100053#include "xfs_mru_cache.h"
54#include "xfs_filestream.h"
Christoph Hellwige13a73f2006-01-11 15:30:08 +110055#include "xfs_fsops.h"
Christoph Hellwig739bfb22007-08-29 10:58:01 +100056#include "xfs_vnodeops.h"
Christoph Hellwig48c872a9f2007-08-30 17:20:31 +100057#include "xfs_vfsops.h"
Christoph Hellwig739bfb22007-08-29 10:58:01 +100058
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Lachlan McIlroyde2eeea2008-02-06 13:37:56 +110060int __init
Linus Torvalds1da177e2005-04-16 15:20:36 -070061xfs_init(void)
62{
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#ifdef XFS_DABUF_DEBUG
Eric Sandeen703e1f02007-10-11 17:41:21 +100064 extern spinlock_t xfs_dabuf_global_lock;
Eric Sandeen007c61c2007-10-11 17:43:56 +100065 spin_lock_init(&xfs_dabuf_global_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066#endif
67
68 /*
69 * Initialize all of the zone allocators we use.
70 */
71 xfs_bmap_free_item_zone = kmem_zone_init(sizeof(xfs_bmap_free_item_t),
72 "xfs_bmap_free_item");
73 xfs_btree_cur_zone = kmem_zone_init(sizeof(xfs_btree_cur_t),
74 "xfs_btree_cur");
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 xfs_trans_zone = kmem_zone_init(sizeof(xfs_trans_t), "xfs_trans");
76 xfs_da_state_zone =
77 kmem_zone_init(sizeof(xfs_da_state_t), "xfs_da_state");
78 xfs_dabuf_zone = kmem_zone_init(sizeof(xfs_dabuf_t), "xfs_dabuf");
Nathan Scott87582802006-03-14 13:18:19 +110079 xfs_ifork_zone = kmem_zone_init(sizeof(xfs_ifork_t), "xfs_ifork");
80 xfs_acl_zone_init(xfs_acl_zone, "xfs_acl");
David Chinner2a82b8b2007-07-11 11:09:12 +100081 xfs_mru_cache_init();
82 xfs_filestream_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
84 /*
85 * The size of the zone allocated buf log item is the maximum
86 * size possible under XFS. This wastes a little bit of memory,
87 * but it is much faster.
88 */
89 xfs_buf_item_zone =
90 kmem_zone_init((sizeof(xfs_buf_log_item_t) +
91 (((XFS_MAX_BLOCKSIZE / XFS_BLI_CHUNK) /
92 NBWORD) * sizeof(int))),
93 "xfs_buf_item");
Nathan Scott87582802006-03-14 13:18:19 +110094 xfs_efd_zone =
95 kmem_zone_init((sizeof(xfs_efd_log_item_t) +
96 ((XFS_EFD_MAX_FAST_EXTENTS - 1) *
97 sizeof(xfs_extent_t))),
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 "xfs_efd_item");
Nathan Scott87582802006-03-14 13:18:19 +110099 xfs_efi_zone =
100 kmem_zone_init((sizeof(xfs_efi_log_item_t) +
101 ((XFS_EFI_MAX_FAST_EXTENTS - 1) *
102 sizeof(xfs_extent_t))),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 "xfs_efi_item");
Nathan Scott87582802006-03-14 13:18:19 +1100104
105 /*
106 * These zones warrant special memory allocator hints
107 */
108 xfs_inode_zone =
109 kmem_zone_init_flags(sizeof(xfs_inode_t), "xfs_inode",
110 KM_ZONE_HWALIGN | KM_ZONE_RECLAIM |
111 KM_ZONE_SPREAD, NULL);
112 xfs_ili_zone =
113 kmem_zone_init_flags(sizeof(xfs_inode_log_item_t), "xfs_ili",
114 KM_ZONE_SPREAD, NULL);
David Chinnerda353b02007-08-28 14:00:13 +1000115 xfs_icluster_zone =
116 kmem_zone_init_flags(sizeof(xfs_icluster_t), "xfs_icluster",
Nathan Scott87582802006-03-14 13:18:19 +1100117 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;
David Chinnerda353b02007-08-28 14:00:13 +1000155 extern kmem_zone_t *xfs_icluster_zone;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
157 xfs_cleanup_procfs();
158 xfs_sysctl_unregister();
David Chinner2a82b8b2007-07-11 11:09:12 +1000159 xfs_filestream_uninit();
160 xfs_mru_cache_uninit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 xfs_acl_zone_destroy(xfs_acl_zone);
162
163#ifdef XFS_DIR2_TRACE
164 ktrace_free(xfs_dir2_trace_buf);
165#endif
166#ifdef XFS_ATTR_TRACE
167 ktrace_free(xfs_attr_trace_buf);
168#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169#ifdef XFS_BMBT_TRACE
170 ktrace_free(xfs_bmbt_trace_buf);
171#endif
172#ifdef XFS_BMAP_TRACE
173 ktrace_free(xfs_bmap_trace_buf);
174#endif
175#ifdef XFS_ALLOC_TRACE
176 ktrace_free(xfs_alloc_trace_buf);
177#endif
178
Nathan Scott3758dee2006-03-22 12:47:28 +1100179 kmem_zone_destroy(xfs_bmap_free_item_zone);
180 kmem_zone_destroy(xfs_btree_cur_zone);
181 kmem_zone_destroy(xfs_inode_zone);
182 kmem_zone_destroy(xfs_trans_zone);
183 kmem_zone_destroy(xfs_da_state_zone);
184 kmem_zone_destroy(xfs_dabuf_zone);
185 kmem_zone_destroy(xfs_buf_item_zone);
186 kmem_zone_destroy(xfs_efd_zone);
187 kmem_zone_destroy(xfs_efi_zone);
188 kmem_zone_destroy(xfs_ifork_zone);
189 kmem_zone_destroy(xfs_ili_zone);
David Chinnerda353b02007-08-28 14:00:13 +1000190 kmem_zone_destroy(xfs_icluster_zone);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191}
192
193/*
194 * xfs_start_flags
195 *
196 * This function fills in xfs_mount_t fields based on mount args.
197 * Note: the superblock has _not_ yet been read in.
198 */
199STATIC int
200xfs_start_flags(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 struct xfs_mount_args *ap,
202 struct xfs_mount *mp)
203{
204 /* Values are in BBs */
205 if ((ap->flags & XFSMNT_NOALIGN) != XFSMNT_NOALIGN) {
206 /*
207 * At this point the superblock has not been read
208 * in, therefore we do not know the block size.
209 * Before the mount call ends we will convert
210 * these to FSBs.
211 */
212 mp->m_dalign = ap->sunit;
213 mp->m_swidth = ap->swidth;
214 }
215
216 if (ap->logbufs != -1 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 ap->logbufs != 0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 (ap->logbufs < XLOG_MIN_ICLOGS ||
219 ap->logbufs > XLOG_MAX_ICLOGS)) {
220 cmn_err(CE_WARN,
221 "XFS: invalid logbufs value: %d [not %d-%d]",
222 ap->logbufs, XLOG_MIN_ICLOGS, XLOG_MAX_ICLOGS);
223 return XFS_ERROR(EINVAL);
224 }
225 mp->m_logbufs = ap->logbufs;
226 if (ap->logbufsize != -1 &&
Nathan Scottcfcbbbd2005-11-02 15:12:04 +1100227 ap->logbufsize != 0 &&
Eric Sandeendcb3b832007-08-16 16:25:33 +1000228 (ap->logbufsize < XLOG_MIN_RECORD_BSIZE ||
229 ap->logbufsize > XLOG_MAX_RECORD_BSIZE ||
230 !is_power_of_2(ap->logbufsize))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 cmn_err(CE_WARN,
232 "XFS: invalid logbufsize: %d [not 16k,32k,64k,128k or 256k]",
233 ap->logbufsize);
234 return XFS_ERROR(EINVAL);
235 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 mp->m_logbsize = ap->logbufsize;
237 mp->m_fsname_len = strlen(ap->fsname) + 1;
238 mp->m_fsname = kmem_alloc(mp->m_fsname_len, KM_SLEEP);
239 strcpy(mp->m_fsname, ap->fsname);
Nathan Scottfc1f8c12005-11-02 11:44:33 +1100240 if (ap->rtname[0]) {
241 mp->m_rtname = kmem_alloc(strlen(ap->rtname) + 1, KM_SLEEP);
242 strcpy(mp->m_rtname, ap->rtname);
243 }
244 if (ap->logname[0]) {
245 mp->m_logname = kmem_alloc(strlen(ap->logname) + 1, KM_SLEEP);
246 strcpy(mp->m_logname, ap->logname);
247 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
249 if (ap->flags & XFSMNT_WSYNC)
250 mp->m_flags |= XFS_MOUNT_WSYNC;
251#if XFS_BIG_INUMS
252 if (ap->flags & XFSMNT_INO64) {
253 mp->m_flags |= XFS_MOUNT_INO64;
254 mp->m_inoadd = XFS_INO64_OFFSET;
255 }
256#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 if (ap->flags & XFSMNT_RETERR)
258 mp->m_flags |= XFS_MOUNT_RETERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 if (ap->flags & XFSMNT_NOALIGN)
260 mp->m_flags |= XFS_MOUNT_NOALIGN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 if (ap->flags & XFSMNT_SWALLOC)
262 mp->m_flags |= XFS_MOUNT_SWALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 if (ap->flags & XFSMNT_OSYNCISOSYNC)
264 mp->m_flags |= XFS_MOUNT_OSYNCISOSYNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 if (ap->flags & XFSMNT_32BITINODES)
Nathan Scottc11e2c32005-11-02 15:11:45 +1100266 mp->m_flags |= XFS_MOUNT_32BITINODES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
268 if (ap->flags & XFSMNT_IOSIZE) {
269 if (ap->iosizelog > XFS_MAX_IO_LOG ||
270 ap->iosizelog < XFS_MIN_IO_LOG) {
271 cmn_err(CE_WARN,
272 "XFS: invalid log iosize: %d [not %d-%d]",
273 ap->iosizelog, XFS_MIN_IO_LOG,
274 XFS_MAX_IO_LOG);
275 return XFS_ERROR(EINVAL);
276 }
277
278 mp->m_flags |= XFS_MOUNT_DFLT_IOSIZE;
279 mp->m_readio_log = mp->m_writeio_log = ap->iosizelog;
280 }
281
Josef Jeff Sipek1bd960e2008-02-29 13:58:40 +1100282 if (ap->flags & XFSMNT_IKEEP)
283 mp->m_flags |= XFS_MOUNT_IKEEP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 if (ap->flags & XFSMNT_DIRSYNC)
285 mp->m_flags |= XFS_MOUNT_DIRSYNC;
Nathan Scott13059ff2006-01-11 15:32:01 +1100286 if (ap->flags & XFSMNT_ATTR2)
287 mp->m_flags |= XFS_MOUNT_ATTR2;
David Chinnere8c8b3a2005-11-02 10:33:05 +1100288
Nathan Scotte718eeb42005-11-02 15:09:22 +1100289 if (ap->flags2 & XFSMNT2_COMPAT_IOSIZE)
290 mp->m_flags |= XFS_MOUNT_COMPAT_IOSIZE;
291
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 /*
293 * no recovery flag requires a read-only mount
294 */
295 if (ap->flags & XFSMNT_NORECOVERY) {
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000296 if (!(mp->m_flags & XFS_MOUNT_RDONLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 cmn_err(CE_WARN,
298 "XFS: tried to mount a FS read-write without recovery!");
299 return XFS_ERROR(EINVAL);
300 }
301 mp->m_flags |= XFS_MOUNT_NORECOVERY;
302 }
303
304 if (ap->flags & XFSMNT_NOUUID)
305 mp->m_flags |= XFS_MOUNT_NOUUID;
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100306 if (ap->flags & XFSMNT_BARRIER)
307 mp->m_flags |= XFS_MOUNT_BARRIER;
Christoph Hellwig4ef19dd2006-01-11 15:27:18 +1100308 else
309 mp->m_flags &= ~XFS_MOUNT_BARRIER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
David Chinner2a82b8b2007-07-11 11:09:12 +1000311 if (ap->flags2 & XFSMNT2_FILESTREAMS)
312 mp->m_flags |= XFS_MOUNT_FILESTREAMS;
313
Christoph Hellwig293688e2007-08-29 11:59:36 +1000314 if (ap->flags & XFSMNT_DMAPI)
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000315 mp->m_flags |= XFS_MOUNT_DMAPI;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 return 0;
317}
318
319/*
320 * This function fills in xfs_mount_t fields based on mount args.
321 * Note: the superblock _has_ now been read in.
322 */
323STATIC int
324xfs_finish_flags(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 struct xfs_mount_args *ap,
326 struct xfs_mount *mp)
327{
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000328 int ronly = (mp->m_flags & XFS_MOUNT_RDONLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
330 /* Fail a mount where the logbuf is smaller then the log stripe */
Eric Sandeen62118702008-03-06 13:44:28 +1100331 if (xfs_sb_version_haslogv2(&mp->m_sb)) {
Nathan Scottc2cd2552006-01-11 15:35:32 +1100332 if ((ap->logbufsize <= 0) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 (mp->m_sb.sb_logsunit > XLOG_BIG_RECORD_BSIZE)) {
334 mp->m_logbsize = mp->m_sb.sb_logsunit;
Nathan Scottc2cd2552006-01-11 15:35:32 +1100335 } else if (ap->logbufsize > 0 &&
336 ap->logbufsize < mp->m_sb.sb_logsunit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 cmn_err(CE_WARN,
338 "XFS: logbuf size must be greater than or equal to log stripe size");
339 return XFS_ERROR(EINVAL);
340 }
341 } else {
342 /* Fail a mount if the logbuf is larger than 32K */
343 if (ap->logbufsize > XLOG_BIG_RECORD_BSIZE) {
344 cmn_err(CE_WARN,
345 "XFS: logbuf size for version 1 logs must be 16K or 32K");
346 return XFS_ERROR(EINVAL);
347 }
348 }
349
Eric Sandeen62118702008-03-06 13:44:28 +1100350 if (xfs_sb_version_hasattr2(&mp->m_sb))
Nathan Scott13059ff2006-01-11 15:32:01 +1100351 mp->m_flags |= XFS_MOUNT_ATTR2;
Nathan Scott13059ff2006-01-11 15:32:01 +1100352
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 /*
354 * prohibit r/w mounts of read-only filesystems
355 */
356 if ((mp->m_sb.sb_flags & XFS_SBF_READONLY) && !ronly) {
357 cmn_err(CE_WARN,
358 "XFS: cannot mount a read-only filesystem as read-write");
359 return XFS_ERROR(EROFS);
360 }
361
362 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 * check for shared mount.
364 */
365 if (ap->flags & XFSMNT_SHARED) {
Eric Sandeen62118702008-03-06 13:44:28 +1100366 if (!xfs_sb_version_hasshared(&mp->m_sb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 return XFS_ERROR(EINVAL);
368
369 /*
370 * For IRIX 6.5, shared mounts must have the shared
371 * version bit set, have the persistent readonly
372 * field set, must be version 0 and can only be mounted
373 * read-only.
374 */
375 if (!ronly || !(mp->m_sb.sb_flags & XFS_SBF_READONLY) ||
376 (mp->m_sb.sb_shared_vn != 0))
377 return XFS_ERROR(EINVAL);
378
379 mp->m_flags |= XFS_MOUNT_SHARED;
380
381 /*
382 * Shared XFS V0 can't deal with DMI. Return EINVAL.
383 */
384 if (mp->m_sb.sb_shared_vn == 0 && (ap->flags & XFSMNT_DMAPI))
385 return XFS_ERROR(EINVAL);
386 }
387
Christoph Hellwigb09cc772007-08-30 17:19:57 +1000388 if (ap->flags & XFSMNT_UQUOTA) {
389 mp->m_qflags |= (XFS_UQUOTA_ACCT | XFS_UQUOTA_ACTIVE);
390 if (ap->flags & XFSMNT_UQUOTAENF)
391 mp->m_qflags |= XFS_UQUOTA_ENFD;
392 }
393
394 if (ap->flags & XFSMNT_GQUOTA) {
395 mp->m_qflags |= (XFS_GQUOTA_ACCT | XFS_GQUOTA_ACTIVE);
396 if (ap->flags & XFSMNT_GQUOTAENF)
397 mp->m_qflags |= XFS_OQUOTA_ENFD;
398 } else if (ap->flags & XFSMNT_PQUOTA) {
399 mp->m_qflags |= (XFS_PQUOTA_ACCT | XFS_PQUOTA_ACTIVE);
400 if (ap->flags & XFSMNT_PQUOTAENF)
401 mp->m_qflags |= XFS_OQUOTA_ENFD;
402 }
403
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 return 0;
405}
406
407/*
408 * xfs_mount
409 *
410 * The file system configurations are:
411 * (1) device (partition) with data and internal log
412 * (2) logical volume with data and log subvolumes.
413 * (3) logical volume with data, log, and realtime subvolumes.
414 *
415 * We only have to handle opening the log and realtime volumes here if
416 * they are present. The data subvolume has already been opened by
417 * get_sb_bdev() and is stored in vfsp->vfs_super->s_bdev.
418 */
Christoph Hellwig48c872a9f2007-08-30 17:20:31 +1000419int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420xfs_mount(
Christoph Hellwig48c872a9f2007-08-30 17:20:31 +1000421 struct xfs_mount *mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 struct xfs_mount_args *args,
423 cred_t *credp)
424{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 struct block_device *ddev, *logdev, *rtdev;
426 int flags = 0, error;
427
Christoph Hellwigb267ce92007-08-30 17:21:30 +1000428 ddev = mp->m_super->s_bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 logdev = rtdev = NULL;
430
Christoph Hellwig293688e2007-08-29 11:59:36 +1000431 error = xfs_dmops_get(mp, args);
432 if (error)
433 return error;
Christoph Hellwigb09cc772007-08-30 17:19:57 +1000434 error = xfs_qmops_get(mp, args);
435 if (error)
436 return error;
Christoph Hellwig293688e2007-08-29 11:59:36 +1000437
Nathan Scott764d1f82006-03-31 13:04:17 +1000438 if (args->flags & XFSMNT_QUIET)
439 flags |= XFS_MFSI_QUIET;
440
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 /*
442 * Open real time and log devices - order is important.
443 */
444 if (args->logname[0]) {
445 error = xfs_blkdev_get(mp, args->logname, &logdev);
446 if (error)
447 return error;
448 }
449 if (args->rtname[0]) {
450 error = xfs_blkdev_get(mp, args->rtname, &rtdev);
451 if (error) {
452 xfs_blkdev_put(logdev);
453 return error;
454 }
455
456 if (rtdev == ddev || rtdev == logdev) {
457 cmn_err(CE_WARN,
458 "XFS: Cannot mount filesystem with identical rtdev and ddev/logdev.");
459 xfs_blkdev_put(logdev);
460 xfs_blkdev_put(rtdev);
461 return EINVAL;
462 }
463 }
464
465 /*
466 * Setup xfs_mount buffer target pointers
467 */
468 error = ENOMEM;
469 mp->m_ddev_targp = xfs_alloc_buftarg(ddev, 0);
470 if (!mp->m_ddev_targp) {
471 xfs_blkdev_put(logdev);
472 xfs_blkdev_put(rtdev);
473 return error;
474 }
475 if (rtdev) {
476 mp->m_rtdev_targp = xfs_alloc_buftarg(rtdev, 1);
Jesper Juhl49ee6c92007-08-16 16:25:42 +1000477 if (!mp->m_rtdev_targp) {
478 xfs_blkdev_put(logdev);
479 xfs_blkdev_put(rtdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 goto error0;
Jesper Juhl49ee6c92007-08-16 16:25:42 +1000481 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 }
483 mp->m_logdev_targp = (logdev && logdev != ddev) ?
484 xfs_alloc_buftarg(logdev, 1) : mp->m_ddev_targp;
Jesper Juhl49ee6c92007-08-16 16:25:42 +1000485 if (!mp->m_logdev_targp) {
486 xfs_blkdev_put(logdev);
487 xfs_blkdev_put(rtdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 goto error0;
Jesper Juhl49ee6c92007-08-16 16:25:42 +1000489 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
491 /*
492 * Setup flags based on mount(2) options and then the superblock
493 */
Christoph Hellwigb267ce92007-08-30 17:21:30 +1000494 error = xfs_start_flags(args, mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 if (error)
496 goto error1;
Nathan Scott764d1f82006-03-31 13:04:17 +1000497 error = xfs_readsb(mp, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 if (error)
499 goto error1;
Christoph Hellwigb267ce92007-08-30 17:21:30 +1000500 error = xfs_finish_flags(args, mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 if (error)
502 goto error2;
503
504 /*
505 * Setup xfs_mount buffer target pointers based on superblock
506 */
507 error = xfs_setsize_buftarg(mp->m_ddev_targp, mp->m_sb.sb_blocksize,
508 mp->m_sb.sb_sectsize);
509 if (!error && logdev && logdev != ddev) {
510 unsigned int log_sector_size = BBSIZE;
511
Eric Sandeen62118702008-03-06 13:44:28 +1100512 if (xfs_sb_version_hassector(&mp->m_sb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 log_sector_size = mp->m_sb.sb_logsectsize;
514 error = xfs_setsize_buftarg(mp->m_logdev_targp,
515 mp->m_sb.sb_blocksize,
516 log_sector_size);
517 }
518 if (!error && rtdev)
519 error = xfs_setsize_buftarg(mp->m_rtdev_targp,
520 mp->m_sb.sb_blocksize,
521 mp->m_sb.sb_sectsize);
522 if (error)
523 goto error2;
524
Nathan Scottb2ea4012006-07-28 17:05:13 +1000525 if (mp->m_flags & XFS_MOUNT_BARRIER)
Christoph Hellwigc7d437d2006-01-11 15:28:56 +1100526 xfs_mountfs_check_barriers(mp);
527
David Chinner2a82b8b2007-07-11 11:09:12 +1000528 if ((error = xfs_filestream_mount(mp)))
529 goto error2;
530
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000531 error = xfs_mountfs(mp, flags);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100532 if (error)
533 goto error2;
534
Christoph Hellwig293688e2007-08-29 11:59:36 +1000535 XFS_SEND_MOUNT(mp, DM_RIGHT_NULL, args->mtpt, args->fsname);
536
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100537 return 0;
538
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539error2:
540 if (mp->m_sb_bp)
541 xfs_freesb(mp);
542error1:
543 xfs_binval(mp->m_ddev_targp);
544 if (logdev && logdev != ddev)
545 xfs_binval(mp->m_logdev_targp);
546 if (rtdev)
547 xfs_binval(mp->m_rtdev_targp);
548error0:
549 xfs_unmountfs_close(mp, credp);
Christoph Hellwigb09cc772007-08-30 17:19:57 +1000550 xfs_qmops_put(mp);
Christoph Hellwig293688e2007-08-29 11:59:36 +1000551 xfs_dmops_put(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 return error;
553}
554
Christoph Hellwig48c872a9f2007-08-30 17:20:31 +1000555int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556xfs_unmount(
Christoph Hellwig48c872a9f2007-08-30 17:20:31 +1000557 xfs_mount_t *mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 int flags,
559 cred_t *credp)
560{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 xfs_inode_t *rip;
Nathan Scott67fcaa72006-06-09 17:00:52 +1000562 bhv_vnode_t *rvp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 int unmount_event_wanted = 0;
564 int unmount_event_flags = 0;
565 int xfs_unmountfs_needed = 0;
566 int error;
567
568 rip = mp->m_rootip;
569 rvp = XFS_ITOV(rip);
570
Eric Sandeen948c6d4f2007-08-23 16:19:57 +1000571#ifdef HAVE_DMAPI
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000572 if (mp->m_flags & XFS_MOUNT_DMAPI) {
Christoph Hellwigb267ce92007-08-30 17:21:30 +1000573 error = XFS_SEND_PREUNMOUNT(mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 rvp, DM_RIGHT_NULL, rvp, DM_RIGHT_NULL,
575 NULL, NULL, 0, 0,
576 (mp->m_dmevmask & (1<<DM_EVENT_PREUNMOUNT))?
577 0:DM_FLAGS_UNWANTED);
578 if (error)
579 return XFS_ERROR(error);
580 unmount_event_wanted = 1;
581 unmount_event_flags = (mp->m_dmevmask & (1<<DM_EVENT_UNMOUNT))?
582 0 : DM_FLAGS_UNWANTED;
583 }
Eric Sandeen948c6d4f2007-08-23 16:19:57 +1000584#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
David Chinner2a82b8b2007-07-11 11:09:12 +1000586 /*
587 * Blow away any referenced inode in the filestreams cache.
588 * This can and will cause log traffic as inodes go inactive
589 * here.
590 */
591 xfs_filestream_unmount(mp);
592
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 XFS_bflush(mp->m_ddev_targp);
594 error = xfs_unmount_flush(mp, 0);
595 if (error)
596 goto out;
597
598 ASSERT(vn_count(rvp) == 1);
599
600 /*
601 * Drop the reference count
602 */
603 VN_RELE(rvp);
604
605 /*
606 * If we're forcing a shutdown, typically because of a media error,
607 * we want to make sure we invalidate dirty pages that belong to
608 * referenced vnodes as well.
609 */
610 if (XFS_FORCED_SHUTDOWN(mp)) {
Christoph Hellwig48c872a9f2007-08-30 17:20:31 +1000611 error = xfs_sync(mp, SYNC_WAIT | SYNC_CLOSE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 ASSERT(error != EFSCORRUPTED);
613 }
614 xfs_unmountfs_needed = 1;
615
616out:
617 /* Send DMAPI event, if required.
618 * Then do xfs_unmountfs() if needed.
619 * Then return error (or zero).
620 */
621 if (unmount_event_wanted) {
622 /* Note: mp structure must still exist for
623 * XFS_SEND_UNMOUNT() call.
624 */
Christoph Hellwigb267ce92007-08-30 17:21:30 +1000625 XFS_SEND_UNMOUNT(mp, error == 0 ? rvp : NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 DM_RIGHT_NULL, 0, error, unmount_event_flags);
627 }
628 if (xfs_unmountfs_needed) {
629 /*
630 * Call common unmount function to flush to disk
631 * and free the super block buffer & mount structures.
632 */
633 xfs_unmountfs(mp, credp);
Christoph Hellwigb09cc772007-08-30 17:19:57 +1000634 xfs_qmops_put(mp);
Christoph Hellwig293688e2007-08-29 11:59:36 +1000635 xfs_dmops_put(mp);
Christoph Hellwigf541d272007-08-29 11:53:22 +1000636 kmem_free(mp, sizeof(xfs_mount_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 }
638
639 return XFS_ERROR(error);
640}
641
Christoph Hellwigf898d6c2005-06-21 15:40:48 +1000642STATIC int
643xfs_quiesce_fs(
644 xfs_mount_t *mp)
645{
646 int count = 0, pincount;
Nathan Scott3758dee2006-03-22 12:47:28 +1100647
Christoph Hellwigf898d6c2005-06-21 15:40:48 +1000648 xfs_flush_buftarg(mp->m_ddev_targp, 0);
649 xfs_finish_reclaim_all(mp, 0);
650
651 /* This loop must run at least twice.
652 * The first instance of the loop will flush
653 * most meta data but that will generate more
654 * meta data (typically directory updates).
655 * Which then must be flushed and logged before
656 * we can write the unmount record.
Nathan Scott3758dee2006-03-22 12:47:28 +1100657 */
Christoph Hellwigf898d6c2005-06-21 15:40:48 +1000658 do {
David Chinner516b2e72007-06-18 16:50:48 +1000659 xfs_syncsub(mp, SYNC_INODE_QUIESCE, NULL);
Christoph Hellwigf898d6c2005-06-21 15:40:48 +1000660 pincount = xfs_flush_buftarg(mp->m_ddev_targp, 1);
661 if (!pincount) {
662 delay(50);
663 count++;
664 }
665 } while (count < 2);
666
667 return 0;
668}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
David Chinner516b2e72007-06-18 16:50:48 +1000670/*
671 * Second stage of a quiesce. The data is already synced, now we have to take
672 * care of the metadata. New transactions are already blocked, so we need to
673 * wait for any remaining transactions to drain out before proceding.
674 */
Christoph Hellwig9909c4a2007-10-11 18:11:14 +1000675void
David Chinner516b2e72007-06-18 16:50:48 +1000676xfs_attr_quiesce(
677 xfs_mount_t *mp)
678{
679 /* wait for all modifications to complete */
680 while (atomic_read(&mp->m_active_trans) > 0)
681 delay(100);
682
683 /* flush inodes and push all remaining buffers out to disk */
684 xfs_quiesce_fs(mp);
685
686 ASSERT_ALWAYS(atomic_read(&mp->m_active_trans) == 0);
687
688 /* Push the superblock and write an unmount record */
689 xfs_log_sbcount(mp, 1);
690 xfs_log_unmount_write(mp);
691 xfs_unmountfs_writesb(mp);
692}
693
Christoph Hellwig48c872a9f2007-08-30 17:20:31 +1000694int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695xfs_mntupdate(
Christoph Hellwig48c872a9f2007-08-30 17:20:31 +1000696 struct xfs_mount *mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 int *flags,
698 struct xfs_mount_args *args)
699{
Nathan Scottd08d3892006-05-08 19:51:28 +1000700 if (!(*flags & MS_RDONLY)) { /* rw/ro -> rw */
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000701 if (mp->m_flags & XFS_MOUNT_RDONLY)
702 mp->m_flags &= ~XFS_MOUNT_RDONLY;
Nathan Scottd08d3892006-05-08 19:51:28 +1000703 if (args->flags & XFSMNT_BARRIER) {
704 mp->m_flags |= XFS_MOUNT_BARRIER;
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100705 xfs_mountfs_check_barriers(mp);
Nathan Scottd08d3892006-05-08 19:51:28 +1000706 } else {
707 mp->m_flags &= ~XFS_MOUNT_BARRIER;
708 }
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000709 } else if (!(mp->m_flags & XFS_MOUNT_RDONLY)) { /* rw -> ro */
David Chinner2a82b8b2007-07-11 11:09:12 +1000710 xfs_filestream_flush(mp);
Christoph Hellwig745f6912007-08-30 17:20:39 +1000711 xfs_sync(mp, SYNC_DATA_QUIESCE);
David Chinner516b2e72007-06-18 16:50:48 +1000712 xfs_attr_quiesce(mp);
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000713 mp->m_flags |= XFS_MOUNT_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 return 0;
716}
717
718/*
719 * xfs_unmount_flush implements a set of flush operation on special
720 * inodes, which are needed as a separate set of operations so that
721 * they can be called as part of relocation process.
722 */
723int
724xfs_unmount_flush(
725 xfs_mount_t *mp, /* Mount structure we are getting
726 rid of. */
727 int relocation) /* Called from vfs relocation. */
728{
729 xfs_inode_t *rip = mp->m_rootip;
730 xfs_inode_t *rbmip;
731 xfs_inode_t *rsumip = NULL;
Nathan Scott67fcaa72006-06-09 17:00:52 +1000732 bhv_vnode_t *rvp = XFS_ITOV(rip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 int error;
734
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +1000735 xfs_ilock(rip, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 xfs_iflock(rip);
737
738 /*
739 * Flush out the real time inodes.
740 */
741 if ((rbmip = mp->m_rbmip) != NULL) {
742 xfs_ilock(rbmip, XFS_ILOCK_EXCL);
743 xfs_iflock(rbmip);
744 error = xfs_iflush(rbmip, XFS_IFLUSH_SYNC);
745 xfs_iunlock(rbmip, XFS_ILOCK_EXCL);
746
747 if (error == EFSCORRUPTED)
748 goto fscorrupt_out;
749
750 ASSERT(vn_count(XFS_ITOV(rbmip)) == 1);
751
752 rsumip = mp->m_rsumip;
753 xfs_ilock(rsumip, XFS_ILOCK_EXCL);
754 xfs_iflock(rsumip);
755 error = xfs_iflush(rsumip, XFS_IFLUSH_SYNC);
756 xfs_iunlock(rsumip, XFS_ILOCK_EXCL);
757
758 if (error == EFSCORRUPTED)
759 goto fscorrupt_out;
760
761 ASSERT(vn_count(XFS_ITOV(rsumip)) == 1);
762 }
763
764 /*
765 * Synchronously flush root inode to disk
766 */
767 error = xfs_iflush(rip, XFS_IFLUSH_SYNC);
768 if (error == EFSCORRUPTED)
769 goto fscorrupt_out2;
770
771 if (vn_count(rvp) != 1 && !relocation) {
772 xfs_iunlock(rip, XFS_ILOCK_EXCL);
773 return XFS_ERROR(EBUSY);
774 }
775
776 /*
777 * Release dquot that rootinode, rbmino and rsumino might be holding,
778 * flush and purge the quota inodes.
779 */
780 error = XFS_QM_UNMOUNT(mp);
781 if (error == EFSCORRUPTED)
782 goto fscorrupt_out2;
783
784 if (rbmip) {
785 VN_RELE(XFS_ITOV(rbmip));
786 VN_RELE(XFS_ITOV(rsumip));
787 }
788
789 xfs_iunlock(rip, XFS_ILOCK_EXCL);
790 return 0;
791
792fscorrupt_out:
793 xfs_ifunlock(rip);
794
795fscorrupt_out2:
796 xfs_iunlock(rip, XFS_ILOCK_EXCL);
797
798 return XFS_ERROR(EFSCORRUPTED);
799}
800
801/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 * xfs_sync flushes any pending I/O to file system vfsp.
803 *
804 * This routine is called by vfs_sync() to make sure that things make it
805 * out to disk eventually, on sync() system calls to flush out everything,
806 * and when the file system is unmounted. For the vfs_sync() case, all
807 * we really need to do is sync out the log to make all of our meta-data
808 * updates permanent (except for timestamps). For calls from pflushd(),
809 * dirty pages are kept moving by calling pdflush() on the inodes
810 * containing them. We also flush the inodes that we can lock without
811 * sleeping and the superblock if we can lock it without sleeping from
812 * vfs_sync() so that items at the tail of the log are always moving out.
813 *
814 * Flags:
815 * SYNC_BDFLUSH - We're being called from vfs_sync() so we don't want
816 * to sleep if we can help it. All we really need
817 * to do is ensure that the log is synced at least
818 * periodically. We also push the inodes and
819 * superblock if we can lock them without sleeping
820 * and they are not pinned.
821 * SYNC_ATTR - We need to flush the inodes. If SYNC_BDFLUSH is not
822 * set, then we really want to lock each inode and flush
823 * it.
824 * SYNC_WAIT - All the flushes that take place in this call should
825 * be synchronous.
826 * SYNC_DELWRI - This tells us to push dirty pages associated with
827 * inodes. SYNC_WAIT and SYNC_BDFLUSH are used to
828 * determine if they should be flushed sync, async, or
829 * delwri.
830 * SYNC_CLOSE - This flag is passed when the system is being
Nathan Scottc41564b2006-03-29 08:55:14 +1000831 * unmounted. We should sync and invalidate everything.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 * SYNC_FSDATA - This indicates that the caller would like to make
833 * sure the superblock is safe on disk. We can ensure
Nathan Scottc41564b2006-03-29 08:55:14 +1000834 * this by simply making sure the log gets flushed
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 * if SYNC_BDFLUSH is set, and by actually writing it
836 * out otherwise.
David Chinner3c0dc772007-02-10 18:37:22 +1100837 * SYNC_IOWAIT - The caller wants us to wait for all data I/O to complete
838 * before we return (including direct I/O). Forms the drain
839 * side of the write barrier needed to safely quiesce the
840 * filesystem.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 *
842 */
Christoph Hellwig48c872a9f2007-08-30 17:20:31 +1000843int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844xfs_sync(
Christoph Hellwig48c872a9f2007-08-30 17:20:31 +1000845 xfs_mount_t *mp,
846 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847{
Christoph Hellwigb09cc772007-08-30 17:19:57 +1000848 int error;
849
850 /*
851 * Get the Quota Manager to flush the dquots.
852 *
853 * If XFS quota support is not enabled or this filesystem
854 * instance does not use quotas XFS_QM_DQSYNC will always
855 * return zero.
856 */
857 error = XFS_QM_DQSYNC(mp, flags);
858 if (error) {
859 /*
860 * If we got an IO error, we will be shutting down.
861 * So, there's nothing more for us to do here.
862 */
863 ASSERT(error != EIO || XFS_FORCED_SHUTDOWN(mp));
864 if (XFS_FORCED_SHUTDOWN(mp))
865 return XFS_ERROR(error);
866 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
David Chinner2a82b8b2007-07-11 11:09:12 +1000868 if (flags & SYNC_IOWAIT)
869 xfs_filestream_flush(mp);
870
David Chinner28239452007-02-10 18:36:40 +1100871 return xfs_syncsub(mp, flags, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872}
873
874/*
875 * xfs sync routine for internal use
876 *
Nathan Scottb83bd132006-06-09 16:48:30 +1000877 * This routine supports all of the flags defined for the generic vfs_sync
Lachlan McIlroy1f9b3b62007-02-10 18:35:33 +1100878 * interface as explained above under xfs_sync.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 *
880 */
Nathan Scottee348072005-11-02 10:32:38 +1100881int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882xfs_sync_inodes(
883 xfs_mount_t *mp,
884 int flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 int *bypassed)
886{
887 xfs_inode_t *ip = NULL;
Nathan Scott67fcaa72006-06-09 17:00:52 +1000888 bhv_vnode_t *vp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 int error;
890 int last_error;
891 uint64_t fflag;
892 uint lock_flags;
893 uint base_lock_flags;
894 boolean_t mount_locked;
895 boolean_t vnode_refed;
896 int preempt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 xfs_iptr_t *ipointer;
898#ifdef DEBUG
899 boolean_t ipointer_in = B_FALSE;
900
901#define IPOINTER_SET ipointer_in = B_TRUE
902#define IPOINTER_CLR ipointer_in = B_FALSE
903#else
904#define IPOINTER_SET
905#define IPOINTER_CLR
906#endif
907
908
909/* Insert a marker record into the inode list after inode ip. The list
910 * must be locked when this is called. After the call the list will no
911 * longer be locked.
912 */
913#define IPOINTER_INSERT(ip, mp) { \
914 ASSERT(ipointer_in == B_FALSE); \
915 ipointer->ip_mnext = ip->i_mnext; \
916 ipointer->ip_mprev = ip; \
917 ip->i_mnext = (xfs_inode_t *)ipointer; \
918 ipointer->ip_mnext->i_mprev = (xfs_inode_t *)ipointer; \
919 preempt = 0; \
920 XFS_MOUNT_IUNLOCK(mp); \
921 mount_locked = B_FALSE; \
922 IPOINTER_SET; \
923 }
924
925/* Remove the marker from the inode list. If the marker was the only item
926 * in the list then there are no remaining inodes and we should zero out
927 * the whole list. If we are the current head of the list then move the head
928 * past us.
929 */
930#define IPOINTER_REMOVE(ip, mp) { \
931 ASSERT(ipointer_in == B_TRUE); \
932 if (ipointer->ip_mnext != (xfs_inode_t *)ipointer) { \
933 ip = ipointer->ip_mnext; \
934 ip->i_mprev = ipointer->ip_mprev; \
935 ipointer->ip_mprev->i_mnext = ip; \
936 if (mp->m_inodes == (xfs_inode_t *)ipointer) { \
937 mp->m_inodes = ip; \
938 } \
939 } else { \
940 ASSERT(mp->m_inodes == (xfs_inode_t *)ipointer); \
941 mp->m_inodes = NULL; \
942 ip = NULL; \
943 } \
944 IPOINTER_CLR; \
945 }
946
947#define XFS_PREEMPT_MASK 0x7f
948
Lachlan McIlroy44866d32007-09-14 15:21:08 +1000949 ASSERT(!(flags & SYNC_BDFLUSH));
950
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 if (bypassed)
952 *bypassed = 0;
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000953 if (mp->m_flags & XFS_MOUNT_RDONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 return 0;
955 error = 0;
956 last_error = 0;
957 preempt = 0;
958
959 /* Allocate a reference marker */
960 ipointer = (xfs_iptr_t *)kmem_zalloc(sizeof(xfs_iptr_t), KM_SLEEP);
961
962 fflag = XFS_B_ASYNC; /* default is don't wait */
Lachlan McIlroy44866d32007-09-14 15:21:08 +1000963 if (flags & SYNC_DELWRI)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 fflag = XFS_B_DELWRI;
965 if (flags & SYNC_WAIT)
966 fflag = 0; /* synchronous overrides all */
967
968 base_lock_flags = XFS_ILOCK_SHARED;
969 if (flags & (SYNC_DELWRI | SYNC_CLOSE)) {
970 /*
971 * We need the I/O lock if we're going to call any of
972 * the flush/inval routines.
973 */
974 base_lock_flags |= XFS_IOLOCK_SHARED;
975 }
976
977 XFS_MOUNT_ILOCK(mp);
978
979 ip = mp->m_inodes;
980
981 mount_locked = B_TRUE;
982 vnode_refed = B_FALSE;
983
984 IPOINTER_CLR;
985
986 do {
987 ASSERT(ipointer_in == B_FALSE);
988 ASSERT(vnode_refed == B_FALSE);
989
990 lock_flags = base_lock_flags;
991
992 /*
993 * There were no inodes in the list, just break out
994 * of the loop.
995 */
996 if (ip == NULL) {
997 break;
998 }
999
1000 /*
1001 * We found another sync thread marker - skip it
1002 */
1003 if (ip->i_mount == NULL) {
1004 ip = ip->i_mnext;
1005 continue;
1006 }
1007
1008 vp = XFS_ITOV_NULL(ip);
1009
1010 /*
1011 * If the vnode is gone then this is being torn down,
1012 * call reclaim if it is flushed, else let regular flush
1013 * code deal with it later in the loop.
1014 */
1015
1016 if (vp == NULL) {
1017 /* Skip ones already in reclaim */
1018 if (ip->i_flags & XFS_IRECLAIM) {
1019 ip = ip->i_mnext;
1020 continue;
1021 }
1022 if (xfs_ilock_nowait(ip, XFS_ILOCK_EXCL) == 0) {
1023 ip = ip->i_mnext;
1024 } else if ((xfs_ipincount(ip) == 0) &&
1025 xfs_iflock_nowait(ip)) {
1026 IPOINTER_INSERT(ip, mp);
1027
1028 xfs_finish_reclaim(ip, 1,
1029 XFS_IFLUSH_DELWRI_ELSE_ASYNC);
1030
1031 XFS_MOUNT_ILOCK(mp);
1032 mount_locked = B_TRUE;
1033 IPOINTER_REMOVE(ip, mp);
1034 } else {
1035 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1036 ip = ip->i_mnext;
1037 }
1038 continue;
1039 }
1040
1041 if (VN_BAD(vp)) {
1042 ip = ip->i_mnext;
1043 continue;
1044 }
1045
1046 if (XFS_FORCED_SHUTDOWN(mp) && !(flags & SYNC_CLOSE)) {
1047 XFS_MOUNT_IUNLOCK(mp);
1048 kmem_free(ipointer, sizeof(xfs_iptr_t));
1049 return 0;
1050 }
1051
1052 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 * Try to lock without sleeping. We're out of order with
1054 * the inode list lock here, so if we fail we need to drop
1055 * the mount lock and try again. If we're called from
1056 * bdflush() here, then don't bother.
1057 *
1058 * The inode lock here actually coordinates with the
1059 * almost spurious inode lock in xfs_ireclaim() to prevent
1060 * the vnode we handle here without a reference from
1061 * being freed while we reference it. If we lock the inode
1062 * while it's on the mount list here, then the spurious inode
1063 * lock in xfs_ireclaim() after the inode is pulled from
1064 * the mount list will sleep until we release it here.
1065 * This keeps the vnode from being freed while we reference
Christoph Hellwigcdb62682005-09-02 16:24:19 +10001066 * it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 */
1068 if (xfs_ilock_nowait(ip, lock_flags) == 0) {
Lachlan McIlroy44866d32007-09-14 15:21:08 +10001069 if (vp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 ip = ip->i_mnext;
1071 continue;
1072 }
1073
Christoph Hellwigcdb62682005-09-02 16:24:19 +10001074 vp = vn_grab(vp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 if (vp == NULL) {
Christoph Hellwigcdb62682005-09-02 16:24:19 +10001076 ip = ip->i_mnext;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 continue;
1078 }
1079
Christoph Hellwigcdb62682005-09-02 16:24:19 +10001080 IPOINTER_INSERT(ip, mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 xfs_ilock(ip, lock_flags);
1082
1083 ASSERT(vp == XFS_ITOV(ip));
1084 ASSERT(ip->i_mount == mp);
1085
1086 vnode_refed = B_TRUE;
1087 }
1088
1089 /* From here on in the loop we may have a marker record
1090 * in the inode list.
1091 */
1092
David Chinner40095b62007-05-14 18:24:09 +10001093 /*
1094 * If we have to flush data or wait for I/O completion
1095 * we need to drop the ilock that we currently hold.
1096 * If we need to drop the lock, insert a marker if we
1097 * have not already done so.
1098 */
1099 if ((flags & (SYNC_CLOSE|SYNC_IOWAIT)) ||
1100 ((flags & SYNC_DELWRI) && VN_DIRTY(vp))) {
1101 if (mount_locked) {
1102 IPOINTER_INSERT(ip, mp);
1103 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 xfs_iunlock(ip, XFS_ILOCK_SHARED);
1105
David Chinner40095b62007-05-14 18:24:09 +10001106 if (flags & SYNC_CLOSE) {
1107 /* Shutdown case. Flush and invalidate. */
1108 if (XFS_FORCED_SHUTDOWN(mp))
Christoph Hellwig739bfb22007-08-29 10:58:01 +10001109 xfs_tosspages(ip, 0, -1,
1110 FI_REMAPF);
David Chinner40095b62007-05-14 18:24:09 +10001111 else
Christoph Hellwig739bfb22007-08-29 10:58:01 +10001112 error = xfs_flushinval_pages(ip,
1113 0, -1, FI_REMAPF);
David Chinner40095b62007-05-14 18:24:09 +10001114 } else if ((flags & SYNC_DELWRI) && VN_DIRTY(vp)) {
Christoph Hellwig739bfb22007-08-29 10:58:01 +10001115 error = xfs_flush_pages(ip, 0,
Nathan Scott67fcaa72006-06-09 17:00:52 +10001116 -1, fflag, FI_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 }
1118
David Chinner40095b62007-05-14 18:24:09 +10001119 /*
1120 * When freezing, we need to wait ensure all I/O (including direct
1121 * I/O) is complete to ensure no further data modification can take
1122 * place after this point
1123 */
1124 if (flags & SYNC_IOWAIT)
Christoph Hellwigb677c212007-08-29 11:46:28 +10001125 vn_iowait(ip);
David Chinner40095b62007-05-14 18:24:09 +10001126
1127 xfs_ilock(ip, XFS_ILOCK_SHARED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 }
1129
Lachlan McIlroy44866d32007-09-14 15:21:08 +10001130 if ((flags & SYNC_ATTR) &&
1131 (ip->i_update_core ||
1132 (ip->i_itemp && ip->i_itemp->ili_format.ilf_fields))) {
1133 if (mount_locked)
1134 IPOINTER_INSERT(ip, mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
Lachlan McIlroy44866d32007-09-14 15:21:08 +10001136 if (flags & SYNC_WAIT) {
1137 xfs_iflock(ip);
1138 error = xfs_iflush(ip, XFS_IFLUSH_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
Lachlan McIlroy44866d32007-09-14 15:21:08 +10001140 /*
1141 * If we can't acquire the flush lock, then the inode
1142 * is already being flushed so don't bother waiting.
1143 *
1144 * If we can lock it then do a delwri flush so we can
1145 * combine multiple inode flushes in each disk write.
1146 */
1147 } else if (xfs_iflock_nowait(ip)) {
1148 error = xfs_iflush(ip, XFS_IFLUSH_DELWRI);
1149 } else if (bypassed) {
1150 (*bypassed)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 }
1152 }
1153
1154 if (lock_flags != 0) {
1155 xfs_iunlock(ip, lock_flags);
1156 }
1157
1158 if (vnode_refed) {
1159 /*
1160 * If we had to take a reference on the vnode
1161 * above, then wait until after we've unlocked
1162 * the inode to release the reference. This is
1163 * because we can be already holding the inode
1164 * lock when VN_RELE() calls xfs_inactive().
1165 *
1166 * Make sure to drop the mount lock before calling
1167 * VN_RELE() so that we don't trip over ourselves if
1168 * we have to go for the mount lock again in the
1169 * inactive code.
1170 */
1171 if (mount_locked) {
1172 IPOINTER_INSERT(ip, mp);
1173 }
1174
1175 VN_RELE(vp);
1176
1177 vnode_refed = B_FALSE;
1178 }
1179
1180 if (error) {
1181 last_error = error;
1182 }
1183
1184 /*
1185 * bail out if the filesystem is corrupted.
1186 */
1187 if (error == EFSCORRUPTED) {
1188 if (!mount_locked) {
1189 XFS_MOUNT_ILOCK(mp);
1190 IPOINTER_REMOVE(ip, mp);
1191 }
1192 XFS_MOUNT_IUNLOCK(mp);
1193 ASSERT(ipointer_in == B_FALSE);
1194 kmem_free(ipointer, sizeof(xfs_iptr_t));
1195 return XFS_ERROR(error);
1196 }
1197
1198 /* Let other threads have a chance at the mount lock
1199 * if we have looped many times without dropping the
1200 * lock.
1201 */
1202 if ((++preempt & XFS_PREEMPT_MASK) == 0) {
1203 if (mount_locked) {
1204 IPOINTER_INSERT(ip, mp);
1205 }
1206 }
1207
1208 if (mount_locked == B_FALSE) {
1209 XFS_MOUNT_ILOCK(mp);
1210 mount_locked = B_TRUE;
1211 IPOINTER_REMOVE(ip, mp);
1212 continue;
1213 }
1214
1215 ASSERT(ipointer_in == B_FALSE);
1216 ip = ip->i_mnext;
1217
1218 } while (ip != mp->m_inodes);
1219
1220 XFS_MOUNT_IUNLOCK(mp);
1221
1222 ASSERT(ipointer_in == B_FALSE);
1223
1224 kmem_free(ipointer, sizeof(xfs_iptr_t));
1225 return XFS_ERROR(last_error);
1226}
1227
1228/*
1229 * xfs sync routine for internal use
1230 *
Nathan Scottb83bd132006-06-09 16:48:30 +10001231 * This routine supports all of the flags defined for the generic vfs_sync
Lachlan McIlroy1f9b3b62007-02-10 18:35:33 +11001232 * interface as explained above under xfs_sync.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 *
1234 */
1235int
1236xfs_syncsub(
1237 xfs_mount_t *mp,
1238 int flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 int *bypassed)
1240{
1241 int error = 0;
1242 int last_error = 0;
1243 uint log_flags = XFS_LOG_FORCE;
1244 xfs_buf_t *bp;
1245 xfs_buf_log_item_t *bip;
1246
1247 /*
1248 * Sync out the log. This ensures that the log is periodically
1249 * flushed even if there is not enough activity to fill it up.
1250 */
1251 if (flags & SYNC_WAIT)
1252 log_flags |= XFS_LOG_SYNC;
1253
1254 xfs_log_force(mp, (xfs_lsn_t)0, log_flags);
1255
1256 if (flags & (SYNC_ATTR|SYNC_DELWRI)) {
1257 if (flags & SYNC_BDFLUSH)
1258 xfs_finish_reclaim_all(mp, 1);
1259 else
Lachlan McIlroy1f9b3b62007-02-10 18:35:33 +11001260 error = xfs_sync_inodes(mp, flags, bypassed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 }
1262
1263 /*
1264 * Flushing out dirty data above probably generated more
1265 * log activity, so if this isn't vfs_sync() then flush
1266 * the log again.
1267 */
1268 if (flags & SYNC_DELWRI) {
1269 xfs_log_force(mp, (xfs_lsn_t)0, log_flags);
1270 }
1271
1272 if (flags & SYNC_FSDATA) {
1273 /*
1274 * If this is vfs_sync() then only sync the superblock
1275 * if we can lock it without sleeping and it is not pinned.
1276 */
1277 if (flags & SYNC_BDFLUSH) {
1278 bp = xfs_getsb(mp, XFS_BUF_TRYLOCK);
1279 if (bp != NULL) {
1280 bip = XFS_BUF_FSPRIVATE(bp,xfs_buf_log_item_t*);
1281 if ((bip != NULL) &&
1282 xfs_buf_item_dirty(bip)) {
1283 if (!(XFS_BUF_ISPINNED(bp))) {
1284 XFS_BUF_ASYNC(bp);
1285 error = xfs_bwrite(mp, bp);
1286 } else {
1287 xfs_buf_relse(bp);
1288 }
1289 } else {
1290 xfs_buf_relse(bp);
1291 }
1292 }
1293 } else {
1294 bp = xfs_getsb(mp, 0);
1295 /*
1296 * If the buffer is pinned then push on the log so
1297 * we won't get stuck waiting in the write for
1298 * someone, maybe ourselves, to flush the log.
1299 * Even though we just pushed the log above, we
1300 * did not have the superblock buffer locked at
1301 * that point so it can become pinned in between
1302 * there and here.
1303 */
1304 if (XFS_BUF_ISPINNED(bp))
1305 xfs_log_force(mp, (xfs_lsn_t)0, XFS_LOG_FORCE);
1306 if (flags & SYNC_WAIT)
1307 XFS_BUF_UNASYNC(bp);
1308 else
1309 XFS_BUF_ASYNC(bp);
1310 error = xfs_bwrite(mp, bp);
1311 }
1312 if (error) {
1313 last_error = error;
1314 }
1315 }
1316
1317 /*
David Chinner92821e22007-05-24 15:26:31 +10001318 * If asked, update the disk superblock with incore counter values if we
1319 * are using non-persistent counters so that they don't get too far out
1320 * of sync if we crash or get a forced shutdown. We don't want to force
1321 * this to disk, just get a transaction into the iclogs....
1322 */
1323 if (flags & SYNC_SUPER)
1324 xfs_log_sbcount(mp, 0);
1325
1326 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 * Now check to see if the log needs a "dummy" transaction.
1328 */
1329
1330 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}