| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
 | 2 |  *  linux/fs/super.c | 
 | 3 |  * | 
 | 4 |  *  Copyright (C) 1991, 1992  Linus Torvalds | 
 | 5 |  * | 
 | 6 |  *  super.c contains code to handle: - mount structures | 
 | 7 |  *                                   - super-block tables | 
 | 8 |  *                                   - filesystem drivers list | 
 | 9 |  *                                   - mount system call | 
 | 10 |  *                                   - umount system call | 
 | 11 |  *                                   - ustat system call | 
 | 12 |  * | 
 | 13 |  * GK 2/5/95  -  Changed to support mounting the root fs via NFS | 
 | 14 |  * | 
 | 15 |  *  Added kerneld support: Jacques Gelinas and Bjorn Ekwall | 
 | 16 |  *  Added change_root: Werner Almesberger & Hans Lermen, Feb '96 | 
 | 17 |  *  Added options to /proc/mounts: | 
 | 18 |  *    Torbjörn Lindh (torbjorn.lindh@gopta.se), April 14, 1996. | 
 | 19 |  *  Added devfs support: Richard Gooch <rgooch@atnf.csiro.au>, 13-JAN-1998 | 
 | 20 |  *  Heavily rewritten for 'one fs - one tree' dcache architecture. AV, Mar 2000 | 
 | 21 |  */ | 
 | 22 |  | 
 | 23 | #include <linux/config.h> | 
 | 24 | #include <linux/module.h> | 
 | 25 | #include <linux/slab.h> | 
 | 26 | #include <linux/init.h> | 
 | 27 | #include <linux/smp_lock.h> | 
 | 28 | #include <linux/acct.h> | 
 | 29 | #include <linux/blkdev.h> | 
 | 30 | #include <linux/quotaops.h> | 
 | 31 | #include <linux/namei.h> | 
 | 32 | #include <linux/buffer_head.h>		/* for fsync_super() */ | 
 | 33 | #include <linux/mount.h> | 
 | 34 | #include <linux/security.h> | 
 | 35 | #include <linux/syscalls.h> | 
 | 36 | #include <linux/vfs.h> | 
 | 37 | #include <linux/writeback.h>		/* for the emergency remount stuff */ | 
 | 38 | #include <linux/idr.h> | 
 | 39 | #include <linux/kobject.h> | 
 | 40 | #include <asm/uaccess.h> | 
 | 41 |  | 
 | 42 |  | 
 | 43 | void get_filesystem(struct file_system_type *fs); | 
 | 44 | void put_filesystem(struct file_system_type *fs); | 
 | 45 | struct file_system_type *get_fs_type(const char *name); | 
 | 46 |  | 
 | 47 | LIST_HEAD(super_blocks); | 
 | 48 | DEFINE_SPINLOCK(sb_lock); | 
 | 49 |  | 
 | 50 | /** | 
 | 51 |  *	alloc_super	-	create new superblock | 
 | 52 |  * | 
 | 53 |  *	Allocates and initializes a new &struct super_block.  alloc_super() | 
 | 54 |  *	returns a pointer new superblock or %NULL if allocation had failed. | 
 | 55 |  */ | 
 | 56 | static struct super_block *alloc_super(void) | 
 | 57 | { | 
 | 58 | 	struct super_block *s = kmalloc(sizeof(struct super_block),  GFP_USER); | 
 | 59 | 	static struct super_operations default_op; | 
 | 60 |  | 
 | 61 | 	if (s) { | 
 | 62 | 		memset(s, 0, sizeof(struct super_block)); | 
 | 63 | 		if (security_sb_alloc(s)) { | 
 | 64 | 			kfree(s); | 
 | 65 | 			s = NULL; | 
 | 66 | 			goto out; | 
 | 67 | 		} | 
 | 68 | 		INIT_LIST_HEAD(&s->s_dirty); | 
 | 69 | 		INIT_LIST_HEAD(&s->s_io); | 
 | 70 | 		INIT_LIST_HEAD(&s->s_files); | 
 | 71 | 		INIT_LIST_HEAD(&s->s_instances); | 
 | 72 | 		INIT_HLIST_HEAD(&s->s_anon); | 
 | 73 | 		INIT_LIST_HEAD(&s->s_inodes); | 
 | 74 | 		init_rwsem(&s->s_umount); | 
 | 75 | 		sema_init(&s->s_lock, 1); | 
 | 76 | 		down_write(&s->s_umount); | 
 | 77 | 		s->s_count = S_BIAS; | 
 | 78 | 		atomic_set(&s->s_active, 1); | 
 | 79 | 		sema_init(&s->s_vfs_rename_sem,1); | 
 | 80 | 		sema_init(&s->s_dquot.dqio_sem, 1); | 
 | 81 | 		sema_init(&s->s_dquot.dqonoff_sem, 1); | 
 | 82 | 		init_rwsem(&s->s_dquot.dqptr_sem); | 
 | 83 | 		init_waitqueue_head(&s->s_wait_unfrozen); | 
 | 84 | 		s->s_maxbytes = MAX_NON_LFS; | 
 | 85 | 		s->dq_op = sb_dquot_ops; | 
 | 86 | 		s->s_qcop = sb_quotactl_ops; | 
 | 87 | 		s->s_op = &default_op; | 
 | 88 | 		s->s_time_gran = 1000000000; | 
 | 89 | 	} | 
 | 90 | out: | 
 | 91 | 	return s; | 
 | 92 | } | 
 | 93 |  | 
 | 94 | /** | 
 | 95 |  *	destroy_super	-	frees a superblock | 
 | 96 |  *	@s: superblock to free | 
 | 97 |  * | 
 | 98 |  *	Frees a superblock. | 
 | 99 |  */ | 
 | 100 | static inline void destroy_super(struct super_block *s) | 
 | 101 | { | 
 | 102 | 	security_sb_free(s); | 
 | 103 | 	kfree(s); | 
 | 104 | } | 
 | 105 |  | 
 | 106 | /* Superblock refcounting  */ | 
 | 107 |  | 
 | 108 | /* | 
 | 109 |  * Drop a superblock's refcount.  Returns non-zero if the superblock was | 
 | 110 |  * destroyed.  The caller must hold sb_lock. | 
 | 111 |  */ | 
 | 112 | int __put_super(struct super_block *sb) | 
 | 113 | { | 
 | 114 | 	int ret = 0; | 
 | 115 |  | 
 | 116 | 	if (!--sb->s_count) { | 
 | 117 | 		destroy_super(sb); | 
 | 118 | 		ret = 1; | 
 | 119 | 	} | 
 | 120 | 	return ret; | 
 | 121 | } | 
 | 122 |  | 
 | 123 | /* | 
 | 124 |  * Drop a superblock's refcount. | 
 | 125 |  * Returns non-zero if the superblock is about to be destroyed and | 
 | 126 |  * at least is already removed from super_blocks list, so if we are | 
 | 127 |  * making a loop through super blocks then we need to restart. | 
 | 128 |  * The caller must hold sb_lock. | 
 | 129 |  */ | 
 | 130 | int __put_super_and_need_restart(struct super_block *sb) | 
 | 131 | { | 
 | 132 | 	/* check for race with generic_shutdown_super() */ | 
 | 133 | 	if (list_empty(&sb->s_list)) { | 
 | 134 | 		/* super block is removed, need to restart... */ | 
 | 135 | 		__put_super(sb); | 
 | 136 | 		return 1; | 
 | 137 | 	} | 
 | 138 | 	/* can't be the last, since s_list is still in use */ | 
 | 139 | 	sb->s_count--; | 
 | 140 | 	BUG_ON(sb->s_count == 0); | 
 | 141 | 	return 0; | 
 | 142 | } | 
 | 143 |  | 
 | 144 | /** | 
 | 145 |  *	put_super	-	drop a temporary reference to superblock | 
 | 146 |  *	@sb: superblock in question | 
 | 147 |  * | 
 | 148 |  *	Drops a temporary reference, frees superblock if there's no | 
 | 149 |  *	references left. | 
 | 150 |  */ | 
 | 151 | static void put_super(struct super_block *sb) | 
 | 152 | { | 
 | 153 | 	spin_lock(&sb_lock); | 
 | 154 | 	__put_super(sb); | 
 | 155 | 	spin_unlock(&sb_lock); | 
 | 156 | } | 
 | 157 |  | 
 | 158 |  | 
 | 159 | /** | 
 | 160 |  *	deactivate_super	-	drop an active reference to superblock | 
 | 161 |  *	@s: superblock to deactivate | 
 | 162 |  * | 
 | 163 |  *	Drops an active reference to superblock, acquiring a temprory one if | 
 | 164 |  *	there is no active references left.  In that case we lock superblock, | 
 | 165 |  *	tell fs driver to shut it down and drop the temporary reference we | 
 | 166 |  *	had just acquired. | 
 | 167 |  */ | 
 | 168 | void deactivate_super(struct super_block *s) | 
 | 169 | { | 
 | 170 | 	struct file_system_type *fs = s->s_type; | 
 | 171 | 	if (atomic_dec_and_lock(&s->s_active, &sb_lock)) { | 
 | 172 | 		s->s_count -= S_BIAS-1; | 
 | 173 | 		spin_unlock(&sb_lock); | 
 | 174 | 		down_write(&s->s_umount); | 
 | 175 | 		fs->kill_sb(s); | 
 | 176 | 		put_filesystem(fs); | 
 | 177 | 		put_super(s); | 
 | 178 | 	} | 
 | 179 | } | 
 | 180 |  | 
 | 181 | EXPORT_SYMBOL(deactivate_super); | 
 | 182 |  | 
 | 183 | /** | 
 | 184 |  *	grab_super - acquire an active reference | 
 | 185 |  *	@s: reference we are trying to make active | 
 | 186 |  * | 
 | 187 |  *	Tries to acquire an active reference.  grab_super() is used when we | 
 | 188 |  * 	had just found a superblock in super_blocks or fs_type->fs_supers | 
 | 189 |  *	and want to turn it into a full-blown active reference.  grab_super() | 
 | 190 |  *	is called with sb_lock held and drops it.  Returns 1 in case of | 
 | 191 |  *	success, 0 if we had failed (superblock contents was already dead or | 
 | 192 |  *	dying when grab_super() had been called). | 
 | 193 |  */ | 
 | 194 | static int grab_super(struct super_block *s) | 
 | 195 | { | 
 | 196 | 	s->s_count++; | 
 | 197 | 	spin_unlock(&sb_lock); | 
 | 198 | 	down_write(&s->s_umount); | 
 | 199 | 	if (s->s_root) { | 
 | 200 | 		spin_lock(&sb_lock); | 
 | 201 | 		if (s->s_count > S_BIAS) { | 
 | 202 | 			atomic_inc(&s->s_active); | 
 | 203 | 			s->s_count--; | 
 | 204 | 			spin_unlock(&sb_lock); | 
 | 205 | 			return 1; | 
 | 206 | 		} | 
 | 207 | 		spin_unlock(&sb_lock); | 
 | 208 | 	} | 
 | 209 | 	up_write(&s->s_umount); | 
 | 210 | 	put_super(s); | 
 | 211 | 	yield(); | 
 | 212 | 	return 0; | 
 | 213 | } | 
 | 214 |  | 
 | 215 | /** | 
 | 216 |  *	generic_shutdown_super	-	common helper for ->kill_sb() | 
 | 217 |  *	@sb: superblock to kill | 
 | 218 |  * | 
 | 219 |  *	generic_shutdown_super() does all fs-independent work on superblock | 
 | 220 |  *	shutdown.  Typical ->kill_sb() should pick all fs-specific objects | 
 | 221 |  *	that need destruction out of superblock, call generic_shutdown_super() | 
 | 222 |  *	and release aforementioned objects.  Note: dentries and inodes _are_ | 
 | 223 |  *	taken care of and do not need specific handling. | 
 | 224 |  */ | 
 | 225 | void generic_shutdown_super(struct super_block *sb) | 
 | 226 | { | 
 | 227 | 	struct dentry *root = sb->s_root; | 
 | 228 | 	struct super_operations *sop = sb->s_op; | 
 | 229 |  | 
 | 230 | 	if (root) { | 
 | 231 | 		sb->s_root = NULL; | 
 | 232 | 		shrink_dcache_parent(root); | 
 | 233 | 		shrink_dcache_anon(&sb->s_anon); | 
 | 234 | 		dput(root); | 
 | 235 | 		fsync_super(sb); | 
 | 236 | 		lock_super(sb); | 
 | 237 | 		sb->s_flags &= ~MS_ACTIVE; | 
 | 238 | 		/* bad name - it should be evict_inodes() */ | 
 | 239 | 		invalidate_inodes(sb); | 
 | 240 | 		lock_kernel(); | 
 | 241 |  | 
 | 242 | 		if (sop->write_super && sb->s_dirt) | 
 | 243 | 			sop->write_super(sb); | 
 | 244 | 		if (sop->put_super) | 
 | 245 | 			sop->put_super(sb); | 
 | 246 |  | 
 | 247 | 		/* Forget any remaining inodes */ | 
 | 248 | 		if (invalidate_inodes(sb)) { | 
 | 249 | 			printk("VFS: Busy inodes after unmount. " | 
 | 250 | 			   "Self-destruct in 5 seconds.  Have a nice day...\n"); | 
 | 251 | 		} | 
 | 252 |  | 
 | 253 | 		unlock_kernel(); | 
 | 254 | 		unlock_super(sb); | 
 | 255 | 	} | 
 | 256 | 	spin_lock(&sb_lock); | 
 | 257 | 	/* should be initialized for __put_super_and_need_restart() */ | 
 | 258 | 	list_del_init(&sb->s_list); | 
 | 259 | 	list_del(&sb->s_instances); | 
 | 260 | 	spin_unlock(&sb_lock); | 
 | 261 | 	up_write(&sb->s_umount); | 
 | 262 | } | 
 | 263 |  | 
 | 264 | EXPORT_SYMBOL(generic_shutdown_super); | 
 | 265 |  | 
 | 266 | /** | 
 | 267 |  *	sget	-	find or create a superblock | 
 | 268 |  *	@type:	filesystem type superblock should belong to | 
 | 269 |  *	@test:	comparison callback | 
 | 270 |  *	@set:	setup callback | 
 | 271 |  *	@data:	argument to each of them | 
 | 272 |  */ | 
 | 273 | struct super_block *sget(struct file_system_type *type, | 
 | 274 | 			int (*test)(struct super_block *,void *), | 
 | 275 | 			int (*set)(struct super_block *,void *), | 
 | 276 | 			void *data) | 
 | 277 | { | 
 | 278 | 	struct super_block *s = NULL; | 
 | 279 | 	struct list_head *p; | 
 | 280 | 	int err; | 
 | 281 |  | 
 | 282 | retry: | 
 | 283 | 	spin_lock(&sb_lock); | 
 | 284 | 	if (test) list_for_each(p, &type->fs_supers) { | 
 | 285 | 		struct super_block *old; | 
 | 286 | 		old = list_entry(p, struct super_block, s_instances); | 
 | 287 | 		if (!test(old, data)) | 
 | 288 | 			continue; | 
 | 289 | 		if (!grab_super(old)) | 
 | 290 | 			goto retry; | 
 | 291 | 		if (s) | 
 | 292 | 			destroy_super(s); | 
 | 293 | 		return old; | 
 | 294 | 	} | 
 | 295 | 	if (!s) { | 
 | 296 | 		spin_unlock(&sb_lock); | 
 | 297 | 		s = alloc_super(); | 
 | 298 | 		if (!s) | 
 | 299 | 			return ERR_PTR(-ENOMEM); | 
 | 300 | 		goto retry; | 
 | 301 | 	} | 
 | 302 | 		 | 
 | 303 | 	err = set(s, data); | 
 | 304 | 	if (err) { | 
 | 305 | 		spin_unlock(&sb_lock); | 
 | 306 | 		destroy_super(s); | 
 | 307 | 		return ERR_PTR(err); | 
 | 308 | 	} | 
 | 309 | 	s->s_type = type; | 
 | 310 | 	strlcpy(s->s_id, type->name, sizeof(s->s_id)); | 
 | 311 | 	list_add_tail(&s->s_list, &super_blocks); | 
 | 312 | 	list_add(&s->s_instances, &type->fs_supers); | 
 | 313 | 	spin_unlock(&sb_lock); | 
 | 314 | 	get_filesystem(type); | 
 | 315 | 	return s; | 
 | 316 | } | 
 | 317 |  | 
 | 318 | EXPORT_SYMBOL(sget); | 
 | 319 |  | 
 | 320 | void drop_super(struct super_block *sb) | 
 | 321 | { | 
 | 322 | 	up_read(&sb->s_umount); | 
 | 323 | 	put_super(sb); | 
 | 324 | } | 
 | 325 |  | 
 | 326 | EXPORT_SYMBOL(drop_super); | 
 | 327 |  | 
 | 328 | static inline void write_super(struct super_block *sb) | 
 | 329 | { | 
 | 330 | 	lock_super(sb); | 
 | 331 | 	if (sb->s_root && sb->s_dirt) | 
 | 332 | 		if (sb->s_op->write_super) | 
 | 333 | 			sb->s_op->write_super(sb); | 
 | 334 | 	unlock_super(sb); | 
 | 335 | } | 
 | 336 |  | 
 | 337 | /* | 
 | 338 |  * Note: check the dirty flag before waiting, so we don't | 
 | 339 |  * hold up the sync while mounting a device. (The newly | 
 | 340 |  * mounted device won't need syncing.) | 
 | 341 |  */ | 
 | 342 | void sync_supers(void) | 
 | 343 | { | 
| Kirill Korotaev | 618f063 | 2005-06-23 00:09:54 -0700 | [diff] [blame] | 344 | 	struct super_block *sb; | 
 | 345 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | 	spin_lock(&sb_lock); | 
| Kirill Korotaev | 618f063 | 2005-06-23 00:09:54 -0700 | [diff] [blame] | 347 | restart: | 
 | 348 | 	list_for_each_entry(sb, &super_blocks, s_list) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | 		if (sb->s_dirt) { | 
 | 350 | 			sb->s_count++; | 
 | 351 | 			spin_unlock(&sb_lock); | 
 | 352 | 			down_read(&sb->s_umount); | 
 | 353 | 			write_super(sb); | 
| Kirill Korotaev | 618f063 | 2005-06-23 00:09:54 -0700 | [diff] [blame] | 354 | 			up_read(&sb->s_umount); | 
 | 355 | 			spin_lock(&sb_lock); | 
 | 356 | 			if (__put_super_and_need_restart(sb)) | 
 | 357 | 				goto restart; | 
 | 358 | 		} | 
 | 359 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | 	spin_unlock(&sb_lock); | 
 | 361 | } | 
 | 362 |  | 
 | 363 | /* | 
 | 364 |  * Call the ->sync_fs super_op against all filesytems which are r/w and | 
 | 365 |  * which implement it. | 
 | 366 |  * | 
 | 367 |  * This operation is careful to avoid the livelock which could easily happen | 
 | 368 |  * if two or more filesystems are being continuously dirtied.  s_need_sync_fs | 
 | 369 |  * is used only here.  We set it against all filesystems and then clear it as | 
 | 370 |  * we sync them.  So redirtied filesystems are skipped. | 
 | 371 |  * | 
 | 372 |  * But if process A is currently running sync_filesytems and then process B | 
 | 373 |  * calls sync_filesystems as well, process B will set all the s_need_sync_fs | 
 | 374 |  * flags again, which will cause process A to resync everything.  Fix that with | 
 | 375 |  * a local mutex. | 
 | 376 |  * | 
 | 377 |  * (Fabian) Avoid sync_fs with clean fs & wait mode 0 | 
 | 378 |  */ | 
 | 379 | void sync_filesystems(int wait) | 
 | 380 | { | 
 | 381 | 	struct super_block *sb; | 
 | 382 | 	static DECLARE_MUTEX(mutex); | 
 | 383 |  | 
 | 384 | 	down(&mutex);		/* Could be down_interruptible */ | 
 | 385 | 	spin_lock(&sb_lock); | 
| Kirill Korotaev | 618f063 | 2005-06-23 00:09:54 -0700 | [diff] [blame] | 386 | 	list_for_each_entry(sb, &super_blocks, s_list) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | 		if (!sb->s_op->sync_fs) | 
 | 388 | 			continue; | 
 | 389 | 		if (sb->s_flags & MS_RDONLY) | 
 | 390 | 			continue; | 
 | 391 | 		sb->s_need_sync_fs = 1; | 
 | 392 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 |  | 
 | 394 | restart: | 
| Kirill Korotaev | 618f063 | 2005-06-23 00:09:54 -0700 | [diff] [blame] | 395 | 	list_for_each_entry(sb, &super_blocks, s_list) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | 		if (!sb->s_need_sync_fs) | 
 | 397 | 			continue; | 
 | 398 | 		sb->s_need_sync_fs = 0; | 
 | 399 | 		if (sb->s_flags & MS_RDONLY) | 
 | 400 | 			continue;	/* hm.  Was remounted r/o meanwhile */ | 
 | 401 | 		sb->s_count++; | 
 | 402 | 		spin_unlock(&sb_lock); | 
 | 403 | 		down_read(&sb->s_umount); | 
 | 404 | 		if (sb->s_root && (wait || sb->s_dirt)) | 
 | 405 | 			sb->s_op->sync_fs(sb, wait); | 
| Kirill Korotaev | 618f063 | 2005-06-23 00:09:54 -0700 | [diff] [blame] | 406 | 		up_read(&sb->s_umount); | 
 | 407 | 		/* restart only when sb is no longer on the list */ | 
 | 408 | 		spin_lock(&sb_lock); | 
 | 409 | 		if (__put_super_and_need_restart(sb)) | 
 | 410 | 			goto restart; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | 	} | 
 | 412 | 	spin_unlock(&sb_lock); | 
 | 413 | 	up(&mutex); | 
 | 414 | } | 
 | 415 |  | 
 | 416 | /** | 
 | 417 |  *	get_super - get the superblock of a device | 
 | 418 |  *	@bdev: device to get the superblock for | 
 | 419 |  *	 | 
 | 420 |  *	Scans the superblock list and finds the superblock of the file system | 
 | 421 |  *	mounted on the device given. %NULL is returned if no match is found. | 
 | 422 |  */ | 
 | 423 |  | 
 | 424 | struct super_block * get_super(struct block_device *bdev) | 
 | 425 | { | 
| Kirill Korotaev | 618f063 | 2005-06-23 00:09:54 -0700 | [diff] [blame] | 426 | 	struct super_block *sb; | 
 | 427 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | 	if (!bdev) | 
 | 429 | 		return NULL; | 
| Kirill Korotaev | 618f063 | 2005-06-23 00:09:54 -0700 | [diff] [blame] | 430 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | 	spin_lock(&sb_lock); | 
| Kirill Korotaev | 618f063 | 2005-06-23 00:09:54 -0700 | [diff] [blame] | 432 | rescan: | 
 | 433 | 	list_for_each_entry(sb, &super_blocks, s_list) { | 
 | 434 | 		if (sb->s_bdev == bdev) { | 
 | 435 | 			sb->s_count++; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | 			spin_unlock(&sb_lock); | 
| Kirill Korotaev | 618f063 | 2005-06-23 00:09:54 -0700 | [diff] [blame] | 437 | 			down_read(&sb->s_umount); | 
 | 438 | 			if (sb->s_root) | 
 | 439 | 				return sb; | 
 | 440 | 			up_read(&sb->s_umount); | 
 | 441 | 			/* restart only when sb is no longer on the list */ | 
 | 442 | 			spin_lock(&sb_lock); | 
 | 443 | 			if (__put_super_and_need_restart(sb)) | 
 | 444 | 				goto rescan; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | 		} | 
 | 446 | 	} | 
 | 447 | 	spin_unlock(&sb_lock); | 
 | 448 | 	return NULL; | 
 | 449 | } | 
 | 450 |  | 
 | 451 | EXPORT_SYMBOL(get_super); | 
 | 452 |   | 
 | 453 | struct super_block * user_get_super(dev_t dev) | 
 | 454 | { | 
| Kirill Korotaev | 618f063 | 2005-06-23 00:09:54 -0700 | [diff] [blame] | 455 | 	struct super_block *sb; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | 	spin_lock(&sb_lock); | 
| Kirill Korotaev | 618f063 | 2005-06-23 00:09:54 -0700 | [diff] [blame] | 458 | rescan: | 
 | 459 | 	list_for_each_entry(sb, &super_blocks, s_list) { | 
 | 460 | 		if (sb->s_dev ==  dev) { | 
 | 461 | 			sb->s_count++; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | 			spin_unlock(&sb_lock); | 
| Kirill Korotaev | 618f063 | 2005-06-23 00:09:54 -0700 | [diff] [blame] | 463 | 			down_read(&sb->s_umount); | 
 | 464 | 			if (sb->s_root) | 
 | 465 | 				return sb; | 
 | 466 | 			up_read(&sb->s_umount); | 
 | 467 | 			/* restart only when sb is no longer on the list */ | 
 | 468 | 			spin_lock(&sb_lock); | 
 | 469 | 			if (__put_super_and_need_restart(sb)) | 
 | 470 | 				goto rescan; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | 		} | 
 | 472 | 	} | 
 | 473 | 	spin_unlock(&sb_lock); | 
 | 474 | 	return NULL; | 
 | 475 | } | 
 | 476 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | asmlinkage long sys_ustat(unsigned dev, struct ustat __user * ubuf) | 
 | 478 | { | 
 | 479 |         struct super_block *s; | 
 | 480 |         struct ustat tmp; | 
 | 481 |         struct kstatfs sbuf; | 
 | 482 | 	int err = -EINVAL; | 
 | 483 |  | 
 | 484 |         s = user_get_super(new_decode_dev(dev)); | 
 | 485 |         if (s == NULL) | 
 | 486 |                 goto out; | 
 | 487 | 	err = vfs_statfs(s, &sbuf); | 
 | 488 | 	drop_super(s); | 
 | 489 | 	if (err) | 
 | 490 | 		goto out; | 
 | 491 |  | 
 | 492 |         memset(&tmp,0,sizeof(struct ustat)); | 
 | 493 |         tmp.f_tfree = sbuf.f_bfree; | 
 | 494 |         tmp.f_tinode = sbuf.f_ffree; | 
 | 495 |  | 
 | 496 |         err = copy_to_user(ubuf,&tmp,sizeof(struct ustat)) ? -EFAULT : 0; | 
 | 497 | out: | 
 | 498 | 	return err; | 
 | 499 | } | 
 | 500 |  | 
 | 501 | /** | 
 | 502 |  *	mark_files_ro | 
 | 503 |  *	@sb: superblock in question | 
 | 504 |  * | 
 | 505 |  *	All files are marked read/only.  We don't care about pending | 
 | 506 |  *	delete files so this should be used in 'force' mode only | 
 | 507 |  */ | 
 | 508 |  | 
 | 509 | static void mark_files_ro(struct super_block *sb) | 
 | 510 | { | 
 | 511 | 	struct file *f; | 
 | 512 |  | 
 | 513 | 	file_list_lock(); | 
| Eric Dumazet | 2f51201 | 2005-10-30 15:02:16 -0800 | [diff] [blame] | 514 | 	list_for_each_entry(f, &sb->s_files, f_u.fu_list) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | 		if (S_ISREG(f->f_dentry->d_inode->i_mode) && file_count(f)) | 
 | 516 | 			f->f_mode &= ~FMODE_WRITE; | 
 | 517 | 	} | 
 | 518 | 	file_list_unlock(); | 
 | 519 | } | 
 | 520 |  | 
 | 521 | /** | 
 | 522 |  *	do_remount_sb - asks filesystem to change mount options. | 
 | 523 |  *	@sb:	superblock in question | 
 | 524 |  *	@flags:	numeric part of options | 
 | 525 |  *	@data:	the rest of options | 
 | 526 |  *      @force: whether or not to force the change | 
 | 527 |  * | 
 | 528 |  *	Alters the mount options of a mounted file system. | 
 | 529 |  */ | 
 | 530 | int do_remount_sb(struct super_block *sb, int flags, void *data, int force) | 
 | 531 | { | 
 | 532 | 	int retval; | 
 | 533 | 	 | 
 | 534 | 	if (!(flags & MS_RDONLY) && bdev_read_only(sb->s_bdev)) | 
 | 535 | 		return -EACCES; | 
 | 536 | 	if (flags & MS_RDONLY) | 
 | 537 | 		acct_auto_close(sb); | 
 | 538 | 	shrink_dcache_sb(sb); | 
 | 539 | 	fsync_super(sb); | 
 | 540 |  | 
 | 541 | 	/* If we are remounting RDONLY and current sb is read/write, | 
 | 542 | 	   make sure there are no rw files opened */ | 
 | 543 | 	if ((flags & MS_RDONLY) && !(sb->s_flags & MS_RDONLY)) { | 
 | 544 | 		if (force) | 
 | 545 | 			mark_files_ro(sb); | 
 | 546 | 		else if (!fs_may_remount_ro(sb)) | 
 | 547 | 			return -EBUSY; | 
 | 548 | 	} | 
 | 549 |  | 
 | 550 | 	if (sb->s_op->remount_fs) { | 
 | 551 | 		lock_super(sb); | 
 | 552 | 		retval = sb->s_op->remount_fs(sb, &flags, data); | 
 | 553 | 		unlock_super(sb); | 
 | 554 | 		if (retval) | 
 | 555 | 			return retval; | 
 | 556 | 	} | 
 | 557 | 	sb->s_flags = (sb->s_flags & ~MS_RMT_MASK) | (flags & MS_RMT_MASK); | 
 | 558 | 	return 0; | 
 | 559 | } | 
 | 560 |  | 
 | 561 | static void do_emergency_remount(unsigned long foo) | 
 | 562 | { | 
 | 563 | 	struct super_block *sb; | 
 | 564 |  | 
 | 565 | 	spin_lock(&sb_lock); | 
 | 566 | 	list_for_each_entry(sb, &super_blocks, s_list) { | 
 | 567 | 		sb->s_count++; | 
 | 568 | 		spin_unlock(&sb_lock); | 
 | 569 | 		down_read(&sb->s_umount); | 
 | 570 | 		if (sb->s_root && sb->s_bdev && !(sb->s_flags & MS_RDONLY)) { | 
 | 571 | 			/* | 
 | 572 | 			 * ->remount_fs needs lock_kernel(). | 
 | 573 | 			 * | 
 | 574 | 			 * What lock protects sb->s_flags?? | 
 | 575 | 			 */ | 
 | 576 | 			lock_kernel(); | 
 | 577 | 			do_remount_sb(sb, MS_RDONLY, NULL, 1); | 
 | 578 | 			unlock_kernel(); | 
 | 579 | 		} | 
 | 580 | 		drop_super(sb); | 
 | 581 | 		spin_lock(&sb_lock); | 
 | 582 | 	} | 
 | 583 | 	spin_unlock(&sb_lock); | 
 | 584 | 	printk("Emergency Remount complete\n"); | 
 | 585 | } | 
 | 586 |  | 
 | 587 | void emergency_remount(void) | 
 | 588 | { | 
 | 589 | 	pdflush_operation(do_emergency_remount, 0); | 
 | 590 | } | 
 | 591 |  | 
 | 592 | /* | 
 | 593 |  * Unnamed block devices are dummy devices used by virtual | 
 | 594 |  * filesystems which don't use real block-devices.  -- jrs | 
 | 595 |  */ | 
 | 596 |  | 
 | 597 | static struct idr unnamed_dev_idr; | 
 | 598 | static DEFINE_SPINLOCK(unnamed_dev_lock);/* protects the above */ | 
 | 599 |  | 
 | 600 | int set_anon_super(struct super_block *s, void *data) | 
 | 601 | { | 
 | 602 | 	int dev; | 
 | 603 | 	int error; | 
 | 604 |  | 
 | 605 |  retry: | 
 | 606 | 	if (idr_pre_get(&unnamed_dev_idr, GFP_ATOMIC) == 0) | 
 | 607 | 		return -ENOMEM; | 
 | 608 | 	spin_lock(&unnamed_dev_lock); | 
 | 609 | 	error = idr_get_new(&unnamed_dev_idr, NULL, &dev); | 
 | 610 | 	spin_unlock(&unnamed_dev_lock); | 
 | 611 | 	if (error == -EAGAIN) | 
 | 612 | 		/* We raced and lost with another CPU. */ | 
 | 613 | 		goto retry; | 
 | 614 | 	else if (error) | 
 | 615 | 		return -EAGAIN; | 
 | 616 |  | 
 | 617 | 	if ((dev & MAX_ID_MASK) == (1 << MINORBITS)) { | 
 | 618 | 		spin_lock(&unnamed_dev_lock); | 
 | 619 | 		idr_remove(&unnamed_dev_idr, dev); | 
 | 620 | 		spin_unlock(&unnamed_dev_lock); | 
 | 621 | 		return -EMFILE; | 
 | 622 | 	} | 
 | 623 | 	s->s_dev = MKDEV(0, dev & MINORMASK); | 
 | 624 | 	return 0; | 
 | 625 | } | 
 | 626 |  | 
 | 627 | EXPORT_SYMBOL(set_anon_super); | 
 | 628 |  | 
 | 629 | void kill_anon_super(struct super_block *sb) | 
 | 630 | { | 
 | 631 | 	int slot = MINOR(sb->s_dev); | 
 | 632 |  | 
 | 633 | 	generic_shutdown_super(sb); | 
 | 634 | 	spin_lock(&unnamed_dev_lock); | 
 | 635 | 	idr_remove(&unnamed_dev_idr, slot); | 
 | 636 | 	spin_unlock(&unnamed_dev_lock); | 
 | 637 | } | 
 | 638 |  | 
 | 639 | EXPORT_SYMBOL(kill_anon_super); | 
 | 640 |  | 
 | 641 | void __init unnamed_dev_init(void) | 
 | 642 | { | 
 | 643 | 	idr_init(&unnamed_dev_idr); | 
 | 644 | } | 
 | 645 |  | 
 | 646 | void kill_litter_super(struct super_block *sb) | 
 | 647 | { | 
 | 648 | 	if (sb->s_root) | 
 | 649 | 		d_genocide(sb->s_root); | 
 | 650 | 	kill_anon_super(sb); | 
 | 651 | } | 
 | 652 |  | 
 | 653 | EXPORT_SYMBOL(kill_litter_super); | 
 | 654 |  | 
 | 655 | static int set_bdev_super(struct super_block *s, void *data) | 
 | 656 | { | 
 | 657 | 	s->s_bdev = data; | 
 | 658 | 	s->s_dev = s->s_bdev->bd_dev; | 
 | 659 | 	return 0; | 
 | 660 | } | 
 | 661 |  | 
 | 662 | static int test_bdev_super(struct super_block *s, void *data) | 
 | 663 | { | 
 | 664 | 	return (void *)s->s_bdev == data; | 
 | 665 | } | 
 | 666 |  | 
 | 667 | static void bdev_uevent(struct block_device *bdev, enum kobject_action action) | 
 | 668 | { | 
 | 669 | 	if (bdev->bd_disk) { | 
 | 670 | 		if (bdev->bd_part) | 
 | 671 | 			kobject_uevent(&bdev->bd_part->kobj, action, NULL); | 
 | 672 | 		else | 
 | 673 | 			kobject_uevent(&bdev->bd_disk->kobj, action, NULL); | 
 | 674 | 	} | 
 | 675 | } | 
 | 676 |  | 
 | 677 | struct super_block *get_sb_bdev(struct file_system_type *fs_type, | 
 | 678 | 	int flags, const char *dev_name, void *data, | 
 | 679 | 	int (*fill_super)(struct super_block *, void *, int)) | 
 | 680 | { | 
 | 681 | 	struct block_device *bdev; | 
 | 682 | 	struct super_block *s; | 
 | 683 | 	int error = 0; | 
 | 684 |  | 
 | 685 | 	bdev = open_bdev_excl(dev_name, flags, fs_type); | 
 | 686 | 	if (IS_ERR(bdev)) | 
 | 687 | 		return (struct super_block *)bdev; | 
 | 688 |  | 
 | 689 | 	/* | 
 | 690 | 	 * once the super is inserted into the list by sget, s_umount | 
 | 691 | 	 * will protect the lockfs code from trying to start a snapshot | 
 | 692 | 	 * while we are mounting | 
 | 693 | 	 */ | 
 | 694 | 	down(&bdev->bd_mount_sem); | 
 | 695 | 	s = sget(fs_type, test_bdev_super, set_bdev_super, bdev); | 
 | 696 | 	up(&bdev->bd_mount_sem); | 
 | 697 | 	if (IS_ERR(s)) | 
 | 698 | 		goto out; | 
 | 699 |  | 
 | 700 | 	if (s->s_root) { | 
 | 701 | 		if ((flags ^ s->s_flags) & MS_RDONLY) { | 
 | 702 | 			up_write(&s->s_umount); | 
 | 703 | 			deactivate_super(s); | 
 | 704 | 			s = ERR_PTR(-EBUSY); | 
 | 705 | 		} | 
 | 706 | 		goto out; | 
 | 707 | 	} else { | 
 | 708 | 		char b[BDEVNAME_SIZE]; | 
 | 709 |  | 
 | 710 | 		s->s_flags = flags; | 
 | 711 | 		strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id)); | 
 | 712 | 		s->s_old_blocksize = block_size(bdev); | 
 | 713 | 		sb_set_blocksize(s, s->s_old_blocksize); | 
 | 714 | 		error = fill_super(s, data, flags & MS_VERBOSE ? 1 : 0); | 
 | 715 | 		if (error) { | 
 | 716 | 			up_write(&s->s_umount); | 
 | 717 | 			deactivate_super(s); | 
 | 718 | 			s = ERR_PTR(error); | 
 | 719 | 		} else { | 
 | 720 | 			s->s_flags |= MS_ACTIVE; | 
 | 721 | 			bdev_uevent(bdev, KOBJ_MOUNT); | 
 | 722 | 		} | 
 | 723 | 	} | 
 | 724 |  | 
 | 725 | 	return s; | 
 | 726 |  | 
 | 727 | out: | 
 | 728 | 	close_bdev_excl(bdev); | 
 | 729 | 	return s; | 
 | 730 | } | 
 | 731 |  | 
 | 732 | EXPORT_SYMBOL(get_sb_bdev); | 
 | 733 |  | 
 | 734 | void kill_block_super(struct super_block *sb) | 
 | 735 | { | 
 | 736 | 	struct block_device *bdev = sb->s_bdev; | 
 | 737 |  | 
 | 738 | 	bdev_uevent(bdev, KOBJ_UMOUNT); | 
 | 739 | 	generic_shutdown_super(sb); | 
 | 740 | 	sync_blockdev(bdev); | 
 | 741 | 	close_bdev_excl(bdev); | 
 | 742 | } | 
 | 743 |  | 
 | 744 | EXPORT_SYMBOL(kill_block_super); | 
 | 745 |  | 
 | 746 | struct super_block *get_sb_nodev(struct file_system_type *fs_type, | 
 | 747 | 	int flags, void *data, | 
 | 748 | 	int (*fill_super)(struct super_block *, void *, int)) | 
 | 749 | { | 
 | 750 | 	int error; | 
 | 751 | 	struct super_block *s = sget(fs_type, NULL, set_anon_super, NULL); | 
 | 752 |  | 
 | 753 | 	if (IS_ERR(s)) | 
 | 754 | 		return s; | 
 | 755 |  | 
 | 756 | 	s->s_flags = flags; | 
 | 757 |  | 
 | 758 | 	error = fill_super(s, data, flags & MS_VERBOSE ? 1 : 0); | 
 | 759 | 	if (error) { | 
 | 760 | 		up_write(&s->s_umount); | 
 | 761 | 		deactivate_super(s); | 
 | 762 | 		return ERR_PTR(error); | 
 | 763 | 	} | 
 | 764 | 	s->s_flags |= MS_ACTIVE; | 
 | 765 | 	return s; | 
 | 766 | } | 
 | 767 |  | 
 | 768 | EXPORT_SYMBOL(get_sb_nodev); | 
 | 769 |  | 
 | 770 | static int compare_single(struct super_block *s, void *p) | 
 | 771 | { | 
 | 772 | 	return 1; | 
 | 773 | } | 
 | 774 |  | 
 | 775 | struct super_block *get_sb_single(struct file_system_type *fs_type, | 
 | 776 | 	int flags, void *data, | 
 | 777 | 	int (*fill_super)(struct super_block *, void *, int)) | 
 | 778 | { | 
 | 779 | 	struct super_block *s; | 
 | 780 | 	int error; | 
 | 781 |  | 
 | 782 | 	s = sget(fs_type, compare_single, set_anon_super, NULL); | 
 | 783 | 	if (IS_ERR(s)) | 
 | 784 | 		return s; | 
 | 785 | 	if (!s->s_root) { | 
 | 786 | 		s->s_flags = flags; | 
 | 787 | 		error = fill_super(s, data, flags & MS_VERBOSE ? 1 : 0); | 
 | 788 | 		if (error) { | 
 | 789 | 			up_write(&s->s_umount); | 
 | 790 | 			deactivate_super(s); | 
 | 791 | 			return ERR_PTR(error); | 
 | 792 | 		} | 
 | 793 | 		s->s_flags |= MS_ACTIVE; | 
 | 794 | 	} | 
 | 795 | 	do_remount_sb(s, flags, data, 0); | 
 | 796 | 	return s; | 
 | 797 | } | 
 | 798 |  | 
 | 799 | EXPORT_SYMBOL(get_sb_single); | 
 | 800 |  | 
 | 801 | struct vfsmount * | 
 | 802 | do_kern_mount(const char *fstype, int flags, const char *name, void *data) | 
 | 803 | { | 
 | 804 | 	struct file_system_type *type = get_fs_type(fstype); | 
 | 805 | 	struct super_block *sb = ERR_PTR(-ENOMEM); | 
 | 806 | 	struct vfsmount *mnt; | 
 | 807 | 	int error; | 
 | 808 | 	char *secdata = NULL; | 
 | 809 |  | 
 | 810 | 	if (!type) | 
 | 811 | 		return ERR_PTR(-ENODEV); | 
 | 812 |  | 
 | 813 | 	mnt = alloc_vfsmnt(name); | 
 | 814 | 	if (!mnt) | 
 | 815 | 		goto out; | 
 | 816 |  | 
 | 817 | 	if (data) { | 
 | 818 | 		secdata = alloc_secdata(); | 
 | 819 | 		if (!secdata) { | 
 | 820 | 			sb = ERR_PTR(-ENOMEM); | 
 | 821 | 			goto out_mnt; | 
 | 822 | 		} | 
 | 823 |  | 
 | 824 | 		error = security_sb_copy_data(type, data, secdata); | 
 | 825 | 		if (error) { | 
 | 826 | 			sb = ERR_PTR(error); | 
 | 827 | 			goto out_free_secdata; | 
 | 828 | 		} | 
 | 829 | 	} | 
 | 830 |  | 
 | 831 | 	sb = type->get_sb(type, flags, name, data); | 
 | 832 | 	if (IS_ERR(sb)) | 
 | 833 | 		goto out_free_secdata; | 
 | 834 |  	error = security_sb_kern_mount(sb, secdata); | 
 | 835 |  	if (error) | 
 | 836 |  		goto out_sb; | 
 | 837 | 	mnt->mnt_sb = sb; | 
 | 838 | 	mnt->mnt_root = dget(sb->s_root); | 
 | 839 | 	mnt->mnt_mountpoint = sb->s_root; | 
 | 840 | 	mnt->mnt_parent = mnt; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 841 | 	up_write(&sb->s_umount); | 
| Gerald Schaefer | 8680e22 | 2005-06-21 17:15:16 -0700 | [diff] [blame] | 842 | 	free_secdata(secdata); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 843 | 	put_filesystem(type); | 
 | 844 | 	return mnt; | 
 | 845 | out_sb: | 
 | 846 | 	up_write(&sb->s_umount); | 
 | 847 | 	deactivate_super(sb); | 
 | 848 | 	sb = ERR_PTR(error); | 
 | 849 | out_free_secdata: | 
 | 850 | 	free_secdata(secdata); | 
 | 851 | out_mnt: | 
 | 852 | 	free_vfsmnt(mnt); | 
 | 853 | out: | 
 | 854 | 	put_filesystem(type); | 
 | 855 | 	return (struct vfsmount *)sb; | 
 | 856 | } | 
 | 857 |  | 
 | 858 | EXPORT_SYMBOL_GPL(do_kern_mount); | 
 | 859 |  | 
 | 860 | struct vfsmount *kern_mount(struct file_system_type *type) | 
 | 861 | { | 
 | 862 | 	return do_kern_mount(type->name, 0, type->name, NULL); | 
 | 863 | } | 
 | 864 |  | 
 | 865 | EXPORT_SYMBOL(kern_mount); |