super.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087
  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. #include <linux/module.h>
  23. #include <linux/slab.h>
  24. #include <linux/init.h>
  25. #include <linux/smp_lock.h>
  26. #include <linux/acct.h>
  27. #include <linux/blkdev.h>
  28. #include <linux/quotaops.h>
  29. #include <linux/namei.h>
  30. #include <linux/buffer_head.h> /* for fsync_super() */
  31. #include <linux/mount.h>
  32. #include <linux/security.h>
  33. #include <linux/syscalls.h>
  34. #include <linux/vfs.h>
  35. #include <linux/writeback.h> /* for the emergency remount stuff */
  36. #include <linux/idr.h>
  37. #include <linux/kobject.h>
  38. #include <linux/mutex.h>
  39. #include <linux/file.h>
  40. #include <linux/async.h>
  41. #include <asm/uaccess.h>
  42. #include "internal.h"
  43. LIST_HEAD(super_blocks);
  44. DEFINE_SPINLOCK(sb_lock);
  45. /**
  46. * alloc_super - create new superblock
  47. * @type: filesystem type superblock should belong to
  48. *
  49. * Allocates and initializes a new &struct super_block. alloc_super()
  50. * returns a pointer new superblock or %NULL if allocation had failed.
  51. */
  52. static struct super_block *alloc_super(struct file_system_type *type)
  53. {
  54. struct super_block *s = kzalloc(sizeof(struct super_block), GFP_USER);
  55. static struct super_operations default_op;
  56. if (s) {
  57. if (security_sb_alloc(s)) {
  58. kfree(s);
  59. s = NULL;
  60. goto out;
  61. }
  62. INIT_LIST_HEAD(&s->s_dirty);
  63. INIT_LIST_HEAD(&s->s_io);
  64. INIT_LIST_HEAD(&s->s_more_io);
  65. INIT_LIST_HEAD(&s->s_files);
  66. INIT_LIST_HEAD(&s->s_instances);
  67. INIT_HLIST_HEAD(&s->s_anon);
  68. INIT_LIST_HEAD(&s->s_inodes);
  69. INIT_LIST_HEAD(&s->s_dentry_lru);
  70. INIT_LIST_HEAD(&s->s_async_list);
  71. init_rwsem(&s->s_umount);
  72. mutex_init(&s->s_lock);
  73. lockdep_set_class(&s->s_umount, &type->s_umount_key);
  74. /*
  75. * The locking rules for s_lock are up to the
  76. * filesystem. For example ext3fs has different
  77. * lock ordering than usbfs:
  78. */
  79. lockdep_set_class(&s->s_lock, &type->s_lock_key);
  80. /*
  81. * sget() can have s_umount recursion.
  82. *
  83. * When it cannot find a suitable sb, it allocates a new
  84. * one (this one), and tries again to find a suitable old
  85. * one.
  86. *
  87. * In case that succeeds, it will acquire the s_umount
  88. * lock of the old one. Since these are clearly distrinct
  89. * locks, and this object isn't exposed yet, there's no
  90. * risk of deadlocks.
  91. *
  92. * Annotate this by putting this lock in a different
  93. * subclass.
  94. */
  95. down_write_nested(&s->s_umount, SINGLE_DEPTH_NESTING);
  96. s->s_count = S_BIAS;
  97. atomic_set(&s->s_active, 1);
  98. mutex_init(&s->s_vfs_rename_mutex);
  99. mutex_init(&s->s_dquot.dqio_mutex);
  100. mutex_init(&s->s_dquot.dqonoff_mutex);
  101. init_rwsem(&s->s_dquot.dqptr_sem);
  102. init_waitqueue_head(&s->s_wait_unfrozen);
  103. s->s_maxbytes = MAX_NON_LFS;
  104. s->dq_op = sb_dquot_ops;
  105. s->s_qcop = sb_quotactl_ops;
  106. s->s_op = &default_op;
  107. s->s_time_gran = 1000000000;
  108. }
  109. out:
  110. return s;
  111. }
  112. /**
  113. * destroy_super - frees a superblock
  114. * @s: superblock to free
  115. *
  116. * Frees a superblock.
  117. */
  118. static inline void destroy_super(struct super_block *s)
  119. {
  120. security_sb_free(s);
  121. kfree(s->s_subtype);
  122. kfree(s->s_options);
  123. kfree(s);
  124. }
  125. /* Superblock refcounting */
  126. /*
  127. * Drop a superblock's refcount. Returns non-zero if the superblock was
  128. * destroyed. The caller must hold sb_lock.
  129. */
  130. static int __put_super(struct super_block *sb)
  131. {
  132. int ret = 0;
  133. if (!--sb->s_count) {
  134. destroy_super(sb);
  135. ret = 1;
  136. }
  137. return ret;
  138. }
  139. /*
  140. * Drop a superblock's refcount.
  141. * Returns non-zero if the superblock is about to be destroyed and
  142. * at least is already removed from super_blocks list, so if we are
  143. * making a loop through super blocks then we need to restart.
  144. * The caller must hold sb_lock.
  145. */
  146. int __put_super_and_need_restart(struct super_block *sb)
  147. {
  148. /* check for race with generic_shutdown_super() */
  149. if (list_empty(&sb->s_list)) {
  150. /* super block is removed, need to restart... */
  151. __put_super(sb);
  152. return 1;
  153. }
  154. /* can't be the last, since s_list is still in use */
  155. sb->s_count--;
  156. BUG_ON(sb->s_count == 0);
  157. return 0;
  158. }
  159. /**
  160. * put_super - drop a temporary reference to superblock
  161. * @sb: superblock in question
  162. *
  163. * Drops a temporary reference, frees superblock if there's no
  164. * references left.
  165. */
  166. static void put_super(struct super_block *sb)
  167. {
  168. spin_lock(&sb_lock);
  169. __put_super(sb);
  170. spin_unlock(&sb_lock);
  171. }
  172. /**
  173. * deactivate_super - drop an active reference to superblock
  174. * @s: superblock to deactivate
  175. *
  176. * Drops an active reference to superblock, acquiring a temprory one if
  177. * there is no active references left. In that case we lock superblock,
  178. * tell fs driver to shut it down and drop the temporary reference we
  179. * had just acquired.
  180. */
  181. void deactivate_super(struct super_block *s)
  182. {
  183. struct file_system_type *fs = s->s_type;
  184. if (atomic_dec_and_lock(&s->s_active, &sb_lock)) {
  185. s->s_count -= S_BIAS-1;
  186. spin_unlock(&sb_lock);
  187. vfs_dq_off(s, 0);
  188. down_write(&s->s_umount);
  189. fs->kill_sb(s);
  190. put_filesystem(fs);
  191. put_super(s);
  192. }
  193. }
  194. EXPORT_SYMBOL(deactivate_super);
  195. /**
  196. * deactivate_locked_super - drop an active reference to superblock
  197. * @s: superblock to deactivate
  198. *
  199. * Equivalent of up_write(&s->s_umount); deactivate_super(s);, except that
  200. * it does not unlock it until it's all over. As the result, it's safe to
  201. * use to dispose of new superblock on ->get_sb() failure exits - nobody
  202. * will see the sucker until it's all over. Equivalent using up_write +
  203. * deactivate_super is safe for that purpose only if superblock is either
  204. * safe to use or has NULL ->s_root when we unlock.
  205. */
  206. void deactivate_locked_super(struct super_block *s)
  207. {
  208. struct file_system_type *fs = s->s_type;
  209. if (atomic_dec_and_lock(&s->s_active, &sb_lock)) {
  210. s->s_count -= S_BIAS-1;
  211. spin_unlock(&sb_lock);
  212. vfs_dq_off(s, 0);
  213. fs->kill_sb(s);
  214. put_filesystem(fs);
  215. put_super(s);
  216. } else {
  217. up_write(&s->s_umount);
  218. }
  219. }
  220. EXPORT_SYMBOL(deactivate_locked_super);
  221. /**
  222. * grab_super - acquire an active reference
  223. * @s: reference we are trying to make active
  224. *
  225. * Tries to acquire an active reference. grab_super() is used when we
  226. * had just found a superblock in super_blocks or fs_type->fs_supers
  227. * and want to turn it into a full-blown active reference. grab_super()
  228. * is called with sb_lock held and drops it. Returns 1 in case of
  229. * success, 0 if we had failed (superblock contents was already dead or
  230. * dying when grab_super() had been called).
  231. */
  232. static int grab_super(struct super_block *s) __releases(sb_lock)
  233. {
  234. s->s_count++;
  235. spin_unlock(&sb_lock);
  236. down_write(&s->s_umount);
  237. if (s->s_root) {
  238. spin_lock(&sb_lock);
  239. if (s->s_count > S_BIAS) {
  240. atomic_inc(&s->s_active);
  241. s->s_count--;
  242. spin_unlock(&sb_lock);
  243. return 1;
  244. }
  245. spin_unlock(&sb_lock);
  246. }
  247. up_write(&s->s_umount);
  248. put_super(s);
  249. yield();
  250. return 0;
  251. }
  252. /*
  253. * Superblock locking. We really ought to get rid of these two.
  254. */
  255. void lock_super(struct super_block * sb)
  256. {
  257. get_fs_excl();
  258. mutex_lock(&sb->s_lock);
  259. }
  260. void unlock_super(struct super_block * sb)
  261. {
  262. put_fs_excl();
  263. mutex_unlock(&sb->s_lock);
  264. }
  265. EXPORT_SYMBOL(lock_super);
  266. EXPORT_SYMBOL(unlock_super);
  267. /*
  268. * Write out and wait upon all dirty data associated with this
  269. * superblock. Filesystem data as well as the underlying block
  270. * device. Takes the superblock lock. Requires a second blkdev
  271. * flush by the caller to complete the operation.
  272. */
  273. void __fsync_super(struct super_block *sb)
  274. {
  275. sync_inodes_sb(sb, 0);
  276. vfs_dq_sync(sb);
  277. lock_super(sb);
  278. if (sb->s_dirt && sb->s_op->write_super)
  279. sb->s_op->write_super(sb);
  280. unlock_super(sb);
  281. if (sb->s_op->sync_fs)
  282. sb->s_op->sync_fs(sb, 1);
  283. sync_blockdev(sb->s_bdev);
  284. sync_inodes_sb(sb, 1);
  285. }
  286. /*
  287. * Write out and wait upon all dirty data associated with this
  288. * superblock. Filesystem data as well as the underlying block
  289. * device. Takes the superblock lock.
  290. */
  291. int fsync_super(struct super_block *sb)
  292. {
  293. __fsync_super(sb);
  294. return sync_blockdev(sb->s_bdev);
  295. }
  296. EXPORT_SYMBOL_GPL(fsync_super);
  297. /**
  298. * generic_shutdown_super - common helper for ->kill_sb()
  299. * @sb: superblock to kill
  300. *
  301. * generic_shutdown_super() does all fs-independent work on superblock
  302. * shutdown. Typical ->kill_sb() should pick all fs-specific objects
  303. * that need destruction out of superblock, call generic_shutdown_super()
  304. * and release aforementioned objects. Note: dentries and inodes _are_
  305. * taken care of and do not need specific handling.
  306. *
  307. * Upon calling this function, the filesystem may no longer alter or
  308. * rearrange the set of dentries belonging to this super_block, nor may it
  309. * change the attachments of dentries to inodes.
  310. */
  311. void generic_shutdown_super(struct super_block *sb)
  312. {
  313. const struct super_operations *sop = sb->s_op;
  314. if (sb->s_root) {
  315. shrink_dcache_for_umount(sb);
  316. fsync_super(sb);
  317. lock_super(sb);
  318. sb->s_flags &= ~MS_ACTIVE;
  319. /*
  320. * wait for asynchronous fs operations to finish before going further
  321. */
  322. async_synchronize_full_domain(&sb->s_async_list);
  323. /* bad name - it should be evict_inodes() */
  324. invalidate_inodes(sb);
  325. lock_kernel();
  326. if (sop->write_super && sb->s_dirt)
  327. sop->write_super(sb);
  328. if (sop->put_super)
  329. sop->put_super(sb);
  330. /* Forget any remaining inodes */
  331. if (invalidate_inodes(sb)) {
  332. printk("VFS: Busy inodes after unmount of %s. "
  333. "Self-destruct in 5 seconds. Have a nice day...\n",
  334. sb->s_id);
  335. }
  336. unlock_kernel();
  337. unlock_super(sb);
  338. }
  339. spin_lock(&sb_lock);
  340. /* should be initialized for __put_super_and_need_restart() */
  341. list_del_init(&sb->s_list);
  342. list_del(&sb->s_instances);
  343. spin_unlock(&sb_lock);
  344. up_write(&sb->s_umount);
  345. }
  346. EXPORT_SYMBOL(generic_shutdown_super);
  347. /**
  348. * sget - find or create a superblock
  349. * @type: filesystem type superblock should belong to
  350. * @test: comparison callback
  351. * @set: setup callback
  352. * @data: argument to each of them
  353. */
  354. struct super_block *sget(struct file_system_type *type,
  355. int (*test)(struct super_block *,void *),
  356. int (*set)(struct super_block *,void *),
  357. void *data)
  358. {
  359. struct super_block *s = NULL;
  360. struct super_block *old;
  361. int err;
  362. retry:
  363. spin_lock(&sb_lock);
  364. if (test) {
  365. list_for_each_entry(old, &type->fs_supers, s_instances) {
  366. if (!test(old, data))
  367. continue;
  368. if (!grab_super(old))
  369. goto retry;
  370. if (s) {
  371. up_write(&s->s_umount);
  372. destroy_super(s);
  373. }
  374. return old;
  375. }
  376. }
  377. if (!s) {
  378. spin_unlock(&sb_lock);
  379. s = alloc_super(type);
  380. if (!s)
  381. return ERR_PTR(-ENOMEM);
  382. goto retry;
  383. }
  384. err = set(s, data);
  385. if (err) {
  386. spin_unlock(&sb_lock);
  387. up_write(&s->s_umount);
  388. destroy_super(s);
  389. return ERR_PTR(err);
  390. }
  391. s->s_type = type;
  392. strlcpy(s->s_id, type->name, sizeof(s->s_id));
  393. list_add_tail(&s->s_list, &super_blocks);
  394. list_add(&s->s_instances, &type->fs_supers);
  395. spin_unlock(&sb_lock);
  396. get_filesystem(type);
  397. return s;
  398. }
  399. EXPORT_SYMBOL(sget);
  400. void drop_super(struct super_block *sb)
  401. {
  402. up_read(&sb->s_umount);
  403. put_super(sb);
  404. }
  405. EXPORT_SYMBOL(drop_super);
  406. static inline void write_super(struct super_block *sb)
  407. {
  408. lock_super(sb);
  409. if (sb->s_root && sb->s_dirt)
  410. if (sb->s_op->write_super)
  411. sb->s_op->write_super(sb);
  412. unlock_super(sb);
  413. }
  414. /*
  415. * Note: check the dirty flag before waiting, so we don't
  416. * hold up the sync while mounting a device. (The newly
  417. * mounted device won't need syncing.)
  418. */
  419. void sync_supers(void)
  420. {
  421. struct super_block *sb;
  422. spin_lock(&sb_lock);
  423. restart:
  424. list_for_each_entry(sb, &super_blocks, s_list) {
  425. if (sb->s_dirt) {
  426. sb->s_count++;
  427. spin_unlock(&sb_lock);
  428. down_read(&sb->s_umount);
  429. write_super(sb);
  430. up_read(&sb->s_umount);
  431. spin_lock(&sb_lock);
  432. if (__put_super_and_need_restart(sb))
  433. goto restart;
  434. }
  435. }
  436. spin_unlock(&sb_lock);
  437. }
  438. /*
  439. * Call the ->sync_fs super_op against all filesystems which are r/w and
  440. * which implement it.
  441. *
  442. * This operation is careful to avoid the livelock which could easily happen
  443. * if two or more filesystems are being continuously dirtied. s_need_sync_fs
  444. * is used only here. We set it against all filesystems and then clear it as
  445. * we sync them. So redirtied filesystems are skipped.
  446. *
  447. * But if process A is currently running sync_filesystems and then process B
  448. * calls sync_filesystems as well, process B will set all the s_need_sync_fs
  449. * flags again, which will cause process A to resync everything. Fix that with
  450. * a local mutex.
  451. *
  452. * (Fabian) Avoid sync_fs with clean fs & wait mode 0
  453. */
  454. void sync_filesystems(int wait)
  455. {
  456. struct super_block *sb;
  457. static DEFINE_MUTEX(mutex);
  458. mutex_lock(&mutex); /* Could be down_interruptible */
  459. spin_lock(&sb_lock);
  460. list_for_each_entry(sb, &super_blocks, s_list) {
  461. if (!sb->s_op->sync_fs)
  462. continue;
  463. if (sb->s_flags & MS_RDONLY)
  464. continue;
  465. sb->s_need_sync_fs = 1;
  466. }
  467. restart:
  468. list_for_each_entry(sb, &super_blocks, s_list) {
  469. if (!sb->s_need_sync_fs)
  470. continue;
  471. sb->s_need_sync_fs = 0;
  472. if (sb->s_flags & MS_RDONLY)
  473. continue; /* hm. Was remounted r/o meanwhile */
  474. sb->s_count++;
  475. spin_unlock(&sb_lock);
  476. down_read(&sb->s_umount);
  477. async_synchronize_full_domain(&sb->s_async_list);
  478. if (sb->s_root && (wait || sb->s_dirt))
  479. sb->s_op->sync_fs(sb, wait);
  480. up_read(&sb->s_umount);
  481. /* restart only when sb is no longer on the list */
  482. spin_lock(&sb_lock);
  483. if (__put_super_and_need_restart(sb))
  484. goto restart;
  485. }
  486. spin_unlock(&sb_lock);
  487. mutex_unlock(&mutex);
  488. }
  489. /**
  490. * get_super - get the superblock of a device
  491. * @bdev: device to get the superblock for
  492. *
  493. * Scans the superblock list and finds the superblock of the file system
  494. * mounted on the device given. %NULL is returned if no match is found.
  495. */
  496. struct super_block * get_super(struct block_device *bdev)
  497. {
  498. struct super_block *sb;
  499. if (!bdev)
  500. return NULL;
  501. spin_lock(&sb_lock);
  502. rescan:
  503. list_for_each_entry(sb, &super_blocks, s_list) {
  504. if (sb->s_bdev == bdev) {
  505. sb->s_count++;
  506. spin_unlock(&sb_lock);
  507. down_read(&sb->s_umount);
  508. if (sb->s_root)
  509. return sb;
  510. up_read(&sb->s_umount);
  511. /* restart only when sb is no longer on the list */
  512. spin_lock(&sb_lock);
  513. if (__put_super_and_need_restart(sb))
  514. goto rescan;
  515. }
  516. }
  517. spin_unlock(&sb_lock);
  518. return NULL;
  519. }
  520. EXPORT_SYMBOL(get_super);
  521. struct super_block * user_get_super(dev_t dev)
  522. {
  523. struct super_block *sb;
  524. spin_lock(&sb_lock);
  525. rescan:
  526. list_for_each_entry(sb, &super_blocks, s_list) {
  527. if (sb->s_dev == dev) {
  528. sb->s_count++;
  529. spin_unlock(&sb_lock);
  530. down_read(&sb->s_umount);
  531. if (sb->s_root)
  532. return sb;
  533. up_read(&sb->s_umount);
  534. /* restart only when sb is no longer on the list */
  535. spin_lock(&sb_lock);
  536. if (__put_super_and_need_restart(sb))
  537. goto rescan;
  538. }
  539. }
  540. spin_unlock(&sb_lock);
  541. return NULL;
  542. }
  543. SYSCALL_DEFINE2(ustat, unsigned, dev, struct ustat __user *, ubuf)
  544. {
  545. struct super_block *s;
  546. struct ustat tmp;
  547. struct kstatfs sbuf;
  548. int err = -EINVAL;
  549. s = user_get_super(new_decode_dev(dev));
  550. if (s == NULL)
  551. goto out;
  552. err = vfs_statfs(s->s_root, &sbuf);
  553. drop_super(s);
  554. if (err)
  555. goto out;
  556. memset(&tmp,0,sizeof(struct ustat));
  557. tmp.f_tfree = sbuf.f_bfree;
  558. tmp.f_tinode = sbuf.f_ffree;
  559. err = copy_to_user(ubuf,&tmp,sizeof(struct ustat)) ? -EFAULT : 0;
  560. out:
  561. return err;
  562. }
  563. /**
  564. * mark_files_ro - mark all files read-only
  565. * @sb: superblock in question
  566. *
  567. * All files are marked read-only. We don't care about pending
  568. * delete files so this should be used in 'force' mode only.
  569. */
  570. static void mark_files_ro(struct super_block *sb)
  571. {
  572. struct file *f;
  573. retry:
  574. file_list_lock();
  575. list_for_each_entry(f, &sb->s_files, f_u.fu_list) {
  576. struct vfsmount *mnt;
  577. if (!S_ISREG(f->f_path.dentry->d_inode->i_mode))
  578. continue;
  579. if (!file_count(f))
  580. continue;
  581. if (!(f->f_mode & FMODE_WRITE))
  582. continue;
  583. f->f_mode &= ~FMODE_WRITE;
  584. if (file_check_writeable(f) != 0)
  585. continue;
  586. file_release_write(f);
  587. mnt = mntget(f->f_path.mnt);
  588. file_list_unlock();
  589. /*
  590. * This can sleep, so we can't hold
  591. * the file_list_lock() spinlock.
  592. */
  593. mnt_drop_write(mnt);
  594. mntput(mnt);
  595. goto retry;
  596. }
  597. file_list_unlock();
  598. }
  599. /**
  600. * do_remount_sb - asks filesystem to change mount options.
  601. * @sb: superblock in question
  602. * @flags: numeric part of options
  603. * @data: the rest of options
  604. * @force: whether or not to force the change
  605. *
  606. * Alters the mount options of a mounted file system.
  607. */
  608. int do_remount_sb(struct super_block *sb, int flags, void *data, int force)
  609. {
  610. int retval;
  611. int remount_rw;
  612. #ifdef CONFIG_BLOCK
  613. if (!(flags & MS_RDONLY) && bdev_read_only(sb->s_bdev))
  614. return -EACCES;
  615. #endif
  616. if (flags & MS_RDONLY)
  617. acct_auto_close(sb);
  618. shrink_dcache_sb(sb);
  619. fsync_super(sb);
  620. /* If we are remounting RDONLY and current sb is read/write,
  621. make sure there are no rw files opened */
  622. if ((flags & MS_RDONLY) && !(sb->s_flags & MS_RDONLY)) {
  623. if (force)
  624. mark_files_ro(sb);
  625. else if (!fs_may_remount_ro(sb))
  626. return -EBUSY;
  627. retval = vfs_dq_off(sb, 1);
  628. if (retval < 0 && retval != -ENOSYS)
  629. return -EBUSY;
  630. }
  631. remount_rw = !(flags & MS_RDONLY) && (sb->s_flags & MS_RDONLY);
  632. if (sb->s_op->remount_fs) {
  633. lock_super(sb);
  634. retval = sb->s_op->remount_fs(sb, &flags, data);
  635. unlock_super(sb);
  636. if (retval)
  637. return retval;
  638. }
  639. sb->s_flags = (sb->s_flags & ~MS_RMT_MASK) | (flags & MS_RMT_MASK);
  640. if (remount_rw)
  641. vfs_dq_quota_on_remount(sb);
  642. return 0;
  643. }
  644. static void do_emergency_remount(struct work_struct *work)
  645. {
  646. struct super_block *sb;
  647. spin_lock(&sb_lock);
  648. list_for_each_entry(sb, &super_blocks, s_list) {
  649. sb->s_count++;
  650. spin_unlock(&sb_lock);
  651. down_read(&sb->s_umount);
  652. if (sb->s_root && sb->s_bdev && !(sb->s_flags & MS_RDONLY)) {
  653. /*
  654. * ->remount_fs needs lock_kernel().
  655. *
  656. * What lock protects sb->s_flags??
  657. */
  658. lock_kernel();
  659. do_remount_sb(sb, MS_RDONLY, NULL, 1);
  660. unlock_kernel();
  661. }
  662. drop_super(sb);
  663. spin_lock(&sb_lock);
  664. }
  665. spin_unlock(&sb_lock);
  666. kfree(work);
  667. printk("Emergency Remount complete\n");
  668. }
  669. void emergency_remount(void)
  670. {
  671. struct work_struct *work;
  672. work = kmalloc(sizeof(*work), GFP_ATOMIC);
  673. if (work) {
  674. INIT_WORK(work, do_emergency_remount);
  675. schedule_work(work);
  676. }
  677. }
  678. /*
  679. * Unnamed block devices are dummy devices used by virtual
  680. * filesystems which don't use real block-devices. -- jrs
  681. */
  682. static DEFINE_IDA(unnamed_dev_ida);
  683. static DEFINE_SPINLOCK(unnamed_dev_lock);/* protects the above */
  684. int set_anon_super(struct super_block *s, void *data)
  685. {
  686. int dev;
  687. int error;
  688. retry:
  689. if (ida_pre_get(&unnamed_dev_ida, GFP_ATOMIC) == 0)
  690. return -ENOMEM;
  691. spin_lock(&unnamed_dev_lock);
  692. error = ida_get_new(&unnamed_dev_ida, &dev);
  693. spin_unlock(&unnamed_dev_lock);
  694. if (error == -EAGAIN)
  695. /* We raced and lost with another CPU. */
  696. goto retry;
  697. else if (error)
  698. return -EAGAIN;
  699. if ((dev & MAX_ID_MASK) == (1 << MINORBITS)) {
  700. spin_lock(&unnamed_dev_lock);
  701. ida_remove(&unnamed_dev_ida, dev);
  702. spin_unlock(&unnamed_dev_lock);
  703. return -EMFILE;
  704. }
  705. s->s_dev = MKDEV(0, dev & MINORMASK);
  706. return 0;
  707. }
  708. EXPORT_SYMBOL(set_anon_super);
  709. void kill_anon_super(struct super_block *sb)
  710. {
  711. int slot = MINOR(sb->s_dev);
  712. generic_shutdown_super(sb);
  713. spin_lock(&unnamed_dev_lock);
  714. ida_remove(&unnamed_dev_ida, slot);
  715. spin_unlock(&unnamed_dev_lock);
  716. }
  717. EXPORT_SYMBOL(kill_anon_super);
  718. void kill_litter_super(struct super_block *sb)
  719. {
  720. if (sb->s_root)
  721. d_genocide(sb->s_root);
  722. kill_anon_super(sb);
  723. }
  724. EXPORT_SYMBOL(kill_litter_super);
  725. static int ns_test_super(struct super_block *sb, void *data)
  726. {
  727. return sb->s_fs_info == data;
  728. }
  729. static int ns_set_super(struct super_block *sb, void *data)
  730. {
  731. sb->s_fs_info = data;
  732. return set_anon_super(sb, NULL);
  733. }
  734. int get_sb_ns(struct file_system_type *fs_type, int flags, void *data,
  735. int (*fill_super)(struct super_block *, void *, int),
  736. struct vfsmount *mnt)
  737. {
  738. struct super_block *sb;
  739. sb = sget(fs_type, ns_test_super, ns_set_super, data);
  740. if (IS_ERR(sb))
  741. return PTR_ERR(sb);
  742. if (!sb->s_root) {
  743. int err;
  744. sb->s_flags = flags;
  745. err = fill_super(sb, data, flags & MS_SILENT ? 1 : 0);
  746. if (err) {
  747. deactivate_locked_super(sb);
  748. return err;
  749. }
  750. sb->s_flags |= MS_ACTIVE;
  751. }
  752. simple_set_mnt(mnt, sb);
  753. return 0;
  754. }
  755. EXPORT_SYMBOL(get_sb_ns);
  756. #ifdef CONFIG_BLOCK
  757. static int set_bdev_super(struct super_block *s, void *data)
  758. {
  759. s->s_bdev = data;
  760. s->s_dev = s->s_bdev->bd_dev;
  761. return 0;
  762. }
  763. static int test_bdev_super(struct super_block *s, void *data)
  764. {
  765. return (void *)s->s_bdev == data;
  766. }
  767. int get_sb_bdev(struct file_system_type *fs_type,
  768. int flags, const char *dev_name, void *data,
  769. int (*fill_super)(struct super_block *, void *, int),
  770. struct vfsmount *mnt)
  771. {
  772. struct block_device *bdev;
  773. struct super_block *s;
  774. fmode_t mode = FMODE_READ;
  775. int error = 0;
  776. if (!(flags & MS_RDONLY))
  777. mode |= FMODE_WRITE;
  778. bdev = open_bdev_exclusive(dev_name, mode, fs_type);
  779. if (IS_ERR(bdev))
  780. return PTR_ERR(bdev);
  781. /*
  782. * once the super is inserted into the list by sget, s_umount
  783. * will protect the lockfs code from trying to start a snapshot
  784. * while we are mounting
  785. */
  786. down(&bdev->bd_mount_sem);
  787. s = sget(fs_type, test_bdev_super, set_bdev_super, bdev);
  788. up(&bdev->bd_mount_sem);
  789. if (IS_ERR(s))
  790. goto error_s;
  791. if (s->s_root) {
  792. if ((flags ^ s->s_flags) & MS_RDONLY) {
  793. deactivate_locked_super(s);
  794. error = -EBUSY;
  795. goto error_bdev;
  796. }
  797. close_bdev_exclusive(bdev, mode);
  798. } else {
  799. char b[BDEVNAME_SIZE];
  800. s->s_flags = flags;
  801. s->s_mode = mode;
  802. strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id));
  803. sb_set_blocksize(s, block_size(bdev));
  804. error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
  805. if (error) {
  806. deactivate_locked_super(s);
  807. goto error;
  808. }
  809. s->s_flags |= MS_ACTIVE;
  810. bdev->bd_super = s;
  811. }
  812. simple_set_mnt(mnt, s);
  813. return 0;
  814. error_s:
  815. error = PTR_ERR(s);
  816. error_bdev:
  817. close_bdev_exclusive(bdev, mode);
  818. error:
  819. return error;
  820. }
  821. EXPORT_SYMBOL(get_sb_bdev);
  822. void kill_block_super(struct super_block *sb)
  823. {
  824. struct block_device *bdev = sb->s_bdev;
  825. fmode_t mode = sb->s_mode;
  826. bdev->bd_super = NULL;
  827. generic_shutdown_super(sb);
  828. sync_blockdev(bdev);
  829. close_bdev_exclusive(bdev, mode);
  830. }
  831. EXPORT_SYMBOL(kill_block_super);
  832. #endif
  833. int get_sb_nodev(struct file_system_type *fs_type,
  834. int flags, void *data,
  835. int (*fill_super)(struct super_block *, void *, int),
  836. struct vfsmount *mnt)
  837. {
  838. int error;
  839. struct super_block *s = sget(fs_type, NULL, set_anon_super, NULL);
  840. if (IS_ERR(s))
  841. return PTR_ERR(s);
  842. s->s_flags = flags;
  843. error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
  844. if (error) {
  845. deactivate_locked_super(s);
  846. return error;
  847. }
  848. s->s_flags |= MS_ACTIVE;
  849. simple_set_mnt(mnt, s);
  850. return 0;
  851. }
  852. EXPORT_SYMBOL(get_sb_nodev);
  853. static int compare_single(struct super_block *s, void *p)
  854. {
  855. return 1;
  856. }
  857. int get_sb_single(struct file_system_type *fs_type,
  858. int flags, void *data,
  859. int (*fill_super)(struct super_block *, void *, int),
  860. struct vfsmount *mnt)
  861. {
  862. struct super_block *s;
  863. int error;
  864. s = sget(fs_type, compare_single, set_anon_super, NULL);
  865. if (IS_ERR(s))
  866. return PTR_ERR(s);
  867. if (!s->s_root) {
  868. s->s_flags = flags;
  869. error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
  870. if (error) {
  871. deactivate_locked_super(s);
  872. return error;
  873. }
  874. s->s_flags |= MS_ACTIVE;
  875. }
  876. do_remount_sb(s, flags, data, 0);
  877. simple_set_mnt(mnt, s);
  878. return 0;
  879. }
  880. EXPORT_SYMBOL(get_sb_single);
  881. struct vfsmount *
  882. vfs_kern_mount(struct file_system_type *type, int flags, const char *name, void *data)
  883. {
  884. struct vfsmount *mnt;
  885. char *secdata = NULL;
  886. int error;
  887. if (!type)
  888. return ERR_PTR(-ENODEV);
  889. error = -ENOMEM;
  890. mnt = alloc_vfsmnt(name);
  891. if (!mnt)
  892. goto out;
  893. if (data && !(type->fs_flags & FS_BINARY_MOUNTDATA)) {
  894. secdata = alloc_secdata();
  895. if (!secdata)
  896. goto out_mnt;
  897. error = security_sb_copy_data(data, secdata);
  898. if (error)
  899. goto out_free_secdata;
  900. }
  901. error = type->get_sb(type, flags, name, data, mnt);
  902. if (error < 0)
  903. goto out_free_secdata;
  904. BUG_ON(!mnt->mnt_sb);
  905. error = security_sb_kern_mount(mnt->mnt_sb, flags, secdata);
  906. if (error)
  907. goto out_sb;
  908. mnt->mnt_mountpoint = mnt->mnt_root;
  909. mnt->mnt_parent = mnt;
  910. up_write(&mnt->mnt_sb->s_umount);
  911. free_secdata(secdata);
  912. return mnt;
  913. out_sb:
  914. dput(mnt->mnt_root);
  915. deactivate_locked_super(mnt->mnt_sb);
  916. out_free_secdata:
  917. free_secdata(secdata);
  918. out_mnt:
  919. free_vfsmnt(mnt);
  920. out:
  921. return ERR_PTR(error);
  922. }
  923. EXPORT_SYMBOL_GPL(vfs_kern_mount);
  924. static struct vfsmount *fs_set_subtype(struct vfsmount *mnt, const char *fstype)
  925. {
  926. int err;
  927. const char *subtype = strchr(fstype, '.');
  928. if (subtype) {
  929. subtype++;
  930. err = -EINVAL;
  931. if (!subtype[0])
  932. goto err;
  933. } else
  934. subtype = "";
  935. mnt->mnt_sb->s_subtype = kstrdup(subtype, GFP_KERNEL);
  936. err = -ENOMEM;
  937. if (!mnt->mnt_sb->s_subtype)
  938. goto err;
  939. return mnt;
  940. err:
  941. mntput(mnt);
  942. return ERR_PTR(err);
  943. }
  944. struct vfsmount *
  945. do_kern_mount(const char *fstype, int flags, const char *name, void *data)
  946. {
  947. struct file_system_type *type = get_fs_type(fstype);
  948. struct vfsmount *mnt;
  949. if (!type)
  950. return ERR_PTR(-ENODEV);
  951. mnt = vfs_kern_mount(type, flags, name, data);
  952. if (!IS_ERR(mnt) && (type->fs_flags & FS_HAS_SUBTYPE) &&
  953. !mnt->mnt_sb->s_subtype)
  954. mnt = fs_set_subtype(mnt, fstype);
  955. put_filesystem(type);
  956. return mnt;
  957. }
  958. EXPORT_SYMBOL_GPL(do_kern_mount);
  959. struct vfsmount *kern_mount_data(struct file_system_type *type, void *data)
  960. {
  961. return vfs_kern_mount(type, MS_KERNMOUNT, type->name, data);
  962. }
  963. EXPORT_SYMBOL_GPL(kern_mount_data);