1/*
2 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
3 * All Rights Reserved.
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * 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.
13 *
14 * 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
17 */
18#include "xfs.h"
19#include "xfs_bit.h"
20#include "xfs_log.h"
21#include "xfs_clnt.h"
22#include "xfs_inum.h"
23#include "xfs_trans.h"
24#include "xfs_sb.h"
25#include "xfs_ag.h"
26#include "xfs_dir2.h"
27#include "xfs_alloc.h"
28#include "xfs_dmapi.h"
29#include "xfs_quota.h"
30#include "xfs_mount.h"
31#include "xfs_bmap_btree.h"
32#include "xfs_alloc_btree.h"
33#include "xfs_ialloc_btree.h"
34#include "xfs_dir2_sf.h"
35#include "xfs_attr_sf.h"
36#include "xfs_dinode.h"
37#include "xfs_inode.h"
38#include "xfs_btree.h"
39#include "xfs_ialloc.h"
40#include "xfs_bmap.h"
41#include "xfs_rtalloc.h"
42#include "xfs_error.h"
43#include "xfs_itable.h"
44#include "xfs_rw.h"
45#include "xfs_acl.h"
46#include "xfs_attr.h"
47#include "xfs_buf_item.h"
48#include "xfs_utils.h"
49#include "xfs_version.h"
50
51#include <linux/namei.h>
52#include <linux/init.h>
53#include <linux/mount.h>
54#include <linux/mempool.h>
55#include <linux/writeback.h>
56#include <linux/kthread.h>
57#include <linux/freezer.h>
58
59static struct quotactl_ops xfs_quotactl_operations;
60static struct super_operations xfs_super_operations;
61static kmem_zone_t *xfs_vnode_zone;
62static kmem_zone_t *xfs_ioend_zone;
63mempool_t *xfs_ioend_pool;
64
65STATIC struct xfs_mount_args *
66xfs_args_allocate(
67	struct super_block	*sb,
68	int			silent)
69{
70	struct xfs_mount_args	*args;
71
72	args = kmem_zalloc(sizeof(struct xfs_mount_args), KM_SLEEP);
73	args->logbufs = args->logbufsize = -1;
74	strncpy(args->fsname, sb->s_id, MAXNAMELEN);
75
76	/* Copy the already-parsed mount(2) flags we're interested in */
77	if (sb->s_flags & MS_DIRSYNC)
78		args->flags |= XFSMNT_DIRSYNC;
79	if (sb->s_flags & MS_SYNCHRONOUS)
80		args->flags |= XFSMNT_WSYNC;
81	if (silent)
82		args->flags |= XFSMNT_QUIET;
83	args->flags |= XFSMNT_32BITINODES;
84
85	return args;
86}
87
88__uint64_t
89xfs_max_file_offset(
90	unsigned int		blockshift)
91{
92	unsigned int		pagefactor = 1;
93	unsigned int		bitshift = BITS_PER_LONG - 1;
94
95	/* Figure out maximum filesize, on Linux this can depend on
96	 * the filesystem blocksize (on 32 bit platforms).
97	 * __block_prepare_write does this in an [unsigned] long...
98	 *      page->index << (PAGE_CACHE_SHIFT - bbits)
99	 * So, for page sized blocks (4K on 32 bit platforms),
100	 * this wraps at around 8Tb (hence MAX_LFS_FILESIZE which is
101	 *      (((u64)PAGE_CACHE_SIZE << (BITS_PER_LONG-1))-1)
102	 * but for smaller blocksizes it is less (bbits = log2 bsize).
103	 * Note1: get_block_t takes a long (implicit cast from above)
104	 * Note2: The Large Block Device (LBD and HAVE_SECTOR_T) patch
105	 * can optionally convert the [unsigned] long from above into
106	 * an [unsigned] long long.
107	 */
108
109#if BITS_PER_LONG == 32
110# if defined(CONFIG_LBD)
111	ASSERT(sizeof(sector_t) == 8);
112	pagefactor = PAGE_CACHE_SIZE;
113	bitshift = BITS_PER_LONG;
114# else
115	pagefactor = PAGE_CACHE_SIZE >> (PAGE_CACHE_SHIFT - blockshift);
116# endif
117#endif
118
119	return (((__uint64_t)pagefactor) << bitshift) - 1;
120}
121
122STATIC_INLINE void
123xfs_set_inodeops(
124	struct inode		*inode)
125{
126	switch (inode->i_mode & S_IFMT) {
127	case S_IFREG:
128		inode->i_op = &xfs_inode_operations;
129		inode->i_fop = &xfs_file_operations;
130		inode->i_mapping->a_ops = &xfs_address_space_operations;
131		break;
132	case S_IFDIR:
133		inode->i_op = &xfs_dir_inode_operations;
134		inode->i_fop = &xfs_dir_file_operations;
135		break;
136	case S_IFLNK:
137		inode->i_op = &xfs_symlink_inode_operations;
138		if (inode->i_blocks)
139			inode->i_mapping->a_ops = &xfs_address_space_operations;
140		break;
141	default:
142		inode->i_op = &xfs_inode_operations;
143		init_special_inode(inode, inode->i_mode, inode->i_rdev);
144		break;
145	}
146}
147
148STATIC_INLINE void
149xfs_revalidate_inode(
150	xfs_mount_t		*mp,
151	bhv_vnode_t		*vp,
152	xfs_inode_t		*ip)
153{
154	struct inode		*inode = vn_to_inode(vp);
155
156	inode->i_mode	= ip->i_d.di_mode;
157	inode->i_nlink	= ip->i_d.di_nlink;
158	inode->i_uid	= ip->i_d.di_uid;
159	inode->i_gid	= ip->i_d.di_gid;
160
161	switch (inode->i_mode & S_IFMT) {
162	case S_IFBLK:
163	case S_IFCHR:
164		inode->i_rdev =
165			MKDEV(sysv_major(ip->i_df.if_u2.if_rdev) & 0x1ff,
166			      sysv_minor(ip->i_df.if_u2.if_rdev));
167		break;
168	default:
169		inode->i_rdev = 0;
170		break;
171	}
172
173	inode->i_generation = ip->i_d.di_gen;
174	i_size_write(inode, ip->i_d.di_size);
175	inode->i_blocks =
176		XFS_FSB_TO_BB(mp, ip->i_d.di_nblocks + ip->i_delayed_blks);
177	inode->i_atime.tv_sec	= ip->i_d.di_atime.t_sec;
178	inode->i_atime.tv_nsec	= ip->i_d.di_atime.t_nsec;
179	inode->i_mtime.tv_sec	= ip->i_d.di_mtime.t_sec;
180	inode->i_mtime.tv_nsec	= ip->i_d.di_mtime.t_nsec;
181	inode->i_ctime.tv_sec	= ip->i_d.di_ctime.t_sec;
182	inode->i_ctime.tv_nsec	= ip->i_d.di_ctime.t_nsec;
183	if (ip->i_d.di_flags & XFS_DIFLAG_IMMUTABLE)
184		inode->i_flags |= S_IMMUTABLE;
185	else
186		inode->i_flags &= ~S_IMMUTABLE;
187	if (ip->i_d.di_flags & XFS_DIFLAG_APPEND)
188		inode->i_flags |= S_APPEND;
189	else
190		inode->i_flags &= ~S_APPEND;
191	if (ip->i_d.di_flags & XFS_DIFLAG_SYNC)
192		inode->i_flags |= S_SYNC;
193	else
194		inode->i_flags &= ~S_SYNC;
195	if (ip->i_d.di_flags & XFS_DIFLAG_NOATIME)
196		inode->i_flags |= S_NOATIME;
197	else
198		inode->i_flags &= ~S_NOATIME;
199	vp->v_flag &= ~VMODIFIED;
200}
201
202void
203xfs_initialize_vnode(
204	bhv_desc_t		*bdp,
205	bhv_vnode_t		*vp,
206	bhv_desc_t		*inode_bhv,
207	int			unlock)
208{
209	xfs_inode_t		*ip = XFS_BHVTOI(inode_bhv);
210	struct inode		*inode = vn_to_inode(vp);
211
212	if (!inode_bhv->bd_vobj) {
213		vp->v_vfsp = bhvtovfs(bdp);
214		bhv_desc_init(inode_bhv, ip, vp, &xfs_vnodeops);
215		bhv_insert(VN_BHV_HEAD(vp), inode_bhv);
216	}
217
218	/*
219	 * We need to set the ops vectors, and unlock the inode, but if
220	 * we have been called during the new inode create process, it is
221	 * too early to fill in the Linux inode.  We will get called a
222	 * second time once the inode is properly set up, and then we can
223	 * finish our work.
224	 */
225	if (ip->i_d.di_mode != 0 && unlock && (inode->i_state & I_NEW)) {
226		xfs_revalidate_inode(XFS_BHVTOM(bdp), vp, ip);
227		xfs_set_inodeops(inode);
228
229		xfs_iflags_clear(ip, XFS_INEW);
230		barrier();
231
232		unlock_new_inode(inode);
233	}
234}
235
236int
237xfs_blkdev_get(
238	xfs_mount_t		*mp,
239	const char		*name,
240	struct block_device	**bdevp)
241{
242	int			error = 0;
243
244	*bdevp = open_bdev_excl(name, 0, mp);
245	if (IS_ERR(*bdevp)) {
246		error = PTR_ERR(*bdevp);
247		printk("XFS: Invalid device [%s], error=%d\n", name, error);
248	}
249
250	return -error;
251}
252
253void
254xfs_blkdev_put(
255	struct block_device	*bdev)
256{
257	if (bdev)
258		close_bdev_excl(bdev);
259}
260
261/*
262 * Try to write out the superblock using barriers.
263 */
264STATIC int
265xfs_barrier_test(
266	xfs_mount_t	*mp)
267{
268	xfs_buf_t	*sbp = xfs_getsb(mp, 0);
269	int		error;
270
271	XFS_BUF_UNDONE(sbp);
272	XFS_BUF_UNREAD(sbp);
273	XFS_BUF_UNDELAYWRITE(sbp);
274	XFS_BUF_WRITE(sbp);
275	XFS_BUF_UNASYNC(sbp);
276	XFS_BUF_ORDERED(sbp);
277
278	xfsbdstrat(mp, sbp);
279	error = xfs_iowait(sbp);
280
281	/*
282	 * Clear all the flags we set and possible error state in the
283	 * buffer.  We only did the write to try out whether barriers
284	 * worked and shouldn't leave any traces in the superblock
285	 * buffer.
286	 */
287	XFS_BUF_DONE(sbp);
288	XFS_BUF_ERROR(sbp, 0);
289	XFS_BUF_UNORDERED(sbp);
290
291	xfs_buf_relse(sbp);
292	return error;
293}
294
295void
296xfs_mountfs_check_barriers(xfs_mount_t *mp)
297{
298	int error;
299
300	if (mp->m_logdev_targp != mp->m_ddev_targp) {
301		xfs_fs_cmn_err(CE_NOTE, mp,
302		  "Disabling barriers, not supported with external log device");
303		mp->m_flags &= ~XFS_MOUNT_BARRIER;
304		return;
305	}
306
307	if (mp->m_ddev_targp->bt_bdev->bd_disk->queue->ordered ==
308					QUEUE_ORDERED_NONE) {
309		xfs_fs_cmn_err(CE_NOTE, mp,
310		  "Disabling barriers, not supported by the underlying device");
311		mp->m_flags &= ~XFS_MOUNT_BARRIER;
312		return;
313	}
314
315	if (xfs_readonly_buftarg(mp->m_ddev_targp)) {
316		xfs_fs_cmn_err(CE_NOTE, mp,
317		  "Disabling barriers, underlying device is readonly");
318		mp->m_flags &= ~XFS_MOUNT_BARRIER;
319		return;
320	}
321
322	error = xfs_barrier_test(mp);
323	if (error) {
324		xfs_fs_cmn_err(CE_NOTE, mp,
325		  "Disabling barriers, trial barrier write failed");
326		mp->m_flags &= ~XFS_MOUNT_BARRIER;
327		return;
328	}
329}
330
331void
332xfs_blkdev_issue_flush(
333	xfs_buftarg_t		*buftarg)
334{
335	blkdev_issue_flush(buftarg->bt_bdev, NULL);
336}
337
338STATIC struct inode *
339xfs_fs_alloc_inode(
340	struct super_block	*sb)
341{
342	bhv_vnode_t		*vp;
343
344	vp = kmem_zone_alloc(xfs_vnode_zone, KM_SLEEP);
345	if (unlikely(!vp))
346		return NULL;
347	return vn_to_inode(vp);
348}
349
350STATIC void
351xfs_fs_destroy_inode(
352	struct inode		*inode)
353{
354	kmem_zone_free(xfs_vnode_zone, vn_from_inode(inode));
355}
356
357STATIC void
358xfs_fs_inode_init_once(
359	void			*vnode,
360	kmem_zone_t		*zonep,
361	unsigned long		flags)
362{
363	inode_init_once(vn_to_inode((bhv_vnode_t *)vnode));
364}
365
366STATIC int
367xfs_init_zones(void)
368{
369	xfs_vnode_zone = kmem_zone_init_flags(sizeof(bhv_vnode_t), "xfs_vnode",
370					KM_ZONE_HWALIGN | KM_ZONE_RECLAIM |
371					KM_ZONE_SPREAD,
372					xfs_fs_inode_init_once);
373	if (!xfs_vnode_zone)
374		goto out;
375
376	xfs_ioend_zone = kmem_zone_init(sizeof(xfs_ioend_t), "xfs_ioend");
377	if (!xfs_ioend_zone)
378		goto out_destroy_vnode_zone;
379
380	xfs_ioend_pool = mempool_create_slab_pool(4 * MAX_BUF_PER_PAGE,
381						  xfs_ioend_zone);
382	if (!xfs_ioend_pool)
383		goto out_free_ioend_zone;
384	return 0;
385
386 out_free_ioend_zone:
387	kmem_zone_destroy(xfs_ioend_zone);
388 out_destroy_vnode_zone:
389	kmem_zone_destroy(xfs_vnode_zone);
390 out:
391	return -ENOMEM;
392}
393
394STATIC void
395xfs_destroy_zones(void)
396{
397	mempool_destroy(xfs_ioend_pool);
398	kmem_zone_destroy(xfs_vnode_zone);
399	kmem_zone_destroy(xfs_ioend_zone);
400}
401
402/*
403 * Attempt to flush the inode, this will actually fail
404 * if the inode is pinned, but we dirty the inode again
405 * at the point when it is unpinned after a log write,
406 * since this is when the inode itself becomes flushable.
407 */
408STATIC int
409xfs_fs_write_inode(
410	struct inode		*inode,
411	int			sync)
412{
413	bhv_vnode_t		*vp = vn_from_inode(inode);
414	int			error = 0, flags = FLUSH_INODE;
415
416	if (vp) {
417		vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
418		if (sync)
419			flags |= FLUSH_SYNC;
420		error = bhv_vop_iflush(vp, flags);
421		if (error == EAGAIN)
422			error = sync? bhv_vop_iflush(vp, flags | FLUSH_LOG) : 0;
423	}
424	return -error;
425}
426
427STATIC void
428xfs_fs_clear_inode(
429	struct inode		*inode)
430{
431	bhv_vnode_t		*vp = vn_from_inode(inode);
432
433	vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
434
435	XFS_STATS_INC(vn_rele);
436	XFS_STATS_INC(vn_remove);
437	XFS_STATS_INC(vn_reclaim);
438	XFS_STATS_DEC(vn_active);
439
440	/*
441	 * This can happen because xfs_iget_core calls xfs_idestroy if we
442	 * find an inode with di_mode == 0 but without IGET_CREATE set.
443	 */
444	if (VNHEAD(vp))
445		bhv_vop_inactive(vp, NULL);
446
447	VN_LOCK(vp);
448	vp->v_flag &= ~VMODIFIED;
449	VN_UNLOCK(vp, 0);
450
451	if (VNHEAD(vp))
452		if (bhv_vop_reclaim(vp))
453			panic("%s: cannot reclaim 0x%p\n", __FUNCTION__, vp);
454
455	ASSERT(VNHEAD(vp) == NULL);
456
457#ifdef XFS_VNODE_TRACE
458	ktrace_free(vp->v_trace);
459#endif
460}
461
462/*
463 * Enqueue a work item to be picked up by the vfs xfssyncd thread.
464 * Doing this has two advantages:
465 * - It saves on stack space, which is tight in certain situations
466 * - It can be used (with care) as a mechanism to avoid deadlocks.
467 * Flushing while allocating in a full filesystem requires both.
468 */
469STATIC void
470xfs_syncd_queue_work(
471	struct bhv_vfs	*vfs,
472	void		*data,
473	void		(*syncer)(bhv_vfs_t *, void *))
474{
475	struct bhv_vfs_sync_work *work;
476
477	work = kmem_alloc(sizeof(struct bhv_vfs_sync_work), KM_SLEEP);
478	INIT_LIST_HEAD(&work->w_list);
479	work->w_syncer = syncer;
480	work->w_data = data;
481	work->w_vfs = vfs;
482	spin_lock(&vfs->vfs_sync_lock);
483	list_add_tail(&work->w_list, &vfs->vfs_sync_list);
484	spin_unlock(&vfs->vfs_sync_lock);
485	wake_up_process(vfs->vfs_sync_task);
486}
487
488/*
489 * Flush delayed allocate data, attempting to free up reserved space
490 * from existing allocations.  At this point a new allocation attempt
491 * has failed with ENOSPC and we are in the process of scratching our
492 * heads, looking about for more room...
493 */
494STATIC void
495xfs_flush_inode_work(
496	bhv_vfs_t	*vfs,
497	void		*inode)
498{
499	filemap_flush(((struct inode *)inode)->i_mapping);
500	iput((struct inode *)inode);
501}
502
503void
504xfs_flush_inode(
505	xfs_inode_t	*ip)
506{
507	struct inode	*inode = vn_to_inode(XFS_ITOV(ip));
508	struct bhv_vfs	*vfs = XFS_MTOVFS(ip->i_mount);
509
510	igrab(inode);
511	xfs_syncd_queue_work(vfs, inode, xfs_flush_inode_work);
512	delay(msecs_to_jiffies(500));
513}
514
515/*
516 * This is the "bigger hammer" version of xfs_flush_inode_work...
517 * (IOW, "If at first you don't succeed, use a Bigger Hammer").
518 */
519STATIC void
520xfs_flush_device_work(
521	bhv_vfs_t	*vfs,
522	void		*inode)
523{
524	sync_blockdev(vfs->vfs_super->s_bdev);
525	iput((struct inode *)inode);
526}
527
528void
529xfs_flush_device(
530	xfs_inode_t	*ip)
531{
532	struct inode	*inode = vn_to_inode(XFS_ITOV(ip));
533	struct bhv_vfs	*vfs = XFS_MTOVFS(ip->i_mount);
534
535	igrab(inode);
536	xfs_syncd_queue_work(vfs, inode, xfs_flush_device_work);
537	delay(msecs_to_jiffies(500));
538	xfs_log_force(ip->i_mount, (xfs_lsn_t)0, XFS_LOG_FORCE|XFS_LOG_SYNC);
539}
540
541STATIC void
542vfs_sync_worker(
543	bhv_vfs_t	*vfsp,
544	void		*unused)
545{
546	int		error;
547
548	if (!(vfsp->vfs_flag & VFS_RDONLY))
549		error = bhv_vfs_sync(vfsp, SYNC_FSDATA | SYNC_BDFLUSH | \
550					SYNC_ATTR | SYNC_REFCACHE, NULL);
551	vfsp->vfs_sync_seq++;
552	wake_up(&vfsp->vfs_wait_single_sync_task);
553}
554
555STATIC int
556xfssyncd(
557	void			*arg)
558{
559	long			timeleft;
560	bhv_vfs_t		*vfsp = (bhv_vfs_t *) arg;
561	bhv_vfs_sync_work_t	*work, *n;
562	LIST_HEAD		(tmp);
563
564	timeleft = xfs_syncd_centisecs * msecs_to_jiffies(10);
565	for (;;) {
566		timeleft = schedule_timeout_interruptible(timeleft);
567		/* swsusp */
568		try_to_freeze();
569		if (kthread_should_stop() && list_empty(&vfsp->vfs_sync_list))
570			break;
571
572		spin_lock(&vfsp->vfs_sync_lock);
573		/*
574		 * We can get woken by laptop mode, to do a sync -
575		 * that's the (only!) case where the list would be
576		 * empty with time remaining.
577		 */
578		if (!timeleft || list_empty(&vfsp->vfs_sync_list)) {
579			if (!timeleft)
580				timeleft = xfs_syncd_centisecs *
581							msecs_to_jiffies(10);
582			INIT_LIST_HEAD(&vfsp->vfs_sync_work.w_list);
583			list_add_tail(&vfsp->vfs_sync_work.w_list,
584					&vfsp->vfs_sync_list);
585		}
586		list_for_each_entry_safe(work, n, &vfsp->vfs_sync_list, w_list)
587			list_move(&work->w_list, &tmp);
588		spin_unlock(&vfsp->vfs_sync_lock);
589
590		list_for_each_entry_safe(work, n, &tmp, w_list) {
591			(*work->w_syncer)(vfsp, work->w_data);
592			list_del(&work->w_list);
593			if (work == &vfsp->vfs_sync_work)
594				continue;
595			kmem_free(work, sizeof(struct bhv_vfs_sync_work));
596		}
597	}
598
599	return 0;
600}
601
602STATIC int
603xfs_fs_start_syncd(
604	bhv_vfs_t		*vfsp)
605{
606	vfsp->vfs_sync_work.w_syncer = vfs_sync_worker;
607	vfsp->vfs_sync_work.w_vfs = vfsp;
608	vfsp->vfs_sync_task = kthread_run(xfssyncd, vfsp, "xfssyncd");
609	if (IS_ERR(vfsp->vfs_sync_task))
610		return -PTR_ERR(vfsp->vfs_sync_task);
611	return 0;
612}
613
614STATIC void
615xfs_fs_stop_syncd(
616	bhv_vfs_t		*vfsp)
617{
618	kthread_stop(vfsp->vfs_sync_task);
619}
620
621STATIC void
622xfs_fs_put_super(
623	struct super_block	*sb)
624{
625	bhv_vfs_t		*vfsp = vfs_from_sb(sb);
626	int			error;
627
628	xfs_fs_stop_syncd(vfsp);
629	bhv_vfs_sync(vfsp, SYNC_ATTR | SYNC_DELWRI, NULL);
630	error = bhv_vfs_unmount(vfsp, 0, NULL);
631	if (error) {
632		printk("XFS: unmount got error=%d\n", error);
633		printk("%s: vfs=0x%p left dangling!\n", __FUNCTION__, vfsp);
634	} else {
635		vfs_deallocate(vfsp);
636	}
637}
638
639STATIC void
640xfs_fs_write_super(
641	struct super_block	*sb)
642{
643	if (!(sb->s_flags & MS_RDONLY))
644		bhv_vfs_sync(vfs_from_sb(sb), SYNC_FSDATA, NULL);
645	sb->s_dirt = 0;
646}
647
648STATIC int
649xfs_fs_sync_super(
650	struct super_block	*sb,
651	int			wait)
652{
653	bhv_vfs_t		*vfsp = vfs_from_sb(sb);
654	int			error;
655	int			flags;
656
657	if (unlikely(sb->s_frozen == SB_FREEZE_WRITE)) {
658		/*
659		 * First stage of freeze - no more writers will make progress
660		 * now we are here, so we flush delwri and delalloc buffers
661		 * here, then wait for all I/O to complete.  Data is frozen at
662		 * that point. Metadata is not frozen, transactions can still
663		 * occur here so don't bother flushing the buftarg (i.e
664		 * SYNC_QUIESCE) because it'll just get dirty again.
665		 */
666		flags = SYNC_FSDATA | SYNC_DELWRI | SYNC_WAIT | SYNC_IOWAIT;
667	} else
668		flags = SYNC_FSDATA | (wait ? SYNC_WAIT : 0);
669
670	error = bhv_vfs_sync(vfsp, flags, NULL);
671	sb->s_dirt = 0;
672
673	if (unlikely(laptop_mode)) {
674		int	prev_sync_seq = vfsp->vfs_sync_seq;
675
676		/*
677		 * The disk must be active because we're syncing.
678		 * We schedule xfssyncd now (now that the disk is
679		 * active) instead of later (when it might not be).
680		 */
681		wake_up_process(vfsp->vfs_sync_task);
682		/*
683		 * We have to wait for the sync iteration to complete.
684		 * If we don't, the disk activity caused by the sync
685		 * will come after the sync is completed, and that
686		 * triggers another sync from laptop mode.
687		 */
688		wait_event(vfsp->vfs_wait_single_sync_task,
689				vfsp->vfs_sync_seq != prev_sync_seq);
690	}
691
692	return -error;
693}
694
695STATIC int
696xfs_fs_statfs(
697	struct dentry		*dentry,
698	struct kstatfs		*statp)
699{
700	return -bhv_vfs_statvfs(vfs_from_sb(dentry->d_sb), statp,
701				vn_from_inode(dentry->d_inode));
702}
703
704STATIC int
705xfs_fs_remount(
706	struct super_block	*sb,
707	int			*flags,
708	char			*options)
709{
710	bhv_vfs_t		*vfsp = vfs_from_sb(sb);
711	struct xfs_mount_args	*args = xfs_args_allocate(sb, 0);
712	int			error;
713
714	error = bhv_vfs_parseargs(vfsp, options, args, 1);
715	if (!error)
716		error = bhv_vfs_mntupdate(vfsp, flags, args);
717	kmem_free(args, sizeof(*args));
718	return -error;
719}
720
721STATIC void
722xfs_fs_lockfs(
723	struct super_block	*sb)
724{
725	bhv_vfs_freeze(vfs_from_sb(sb));
726}
727
728STATIC int
729xfs_fs_show_options(
730	struct seq_file		*m,
731	struct vfsmount		*mnt)
732{
733	return -bhv_vfs_showargs(vfs_from_sb(mnt->mnt_sb), m);
734}
735
736STATIC int
737xfs_fs_quotasync(
738	struct super_block	*sb,
739	int			type)
740{
741	return -bhv_vfs_quotactl(vfs_from_sb(sb), Q_XQUOTASYNC, 0, NULL);
742}
743
744STATIC int
745xfs_fs_getxstate(
746	struct super_block	*sb,
747	struct fs_quota_stat	*fqs)
748{
749	return -bhv_vfs_quotactl(vfs_from_sb(sb), Q_XGETQSTAT, 0, (caddr_t)fqs);
750}
751
752STATIC int
753xfs_fs_setxstate(
754	struct super_block	*sb,
755	unsigned int		flags,
756	int			op)
757{
758	return -bhv_vfs_quotactl(vfs_from_sb(sb), op, 0, (caddr_t)&flags);
759}
760
761STATIC int
762xfs_fs_getxquota(
763	struct super_block	*sb,
764	int			type,
765	qid_t			id,
766	struct fs_disk_quota	*fdq)
767{
768	return -bhv_vfs_quotactl(vfs_from_sb(sb),
769				 (type == USRQUOTA) ? Q_XGETQUOTA :
770				  ((type == GRPQUOTA) ? Q_XGETGQUOTA :
771				   Q_XGETPQUOTA), id, (caddr_t)fdq);
772}
773
774STATIC int
775xfs_fs_setxquota(
776	struct super_block	*sb,
777	int			type,
778	qid_t			id,
779	struct fs_disk_quota	*fdq)
780{
781	return -bhv_vfs_quotactl(vfs_from_sb(sb),
782				 (type == USRQUOTA) ? Q_XSETQLIM :
783				  ((type == GRPQUOTA) ? Q_XSETGQLIM :
784				   Q_XSETPQLIM), id, (caddr_t)fdq);
785}
786
787STATIC int
788xfs_fs_fill_super(
789	struct super_block	*sb,
790	void			*data,
791	int			silent)
792{
793	struct bhv_vnode	*rootvp;
794	struct bhv_vfs		*vfsp = vfs_allocate(sb);
795	struct xfs_mount_args	*args = xfs_args_allocate(sb, silent);
796	struct kstatfs		statvfs;
797	int			error;
798
799	bhv_insert_all_vfsops(vfsp);
800
801	error = bhv_vfs_parseargs(vfsp, (char *)data, args, 0);
802	if (error) {
803		bhv_remove_all_vfsops(vfsp, 1);
804		goto fail_vfsop;
805	}
806
807	sb_min_blocksize(sb, BBSIZE);
808	sb->s_export_op = &xfs_export_operations;
809	sb->s_qcop = &xfs_quotactl_operations;
810	sb->s_op = &xfs_super_operations;
811
812	error = bhv_vfs_mount(vfsp, args, NULL);
813	if (error) {
814		bhv_remove_all_vfsops(vfsp, 1);
815		goto fail_vfsop;
816	}
817
818	error = bhv_vfs_statvfs(vfsp, &statvfs, NULL);
819	if (error)
820		goto fail_unmount;
821
822	sb->s_dirt = 1;
823	sb->s_magic = statvfs.f_type;
824	sb->s_blocksize = statvfs.f_bsize;
825	sb->s_blocksize_bits = ffs(statvfs.f_bsize) - 1;
826	sb->s_maxbytes = xfs_max_file_offset(sb->s_blocksize_bits);
827	sb->s_time_gran = 1;
828	set_posix_acl_flag(sb);
829
830	error = bhv_vfs_root(vfsp, &rootvp);
831	if (error)
832		goto fail_unmount;
833
834	sb->s_root = d_alloc_root(vn_to_inode(rootvp));
835	if (!sb->s_root) {
836		error = ENOMEM;
837		goto fail_vnrele;
838	}
839	if (is_bad_inode(sb->s_root->d_inode)) {
840		error = EINVAL;
841		goto fail_vnrele;
842	}
843	if ((error = xfs_fs_start_syncd(vfsp)))
844		goto fail_vnrele;
845	vn_trace_exit(rootvp, __FUNCTION__, (inst_t *)__return_address);
846
847	kmem_free(args, sizeof(*args));
848	return 0;
849
850fail_vnrele:
851	if (sb->s_root) {
852		dput(sb->s_root);
853		sb->s_root = NULL;
854	} else {
855		VN_RELE(rootvp);
856	}
857
858fail_unmount:
859	bhv_vfs_unmount(vfsp, 0, NULL);
860
861fail_vfsop:
862	vfs_deallocate(vfsp);
863	kmem_free(args, sizeof(*args));
864	return -error;
865}
866
867STATIC int
868xfs_fs_get_sb(
869	struct file_system_type	*fs_type,
870	int			flags,
871	const char		*dev_name,
872	void			*data,
873	struct vfsmount		*mnt)
874{
875	return get_sb_bdev(fs_type, flags, dev_name, data, xfs_fs_fill_super,
876			   mnt);
877}
878
879static struct super_operations xfs_super_operations = {
880	.alloc_inode		= xfs_fs_alloc_inode,
881	.destroy_inode		= xfs_fs_destroy_inode,
882	.write_inode		= xfs_fs_write_inode,
883	.clear_inode		= xfs_fs_clear_inode,
884	.put_super		= xfs_fs_put_super,
885	.write_super		= xfs_fs_write_super,
886	.sync_fs		= xfs_fs_sync_super,
887	.write_super_lockfs	= xfs_fs_lockfs,
888	.statfs			= xfs_fs_statfs,
889	.remount_fs		= xfs_fs_remount,
890	.show_options		= xfs_fs_show_options,
891};
892
893static struct quotactl_ops xfs_quotactl_operations = {
894	.quota_sync		= xfs_fs_quotasync,
895	.get_xstate		= xfs_fs_getxstate,
896	.set_xstate		= xfs_fs_setxstate,
897	.get_xquota		= xfs_fs_getxquota,
898	.set_xquota		= xfs_fs_setxquota,
899};
900
901static struct file_system_type xfs_fs_type = {
902	.owner			= THIS_MODULE,
903	.name			= "xfs",
904	.get_sb			= xfs_fs_get_sb,
905	.kill_sb		= kill_block_super,
906	.fs_flags		= FS_REQUIRES_DEV,
907};
908
909
910STATIC int __init
911init_xfs_fs( void )
912{
913	int			error;
914	struct sysinfo		si;
915	static char		message[] __initdata = KERN_INFO \
916		XFS_VERSION_STRING " with " XFS_BUILD_OPTIONS " enabled\n";
917
918	printk(message);
919
920	si_meminfo(&si);
921	xfs_physmem = si.totalram;
922
923	ktrace_init(64);
924
925	error = xfs_init_zones();
926	if (error < 0)
927		goto undo_zones;
928
929	error = xfs_buf_init();
930	if (error < 0)
931		goto undo_buffers;
932
933	vn_init();
934	xfs_init();
935	uuid_init();
936	vfs_initquota();
937
938	error = register_filesystem(&xfs_fs_type);
939	if (error)
940		goto undo_register;
941	return 0;
942
943undo_register:
944	xfs_buf_terminate();
945
946undo_buffers:
947	xfs_destroy_zones();
948
949undo_zones:
950	return error;
951}
952
953STATIC void __exit
954exit_xfs_fs( void )
955{
956	vfs_exitquota();
957	unregister_filesystem(&xfs_fs_type);
958	xfs_cleanup();
959	xfs_buf_terminate();
960	xfs_destroy_zones();
961	ktrace_uninit();
962}
963
964module_init(init_xfs_fs);
965module_exit(exit_xfs_fs);
966
967MODULE_AUTHOR("Silicon Graphics, Inc.");
968MODULE_DESCRIPTION(XFS_VERSION_STRING " with " XFS_BUILD_OPTIONS " enabled");
969MODULE_LICENSE("GPL");
970