super.c 25 KB

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