1 /*
2  * Copyright (c) 2000-2004 Silicon Graphics, Inc.  All Rights Reserved.
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of version 2 of the GNU General Public License as
6  * published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it would be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  *
12  * Further, this software is distributed without any warranty that it is
13  * free of the rightful claim of any third person regarding infringement
14  * or the like.  Any license provided herein, whether implied or
15  * otherwise, applies only to this software file.  Patent licenses, if
16  * any, provided herein do not apply to combinations of this program with
17  * other software, or any other product whatsoever.
18  *
19  * You should have received a copy of the GNU General Public License along
20  * with this program; if not, write the Free Software Foundation, Inc., 59
21  * Temple Place - Suite 330, Boston MA 02111-1307, USA.
22  *
23  * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24  * Mountain View, CA  94043, or:
25  *
26  * http://www.sgi.com
27  *
28  * For further information regarding this notice, see:
29  *
30  * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
31  */
32 
33 #include "xfs.h"
34 
35 #include "xfs_inum.h"
36 #include "xfs_log.h"
37 #include "xfs_clnt.h"
38 #include "xfs_trans.h"
39 #include "xfs_sb.h"
40 #include "xfs_dir.h"
41 #include "xfs_dir2.h"
42 #include "xfs_alloc.h"
43 #include "xfs_dmapi.h"
44 #include "xfs_quota.h"
45 #include "xfs_mount.h"
46 #include "xfs_alloc_btree.h"
47 #include "xfs_bmap_btree.h"
48 #include "xfs_ialloc_btree.h"
49 #include "xfs_btree.h"
50 #include "xfs_ialloc.h"
51 #include "xfs_attr_sf.h"
52 #include "xfs_dir_sf.h"
53 #include "xfs_dir2_sf.h"
54 #include "xfs_dinode.h"
55 #include "xfs_inode.h"
56 #include "xfs_bmap.h"
57 #include "xfs_bit.h"
58 #include "xfs_rtalloc.h"
59 #include "xfs_error.h"
60 #include "xfs_itable.h"
61 #include "xfs_rw.h"
62 #include "xfs_acl.h"
63 #include "xfs_cap.h"
64 #include "xfs_mac.h"
65 #include "xfs_attr.h"
66 #include "xfs_buf_item.h"
67 #include "xfs_utils.h"
68 #include "xfs_version.h"
69 
70 #include <linux/init.h>
71 
72 STATIC struct quotactl_ops linvfs_qops;
73 STATIC struct super_operations linvfs_sops;
74 STATIC kmem_zone_t *linvfs_inode_zone;
75 STATIC kmem_shaker_t xfs_inode_shaker;
76 
77 STATIC struct xfs_mount_args *
xfs_args_allocate(struct super_block * sb)78 xfs_args_allocate(
79 	struct super_block	*sb)
80 {
81 	struct xfs_mount_args	*args;
82 
83 	args = kmem_zalloc(sizeof(struct xfs_mount_args), KM_SLEEP);
84 	args->logbufs = args->logbufsize = -1;
85 	strncpy(args->fsname, bdevname(sb->s_dev), MAXNAMELEN);
86 
87 	/* Copy the already-parsed mount(2) flags we're interested in */
88 	if (sb->s_flags & MS_NOATIME)
89 		args->flags |= XFSMNT_NOATIME;
90 
91 	/* Default to 32 bit inodes on Linux all the time */
92 	args->flags |= XFSMNT_32BITINODES;
93 
94 	return args;
95 }
96 
97 __uint64_t
xfs_max_file_offset(unsigned int blockshift)98 xfs_max_file_offset(
99 	unsigned int		blockshift)
100 {
101 	unsigned int		pagefactor = 1;
102 	unsigned int		bitshift = BITS_PER_LONG - 1;
103 
104 	/* Figure out maximum filesize, on Linux this can depend on
105 	 * the filesystem blocksize (on 32 bit platforms).
106 	 * __block_prepare_write does this in an [unsigned] long...
107 	 *      page->index << (PAGE_CACHE_SHIFT - bbits)
108 	 * So, for page sized blocks (4K on 32 bit platforms),
109 	 * this wraps at around 8Tb (hence MAX_LFS_FILESIZE which is
110 	 *      (((u64)PAGE_CACHE_SIZE << (BITS_PER_LONG-1))-1)
111 	 * but for smaller blocksizes it is less (bbits = log2 bsize).
112 	 * Note1: get_block_t takes a long (implicit cast from above)
113 	 * Note2: The Large Block Device (LBD and HAVE_SECTOR_T) patch
114 	 * can optionally convert the [unsigned] long from above into
115 	 * an [unsigned] long long.
116 	 */
117 
118 #if BITS_PER_LONG == 32
119 	pagefactor = PAGE_CACHE_SIZE >> (PAGE_CACHE_SHIFT - blockshift);
120 #endif
121 
122 	return (((__uint64_t)pagefactor) << bitshift) - 1;
123 }
124 
125 STATIC __inline__ void
xfs_set_inodeops(struct inode * inode)126 xfs_set_inodeops(
127 	struct inode		*inode)
128 {
129 	vnode_t			*vp = LINVFS_GET_VP(inode);
130 
131 	if (vp->v_type == VNON) {
132 		vn_mark_bad(vp);
133 	} else if (S_ISREG(inode->i_mode)) {
134 		inode->i_op = &linvfs_file_inode_operations;
135 		inode->i_fop = &linvfs_file_operations;
136 		inode->i_mapping->a_ops = &linvfs_aops;
137 	} else if (S_ISDIR(inode->i_mode)) {
138 		inode->i_op = &linvfs_dir_inode_operations;
139 		inode->i_fop = &linvfs_dir_operations;
140 	} else if (S_ISLNK(inode->i_mode)) {
141 		inode->i_op = &linvfs_symlink_inode_operations;
142 		if (inode->i_blocks)
143 			inode->i_mapping->a_ops = &linvfs_aops;
144 	} else {
145 		inode->i_op = &linvfs_file_inode_operations;
146 		init_special_inode(inode, inode->i_mode,
147 					kdev_t_to_nr(inode->i_rdev));
148 	}
149 }
150 
151 STATIC __inline__ void
xfs_revalidate_inode(xfs_mount_t * mp,vnode_t * vp,xfs_inode_t * ip)152 xfs_revalidate_inode(
153 	xfs_mount_t		*mp,
154 	vnode_t			*vp,
155 	xfs_inode_t		*ip)
156 {
157 	struct inode		*inode = LINVFS_GET_IP(vp);
158 
159 	inode->i_mode	= (ip->i_d.di_mode & MODEMASK) | VTTOIF(vp->v_type);
160 	inode->i_nlink	= ip->i_d.di_nlink;
161 	inode->i_uid	= ip->i_d.di_uid;
162 	inode->i_gid	= ip->i_d.di_gid;
163 	if (((1 << vp->v_type) & ((1<<VBLK) | (1<<VCHR))) == 0) {
164 		inode->i_rdev = NODEV;
165 	} else {
166 		xfs_dev_t dev = ip->i_df.if_u2.if_rdev;
167 		inode->i_rdev = XFS_DEV_TO_KDEVT(dev);
168 	}
169 	inode->i_blksize = PAGE_CACHE_SIZE;
170 	inode->i_generation = ip->i_d.di_gen;
171 	i_size_write(inode, ip->i_d.di_size);
172 	inode->i_blocks =
173 		XFS_FSB_TO_BB(mp, ip->i_d.di_nblocks + ip->i_delayed_blks);
174 	inode->i_atime	= ip->i_d.di_atime.t_sec;
175 	inode->i_mtime	= ip->i_d.di_mtime.t_sec;
176 	inode->i_ctime	= ip->i_d.di_ctime.t_sec;
177 	if (ip->i_d.di_flags & XFS_DIFLAG_IMMUTABLE)
178 		inode->i_flags |= S_IMMUTABLE;
179 	else
180 		inode->i_flags &= ~S_IMMUTABLE;
181 	if (ip->i_d.di_flags & XFS_DIFLAG_APPEND)
182 		inode->i_flags |= S_APPEND;
183 	else
184 		inode->i_flags &= ~S_APPEND;
185 	if (ip->i_d.di_flags & XFS_DIFLAG_SYNC)
186 		inode->i_flags |= S_SYNC;
187 	else
188 		inode->i_flags &= ~S_SYNC;
189 	if (ip->i_d.di_flags & XFS_DIFLAG_NOATIME)
190 		inode->i_flags |= S_NOATIME;
191 	else
192 		inode->i_flags &= ~S_NOATIME;
193 
194 	vp->v_flag &= ~VMODIFIED;
195 }
196 
197 void
xfs_initialize_vnode(bhv_desc_t * bdp,vnode_t * vp,bhv_desc_t * inode_bhv,int unlock)198 xfs_initialize_vnode(
199 	bhv_desc_t		*bdp,
200 	vnode_t			*vp,
201 	bhv_desc_t		*inode_bhv,
202 	int			unlock)
203 {
204 	xfs_inode_t		*ip = XFS_BHVTOI(inode_bhv);
205 	struct inode		*inode = LINVFS_GET_IP(vp);
206 
207 	if (!inode_bhv->bd_vobj) {
208 		vp->v_vfsp = bhvtovfs(bdp);
209 		bhv_desc_init(inode_bhv, ip, vp, &xfs_vnodeops);
210 		bhv_insert(VN_BHV_HEAD(vp), inode_bhv);
211 	}
212 
213 	/*
214 	 * We need to set the ops vectors, and unlock the inode, but if
215 	 * we have been called during the new inode create process, it is
216 	 * too early to fill in the Linux inode.  We will get called a
217 	 * second time once the inode is properly set up, and then we can
218 	 * finish our work.
219 	 */
220 	if (ip->i_d.di_mode != 0 && unlock && (inode->i_state & I_NEW)) {
221 		vp->v_type = IFTOVT(ip->i_d.di_mode);
222 		xfs_revalidate_inode(XFS_BHVTOM(bdp), vp, ip);
223 		xfs_set_inodeops(inode);
224 
225 		ip->i_flags &= ~XFS_INEW;
226 		barrier();
227 
228 		unlock_new_inode(inode);
229 	}
230 }
231 
232 int
xfs_inode_shake(int priority,unsigned int gfp_mask)233 xfs_inode_shake(
234 	int		priority,
235 	unsigned int	gfp_mask)
236 {
237 	int		pages;
238 
239 	pages = kmem_zone_shrink(linvfs_inode_zone);
240 	pages += kmem_zone_shrink(xfs_inode_zone);
241 	return pages;
242 }
243 
244 struct inode *
xfs_get_inode(bhv_desc_t * bdp,xfs_ino_t ino,int flags)245 xfs_get_inode(
246 	bhv_desc_t	*bdp,
247 	xfs_ino_t	ino,
248 	int		flags)
249 {
250 	struct vfs	*vfsp = bhvtovfs(bdp);
251 
252 	return iget_locked(vfsp->vfs_super, ino);
253 }
254 
255 struct dentry *
d_alloc_anon(struct inode * inode)256 d_alloc_anon(struct inode *inode)
257 {
258 	struct dentry *dentry;
259 
260 	spin_lock(&dcache_lock);
261 	list_for_each_entry(dentry, &inode->i_dentry, d_alias) {
262 		if (!(dentry->d_flags & DCACHE_NFSD_DISCONNECTED))
263 			goto found;
264 	}
265 	spin_unlock(&dcache_lock);
266 
267 	dentry = d_alloc_root(inode);
268 	if (likely(dentry != NULL))
269 		dentry->d_flags |= DCACHE_NFSD_DISCONNECTED;
270 	return dentry;
271  found:
272 	dget_locked(dentry);
273 	dentry->d_vfs_flags |= DCACHE_REFERENCED;
274 	spin_unlock(&dcache_lock);
275 	iput(inode);
276 	return dentry;
277 }
278 
279 /*ARGSUSED*/
280 int
xfs_blkdev_get(xfs_mount_t * mp,const char * name,struct block_device ** bdevp)281 xfs_blkdev_get(
282 	xfs_mount_t		*mp,
283 	const char		*name,
284 	struct block_device	**bdevp)
285 {
286 	struct nameidata	nd;
287 	int			error;
288 
289 	error = path_lookup(name, LOOKUP_POSITIVE|LOOKUP_FOLLOW, &nd);
290 	if (error) {
291 		printk("XFS: Invalid device [%s], error=%d\n", name, error);
292 		return -error;
293 	}
294 
295 	/* I think we actually want bd_acquire here..  --hch */
296 	*bdevp = bdget(kdev_t_to_nr(nd.dentry->d_inode->i_rdev));
297 	if (*bdevp)
298 		error = blkdev_get(*bdevp, FMODE_READ|FMODE_WRITE, 0, BDEV_FS);
299 	else
300 		error = -ENOMEM;
301 
302 	path_release(&nd);
303 	return -error;
304 }
305 
306 void
xfs_blkdev_put(struct block_device * bdev)307 xfs_blkdev_put(
308 	struct block_device	*bdev)
309 {
310 	if (bdev)
311 		blkdev_put(bdev, BDEV_FS);
312 }
313 
314 STATIC struct inode *
linvfs_alloc_inode(struct super_block * sb)315 linvfs_alloc_inode(
316 	struct super_block	*sb)
317 {
318 	vnode_t			*vp;
319 
320 	vp = (vnode_t *)kmem_cache_alloc(linvfs_inode_zone,
321                 kmem_flags_convert(KM_SLEEP));
322 	if (!vp)
323 		return NULL;
324 	return LINVFS_GET_IP(vp);
325 }
326 
327 STATIC void
linvfs_destroy_inode(struct inode * inode)328 linvfs_destroy_inode(
329 	struct inode		*inode)
330 {
331 	kmem_cache_free(linvfs_inode_zone, LINVFS_GET_VP(inode));
332 }
333 
334 #define VNODE_SIZE	\
335 	(sizeof(vnode_t) - sizeof(struct inode) + offsetof(struct inode, u))
336 
337 STATIC void
init_once(void * data,kmem_cache_t * cachep,unsigned long flags)338 init_once(
339 	void			*data,
340 	kmem_cache_t		*cachep,
341 	unsigned long		flags)
342 {
343 	vnode_t			*vp = (vnode_t *)data;
344 
345 	if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
346 	    SLAB_CTOR_CONSTRUCTOR) {
347 		struct inode *inode = LINVFS_GET_IP(vp);
348 		memset(vp, 0, VNODE_SIZE);
349 		__inode_init_once(inode);
350 	}
351 }
352 
353 STATIC int
init_inodecache(void)354 init_inodecache( void )
355 {
356 	linvfs_inode_zone = kmem_cache_create("linvfs_icache",
357 				VNODE_SIZE, 0, SLAB_HWCACHE_ALIGN,
358 				init_once, NULL);
359 	if (linvfs_inode_zone == NULL)
360 		return -ENOMEM;
361 	return 0;
362 }
363 
364 STATIC void
destroy_inodecache(void)365 destroy_inodecache( void )
366 {
367 	if (kmem_cache_destroy(linvfs_inode_zone))
368 		printk(KERN_WARNING "%s: cache still in use!\n", __FUNCTION__);
369 }
370 
371 /*
372  * Attempt to flush the inode, this will actually fail
373  * if the inode is pinned, but we dirty the inode again
374  * at the point when it is unpinned after a log write,
375  * since this is when the inode itself becomes flushable.
376  */
377 STATIC void
linvfs_write_inode(struct inode * inode,int sync)378 linvfs_write_inode(
379 	struct inode		*inode,
380 	int			sync)
381 {
382 	vnode_t			*vp = LINVFS_GET_VP(inode);
383 	int			error, flags = FLUSH_INODE;
384 
385 	if (vp) {
386 		vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
387 		if (sync)
388 			flags |= FLUSH_SYNC;
389 		VOP_IFLUSH(vp, flags, error);
390 	}
391 }
392 
393 STATIC void
linvfs_clear_inode(struct inode * inode)394 linvfs_clear_inode(
395 	struct inode		*inode)
396 {
397 	vnode_t			*vp = LINVFS_GET_VP(inode);
398 
399 	if (vp) {
400 		vn_rele(vp);
401 		vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
402 		/*
403 		 * Do all our cleanup, and remove this vnode.
404 		 */
405 		vn_remove(vp);
406 	}
407 }
408 
409 
410 /*
411  * Enqueue a work item to be picked up by the vfs xfssyncd thread.
412  * Doing this has two advantages:
413  * - It saves on stack space, which is tight in certain situations
414  * - It can be used (with care) as a mechanism to avoid deadlocks.
415  * Flushing while allocating in a full filesystem requires both.
416  */
417 STATIC void
xfs_syncd_queue_work(struct vfs * vfs,void * data,void (* syncer)(vfs_t *,void *))418 xfs_syncd_queue_work(
419 	struct vfs	*vfs,
420 	void		*data,
421 	void		(*syncer)(vfs_t *, void *))
422 {
423 	vfs_sync_work_t	*work;
424 
425 	work = kmem_alloc(sizeof(struct vfs_sync_work), KM_SLEEP);
426 	INIT_LIST_HEAD(&work->w_list);
427 	work->w_syncer = syncer;
428 	work->w_data = data;
429 	work->w_vfs = vfs;
430 	spin_lock(&vfs->vfs_sync_lock);
431 	list_add_tail(&work->w_list, &vfs->vfs_sync_list);
432 	spin_unlock(&vfs->vfs_sync_lock);
433 	wake_up_process(vfs->vfs_sync_task);
434 }
435 
436 /*
437  * Flush delayed allocate data, attempting to free up reserved space
438  * from existing allocations.  At this point a new allocation attempt
439  * has failed with ENOSPC and we are in the process of scratching our
440  * heads, looking about for more room...
441  */
442 STATIC void
xfs_flush_inode_work(vfs_t * vfs,void * inode)443 xfs_flush_inode_work(
444 	vfs_t		*vfs,
445 	void		*inode)
446 {
447 	filemap_fdatawrite(((struct inode *)inode)->i_mapping);
448 	iput((struct inode *)inode);
449 }
450 
451 void
xfs_flush_inode(xfs_inode_t * ip)452 xfs_flush_inode(
453 	xfs_inode_t	*ip)
454 {
455 	struct inode	*inode = LINVFS_GET_IP(XFS_ITOV(ip));
456 	struct vfs	*vfs = XFS_MTOVFS(ip->i_mount);
457 
458 	igrab(inode);
459 	xfs_syncd_queue_work(vfs, inode, xfs_flush_inode_work);
460 	delay(HZ/2);
461 }
462 
463 /*
464  * This is the "bigger hammer" version of xfs_flush_inode_work...
465  * (IOW, "If at first you don't succeed, use a Bigger Hammer").
466  */
467 STATIC void
xfs_flush_device_work(vfs_t * vfs,void * inode)468 xfs_flush_device_work(
469 	vfs_t		*vfs,
470 	void		*inode)
471 {
472 	fsync_no_super(((struct inode *)inode)->i_dev);
473 	iput((struct inode *)inode);
474 }
475 
476 void
xfs_flush_device(xfs_inode_t * ip)477 xfs_flush_device(
478 	xfs_inode_t	*ip)
479 {
480 	struct inode	*inode = LINVFS_GET_IP(XFS_ITOV(ip));
481 	struct vfs	*vfs = XFS_MTOVFS(ip->i_mount);
482 
483 	igrab(inode);
484 	xfs_syncd_queue_work(vfs, inode, xfs_flush_device_work);
485 	delay(HZ/2);
486 	xfs_log_force(ip->i_mount, (xfs_lsn_t)0, XFS_LOG_FORCE|XFS_LOG_SYNC);
487 }
488 
489 #define SYNCD_FLAGS	(SYNC_FSDATA|SYNC_BDFLUSH|SYNC_ATTR|SYNC_REFCACHE)
490 STATIC void
vfs_sync_worker(vfs_t * vfsp,void * unused)491 vfs_sync_worker(
492 	vfs_t		*vfsp,
493 	void		*unused)
494 {
495 	int		error;
496 
497 	if (!(vfsp->vfs_flag & VFS_RDONLY))
498 		VFS_SYNC(vfsp, SYNCD_FLAGS, NULL, error);
499 }
500 
501 STATIC int
xfssyncd(void * arg)502 xfssyncd(
503 	void			*arg)
504 {
505 	long			timeleft;
506 	vfs_t			*vfsp = (vfs_t *) arg;
507 	struct list_head	tmp;
508 	struct vfs_sync_work	*work, *n;
509 
510 	daemonize();
511 	reparent_to_init();
512 	sigmask_lock();
513 	sigfillset(&current->blocked);
514 	__recalc_sigpending(current);
515 	sigmask_unlock();
516 
517 	sprintf(current->comm, "xfssyncd");
518 
519 	vfsp->vfs_sync_work.w_vfs = vfsp;
520 	vfsp->vfs_sync_work.w_syncer = vfs_sync_worker;
521 	vfsp->vfs_sync_task = current;
522 	wmb();
523 	wake_up(&vfsp->vfs_wait_sync_task);
524 
525 	INIT_LIST_HEAD(&tmp);
526 	timeleft = (xfs_syncd_centisecs * HZ) / 100;
527 	for (;;) {
528 		set_current_state(TASK_INTERRUPTIBLE);
529 		timeleft = schedule_timeout(timeleft);
530 		if (vfsp->vfs_flag & VFS_UMOUNT)
531 			break;
532 
533 		spin_lock(&vfsp->vfs_sync_lock);
534 		if (!timeleft) {
535 			timeleft = (xfs_syncd_centisecs * HZ) / 100;
536 			INIT_LIST_HEAD(&vfsp->vfs_sync_work.w_list);
537 			list_add_tail(&vfsp->vfs_sync_work.w_list,
538 					&vfsp->vfs_sync_list);
539 		}
540 		list_for_each_entry_safe(work, n, &vfsp->vfs_sync_list, w_list)
541 			list_move(&work->w_list, &tmp);
542 		spin_unlock(&vfsp->vfs_sync_lock);
543 
544 		list_for_each_entry_safe(work, n, &tmp, w_list) {
545 			(*work->w_syncer)(vfsp, work->w_data);
546 			list_del(&work->w_list);
547 			if (work == &vfsp->vfs_sync_work)
548 				continue;
549 			kmem_free(work, sizeof(struct vfs_sync_work));
550 		}
551 	}
552 
553 	vfsp->vfs_sync_task = NULL;
554 	wmb();
555 	wake_up(&vfsp->vfs_wait_sync_task);
556 
557 	return 0;
558 }
559 
560 STATIC int
linvfs_start_syncd(vfs_t * vfsp)561 linvfs_start_syncd(
562 	vfs_t			*vfsp)
563 {
564 	int			pid;
565 
566 	pid = kernel_thread(xfssyncd, (void *) vfsp,
567 			CLONE_VM | CLONE_FS | CLONE_FILES);
568 	if (pid < 0)
569 		return pid;
570 	wait_event(vfsp->vfs_wait_sync_task, vfsp->vfs_sync_task);
571 	return 0;
572 }
573 
574 STATIC void
linvfs_stop_syncd(vfs_t * vfsp)575 linvfs_stop_syncd(
576 	vfs_t			*vfsp)
577 {
578 	vfsp->vfs_flag |= VFS_UMOUNT;
579 	wmb();
580 
581 	wake_up_process(vfsp->vfs_sync_task);
582 	wait_event(vfsp->vfs_wait_sync_task, !vfsp->vfs_sync_task);
583 }
584 
585 STATIC void
linvfs_put_super(struct super_block * sb)586 linvfs_put_super(
587 	struct super_block	*sb)
588 {
589 	vfs_t			*vfsp = LINVFS_GET_VFS(sb);
590 	int			error;
591 
592 	linvfs_stop_syncd(vfsp);
593 	VFS_SYNC(vfsp, SYNC_ATTR|SYNC_DELWRI, NULL, error);
594 	if (!error)
595 		VFS_UNMOUNT(vfsp, 0, NULL, error);
596 	if (error) {
597 		printk("XFS unmount got error %d\n", error);
598 		printk("%s: vfsp/0x%p left dangling!\n", __FUNCTION__, vfsp);
599 		return;
600 	}
601 
602 	vfs_deallocate(vfsp);
603 }
604 
605 STATIC void
linvfs_write_super(struct super_block * sb)606 linvfs_write_super(
607 	struct super_block	*sb)
608 {
609 	vfs_t			*vfsp = LINVFS_GET_VFS(sb);
610 	int			error;
611 
612 	if (sb->s_flags & MS_RDONLY) {
613 		sb->s_dirt = 0; /* paranoia */
614 		return;
615 	}
616 	/* Push the log and superblock a little */
617 	VFS_SYNC(vfsp, SYNC_FSDATA, NULL, error);
618 	sb->s_dirt = 0;
619 }
620 
621 STATIC int
linvfs_sync_super(struct super_block * sb)622 linvfs_sync_super(
623 	struct super_block	*sb)
624 {
625 	vfs_t		*vfsp = LINVFS_GET_VFS(sb);
626 	int		error;
627 
628 	VFS_SYNC(vfsp, SYNC_FSDATA|SYNC_WAIT, NULL, error);
629 	sb->s_dirt = 0;
630 	return -error;
631 }
632 
633 STATIC int
linvfs_statfs(struct super_block * sb,struct statfs * statp)634 linvfs_statfs(
635 	struct super_block	*sb,
636 	struct statfs		*statp)
637 {
638 	vfs_t			*vfsp = LINVFS_GET_VFS(sb);
639 	int			error;
640 
641 	VFS_STATVFS(vfsp, statp, NULL, error);
642 	return -error;
643 }
644 
645 STATIC int
linvfs_remount(struct super_block * sb,int * flags,char * options)646 linvfs_remount(
647 	struct super_block	*sb,
648 	int			*flags,
649 	char			*options)
650 {
651 	vfs_t			*vfsp = LINVFS_GET_VFS(sb);
652 	struct xfs_mount_args	*args = xfs_args_allocate(sb);
653 	int			error;
654 
655 	VFS_PARSEARGS(vfsp, options, args, 1, error);
656 	if (!error)
657 		VFS_MNTUPDATE(vfsp, flags, args, error);
658 	kmem_free(args, sizeof(*args));
659 	return -error;
660 }
661 
freeze_bdev(struct block_device * bdev)662 struct super_block *freeze_bdev(struct block_device *bdev)
663 {
664 	struct super_block *sb;
665 	struct vfs *vfsp;
666 	int error;
667 
668 	sb = get_super(to_kdev_t(bdev->bd_dev));
669 	if (sb && !(sb->s_flags & MS_RDONLY)) {
670 		vfsp = LINVFS_GET_VFS(sb);
671 
672 		/* Stop new writers */
673 		vfsp->vfs_frozen = SB_FREEZE_WRITE;
674 		wmb();
675 
676 		/* Flush the refcache */
677 		VFS_SYNC(vfsp, SYNC_REFCACHE|SYNC_WAIT, NULL, error);
678 
679 		/* Flush delalloc and delwri data */
680 		VFS_SYNC(vfsp, SYNC_DELWRI|SYNC_WAIT, NULL, error);
681 
682 		/* Pause transaction subsystem */
683 		vfsp->vfs_frozen = SB_FREEZE_TRANS;
684 		wmb();
685 
686 		/* Flush any remaining inodes into buffers */
687 		VFS_SYNC(vfsp, SYNC_ATTR|SYNC_WAIT, NULL, error);
688 
689 		 /* Push all buffers out to disk */
690 		sync_buffers(sb->s_dev, 1);
691 
692 		/* Push the superblock and write an unmount record */
693 		VFS_FREEZE(vfsp);
694 	}
695 
696 	sync_buffers(to_kdev_t(bdev->bd_dev), 1);
697 	return sb;      /* thaw_bdev releases sb->s_umount */
698 }
699 
thaw_bdev(struct block_device * bdev,struct super_block * sb)700 void thaw_bdev(struct block_device *bdev, struct super_block *sb)
701 {
702 	if (sb) {
703 		struct vfs *vfsp = LINVFS_GET_VFS(sb);
704 
705 		BUG_ON(sb->s_bdev != bdev);
706 
707 		vfsp->vfs_frozen = SB_UNFROZEN;
708 		wmb();
709 		wake_up(&vfsp->vfs_wait_unfrozen);
710 
711 		drop_super(sb);
712 	}
713 }
714 
715 STATIC void
linvfs_freeze_fs(struct super_block * sb)716 linvfs_freeze_fs(
717 	struct super_block	*sb)
718 {
719 	if (sb->s_flags & MS_RDONLY)
720 		return;
721 	freeze_bdev(sb->s_bdev);
722 }
723 
724 STATIC void
linvfs_unfreeze_fs(struct super_block * sb)725 linvfs_unfreeze_fs(
726 	struct super_block	*sb)
727 {
728 	thaw_bdev(sb->s_bdev, sb);
729 }
730 
731 STATIC int
linvfs_dentry_to_fh(struct dentry * dentry,__u32 * data,int * lenp,int need_parent)732 linvfs_dentry_to_fh(
733 	struct dentry		*dentry,
734 	__u32			*data,
735 	int			*lenp,
736 	int			need_parent)
737 {
738 	struct inode		*inode = dentry->d_inode ;
739 	vnode_t			*vp = LINVFS_GET_VP(inode);
740 	int			maxlen = *lenp;
741 	xfs_fid2_t		fid;
742 	int			error;
743 
744 	if (maxlen < 3)
745 		return 255 ;
746 
747 	VOP_FID2(vp, (struct fid *)&fid, error);
748 	data[0] = (__u32)fid.fid_ino;	/* 32 bits of inode is OK */
749 	data[1] = fid.fid_gen;
750 
751 	*lenp = 2 ;
752 	if (maxlen < 4 || ! need_parent)
753 		return 2 ;
754 
755 	inode = dentry->d_parent->d_inode ;
756 	vp = LINVFS_GET_VP(inode);
757 
758 	VOP_FID2(vp, (struct fid *)&fid, error);
759 	data[2] = (__u32)fid.fid_ino;	/* 32 bits of inode is OK */
760 	*lenp = 3 ;
761 	if (maxlen < 4)
762 		return 3 ;
763 	data[3] = fid.fid_gen;
764 	*lenp = 4 ;
765 	return 4 ;
766 }
767 
768 STATIC struct dentry *
linvfs_fh_to_dentry(struct super_block * sb,__u32 * data,int len,int fhtype,int parent)769 linvfs_fh_to_dentry(
770 	struct super_block	*sb,
771 	__u32			*data,
772 	int			len,
773 	int			fhtype,
774 	int			parent)
775 {
776 	vnode_t			*vp;
777 	struct inode		*inode = NULL;
778 	struct dentry		*result;
779 	xfs_fid2_t		xfid;
780 	vfs_t			*vfsp = LINVFS_GET_VFS(sb);
781 	int			error;
782 
783 	xfid.fid_len = sizeof(xfs_fid2_t) - sizeof(xfid.fid_len);
784 	xfid.fid_pad = 0;
785 
786 	if (!parent) {
787 		xfid.fid_gen = data[1];
788 		xfid.fid_ino = (__u64)data[0];
789 	} else {
790 		if (fhtype != 4) {
791 			printk(KERN_WARNING
792 			       "XFS: detected filehandle without "
793 			       "parent inode generation information.");
794 			return ERR_PTR(-ESTALE);
795 		}
796 
797 		xfid.fid_gen = data[3];
798 		xfid.fid_ino = (__u64)data[2];
799 	}
800 
801 	VFS_VGET(vfsp, &vp, (fid_t *)&xfid, error);
802 	if (error || vp == NULL)
803 		return ERR_PTR(-ESTALE) ;
804 
805 	inode = LINVFS_GET_IP(vp);
806 
807 	result = d_alloc_anon(inode);
808 	if (unlikely(result == NULL)) {
809 		iput(inode);
810 		return ERR_PTR(-ENOMEM);
811 	}
812 	return result;
813 }
814 
815 STATIC int
linvfs_show_options(struct seq_file * m,struct vfsmount * mnt)816 linvfs_show_options(
817 	struct seq_file		*m,
818 	struct vfsmount		*mnt)
819 {
820 	struct vfs		*vfsp = LINVFS_GET_VFS(mnt->mnt_sb);
821 	int			error;
822 
823 	VFS_SHOWARGS(vfsp, m, error);
824 	return error;
825 }
826 
827 STATIC int
linvfs_getxstate(struct super_block * sb,struct fs_quota_stat * fqs)828 linvfs_getxstate(
829 	struct super_block	*sb,
830 	struct fs_quota_stat	*fqs)
831 {
832 	struct vfs		*vfsp = LINVFS_GET_VFS(sb);
833 	int			error;
834 
835 	VFS_QUOTACTL(vfsp, Q_XGETQSTAT, 0, (caddr_t)fqs, error);
836 	return -error;
837 }
838 
839 STATIC int
linvfs_setxstate(struct super_block * sb,unsigned int flags,int op)840 linvfs_setxstate(
841 	struct super_block	*sb,
842 	unsigned int		flags,
843 	int			op)
844 {
845 	struct vfs		*vfsp = LINVFS_GET_VFS(sb);
846 	int			error;
847 
848 	VFS_QUOTACTL(vfsp, op, 0, (caddr_t)&flags, error);
849 	return -error;
850 }
851 
852 STATIC int
linvfs_getxquota(struct super_block * sb,int type,qid_t id,struct fs_disk_quota * fdq)853 linvfs_getxquota(
854 	struct super_block	*sb,
855 	int			type,
856 	qid_t			id,
857 	struct fs_disk_quota	*fdq)
858 {
859 	struct vfs		*vfsp = LINVFS_GET_VFS(sb);
860 	int			error, getmode;
861 
862 	getmode = (type == GRPQUOTA) ? Q_XGETGQUOTA : Q_XGETQUOTA;
863 	VFS_QUOTACTL(vfsp, getmode, id, (caddr_t)fdq, error);
864 	return -error;
865 }
866 
867 STATIC int
linvfs_setxquota(struct super_block * sb,int type,qid_t id,struct fs_disk_quota * fdq)868 linvfs_setxquota(
869 	struct super_block	*sb,
870 	int			type,
871 	qid_t			id,
872 	struct fs_disk_quota	*fdq)
873 {
874 	struct vfs		*vfsp = LINVFS_GET_VFS(sb);
875 	int			error, setmode;
876 
877 	setmode = (type == GRPQUOTA) ? Q_XSETGQLIM : Q_XSETQLIM;
878 	VFS_QUOTACTL(vfsp, setmode, id, (caddr_t)fdq, error);
879 	return -error;
880 }
881 
882 STATIC struct super_block *
linvfs_read_super(struct super_block * sb,void * data,int silent)883 linvfs_read_super(
884 	struct super_block	*sb,
885 	void			*data,
886 	int			silent)
887 {
888 	vnode_t			*rootvp;
889 	struct vfs		*vfsp = vfs_allocate();
890 	struct xfs_mount_args	*args = xfs_args_allocate(sb);
891 	struct statfs		statvfs;
892 	int			error;
893 
894 	vfsp->vfs_super = sb;
895 	LINVFS_SET_VFS(sb, vfsp);
896 	if (sb->s_flags & MS_RDONLY)
897 		vfsp->vfs_flag |= VFS_RDONLY;
898 	bhv_insert_all_vfsops(vfsp);
899 
900 	VFS_PARSEARGS(vfsp, (char *)data, args, 0, error);
901 	if (error) {
902 		bhv_remove_all_vfsops(vfsp, 1);
903 		goto fail_vfsop;
904 	}
905 
906 	sb_min_blocksize(sb, BBSIZE);
907 	sb->s_qcop = &linvfs_qops;
908 	sb->s_op = &linvfs_sops;
909 
910 	VFS_MOUNT(vfsp, args, NULL, error);
911 	if (error) {
912 		bhv_remove_all_vfsops(vfsp, 1);
913 		goto fail_vfsop;
914 	}
915 
916 	VFS_STATVFS(vfsp, &statvfs, NULL, error);
917 	if (error)
918 		goto fail_unmount;
919 
920 	sb->s_dirt = 1;
921 	sb->s_magic = statvfs.f_type;
922 	sb->s_blocksize = statvfs.f_bsize;
923 	sb->s_blocksize_bits = ffs(statvfs.f_bsize) - 1;
924 	sb->s_maxbytes = xfs_max_file_offset(sb->s_blocksize_bits);
925 	set_posix_acl_flag(sb);
926 
927 	VFS_ROOT(vfsp, &rootvp, error);
928 	if (error)
929 		goto fail_unmount;
930 
931 	sb->s_root = d_alloc_root(LINVFS_GET_IP(rootvp));
932 	if (!sb->s_root)
933 		goto fail_vnrele;
934 	if (is_bad_inode(sb->s_root->d_inode))
935 		goto fail_vnrele;
936 	if (linvfs_start_syncd(vfsp))
937 		goto fail_vnrele;
938 	vn_trace_exit(rootvp, __FUNCTION__, (inst_t *)__return_address);
939 
940 	kmem_free(args, sizeof(*args));
941 	return sb;
942 
943 fail_vnrele:
944 	if (sb->s_root) {
945 		dput(sb->s_root);
946 		sb->s_root = NULL;
947 	} else {
948 		VN_RELE(rootvp);
949 	}
950 
951 fail_unmount:
952 	VFS_UNMOUNT(vfsp, 0, NULL, error);
953 
954 fail_vfsop:
955 	vfs_deallocate(vfsp);
956 	kmem_free(args, sizeof(*args));
957 	return NULL;
958 }
959 
960 
961 STATIC struct super_operations linvfs_sops = {
962 	.alloc_inode		= linvfs_alloc_inode,
963 	.destroy_inode		= linvfs_destroy_inode,
964 	.write_inode		= linvfs_write_inode,
965 	.clear_inode		= linvfs_clear_inode,
966 	.put_super		= linvfs_put_super,
967 	.write_super		= linvfs_write_super,
968 	.sync_fs		= linvfs_sync_super,
969 	.write_super_lockfs	= linvfs_freeze_fs,
970 	.unlockfs		= linvfs_unfreeze_fs,
971 	.statfs			= linvfs_statfs,
972 	.remount_fs		= linvfs_remount,
973 	.fh_to_dentry		= linvfs_fh_to_dentry,
974 	.dentry_to_fh		= linvfs_dentry_to_fh,
975 	.show_options		= linvfs_show_options,
976 };
977 
978 STATIC struct quotactl_ops linvfs_qops = {
979 	.get_xstate		= linvfs_getxstate,
980 	.set_xstate		= linvfs_setxstate,
981 	.get_xquota		= linvfs_getxquota,
982 	.set_xquota		= linvfs_setxquota,
983 };
984 
985 STATIC struct file_system_type xfs_fs_type = {
986 	.owner			= THIS_MODULE,
987 	.name			= "xfs",
988 	.read_super		= linvfs_read_super,
989 	.fs_flags		= FS_REQUIRES_DEV,
990 };
991 
992 
993 STATIC int __init
init_xfs_fs(void)994 init_xfs_fs( void )
995 {
996 	int			error;
997 	struct sysinfo		si;
998 	static char		message[] __initdata = KERN_INFO \
999 		XFS_VERSION_STRING " with " XFS_BUILD_OPTIONS " enabled\n";
1000 
1001 	printk(message);
1002 
1003 	si_meminfo(&si);
1004 	xfs_physmem = si.totalram;
1005 
1006 	ktrace_init(64);
1007 
1008 	error = init_inodecache();
1009 	if (error < 0)
1010 		goto undo_inodecache;
1011 
1012 	error = pagebuf_init();
1013 	if (error < 0)
1014 		goto undo_pagebuf;
1015 
1016 	vn_init();
1017 	xfs_init();
1018 	uuid_init();
1019 	vfs_initdmapi();
1020 	vfs_initquota();
1021 
1022 	xfs_inode_shaker = kmem_shake_register(xfs_inode_shake);
1023 	if (!xfs_inode_shaker) {
1024 		error = -ENOMEM;
1025 		goto undo_shaker;
1026 	}
1027 
1028 	error = register_filesystem(&xfs_fs_type);
1029 	if (error)
1030 		goto undo_register;
1031 	XFS_DM_INIT(&xfs_fs_type);
1032 	return 0;
1033 
1034 undo_register:
1035 	kmem_shake_deregister(xfs_inode_shaker);
1036 
1037 undo_shaker:
1038 	pagebuf_terminate();
1039 
1040 undo_pagebuf:
1041 	destroy_inodecache();
1042 
1043 undo_inodecache:
1044 	return error;
1045 }
1046 
1047 STATIC void __exit
exit_xfs_fs(void)1048 exit_xfs_fs( void )
1049 {
1050 	XFS_DM_EXIT(&xfs_fs_type);
1051 	unregister_filesystem(&xfs_fs_type);
1052 	kmem_shake_deregister(xfs_inode_shaker);
1053 	xfs_cleanup();
1054 	vfs_exitquota();
1055 	vfs_exitdmapi();
1056 	pagebuf_terminate();
1057 	destroy_inodecache();
1058 	ktrace_uninit();
1059 }
1060 
1061 module_init(init_xfs_fs);
1062 module_exit(exit_xfs_fs);
1063 
1064 MODULE_AUTHOR("Silicon Graphics, Inc.");
1065 MODULE_DESCRIPTION(XFS_VERSION_STRING " with " XFS_BUILD_OPTIONS " enabled");
1066 MODULE_LICENSE("GPL");
1067