super.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167
  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/acct.h>
  25. #include <linux/blkdev.h>
  26. #include <linux/mount.h>
  27. #include <linux/security.h>
  28. #include <linux/writeback.h> /* for the emergency remount stuff */
  29. #include <linux/idr.h>
  30. #include <linux/mutex.h>
  31. #include <linux/backing-dev.h>
  32. #include "internal.h"
  33. LIST_HEAD(super_blocks);
  34. DEFINE_SPINLOCK(sb_lock);
  35. /**
  36. * alloc_super - create new superblock
  37. * @type: filesystem type superblock should belong to
  38. *
  39. * Allocates and initializes a new &struct super_block. alloc_super()
  40. * returns a pointer new superblock or %NULL if allocation had failed.
  41. */
  42. static struct super_block *alloc_super(struct file_system_type *type)
  43. {
  44. struct super_block *s = kzalloc(sizeof(struct super_block), GFP_USER);
  45. static const struct super_operations default_op;
  46. if (s) {
  47. if (security_sb_alloc(s)) {
  48. kfree(s);
  49. s = NULL;
  50. goto out;
  51. }
  52. #ifdef CONFIG_SMP
  53. s->s_files = alloc_percpu(struct list_head);
  54. if (!s->s_files) {
  55. security_sb_free(s);
  56. kfree(s);
  57. s = NULL;
  58. goto out;
  59. } else {
  60. int i;
  61. for_each_possible_cpu(i)
  62. INIT_LIST_HEAD(per_cpu_ptr(s->s_files, i));
  63. }
  64. #else
  65. INIT_LIST_HEAD(&s->s_files);
  66. #endif
  67. INIT_LIST_HEAD(&s->s_instances);
  68. INIT_HLIST_HEAD(&s->s_anon);
  69. INIT_LIST_HEAD(&s->s_inodes);
  70. INIT_LIST_HEAD(&s->s_dentry_lru);
  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 = 1;
  97. atomic_set(&s->s_active, 1);
  98. mutex_init(&s->s_vfs_rename_mutex);
  99. lockdep_set_class(&s->s_vfs_rename_mutex, &type->s_vfs_rename_key);
  100. mutex_init(&s->s_dquot.dqio_mutex);
  101. mutex_init(&s->s_dquot.dqonoff_mutex);
  102. init_rwsem(&s->s_dquot.dqptr_sem);
  103. init_waitqueue_head(&s->s_wait_unfrozen);
  104. s->s_maxbytes = MAX_NON_LFS;
  105. s->s_op = &default_op;
  106. s->s_time_gran = 1000000000;
  107. }
  108. out:
  109. return s;
  110. }
  111. /**
  112. * destroy_super - frees a superblock
  113. * @s: superblock to free
  114. *
  115. * Frees a superblock.
  116. */
  117. static inline void destroy_super(struct super_block *s)
  118. {
  119. #ifdef CONFIG_SMP
  120. free_percpu(s->s_files);
  121. #endif
  122. security_sb_free(s);
  123. kfree(s->s_subtype);
  124. kfree(s->s_options);
  125. kfree(s);
  126. }
  127. /* Superblock refcounting */
  128. /*
  129. * Drop a superblock's refcount. The caller must hold sb_lock.
  130. */
  131. void __put_super(struct super_block *sb)
  132. {
  133. if (!--sb->s_count) {
  134. list_del_init(&sb->s_list);
  135. destroy_super(sb);
  136. }
  137. }
  138. /**
  139. * put_super - drop a temporary reference to superblock
  140. * @sb: superblock in question
  141. *
  142. * Drops a temporary reference, frees superblock if there's no
  143. * references left.
  144. */
  145. void put_super(struct super_block *sb)
  146. {
  147. spin_lock(&sb_lock);
  148. __put_super(sb);
  149. spin_unlock(&sb_lock);
  150. }
  151. /**
  152. * deactivate_locked_super - drop an active reference to superblock
  153. * @s: superblock to deactivate
  154. *
  155. * Drops an active reference to superblock, converting it into a temprory
  156. * one if there is no other active references left. In that case we
  157. * tell fs driver to shut it down and drop the temporary reference we
  158. * had just acquired.
  159. *
  160. * Caller holds exclusive lock on superblock; that lock is released.
  161. */
  162. void deactivate_locked_super(struct super_block *s)
  163. {
  164. struct file_system_type *fs = s->s_type;
  165. if (atomic_dec_and_test(&s->s_active)) {
  166. fs->kill_sb(s);
  167. put_filesystem(fs);
  168. put_super(s);
  169. } else {
  170. up_write(&s->s_umount);
  171. }
  172. }
  173. EXPORT_SYMBOL(deactivate_locked_super);
  174. /**
  175. * deactivate_super - drop an active reference to superblock
  176. * @s: superblock to deactivate
  177. *
  178. * Variant of deactivate_locked_super(), except that superblock is *not*
  179. * locked by caller. If we are going to drop the final active reference,
  180. * lock will be acquired prior to that.
  181. */
  182. void deactivate_super(struct super_block *s)
  183. {
  184. if (!atomic_add_unless(&s->s_active, -1, 1)) {
  185. down_write(&s->s_umount);
  186. deactivate_locked_super(s);
  187. }
  188. }
  189. EXPORT_SYMBOL(deactivate_super);
  190. /**
  191. * grab_super - acquire an active reference
  192. * @s: reference we are trying to make active
  193. *
  194. * Tries to acquire an active reference. grab_super() is used when we
  195. * had just found a superblock in super_blocks or fs_type->fs_supers
  196. * and want to turn it into a full-blown active reference. grab_super()
  197. * is called with sb_lock held and drops it. Returns 1 in case of
  198. * success, 0 if we had failed (superblock contents was already dead or
  199. * dying when grab_super() had been called).
  200. */
  201. static int grab_super(struct super_block *s) __releases(sb_lock)
  202. {
  203. if (atomic_inc_not_zero(&s->s_active)) {
  204. spin_unlock(&sb_lock);
  205. return 1;
  206. }
  207. /* it's going away */
  208. s->s_count++;
  209. spin_unlock(&sb_lock);
  210. /* wait for it to die */
  211. down_write(&s->s_umount);
  212. up_write(&s->s_umount);
  213. put_super(s);
  214. return 0;
  215. }
  216. /*
  217. * Superblock locking. We really ought to get rid of these two.
  218. */
  219. void lock_super(struct super_block * sb)
  220. {
  221. get_fs_excl();
  222. mutex_lock(&sb->s_lock);
  223. }
  224. void unlock_super(struct super_block * sb)
  225. {
  226. put_fs_excl();
  227. mutex_unlock(&sb->s_lock);
  228. }
  229. EXPORT_SYMBOL(lock_super);
  230. EXPORT_SYMBOL(unlock_super);
  231. /**
  232. * generic_shutdown_super - common helper for ->kill_sb()
  233. * @sb: superblock to kill
  234. *
  235. * generic_shutdown_super() does all fs-independent work on superblock
  236. * shutdown. Typical ->kill_sb() should pick all fs-specific objects
  237. * that need destruction out of superblock, call generic_shutdown_super()
  238. * and release aforementioned objects. Note: dentries and inodes _are_
  239. * taken care of and do not need specific handling.
  240. *
  241. * Upon calling this function, the filesystem may no longer alter or
  242. * rearrange the set of dentries belonging to this super_block, nor may it
  243. * change the attachments of dentries to inodes.
  244. */
  245. void generic_shutdown_super(struct super_block *sb)
  246. {
  247. const struct super_operations *sop = sb->s_op;
  248. if (sb->s_root) {
  249. shrink_dcache_for_umount(sb);
  250. sync_filesystem(sb);
  251. get_fs_excl();
  252. sb->s_flags &= ~MS_ACTIVE;
  253. fsnotify_unmount_inodes(&sb->s_inodes);
  254. evict_inodes(sb);
  255. if (sop->put_super)
  256. sop->put_super(sb);
  257. if (!list_empty(&sb->s_inodes)) {
  258. printk("VFS: Busy inodes after unmount of %s. "
  259. "Self-destruct in 5 seconds. Have a nice day...\n",
  260. sb->s_id);
  261. }
  262. put_fs_excl();
  263. }
  264. spin_lock(&sb_lock);
  265. /* should be initialized for __put_super_and_need_restart() */
  266. list_del_init(&sb->s_instances);
  267. spin_unlock(&sb_lock);
  268. up_write(&sb->s_umount);
  269. }
  270. EXPORT_SYMBOL(generic_shutdown_super);
  271. /**
  272. * sget - find or create a superblock
  273. * @type: filesystem type superblock should belong to
  274. * @test: comparison callback
  275. * @set: setup callback
  276. * @data: argument to each of them
  277. */
  278. struct super_block *sget(struct file_system_type *type,
  279. int (*test)(struct super_block *,void *),
  280. int (*set)(struct super_block *,void *),
  281. void *data)
  282. {
  283. struct super_block *s = NULL;
  284. struct super_block *old;
  285. int err;
  286. retry:
  287. spin_lock(&sb_lock);
  288. if (test) {
  289. list_for_each_entry(old, &type->fs_supers, s_instances) {
  290. if (!test(old, data))
  291. continue;
  292. if (!grab_super(old))
  293. goto retry;
  294. if (s) {
  295. up_write(&s->s_umount);
  296. destroy_super(s);
  297. s = NULL;
  298. }
  299. down_write(&old->s_umount);
  300. if (unlikely(!(old->s_flags & MS_BORN))) {
  301. deactivate_locked_super(old);
  302. goto retry;
  303. }
  304. return old;
  305. }
  306. }
  307. if (!s) {
  308. spin_unlock(&sb_lock);
  309. s = alloc_super(type);
  310. if (!s)
  311. return ERR_PTR(-ENOMEM);
  312. goto retry;
  313. }
  314. err = set(s, data);
  315. if (err) {
  316. spin_unlock(&sb_lock);
  317. up_write(&s->s_umount);
  318. destroy_super(s);
  319. return ERR_PTR(err);
  320. }
  321. s->s_type = type;
  322. strlcpy(s->s_id, type->name, sizeof(s->s_id));
  323. list_add_tail(&s->s_list, &super_blocks);
  324. list_add(&s->s_instances, &type->fs_supers);
  325. spin_unlock(&sb_lock);
  326. get_filesystem(type);
  327. return s;
  328. }
  329. EXPORT_SYMBOL(sget);
  330. void drop_super(struct super_block *sb)
  331. {
  332. up_read(&sb->s_umount);
  333. put_super(sb);
  334. }
  335. EXPORT_SYMBOL(drop_super);
  336. /**
  337. * sync_supers - helper for periodic superblock writeback
  338. *
  339. * Call the write_super method if present on all dirty superblocks in
  340. * the system. This is for the periodic writeback used by most older
  341. * filesystems. For data integrity superblock writeback use
  342. * sync_filesystems() instead.
  343. *
  344. * Note: check the dirty flag before waiting, so we don't
  345. * hold up the sync while mounting a device. (The newly
  346. * mounted device won't need syncing.)
  347. */
  348. void sync_supers(void)
  349. {
  350. struct super_block *sb, *p = NULL;
  351. spin_lock(&sb_lock);
  352. list_for_each_entry(sb, &super_blocks, s_list) {
  353. if (list_empty(&sb->s_instances))
  354. continue;
  355. if (sb->s_op->write_super && sb->s_dirt) {
  356. sb->s_count++;
  357. spin_unlock(&sb_lock);
  358. down_read(&sb->s_umount);
  359. if (sb->s_root && sb->s_dirt)
  360. sb->s_op->write_super(sb);
  361. up_read(&sb->s_umount);
  362. spin_lock(&sb_lock);
  363. if (p)
  364. __put_super(p);
  365. p = sb;
  366. }
  367. }
  368. if (p)
  369. __put_super(p);
  370. spin_unlock(&sb_lock);
  371. }
  372. /**
  373. * iterate_supers - call function for all active superblocks
  374. * @f: function to call
  375. * @arg: argument to pass to it
  376. *
  377. * Scans the superblock list and calls given function, passing it
  378. * locked superblock and given argument.
  379. */
  380. void iterate_supers(void (*f)(struct super_block *, void *), void *arg)
  381. {
  382. struct super_block *sb, *p = NULL;
  383. spin_lock(&sb_lock);
  384. list_for_each_entry(sb, &super_blocks, s_list) {
  385. if (list_empty(&sb->s_instances))
  386. continue;
  387. sb->s_count++;
  388. spin_unlock(&sb_lock);
  389. down_read(&sb->s_umount);
  390. if (sb->s_root)
  391. f(sb, arg);
  392. up_read(&sb->s_umount);
  393. spin_lock(&sb_lock);
  394. if (p)
  395. __put_super(p);
  396. p = sb;
  397. }
  398. if (p)
  399. __put_super(p);
  400. spin_unlock(&sb_lock);
  401. }
  402. /**
  403. * get_super - get the superblock of a device
  404. * @bdev: device to get the superblock for
  405. *
  406. * Scans the superblock list and finds the superblock of the file system
  407. * mounted on the device given. %NULL is returned if no match is found.
  408. */
  409. struct super_block *get_super(struct block_device *bdev)
  410. {
  411. struct super_block *sb;
  412. if (!bdev)
  413. return NULL;
  414. spin_lock(&sb_lock);
  415. rescan:
  416. list_for_each_entry(sb, &super_blocks, s_list) {
  417. if (list_empty(&sb->s_instances))
  418. continue;
  419. if (sb->s_bdev == bdev) {
  420. sb->s_count++;
  421. spin_unlock(&sb_lock);
  422. down_read(&sb->s_umount);
  423. /* still alive? */
  424. if (sb->s_root)
  425. return sb;
  426. up_read(&sb->s_umount);
  427. /* nope, got unmounted */
  428. spin_lock(&sb_lock);
  429. __put_super(sb);
  430. goto rescan;
  431. }
  432. }
  433. spin_unlock(&sb_lock);
  434. return NULL;
  435. }
  436. EXPORT_SYMBOL(get_super);
  437. /**
  438. * get_active_super - get an active reference to the superblock of a device
  439. * @bdev: device to get the superblock for
  440. *
  441. * Scans the superblock list and finds the superblock of the file system
  442. * mounted on the device given. Returns the superblock with an active
  443. * reference or %NULL if none was found.
  444. */
  445. struct super_block *get_active_super(struct block_device *bdev)
  446. {
  447. struct super_block *sb;
  448. if (!bdev)
  449. return NULL;
  450. restart:
  451. spin_lock(&sb_lock);
  452. list_for_each_entry(sb, &super_blocks, s_list) {
  453. if (list_empty(&sb->s_instances))
  454. continue;
  455. if (sb->s_bdev == bdev) {
  456. if (grab_super(sb)) /* drops sb_lock */
  457. return sb;
  458. else
  459. goto restart;
  460. }
  461. }
  462. spin_unlock(&sb_lock);
  463. return NULL;
  464. }
  465. struct super_block *user_get_super(dev_t dev)
  466. {
  467. struct super_block *sb;
  468. spin_lock(&sb_lock);
  469. rescan:
  470. list_for_each_entry(sb, &super_blocks, s_list) {
  471. if (list_empty(&sb->s_instances))
  472. continue;
  473. if (sb->s_dev == dev) {
  474. sb->s_count++;
  475. spin_unlock(&sb_lock);
  476. down_read(&sb->s_umount);
  477. /* still alive? */
  478. if (sb->s_root)
  479. return sb;
  480. up_read(&sb->s_umount);
  481. /* nope, got unmounted */
  482. spin_lock(&sb_lock);
  483. __put_super(sb);
  484. goto rescan;
  485. }
  486. }
  487. spin_unlock(&sb_lock);
  488. return NULL;
  489. }
  490. /**
  491. * do_remount_sb - asks filesystem to change mount options.
  492. * @sb: superblock in question
  493. * @flags: numeric part of options
  494. * @data: the rest of options
  495. * @force: whether or not to force the change
  496. *
  497. * Alters the mount options of a mounted file system.
  498. */
  499. int do_remount_sb(struct super_block *sb, int flags, void *data, int force)
  500. {
  501. int retval;
  502. int remount_ro;
  503. if (sb->s_frozen != SB_UNFROZEN)
  504. return -EBUSY;
  505. #ifdef CONFIG_BLOCK
  506. if (!(flags & MS_RDONLY) && bdev_read_only(sb->s_bdev))
  507. return -EACCES;
  508. #endif
  509. if (flags & MS_RDONLY)
  510. acct_auto_close(sb);
  511. shrink_dcache_sb(sb);
  512. sync_filesystem(sb);
  513. remount_ro = (flags & MS_RDONLY) && !(sb->s_flags & MS_RDONLY);
  514. /* If we are remounting RDONLY and current sb is read/write,
  515. make sure there are no rw files opened */
  516. if (remount_ro) {
  517. if (force)
  518. mark_files_ro(sb);
  519. else if (!fs_may_remount_ro(sb))
  520. return -EBUSY;
  521. }
  522. if (sb->s_op->remount_fs) {
  523. retval = sb->s_op->remount_fs(sb, &flags, data);
  524. if (retval)
  525. return retval;
  526. }
  527. sb->s_flags = (sb->s_flags & ~MS_RMT_MASK) | (flags & MS_RMT_MASK);
  528. /*
  529. * Some filesystems modify their metadata via some other path than the
  530. * bdev buffer cache (eg. use a private mapping, or directories in
  531. * pagecache, etc). Also file data modifications go via their own
  532. * mappings. So If we try to mount readonly then copy the filesystem
  533. * from bdev, we could get stale data, so invalidate it to give a best
  534. * effort at coherency.
  535. */
  536. if (remount_ro && sb->s_bdev)
  537. invalidate_bdev(sb->s_bdev);
  538. return 0;
  539. }
  540. static void do_emergency_remount(struct work_struct *work)
  541. {
  542. struct super_block *sb, *p = NULL;
  543. spin_lock(&sb_lock);
  544. list_for_each_entry(sb, &super_blocks, s_list) {
  545. if (list_empty(&sb->s_instances))
  546. continue;
  547. sb->s_count++;
  548. spin_unlock(&sb_lock);
  549. down_write(&sb->s_umount);
  550. if (sb->s_root && sb->s_bdev && !(sb->s_flags & MS_RDONLY)) {
  551. /*
  552. * What lock protects sb->s_flags??
  553. */
  554. do_remount_sb(sb, MS_RDONLY, NULL, 1);
  555. }
  556. up_write(&sb->s_umount);
  557. spin_lock(&sb_lock);
  558. if (p)
  559. __put_super(p);
  560. p = sb;
  561. }
  562. if (p)
  563. __put_super(p);
  564. spin_unlock(&sb_lock);
  565. kfree(work);
  566. printk("Emergency Remount complete\n");
  567. }
  568. void emergency_remount(void)
  569. {
  570. struct work_struct *work;
  571. work = kmalloc(sizeof(*work), GFP_ATOMIC);
  572. if (work) {
  573. INIT_WORK(work, do_emergency_remount);
  574. schedule_work(work);
  575. }
  576. }
  577. /*
  578. * Unnamed block devices are dummy devices used by virtual
  579. * filesystems which don't use real block-devices. -- jrs
  580. */
  581. static DEFINE_IDA(unnamed_dev_ida);
  582. static DEFINE_SPINLOCK(unnamed_dev_lock);/* protects the above */
  583. static int unnamed_dev_start = 0; /* don't bother trying below it */
  584. int set_anon_super(struct super_block *s, void *data)
  585. {
  586. int dev;
  587. int error;
  588. retry:
  589. if (ida_pre_get(&unnamed_dev_ida, GFP_ATOMIC) == 0)
  590. return -ENOMEM;
  591. spin_lock(&unnamed_dev_lock);
  592. error = ida_get_new_above(&unnamed_dev_ida, unnamed_dev_start, &dev);
  593. if (!error)
  594. unnamed_dev_start = dev + 1;
  595. spin_unlock(&unnamed_dev_lock);
  596. if (error == -EAGAIN)
  597. /* We raced and lost with another CPU. */
  598. goto retry;
  599. else if (error)
  600. return -EAGAIN;
  601. if ((dev & MAX_ID_MASK) == (1 << MINORBITS)) {
  602. spin_lock(&unnamed_dev_lock);
  603. ida_remove(&unnamed_dev_ida, dev);
  604. if (unnamed_dev_start > dev)
  605. unnamed_dev_start = dev;
  606. spin_unlock(&unnamed_dev_lock);
  607. return -EMFILE;
  608. }
  609. s->s_dev = MKDEV(0, dev & MINORMASK);
  610. s->s_bdi = &noop_backing_dev_info;
  611. return 0;
  612. }
  613. EXPORT_SYMBOL(set_anon_super);
  614. void kill_anon_super(struct super_block *sb)
  615. {
  616. int slot = MINOR(sb->s_dev);
  617. generic_shutdown_super(sb);
  618. spin_lock(&unnamed_dev_lock);
  619. ida_remove(&unnamed_dev_ida, slot);
  620. if (slot < unnamed_dev_start)
  621. unnamed_dev_start = slot;
  622. spin_unlock(&unnamed_dev_lock);
  623. }
  624. EXPORT_SYMBOL(kill_anon_super);
  625. void kill_litter_super(struct super_block *sb)
  626. {
  627. if (sb->s_root)
  628. d_genocide(sb->s_root);
  629. kill_anon_super(sb);
  630. }
  631. EXPORT_SYMBOL(kill_litter_super);
  632. static int ns_test_super(struct super_block *sb, void *data)
  633. {
  634. return sb->s_fs_info == data;
  635. }
  636. static int ns_set_super(struct super_block *sb, void *data)
  637. {
  638. sb->s_fs_info = data;
  639. return set_anon_super(sb, NULL);
  640. }
  641. struct dentry *mount_ns(struct file_system_type *fs_type, int flags,
  642. void *data, int (*fill_super)(struct super_block *, void *, int))
  643. {
  644. struct super_block *sb;
  645. sb = sget(fs_type, ns_test_super, ns_set_super, data);
  646. if (IS_ERR(sb))
  647. return ERR_CAST(sb);
  648. if (!sb->s_root) {
  649. int err;
  650. sb->s_flags = flags;
  651. err = fill_super(sb, data, flags & MS_SILENT ? 1 : 0);
  652. if (err) {
  653. deactivate_locked_super(sb);
  654. return ERR_PTR(err);
  655. }
  656. sb->s_flags |= MS_ACTIVE;
  657. }
  658. return dget(sb->s_root);
  659. }
  660. EXPORT_SYMBOL(mount_ns);
  661. #ifdef CONFIG_BLOCK
  662. static int set_bdev_super(struct super_block *s, void *data)
  663. {
  664. s->s_bdev = data;
  665. s->s_dev = s->s_bdev->bd_dev;
  666. /*
  667. * We set the bdi here to the queue backing, file systems can
  668. * overwrite this in ->fill_super()
  669. */
  670. s->s_bdi = &bdev_get_queue(s->s_bdev)->backing_dev_info;
  671. return 0;
  672. }
  673. static int test_bdev_super(struct super_block *s, void *data)
  674. {
  675. return (void *)s->s_bdev == data;
  676. }
  677. struct dentry *mount_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. fmode_t mode = FMODE_READ;
  684. int error = 0;
  685. if (!(flags & MS_RDONLY))
  686. mode |= FMODE_WRITE;
  687. bdev = open_bdev_exclusive(dev_name, mode, fs_type);
  688. if (IS_ERR(bdev))
  689. return ERR_CAST(bdev);
  690. /*
  691. * once the super is inserted into the list by sget, s_umount
  692. * will protect the lockfs code from trying to start a snapshot
  693. * while we are mounting
  694. */
  695. mutex_lock(&bdev->bd_fsfreeze_mutex);
  696. if (bdev->bd_fsfreeze_count > 0) {
  697. mutex_unlock(&bdev->bd_fsfreeze_mutex);
  698. error = -EBUSY;
  699. goto error_bdev;
  700. }
  701. s = sget(fs_type, test_bdev_super, set_bdev_super, bdev);
  702. mutex_unlock(&bdev->bd_fsfreeze_mutex);
  703. if (IS_ERR(s))
  704. goto error_s;
  705. if (s->s_root) {
  706. if ((flags ^ s->s_flags) & MS_RDONLY) {
  707. deactivate_locked_super(s);
  708. error = -EBUSY;
  709. goto error_bdev;
  710. }
  711. /*
  712. * s_umount nests inside bd_mutex during
  713. * __invalidate_device(). close_bdev_exclusive()
  714. * acquires bd_mutex and can't be called under
  715. * s_umount. Drop s_umount temporarily. This is safe
  716. * as we're holding an active reference.
  717. */
  718. up_write(&s->s_umount);
  719. close_bdev_exclusive(bdev, mode);
  720. down_write(&s->s_umount);
  721. } else {
  722. char b[BDEVNAME_SIZE];
  723. s->s_flags = flags;
  724. s->s_mode = mode;
  725. strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id));
  726. sb_set_blocksize(s, block_size(bdev));
  727. error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
  728. if (error) {
  729. deactivate_locked_super(s);
  730. goto error;
  731. }
  732. s->s_flags |= MS_ACTIVE;
  733. bdev->bd_super = s;
  734. }
  735. return dget(s->s_root);
  736. error_s:
  737. error = PTR_ERR(s);
  738. error_bdev:
  739. close_bdev_exclusive(bdev, mode);
  740. error:
  741. return ERR_PTR(error);
  742. }
  743. EXPORT_SYMBOL(mount_bdev);
  744. int get_sb_bdev(struct file_system_type *fs_type,
  745. int flags, const char *dev_name, void *data,
  746. int (*fill_super)(struct super_block *, void *, int),
  747. struct vfsmount *mnt)
  748. {
  749. struct dentry *root;
  750. root = mount_bdev(fs_type, flags, dev_name, data, fill_super);
  751. if (IS_ERR(root))
  752. return PTR_ERR(root);
  753. mnt->mnt_root = root;
  754. mnt->mnt_sb = root->d_sb;
  755. return 0;
  756. }
  757. EXPORT_SYMBOL(get_sb_bdev);
  758. void kill_block_super(struct super_block *sb)
  759. {
  760. struct block_device *bdev = sb->s_bdev;
  761. fmode_t mode = sb->s_mode;
  762. bdev->bd_super = NULL;
  763. generic_shutdown_super(sb);
  764. sync_blockdev(bdev);
  765. close_bdev_exclusive(bdev, mode);
  766. }
  767. EXPORT_SYMBOL(kill_block_super);
  768. #endif
  769. struct dentry *mount_nodev(struct file_system_type *fs_type,
  770. int flags, void *data,
  771. int (*fill_super)(struct super_block *, void *, int))
  772. {
  773. int error;
  774. struct super_block *s = sget(fs_type, NULL, set_anon_super, NULL);
  775. if (IS_ERR(s))
  776. return ERR_CAST(s);
  777. s->s_flags = flags;
  778. error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
  779. if (error) {
  780. deactivate_locked_super(s);
  781. return ERR_PTR(error);
  782. }
  783. s->s_flags |= MS_ACTIVE;
  784. return dget(s->s_root);
  785. }
  786. EXPORT_SYMBOL(mount_nodev);
  787. int get_sb_nodev(struct file_system_type *fs_type,
  788. int flags, void *data,
  789. int (*fill_super)(struct super_block *, void *, int),
  790. struct vfsmount *mnt)
  791. {
  792. struct dentry *root;
  793. root = mount_nodev(fs_type, flags, data, fill_super);
  794. if (IS_ERR(root))
  795. return PTR_ERR(root);
  796. mnt->mnt_root = root;
  797. mnt->mnt_sb = root->d_sb;
  798. return 0;
  799. }
  800. EXPORT_SYMBOL(get_sb_nodev);
  801. static int compare_single(struct super_block *s, void *p)
  802. {
  803. return 1;
  804. }
  805. struct dentry *mount_single(struct file_system_type *fs_type,
  806. int flags, void *data,
  807. int (*fill_super)(struct super_block *, void *, int))
  808. {
  809. struct super_block *s;
  810. int error;
  811. s = sget(fs_type, compare_single, set_anon_super, NULL);
  812. if (IS_ERR(s))
  813. return ERR_CAST(s);
  814. if (!s->s_root) {
  815. s->s_flags = flags;
  816. error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
  817. if (error) {
  818. deactivate_locked_super(s);
  819. return ERR_PTR(error);
  820. }
  821. s->s_flags |= MS_ACTIVE;
  822. } else {
  823. do_remount_sb(s, flags, data, 0);
  824. }
  825. return dget(s->s_root);
  826. }
  827. EXPORT_SYMBOL(mount_single);
  828. int get_sb_single(struct file_system_type *fs_type,
  829. int flags, void *data,
  830. int (*fill_super)(struct super_block *, void *, int),
  831. struct vfsmount *mnt)
  832. {
  833. struct dentry *root;
  834. root = mount_single(fs_type, flags, data, fill_super);
  835. if (IS_ERR(root))
  836. return PTR_ERR(root);
  837. mnt->mnt_root = root;
  838. mnt->mnt_sb = root->d_sb;
  839. return 0;
  840. }
  841. EXPORT_SYMBOL(get_sb_single);
  842. struct vfsmount *
  843. vfs_kern_mount(struct file_system_type *type, int flags, const char *name, void *data)
  844. {
  845. struct vfsmount *mnt;
  846. struct dentry *root;
  847. char *secdata = NULL;
  848. int error;
  849. if (!type)
  850. return ERR_PTR(-ENODEV);
  851. error = -ENOMEM;
  852. mnt = alloc_vfsmnt(name);
  853. if (!mnt)
  854. goto out;
  855. if (flags & MS_KERNMOUNT)
  856. mnt->mnt_flags = MNT_INTERNAL;
  857. if (data && !(type->fs_flags & FS_BINARY_MOUNTDATA)) {
  858. secdata = alloc_secdata();
  859. if (!secdata)
  860. goto out_mnt;
  861. error = security_sb_copy_data(data, secdata);
  862. if (error)
  863. goto out_free_secdata;
  864. }
  865. if (type->mount) {
  866. root = type->mount(type, flags, name, data);
  867. if (IS_ERR(root)) {
  868. error = PTR_ERR(root);
  869. goto out_free_secdata;
  870. }
  871. mnt->mnt_root = root;
  872. mnt->mnt_sb = root->d_sb;
  873. } else {
  874. error = type->get_sb(type, flags, name, data, mnt);
  875. if (error < 0)
  876. goto out_free_secdata;
  877. }
  878. BUG_ON(!mnt->mnt_sb);
  879. WARN_ON(!mnt->mnt_sb->s_bdi);
  880. mnt->mnt_sb->s_flags |= MS_BORN;
  881. error = security_sb_kern_mount(mnt->mnt_sb, flags, secdata);
  882. if (error)
  883. goto out_sb;
  884. /*
  885. * filesystems should never set s_maxbytes larger than MAX_LFS_FILESIZE
  886. * but s_maxbytes was an unsigned long long for many releases. Throw
  887. * this warning for a little while to try and catch filesystems that
  888. * violate this rule. This warning should be either removed or
  889. * converted to a BUG() in 2.6.34.
  890. */
  891. WARN((mnt->mnt_sb->s_maxbytes < 0), "%s set sb->s_maxbytes to "
  892. "negative value (%lld)\n", type->name, mnt->mnt_sb->s_maxbytes);
  893. mnt->mnt_mountpoint = mnt->mnt_root;
  894. mnt->mnt_parent = mnt;
  895. up_write(&mnt->mnt_sb->s_umount);
  896. free_secdata(secdata);
  897. return mnt;
  898. out_sb:
  899. dput(mnt->mnt_root);
  900. deactivate_locked_super(mnt->mnt_sb);
  901. out_free_secdata:
  902. free_secdata(secdata);
  903. out_mnt:
  904. free_vfsmnt(mnt);
  905. out:
  906. return ERR_PTR(error);
  907. }
  908. EXPORT_SYMBOL_GPL(vfs_kern_mount);
  909. /**
  910. * freeze_super - lock the filesystem and force it into a consistent state
  911. * @sb: the super to lock
  912. *
  913. * Syncs the super to make sure the filesystem is consistent and calls the fs's
  914. * freeze_fs. Subsequent calls to this without first thawing the fs will return
  915. * -EBUSY.
  916. */
  917. int freeze_super(struct super_block *sb)
  918. {
  919. int ret;
  920. atomic_inc(&sb->s_active);
  921. down_write(&sb->s_umount);
  922. if (sb->s_frozen) {
  923. deactivate_locked_super(sb);
  924. return -EBUSY;
  925. }
  926. if (sb->s_flags & MS_RDONLY) {
  927. sb->s_frozen = SB_FREEZE_TRANS;
  928. smp_wmb();
  929. up_write(&sb->s_umount);
  930. return 0;
  931. }
  932. sb->s_frozen = SB_FREEZE_WRITE;
  933. smp_wmb();
  934. sync_filesystem(sb);
  935. sb->s_frozen = SB_FREEZE_TRANS;
  936. smp_wmb();
  937. sync_blockdev(sb->s_bdev);
  938. if (sb->s_op->freeze_fs) {
  939. ret = sb->s_op->freeze_fs(sb);
  940. if (ret) {
  941. printk(KERN_ERR
  942. "VFS:Filesystem freeze failed\n");
  943. sb->s_frozen = SB_UNFROZEN;
  944. deactivate_locked_super(sb);
  945. return ret;
  946. }
  947. }
  948. up_write(&sb->s_umount);
  949. return 0;
  950. }
  951. EXPORT_SYMBOL(freeze_super);
  952. /**
  953. * thaw_super -- unlock filesystem
  954. * @sb: the super to thaw
  955. *
  956. * Unlocks the filesystem and marks it writeable again after freeze_super().
  957. */
  958. int thaw_super(struct super_block *sb)
  959. {
  960. int error;
  961. down_write(&sb->s_umount);
  962. if (sb->s_frozen == SB_UNFROZEN) {
  963. up_write(&sb->s_umount);
  964. return -EINVAL;
  965. }
  966. if (sb->s_flags & MS_RDONLY)
  967. goto out;
  968. if (sb->s_op->unfreeze_fs) {
  969. error = sb->s_op->unfreeze_fs(sb);
  970. if (error) {
  971. printk(KERN_ERR
  972. "VFS:Filesystem thaw failed\n");
  973. sb->s_frozen = SB_FREEZE_TRANS;
  974. up_write(&sb->s_umount);
  975. return error;
  976. }
  977. }
  978. out:
  979. sb->s_frozen = SB_UNFROZEN;
  980. smp_wmb();
  981. wake_up(&sb->s_wait_unfrozen);
  982. deactivate_locked_super(sb);
  983. return 0;
  984. }
  985. EXPORT_SYMBOL(thaw_super);
  986. static struct vfsmount *fs_set_subtype(struct vfsmount *mnt, const char *fstype)
  987. {
  988. int err;
  989. const char *subtype = strchr(fstype, '.');
  990. if (subtype) {
  991. subtype++;
  992. err = -EINVAL;
  993. if (!subtype[0])
  994. goto err;
  995. } else
  996. subtype = "";
  997. mnt->mnt_sb->s_subtype = kstrdup(subtype, GFP_KERNEL);
  998. err = -ENOMEM;
  999. if (!mnt->mnt_sb->s_subtype)
  1000. goto err;
  1001. return mnt;
  1002. err:
  1003. mntput(mnt);
  1004. return ERR_PTR(err);
  1005. }
  1006. struct vfsmount *
  1007. do_kern_mount(const char *fstype, int flags, const char *name, void *data)
  1008. {
  1009. struct file_system_type *type = get_fs_type(fstype);
  1010. struct vfsmount *mnt;
  1011. if (!type)
  1012. return ERR_PTR(-ENODEV);
  1013. mnt = vfs_kern_mount(type, flags, name, data);
  1014. if (!IS_ERR(mnt) && (type->fs_flags & FS_HAS_SUBTYPE) &&
  1015. !mnt->mnt_sb->s_subtype)
  1016. mnt = fs_set_subtype(mnt, fstype);
  1017. put_filesystem(type);
  1018. return mnt;
  1019. }
  1020. EXPORT_SYMBOL_GPL(do_kern_mount);
  1021. struct vfsmount *kern_mount_data(struct file_system_type *type, void *data)
  1022. {
  1023. return vfs_kern_mount(type, MS_KERNMOUNT, type->name, data);
  1024. }
  1025. EXPORT_SYMBOL_GPL(kern_mount_data);