blob: 8b5a3376c2f71e73133dafa787fe7fd610731dff [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110019#include "xfs_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "xfs_types.h"
Nathan Scotta844f452005-11-02 14:38:42 +110021#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "xfs_log.h"
Nathan Scotta844f452005-11-02 14:38:42 +110023#include "xfs_inum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "xfs_trans.h"
25#include "xfs_sb.h"
Nathan Scotta844f452005-11-02 14:38:42 +110026#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "xfs_dir2.h"
28#include "xfs_dmapi.h"
29#include "xfs_mount.h"
Nathan Scotta844f452005-11-02 14:38:42 +110030#include "xfs_da_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include "xfs_bmap_btree.h"
32#include "xfs_ialloc_btree.h"
33#include "xfs_alloc_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110034#include "xfs_dir2_sf.h"
35#include "xfs_attr_sf.h"
36#include "xfs_dinode.h"
37#include "xfs_inode.h"
38#include "xfs_inode_item.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include "xfs_btree.h"
40#include "xfs_alloc.h"
41#include "xfs_ialloc.h"
Nathan Scotta844f452005-11-02 14:38:42 +110042#include "xfs_quota.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include "xfs_error.h"
44#include "xfs_bmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include "xfs_rw.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include "xfs_buf_item.h"
Nathan Scotta844f452005-11-02 14:38:42 +110047#include "xfs_log_priv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include "xfs_dir2_trace.h"
Nathan Scotta844f452005-11-02 14:38:42 +110049#include "xfs_extfree_item.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include "xfs_acl.h"
51#include "xfs_attr.h"
52#include "xfs_clnt.h"
David Chinner2a82b8b2007-07-11 11:09:12 +100053#include "xfs_mru_cache.h"
54#include "xfs_filestream.h"
Christoph Hellwige13a73f2006-01-11 15:30:08 +110055#include "xfs_fsops.h"
Christoph Hellwig739bfb22007-08-29 10:58:01 +100056#include "xfs_vnodeops.h"
Christoph Hellwig48c872a9f2007-08-30 17:20:31 +100057#include "xfs_vfsops.h"
Christoph Hellwig43355092008-03-27 18:01:08 +110058#include "xfs_utils.h"
Christoph Hellwig739bfb22007-08-29 10:58:01 +100059
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Lachlan McIlroyde2eeea2008-02-06 13:37:56 +110061int __init
Linus Torvalds1da177e2005-04-16 15:20:36 -070062xfs_init(void)
63{
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#ifdef XFS_DABUF_DEBUG
Eric Sandeen703e1f02007-10-11 17:41:21 +100065 extern spinlock_t xfs_dabuf_global_lock;
Eric Sandeen007c61c2007-10-11 17:43:56 +100066 spin_lock_init(&xfs_dabuf_global_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#endif
68
69 /*
70 * Initialize all of the zone allocators we use.
71 */
David Chinnereb01c9c2008-04-10 12:18:46 +100072 xfs_log_ticket_zone = kmem_zone_init(sizeof(xlog_ticket_t),
73 "xfs_log_ticket");
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 xfs_bmap_free_item_zone = kmem_zone_init(sizeof(xfs_bmap_free_item_t),
David Chinnereb01c9c2008-04-10 12:18:46 +100075 "xfs_bmap_free_item");
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 xfs_btree_cur_zone = kmem_zone_init(sizeof(xfs_btree_cur_t),
David Chinnereb01c9c2008-04-10 12:18:46 +100077 "xfs_btree_cur");
78 xfs_da_state_zone = kmem_zone_init(sizeof(xfs_da_state_t),
79 "xfs_da_state");
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 xfs_dabuf_zone = kmem_zone_init(sizeof(xfs_dabuf_t), "xfs_dabuf");
Nathan Scott87582802006-03-14 13:18:19 +110081 xfs_ifork_zone = kmem_zone_init(sizeof(xfs_ifork_t), "xfs_ifork");
David Chinnereb01c9c2008-04-10 12:18:46 +100082 xfs_trans_zone = kmem_zone_init(sizeof(xfs_trans_t), "xfs_trans");
Nathan Scott87582802006-03-14 13:18:19 +110083 xfs_acl_zone_init(xfs_acl_zone, "xfs_acl");
David Chinner2a82b8b2007-07-11 11:09:12 +100084 xfs_mru_cache_init();
85 xfs_filestream_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87 /*
88 * The size of the zone allocated buf log item is the maximum
89 * size possible under XFS. This wastes a little bit of memory,
90 * but it is much faster.
91 */
92 xfs_buf_item_zone =
93 kmem_zone_init((sizeof(xfs_buf_log_item_t) +
94 (((XFS_MAX_BLOCKSIZE / XFS_BLI_CHUNK) /
95 NBWORD) * sizeof(int))),
96 "xfs_buf_item");
Nathan Scott87582802006-03-14 13:18:19 +110097 xfs_efd_zone =
98 kmem_zone_init((sizeof(xfs_efd_log_item_t) +
99 ((XFS_EFD_MAX_FAST_EXTENTS - 1) *
100 sizeof(xfs_extent_t))),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 "xfs_efd_item");
Nathan Scott87582802006-03-14 13:18:19 +1100102 xfs_efi_zone =
103 kmem_zone_init((sizeof(xfs_efi_log_item_t) +
104 ((XFS_EFI_MAX_FAST_EXTENTS - 1) *
105 sizeof(xfs_extent_t))),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 "xfs_efi_item");
Nathan Scott87582802006-03-14 13:18:19 +1100107
108 /*
109 * These zones warrant special memory allocator hints
110 */
111 xfs_inode_zone =
112 kmem_zone_init_flags(sizeof(xfs_inode_t), "xfs_inode",
113 KM_ZONE_HWALIGN | KM_ZONE_RECLAIM |
114 KM_ZONE_SPREAD, NULL);
115 xfs_ili_zone =
116 kmem_zone_init_flags(sizeof(xfs_inode_log_item_t), "xfs_ili",
117 KM_ZONE_SPREAD, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
119 /*
120 * Allocate global trace buffers.
121 */
122#ifdef XFS_ALLOC_TRACE
123 xfs_alloc_trace_buf = ktrace_alloc(XFS_ALLOC_TRACE_SIZE, KM_SLEEP);
124#endif
125#ifdef XFS_BMAP_TRACE
126 xfs_bmap_trace_buf = ktrace_alloc(XFS_BMAP_TRACE_SIZE, KM_SLEEP);
127#endif
128#ifdef XFS_BMBT_TRACE
129 xfs_bmbt_trace_buf = ktrace_alloc(XFS_BMBT_TRACE_SIZE, KM_SLEEP);
130#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131#ifdef XFS_ATTR_TRACE
132 xfs_attr_trace_buf = ktrace_alloc(XFS_ATTR_TRACE_SIZE, KM_SLEEP);
133#endif
134#ifdef XFS_DIR2_TRACE
135 xfs_dir2_trace_buf = ktrace_alloc(XFS_DIR2_GTRACE_SIZE, KM_SLEEP);
136#endif
137
138 xfs_dir_startup();
139
140#if (defined(DEBUG) || defined(INDUCE_IO_ERROR))
141 xfs_error_test_init();
142#endif /* DEBUG || INDUCE_IO_ERROR */
143
144 xfs_init_procfs();
145 xfs_sysctl_register();
146 return 0;
147}
148
Lachlan McIlroyde2eeea2008-02-06 13:37:56 +1100149void __exit
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150xfs_cleanup(void)
151{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 extern kmem_zone_t *xfs_inode_zone;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 extern kmem_zone_t *xfs_efd_zone;
154 extern kmem_zone_t *xfs_efi_zone;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
156 xfs_cleanup_procfs();
157 xfs_sysctl_unregister();
David Chinner2a82b8b2007-07-11 11:09:12 +1000158 xfs_filestream_uninit();
159 xfs_mru_cache_uninit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 xfs_acl_zone_destroy(xfs_acl_zone);
161
162#ifdef XFS_DIR2_TRACE
163 ktrace_free(xfs_dir2_trace_buf);
164#endif
165#ifdef XFS_ATTR_TRACE
166 ktrace_free(xfs_attr_trace_buf);
167#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168#ifdef XFS_BMBT_TRACE
169 ktrace_free(xfs_bmbt_trace_buf);
170#endif
171#ifdef XFS_BMAP_TRACE
172 ktrace_free(xfs_bmap_trace_buf);
173#endif
174#ifdef XFS_ALLOC_TRACE
175 ktrace_free(xfs_alloc_trace_buf);
176#endif
177
Nathan Scott3758dee2006-03-22 12:47:28 +1100178 kmem_zone_destroy(xfs_bmap_free_item_zone);
179 kmem_zone_destroy(xfs_btree_cur_zone);
180 kmem_zone_destroy(xfs_inode_zone);
181 kmem_zone_destroy(xfs_trans_zone);
182 kmem_zone_destroy(xfs_da_state_zone);
183 kmem_zone_destroy(xfs_dabuf_zone);
184 kmem_zone_destroy(xfs_buf_item_zone);
185 kmem_zone_destroy(xfs_efd_zone);
186 kmem_zone_destroy(xfs_efi_zone);
187 kmem_zone_destroy(xfs_ifork_zone);
188 kmem_zone_destroy(xfs_ili_zone);
Alexey Dobriyanfe0754f2008-04-29 12:53:08 +1000189 kmem_zone_destroy(xfs_log_ticket_zone);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190}
191
David Chinner3c85c362008-04-10 12:19:40 +1000192STATIC void
Christoph Hellwigf898d6c2005-06-21 15:40:48 +1000193xfs_quiesce_fs(
194 xfs_mount_t *mp)
195{
196 int count = 0, pincount;
Nathan Scott3758dee2006-03-22 12:47:28 +1100197
Christoph Hellwigf898d6c2005-06-21 15:40:48 +1000198 xfs_flush_buftarg(mp->m_ddev_targp, 0);
199 xfs_finish_reclaim_all(mp, 0);
200
201 /* This loop must run at least twice.
202 * The first instance of the loop will flush
203 * most meta data but that will generate more
204 * meta data (typically directory updates).
205 * Which then must be flushed and logged before
206 * we can write the unmount record.
Nathan Scott3758dee2006-03-22 12:47:28 +1100207 */
Christoph Hellwigf898d6c2005-06-21 15:40:48 +1000208 do {
David Chinner516b2e72007-06-18 16:50:48 +1000209 xfs_syncsub(mp, SYNC_INODE_QUIESCE, NULL);
Christoph Hellwigf898d6c2005-06-21 15:40:48 +1000210 pincount = xfs_flush_buftarg(mp->m_ddev_targp, 1);
211 if (!pincount) {
212 delay(50);
213 count++;
214 }
215 } while (count < 2);
Christoph Hellwigf898d6c2005-06-21 15:40:48 +1000216}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
David Chinner516b2e72007-06-18 16:50:48 +1000218/*
219 * Second stage of a quiesce. The data is already synced, now we have to take
220 * care of the metadata. New transactions are already blocked, so we need to
221 * wait for any remaining transactions to drain out before proceding.
222 */
Christoph Hellwig9909c4a2007-10-11 18:11:14 +1000223void
David Chinner516b2e72007-06-18 16:50:48 +1000224xfs_attr_quiesce(
225 xfs_mount_t *mp)
226{
David Chinnere5720ee2008-04-10 12:21:18 +1000227 int error = 0;
228
David Chinner516b2e72007-06-18 16:50:48 +1000229 /* wait for all modifications to complete */
230 while (atomic_read(&mp->m_active_trans) > 0)
231 delay(100);
232
233 /* flush inodes and push all remaining buffers out to disk */
234 xfs_quiesce_fs(mp);
235
236 ASSERT_ALWAYS(atomic_read(&mp->m_active_trans) == 0);
237
238 /* Push the superblock and write an unmount record */
David Chinnere5720ee2008-04-10 12:21:18 +1000239 error = xfs_log_sbcount(mp, 1);
240 if (error)
241 xfs_fs_cmn_err(CE_WARN, mp,
242 "xfs_attr_quiesce: failed to log sb changes. "
243 "Frozen image may not be consistent.");
David Chinner516b2e72007-06-18 16:50:48 +1000244 xfs_log_unmount_write(mp);
245 xfs_unmountfs_writesb(mp);
246}
247
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248/*
249 * xfs_unmount_flush implements a set of flush operation on special
250 * inodes, which are needed as a separate set of operations so that
251 * they can be called as part of relocation process.
252 */
253int
254xfs_unmount_flush(
255 xfs_mount_t *mp, /* Mount structure we are getting
256 rid of. */
257 int relocation) /* Called from vfs relocation. */
258{
259 xfs_inode_t *rip = mp->m_rootip;
260 xfs_inode_t *rbmip;
261 xfs_inode_t *rsumip = NULL;
Nathan Scott67fcaa72006-06-09 17:00:52 +1000262 bhv_vnode_t *rvp = XFS_ITOV(rip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 int error;
264
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +1000265 xfs_ilock(rip, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 xfs_iflock(rip);
267
268 /*
269 * Flush out the real time inodes.
270 */
271 if ((rbmip = mp->m_rbmip) != NULL) {
272 xfs_ilock(rbmip, XFS_ILOCK_EXCL);
273 xfs_iflock(rbmip);
274 error = xfs_iflush(rbmip, XFS_IFLUSH_SYNC);
275 xfs_iunlock(rbmip, XFS_ILOCK_EXCL);
276
277 if (error == EFSCORRUPTED)
278 goto fscorrupt_out;
279
280 ASSERT(vn_count(XFS_ITOV(rbmip)) == 1);
281
282 rsumip = mp->m_rsumip;
283 xfs_ilock(rsumip, XFS_ILOCK_EXCL);
284 xfs_iflock(rsumip);
285 error = xfs_iflush(rsumip, XFS_IFLUSH_SYNC);
286 xfs_iunlock(rsumip, XFS_ILOCK_EXCL);
287
288 if (error == EFSCORRUPTED)
289 goto fscorrupt_out;
290
291 ASSERT(vn_count(XFS_ITOV(rsumip)) == 1);
292 }
293
294 /*
295 * Synchronously flush root inode to disk
296 */
297 error = xfs_iflush(rip, XFS_IFLUSH_SYNC);
298 if (error == EFSCORRUPTED)
299 goto fscorrupt_out2;
300
301 if (vn_count(rvp) != 1 && !relocation) {
302 xfs_iunlock(rip, XFS_ILOCK_EXCL);
303 return XFS_ERROR(EBUSY);
304 }
305
306 /*
307 * Release dquot that rootinode, rbmino and rsumino might be holding,
308 * flush and purge the quota inodes.
309 */
310 error = XFS_QM_UNMOUNT(mp);
311 if (error == EFSCORRUPTED)
312 goto fscorrupt_out2;
313
314 if (rbmip) {
Christoph Hellwig43355092008-03-27 18:01:08 +1100315 IRELE(rbmip);
316 IRELE(rsumip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 }
318
319 xfs_iunlock(rip, XFS_ILOCK_EXCL);
320 return 0;
321
322fscorrupt_out:
323 xfs_ifunlock(rip);
324
325fscorrupt_out2:
326 xfs_iunlock(rip, XFS_ILOCK_EXCL);
327
328 return XFS_ERROR(EFSCORRUPTED);
329}
330
331/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 * xfs_sync flushes any pending I/O to file system vfsp.
333 *
334 * This routine is called by vfs_sync() to make sure that things make it
335 * out to disk eventually, on sync() system calls to flush out everything,
336 * and when the file system is unmounted. For the vfs_sync() case, all
337 * we really need to do is sync out the log to make all of our meta-data
338 * updates permanent (except for timestamps). For calls from pflushd(),
339 * dirty pages are kept moving by calling pdflush() on the inodes
340 * containing them. We also flush the inodes that we can lock without
341 * sleeping and the superblock if we can lock it without sleeping from
342 * vfs_sync() so that items at the tail of the log are always moving out.
343 *
344 * Flags:
345 * SYNC_BDFLUSH - We're being called from vfs_sync() so we don't want
346 * to sleep if we can help it. All we really need
347 * to do is ensure that the log is synced at least
348 * periodically. We also push the inodes and
349 * superblock if we can lock them without sleeping
350 * and they are not pinned.
351 * SYNC_ATTR - We need to flush the inodes. If SYNC_BDFLUSH is not
352 * set, then we really want to lock each inode and flush
353 * it.
354 * SYNC_WAIT - All the flushes that take place in this call should
355 * be synchronous.
356 * SYNC_DELWRI - This tells us to push dirty pages associated with
357 * inodes. SYNC_WAIT and SYNC_BDFLUSH are used to
358 * determine if they should be flushed sync, async, or
359 * delwri.
360 * SYNC_CLOSE - This flag is passed when the system is being
Nathan Scottc41564b2006-03-29 08:55:14 +1000361 * unmounted. We should sync and invalidate everything.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 * SYNC_FSDATA - This indicates that the caller would like to make
363 * sure the superblock is safe on disk. We can ensure
Nathan Scottc41564b2006-03-29 08:55:14 +1000364 * this by simply making sure the log gets flushed
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 * if SYNC_BDFLUSH is set, and by actually writing it
366 * out otherwise.
David Chinner3c0dc772007-02-10 18:37:22 +1100367 * SYNC_IOWAIT - The caller wants us to wait for all data I/O to complete
368 * before we return (including direct I/O). Forms the drain
369 * side of the write barrier needed to safely quiesce the
370 * filesystem.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 *
372 */
Christoph Hellwig48c872a9f2007-08-30 17:20:31 +1000373int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374xfs_sync(
Christoph Hellwig48c872a9f2007-08-30 17:20:31 +1000375 xfs_mount_t *mp,
376 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377{
Christoph Hellwigb09cc772007-08-30 17:19:57 +1000378 int error;
379
380 /*
381 * Get the Quota Manager to flush the dquots.
382 *
383 * If XFS quota support is not enabled or this filesystem
384 * instance does not use quotas XFS_QM_DQSYNC will always
385 * return zero.
386 */
387 error = XFS_QM_DQSYNC(mp, flags);
388 if (error) {
389 /*
390 * If we got an IO error, we will be shutting down.
391 * So, there's nothing more for us to do here.
392 */
393 ASSERT(error != EIO || XFS_FORCED_SHUTDOWN(mp));
394 if (XFS_FORCED_SHUTDOWN(mp))
395 return XFS_ERROR(error);
396 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
David Chinner2a82b8b2007-07-11 11:09:12 +1000398 if (flags & SYNC_IOWAIT)
399 xfs_filestream_flush(mp);
400
David Chinner28239452007-02-10 18:36:40 +1100401 return xfs_syncsub(mp, flags, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402}
403
404/*
405 * xfs sync routine for internal use
406 *
Nathan Scottb83bd132006-06-09 16:48:30 +1000407 * This routine supports all of the flags defined for the generic vfs_sync
Lachlan McIlroy1f9b3b62007-02-10 18:35:33 +1100408 * interface as explained above under xfs_sync.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 *
410 */
Nathan Scottee348072005-11-02 10:32:38 +1100411int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412xfs_sync_inodes(
413 xfs_mount_t *mp,
414 int flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 int *bypassed)
416{
417 xfs_inode_t *ip = NULL;
Nathan Scott67fcaa72006-06-09 17:00:52 +1000418 bhv_vnode_t *vp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 int error;
420 int last_error;
421 uint64_t fflag;
422 uint lock_flags;
423 uint base_lock_flags;
424 boolean_t mount_locked;
425 boolean_t vnode_refed;
426 int preempt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 xfs_iptr_t *ipointer;
428#ifdef DEBUG
429 boolean_t ipointer_in = B_FALSE;
430
431#define IPOINTER_SET ipointer_in = B_TRUE
432#define IPOINTER_CLR ipointer_in = B_FALSE
433#else
434#define IPOINTER_SET
435#define IPOINTER_CLR
436#endif
437
438
439/* Insert a marker record into the inode list after inode ip. The list
440 * must be locked when this is called. After the call the list will no
441 * longer be locked.
442 */
443#define IPOINTER_INSERT(ip, mp) { \
444 ASSERT(ipointer_in == B_FALSE); \
445 ipointer->ip_mnext = ip->i_mnext; \
446 ipointer->ip_mprev = ip; \
447 ip->i_mnext = (xfs_inode_t *)ipointer; \
448 ipointer->ip_mnext->i_mprev = (xfs_inode_t *)ipointer; \
449 preempt = 0; \
450 XFS_MOUNT_IUNLOCK(mp); \
451 mount_locked = B_FALSE; \
452 IPOINTER_SET; \
453 }
454
455/* Remove the marker from the inode list. If the marker was the only item
456 * in the list then there are no remaining inodes and we should zero out
457 * the whole list. If we are the current head of the list then move the head
458 * past us.
459 */
460#define IPOINTER_REMOVE(ip, mp) { \
461 ASSERT(ipointer_in == B_TRUE); \
462 if (ipointer->ip_mnext != (xfs_inode_t *)ipointer) { \
463 ip = ipointer->ip_mnext; \
464 ip->i_mprev = ipointer->ip_mprev; \
465 ipointer->ip_mprev->i_mnext = ip; \
466 if (mp->m_inodes == (xfs_inode_t *)ipointer) { \
467 mp->m_inodes = ip; \
468 } \
469 } else { \
470 ASSERT(mp->m_inodes == (xfs_inode_t *)ipointer); \
471 mp->m_inodes = NULL; \
472 ip = NULL; \
473 } \
474 IPOINTER_CLR; \
475 }
476
477#define XFS_PREEMPT_MASK 0x7f
478
Lachlan McIlroy44866d32007-09-14 15:21:08 +1000479 ASSERT(!(flags & SYNC_BDFLUSH));
480
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 if (bypassed)
482 *bypassed = 0;
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000483 if (mp->m_flags & XFS_MOUNT_RDONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 return 0;
485 error = 0;
486 last_error = 0;
487 preempt = 0;
488
489 /* Allocate a reference marker */
490 ipointer = (xfs_iptr_t *)kmem_zalloc(sizeof(xfs_iptr_t), KM_SLEEP);
491
492 fflag = XFS_B_ASYNC; /* default is don't wait */
Lachlan McIlroy44866d32007-09-14 15:21:08 +1000493 if (flags & SYNC_DELWRI)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 fflag = XFS_B_DELWRI;
495 if (flags & SYNC_WAIT)
496 fflag = 0; /* synchronous overrides all */
497
498 base_lock_flags = XFS_ILOCK_SHARED;
499 if (flags & (SYNC_DELWRI | SYNC_CLOSE)) {
500 /*
501 * We need the I/O lock if we're going to call any of
502 * the flush/inval routines.
503 */
504 base_lock_flags |= XFS_IOLOCK_SHARED;
505 }
506
507 XFS_MOUNT_ILOCK(mp);
508
509 ip = mp->m_inodes;
510
511 mount_locked = B_TRUE;
512 vnode_refed = B_FALSE;
513
514 IPOINTER_CLR;
515
516 do {
517 ASSERT(ipointer_in == B_FALSE);
518 ASSERT(vnode_refed == B_FALSE);
519
520 lock_flags = base_lock_flags;
521
522 /*
523 * There were no inodes in the list, just break out
524 * of the loop.
525 */
526 if (ip == NULL) {
527 break;
528 }
529
530 /*
531 * We found another sync thread marker - skip it
532 */
533 if (ip->i_mount == NULL) {
534 ip = ip->i_mnext;
535 continue;
536 }
537
538 vp = XFS_ITOV_NULL(ip);
539
540 /*
541 * If the vnode is gone then this is being torn down,
542 * call reclaim if it is flushed, else let regular flush
543 * code deal with it later in the loop.
544 */
545
546 if (vp == NULL) {
547 /* Skip ones already in reclaim */
548 if (ip->i_flags & XFS_IRECLAIM) {
549 ip = ip->i_mnext;
550 continue;
551 }
552 if (xfs_ilock_nowait(ip, XFS_ILOCK_EXCL) == 0) {
553 ip = ip->i_mnext;
554 } else if ((xfs_ipincount(ip) == 0) &&
555 xfs_iflock_nowait(ip)) {
556 IPOINTER_INSERT(ip, mp);
557
558 xfs_finish_reclaim(ip, 1,
559 XFS_IFLUSH_DELWRI_ELSE_ASYNC);
560
561 XFS_MOUNT_ILOCK(mp);
562 mount_locked = B_TRUE;
563 IPOINTER_REMOVE(ip, mp);
564 } else {
565 xfs_iunlock(ip, XFS_ILOCK_EXCL);
566 ip = ip->i_mnext;
567 }
568 continue;
569 }
570
571 if (VN_BAD(vp)) {
572 ip = ip->i_mnext;
573 continue;
574 }
575
576 if (XFS_FORCED_SHUTDOWN(mp) && !(flags & SYNC_CLOSE)) {
577 XFS_MOUNT_IUNLOCK(mp);
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000578 kmem_free(ipointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 return 0;
580 }
581
582 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 * Try to lock without sleeping. We're out of order with
584 * the inode list lock here, so if we fail we need to drop
585 * the mount lock and try again. If we're called from
586 * bdflush() here, then don't bother.
587 *
588 * The inode lock here actually coordinates with the
589 * almost spurious inode lock in xfs_ireclaim() to prevent
590 * the vnode we handle here without a reference from
591 * being freed while we reference it. If we lock the inode
592 * while it's on the mount list here, then the spurious inode
593 * lock in xfs_ireclaim() after the inode is pulled from
594 * the mount list will sleep until we release it here.
595 * This keeps the vnode from being freed while we reference
Christoph Hellwigcdb62682005-09-02 16:24:19 +1000596 * it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 */
598 if (xfs_ilock_nowait(ip, lock_flags) == 0) {
Lachlan McIlroy44866d32007-09-14 15:21:08 +1000599 if (vp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 ip = ip->i_mnext;
601 continue;
602 }
603
Christoph Hellwigcdb62682005-09-02 16:24:19 +1000604 vp = vn_grab(vp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 if (vp == NULL) {
Christoph Hellwigcdb62682005-09-02 16:24:19 +1000606 ip = ip->i_mnext;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 continue;
608 }
609
Christoph Hellwigcdb62682005-09-02 16:24:19 +1000610 IPOINTER_INSERT(ip, mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 xfs_ilock(ip, lock_flags);
612
613 ASSERT(vp == XFS_ITOV(ip));
614 ASSERT(ip->i_mount == mp);
615
616 vnode_refed = B_TRUE;
617 }
618
619 /* From here on in the loop we may have a marker record
620 * in the inode list.
621 */
622
David Chinner40095b62007-05-14 18:24:09 +1000623 /*
624 * If we have to flush data or wait for I/O completion
625 * we need to drop the ilock that we currently hold.
626 * If we need to drop the lock, insert a marker if we
627 * have not already done so.
628 */
629 if ((flags & (SYNC_CLOSE|SYNC_IOWAIT)) ||
630 ((flags & SYNC_DELWRI) && VN_DIRTY(vp))) {
631 if (mount_locked) {
632 IPOINTER_INSERT(ip, mp);
633 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 xfs_iunlock(ip, XFS_ILOCK_SHARED);
635
David Chinner40095b62007-05-14 18:24:09 +1000636 if (flags & SYNC_CLOSE) {
637 /* Shutdown case. Flush and invalidate. */
638 if (XFS_FORCED_SHUTDOWN(mp))
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000639 xfs_tosspages(ip, 0, -1,
640 FI_REMAPF);
David Chinner40095b62007-05-14 18:24:09 +1000641 else
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000642 error = xfs_flushinval_pages(ip,
643 0, -1, FI_REMAPF);
David Chinner40095b62007-05-14 18:24:09 +1000644 } else if ((flags & SYNC_DELWRI) && VN_DIRTY(vp)) {
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000645 error = xfs_flush_pages(ip, 0,
Nathan Scott67fcaa72006-06-09 17:00:52 +1000646 -1, fflag, FI_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 }
648
David Chinner40095b62007-05-14 18:24:09 +1000649 /*
650 * When freezing, we need to wait ensure all I/O (including direct
651 * I/O) is complete to ensure no further data modification can take
652 * place after this point
653 */
654 if (flags & SYNC_IOWAIT)
Christoph Hellwigb677c212007-08-29 11:46:28 +1000655 vn_iowait(ip);
David Chinner40095b62007-05-14 18:24:09 +1000656
657 xfs_ilock(ip, XFS_ILOCK_SHARED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 }
659
Lachlan McIlroy44866d32007-09-14 15:21:08 +1000660 if ((flags & SYNC_ATTR) &&
661 (ip->i_update_core ||
662 (ip->i_itemp && ip->i_itemp->ili_format.ilf_fields))) {
663 if (mount_locked)
664 IPOINTER_INSERT(ip, mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
Lachlan McIlroy44866d32007-09-14 15:21:08 +1000666 if (flags & SYNC_WAIT) {
667 xfs_iflock(ip);
668 error = xfs_iflush(ip, XFS_IFLUSH_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
Lachlan McIlroy44866d32007-09-14 15:21:08 +1000670 /*
671 * If we can't acquire the flush lock, then the inode
672 * is already being flushed so don't bother waiting.
673 *
674 * If we can lock it then do a delwri flush so we can
675 * combine multiple inode flushes in each disk write.
676 */
677 } else if (xfs_iflock_nowait(ip)) {
678 error = xfs_iflush(ip, XFS_IFLUSH_DELWRI);
679 } else if (bypassed) {
680 (*bypassed)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 }
682 }
683
684 if (lock_flags != 0) {
685 xfs_iunlock(ip, lock_flags);
686 }
687
688 if (vnode_refed) {
689 /*
690 * If we had to take a reference on the vnode
691 * above, then wait until after we've unlocked
692 * the inode to release the reference. This is
693 * because we can be already holding the inode
Christoph Hellwig43355092008-03-27 18:01:08 +1100694 * lock when IRELE() calls xfs_inactive().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 *
696 * Make sure to drop the mount lock before calling
Christoph Hellwig43355092008-03-27 18:01:08 +1100697 * IRELE() so that we don't trip over ourselves if
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 * we have to go for the mount lock again in the
699 * inactive code.
700 */
701 if (mount_locked) {
702 IPOINTER_INSERT(ip, mp);
703 }
704
Christoph Hellwig43355092008-03-27 18:01:08 +1100705 IRELE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
707 vnode_refed = B_FALSE;
708 }
709
710 if (error) {
711 last_error = error;
712 }
713
714 /*
715 * bail out if the filesystem is corrupted.
716 */
717 if (error == EFSCORRUPTED) {
718 if (!mount_locked) {
719 XFS_MOUNT_ILOCK(mp);
720 IPOINTER_REMOVE(ip, mp);
721 }
722 XFS_MOUNT_IUNLOCK(mp);
723 ASSERT(ipointer_in == B_FALSE);
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000724 kmem_free(ipointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 return XFS_ERROR(error);
726 }
727
728 /* Let other threads have a chance at the mount lock
729 * if we have looped many times without dropping the
730 * lock.
731 */
732 if ((++preempt & XFS_PREEMPT_MASK) == 0) {
733 if (mount_locked) {
734 IPOINTER_INSERT(ip, mp);
735 }
736 }
737
738 if (mount_locked == B_FALSE) {
739 XFS_MOUNT_ILOCK(mp);
740 mount_locked = B_TRUE;
741 IPOINTER_REMOVE(ip, mp);
742 continue;
743 }
744
745 ASSERT(ipointer_in == B_FALSE);
746 ip = ip->i_mnext;
747
748 } while (ip != mp->m_inodes);
749
750 XFS_MOUNT_IUNLOCK(mp);
751
752 ASSERT(ipointer_in == B_FALSE);
753
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000754 kmem_free(ipointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 return XFS_ERROR(last_error);
756}
757
758/*
759 * xfs sync routine for internal use
760 *
Nathan Scottb83bd132006-06-09 16:48:30 +1000761 * This routine supports all of the flags defined for the generic vfs_sync
Lachlan McIlroy1f9b3b62007-02-10 18:35:33 +1100762 * interface as explained above under xfs_sync.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 *
764 */
765int
766xfs_syncsub(
767 xfs_mount_t *mp,
768 int flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 int *bypassed)
770{
771 int error = 0;
772 int last_error = 0;
773 uint log_flags = XFS_LOG_FORCE;
774 xfs_buf_t *bp;
775 xfs_buf_log_item_t *bip;
776
777 /*
778 * Sync out the log. This ensures that the log is periodically
779 * flushed even if there is not enough activity to fill it up.
780 */
781 if (flags & SYNC_WAIT)
782 log_flags |= XFS_LOG_SYNC;
783
784 xfs_log_force(mp, (xfs_lsn_t)0, log_flags);
785
786 if (flags & (SYNC_ATTR|SYNC_DELWRI)) {
787 if (flags & SYNC_BDFLUSH)
788 xfs_finish_reclaim_all(mp, 1);
789 else
Lachlan McIlroy1f9b3b62007-02-10 18:35:33 +1100790 error = xfs_sync_inodes(mp, flags, bypassed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 }
792
793 /*
794 * Flushing out dirty data above probably generated more
795 * log activity, so if this isn't vfs_sync() then flush
796 * the log again.
797 */
798 if (flags & SYNC_DELWRI) {
799 xfs_log_force(mp, (xfs_lsn_t)0, log_flags);
800 }
801
802 if (flags & SYNC_FSDATA) {
803 /*
804 * If this is vfs_sync() then only sync the superblock
805 * if we can lock it without sleeping and it is not pinned.
806 */
807 if (flags & SYNC_BDFLUSH) {
808 bp = xfs_getsb(mp, XFS_BUF_TRYLOCK);
809 if (bp != NULL) {
810 bip = XFS_BUF_FSPRIVATE(bp,xfs_buf_log_item_t*);
811 if ((bip != NULL) &&
812 xfs_buf_item_dirty(bip)) {
813 if (!(XFS_BUF_ISPINNED(bp))) {
814 XFS_BUF_ASYNC(bp);
815 error = xfs_bwrite(mp, bp);
816 } else {
817 xfs_buf_relse(bp);
818 }
819 } else {
820 xfs_buf_relse(bp);
821 }
822 }
823 } else {
824 bp = xfs_getsb(mp, 0);
825 /*
826 * If the buffer is pinned then push on the log so
827 * we won't get stuck waiting in the write for
828 * someone, maybe ourselves, to flush the log.
829 * Even though we just pushed the log above, we
830 * did not have the superblock buffer locked at
831 * that point so it can become pinned in between
832 * there and here.
833 */
834 if (XFS_BUF_ISPINNED(bp))
835 xfs_log_force(mp, (xfs_lsn_t)0, XFS_LOG_FORCE);
836 if (flags & SYNC_WAIT)
837 XFS_BUF_UNASYNC(bp);
838 else
839 XFS_BUF_ASYNC(bp);
840 error = xfs_bwrite(mp, bp);
841 }
842 if (error) {
843 last_error = error;
844 }
845 }
846
847 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 * Now check to see if the log needs a "dummy" transaction.
849 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 if (!(flags & SYNC_REMOUNT) && xfs_log_need_covered(mp)) {
851 xfs_trans_t *tp;
852 xfs_inode_t *ip;
853
854 /*
855 * Put a dummy transaction in the log to tell
856 * recovery that all others are OK.
857 */
858 tp = xfs_trans_alloc(mp, XFS_TRANS_DUMMY1);
859 if ((error = xfs_trans_reserve(tp, 0,
860 XFS_ICHANGE_LOG_RES(mp),
861 0, 0, 0))) {
862 xfs_trans_cancel(tp, 0);
863 return error;
864 }
865
866 ip = mp->m_rootip;
867 xfs_ilock(ip, XFS_ILOCK_EXCL);
868
869 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
870 xfs_trans_ihold(tp, ip);
871 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000872 error = xfs_trans_commit(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 xfs_iunlock(ip, XFS_ILOCK_EXCL);
874 xfs_log_force(mp, (xfs_lsn_t)0, log_flags);
875 }
876
877 /*
878 * When shutting down, we need to insure that the AIL is pushed
879 * to disk or the filesystem can appear corrupt from the PROM.
880 */
881 if ((flags & (SYNC_CLOSE|SYNC_WAIT)) == (SYNC_CLOSE|SYNC_WAIT)) {
882 XFS_bflush(mp->m_ddev_targp);
883 if (mp->m_rtdev_targp) {
884 XFS_bflush(mp->m_rtdev_targp);
885 }
886 }
887
888 return XFS_ERROR(last_error);
889}