super.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100
  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, *p = NULL;
  335. spin_lock(&sb_lock);
  336. list_for_each_entry(sb, &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. if (p)
  348. __put_super(p);
  349. p = sb;
  350. }
  351. }
  352. if (p)
  353. __put_super(p);
  354. spin_unlock(&sb_lock);
  355. }
  356. /**
  357. * iterate_supers - call function for all active superblocks
  358. * @f: function to call
  359. * @arg: argument to pass to it
  360. *
  361. * Scans the superblock list and calls given function, passing it
  362. * locked superblock and given argument.
  363. */
  364. void iterate_supers(void (*f)(struct super_block *, void *), void *arg)
  365. {
  366. struct super_block *sb, *p = NULL;
  367. spin_lock(&sb_lock);
  368. list_for_each_entry(sb, &super_blocks, s_list) {
  369. if (list_empty(&sb->s_instances))
  370. continue;
  371. sb->s_count++;
  372. spin_unlock(&sb_lock);
  373. down_read(&sb->s_umount);
  374. if (sb->s_root)
  375. f(sb, arg);
  376. up_read(&sb->s_umount);
  377. spin_lock(&sb_lock);
  378. if (p)
  379. __put_super(p);
  380. p = sb;
  381. }
  382. if (p)
  383. __put_super(p);
  384. spin_unlock(&sb_lock);
  385. }
  386. /**
  387. * get_super - get the superblock of a device
  388. * @bdev: device to get the superblock for
  389. *
  390. * Scans the superblock list and finds the superblock of the file system
  391. * mounted on the device given. %NULL is returned if no match is found.
  392. */
  393. struct super_block *get_super(struct block_device *bdev)
  394. {
  395. struct super_block *sb;
  396. if (!bdev)
  397. return NULL;
  398. spin_lock(&sb_lock);
  399. rescan:
  400. list_for_each_entry(sb, &super_blocks, s_list) {
  401. if (list_empty(&sb->s_instances))
  402. continue;
  403. if (sb->s_bdev == bdev) {
  404. sb->s_count++;
  405. spin_unlock(&sb_lock);
  406. down_read(&sb->s_umount);
  407. /* still alive? */
  408. if (sb->s_root)
  409. return sb;
  410. up_read(&sb->s_umount);
  411. /* nope, got unmounted */
  412. spin_lock(&sb_lock);
  413. __put_super(sb);
  414. goto rescan;
  415. }
  416. }
  417. spin_unlock(&sb_lock);
  418. return NULL;
  419. }
  420. EXPORT_SYMBOL(get_super);
  421. /**
  422. * get_active_super - get an active reference to the superblock of a device
  423. * @bdev: device to get the superblock for
  424. *
  425. * Scans the superblock list and finds the superblock of the file system
  426. * mounted on the device given. Returns the superblock with an active
  427. * reference or %NULL if none was found.
  428. */
  429. struct super_block *get_active_super(struct block_device *bdev)
  430. {
  431. struct super_block *sb;
  432. if (!bdev)
  433. return NULL;
  434. restart:
  435. spin_lock(&sb_lock);
  436. list_for_each_entry(sb, &super_blocks, s_list) {
  437. if (list_empty(&sb->s_instances))
  438. continue;
  439. if (sb->s_bdev == bdev) {
  440. if (grab_super(sb)) /* drops sb_lock */
  441. return sb;
  442. else
  443. goto restart;
  444. }
  445. }
  446. spin_unlock(&sb_lock);
  447. return NULL;
  448. }
  449. struct super_block *user_get_super(dev_t dev)
  450. {
  451. struct super_block *sb;
  452. spin_lock(&sb_lock);
  453. rescan:
  454. list_for_each_entry(sb, &super_blocks, s_list) {
  455. if (list_empty(&sb->s_instances))
  456. continue;
  457. if (sb->s_dev == dev) {
  458. sb->s_count++;
  459. spin_unlock(&sb_lock);
  460. down_read(&sb->s_umount);
  461. /* still alive? */
  462. if (sb->s_root)
  463. return sb;
  464. up_read(&sb->s_umount);
  465. /* nope, got unmounted */
  466. spin_lock(&sb_lock);
  467. __put_super(sb);
  468. goto rescan;
  469. }
  470. }
  471. spin_unlock(&sb_lock);
  472. return NULL;
  473. }
  474. /**
  475. * do_remount_sb - asks filesystem to change mount options.
  476. * @sb: superblock in question
  477. * @flags: numeric part of options
  478. * @data: the rest of options
  479. * @force: whether or not to force the change
  480. *
  481. * Alters the mount options of a mounted file system.
  482. */
  483. int do_remount_sb(struct super_block *sb, int flags, void *data, int force)
  484. {
  485. int retval;
  486. int remount_ro;
  487. if (sb->s_frozen != SB_UNFROZEN)
  488. return -EBUSY;
  489. #ifdef CONFIG_BLOCK
  490. if (!(flags & MS_RDONLY) && bdev_read_only(sb->s_bdev))
  491. return -EACCES;
  492. #endif
  493. if (flags & MS_RDONLY)
  494. acct_auto_close(sb);
  495. shrink_dcache_sb(sb);
  496. sync_filesystem(sb);
  497. remount_ro = (flags & MS_RDONLY) && !(sb->s_flags & MS_RDONLY);
  498. /* If we are remounting RDONLY and current sb is read/write,
  499. make sure there are no rw files opened */
  500. if (remount_ro) {
  501. if (force)
  502. mark_files_ro(sb);
  503. else if (!fs_may_remount_ro(sb))
  504. return -EBUSY;
  505. }
  506. if (sb->s_op->remount_fs) {
  507. retval = sb->s_op->remount_fs(sb, &flags, data);
  508. if (retval)
  509. return retval;
  510. }
  511. sb->s_flags = (sb->s_flags & ~MS_RMT_MASK) | (flags & MS_RMT_MASK);
  512. /*
  513. * Some filesystems modify their metadata via some other path than the
  514. * bdev buffer cache (eg. use a private mapping, or directories in
  515. * pagecache, etc). Also file data modifications go via their own
  516. * mappings. So If we try to mount readonly then copy the filesystem
  517. * from bdev, we could get stale data, so invalidate it to give a best
  518. * effort at coherency.
  519. */
  520. if (remount_ro && sb->s_bdev)
  521. invalidate_bdev(sb->s_bdev);
  522. return 0;
  523. }
  524. static void do_emergency_remount(struct work_struct *work)
  525. {
  526. struct super_block *sb, *p = NULL;
  527. spin_lock(&sb_lock);
  528. list_for_each_entry(sb, &super_blocks, s_list) {
  529. if (list_empty(&sb->s_instances))
  530. continue;
  531. sb->s_count++;
  532. spin_unlock(&sb_lock);
  533. down_write(&sb->s_umount);
  534. if (sb->s_root && sb->s_bdev && !(sb->s_flags & MS_RDONLY)) {
  535. /*
  536. * What lock protects sb->s_flags??
  537. */
  538. do_remount_sb(sb, MS_RDONLY, NULL, 1);
  539. }
  540. up_write(&sb->s_umount);
  541. spin_lock(&sb_lock);
  542. if (p)
  543. __put_super(p);
  544. p = sb;
  545. }
  546. if (p)
  547. __put_super(p);
  548. spin_unlock(&sb_lock);
  549. kfree(work);
  550. printk("Emergency Remount complete\n");
  551. }
  552. void emergency_remount(void)
  553. {
  554. struct work_struct *work;
  555. work = kmalloc(sizeof(*work), GFP_ATOMIC);
  556. if (work) {
  557. INIT_WORK(work, do_emergency_remount);
  558. schedule_work(work);
  559. }
  560. }
  561. /*
  562. * Unnamed block devices are dummy devices used by virtual
  563. * filesystems which don't use real block-devices. -- jrs
  564. */
  565. static DEFINE_IDA(unnamed_dev_ida);
  566. static DEFINE_SPINLOCK(unnamed_dev_lock);/* protects the above */
  567. static int unnamed_dev_start = 0; /* don't bother trying below it */
  568. int set_anon_super(struct super_block *s, void *data)
  569. {
  570. int dev;
  571. int error;
  572. retry:
  573. if (ida_pre_get(&unnamed_dev_ida, GFP_ATOMIC) == 0)
  574. return -ENOMEM;
  575. spin_lock(&unnamed_dev_lock);
  576. error = ida_get_new_above(&unnamed_dev_ida, unnamed_dev_start, &dev);
  577. if (!error)
  578. unnamed_dev_start = dev + 1;
  579. spin_unlock(&unnamed_dev_lock);
  580. if (error == -EAGAIN)
  581. /* We raced and lost with another CPU. */
  582. goto retry;
  583. else if (error)
  584. return -EAGAIN;
  585. if ((dev & MAX_ID_MASK) == (1 << MINORBITS)) {
  586. spin_lock(&unnamed_dev_lock);
  587. ida_remove(&unnamed_dev_ida, dev);
  588. if (unnamed_dev_start > dev)
  589. unnamed_dev_start = dev;
  590. spin_unlock(&unnamed_dev_lock);
  591. return -EMFILE;
  592. }
  593. s->s_dev = MKDEV(0, dev & MINORMASK);
  594. s->s_bdi = &noop_backing_dev_info;
  595. return 0;
  596. }
  597. EXPORT_SYMBOL(set_anon_super);
  598. void kill_anon_super(struct super_block *sb)
  599. {
  600. int slot = MINOR(sb->s_dev);
  601. generic_shutdown_super(sb);
  602. spin_lock(&unnamed_dev_lock);
  603. ida_remove(&unnamed_dev_ida, slot);
  604. if (slot < unnamed_dev_start)
  605. unnamed_dev_start = slot;
  606. spin_unlock(&unnamed_dev_lock);
  607. }
  608. EXPORT_SYMBOL(kill_anon_super);
  609. void kill_litter_super(struct super_block *sb)
  610. {
  611. if (sb->s_root)
  612. d_genocide(sb->s_root);
  613. kill_anon_super(sb);
  614. }
  615. EXPORT_SYMBOL(kill_litter_super);
  616. static int ns_test_super(struct super_block *sb, void *data)
  617. {
  618. return sb->s_fs_info == data;
  619. }
  620. static int ns_set_super(struct super_block *sb, void *data)
  621. {
  622. sb->s_fs_info = data;
  623. return set_anon_super(sb, NULL);
  624. }
  625. int get_sb_ns(struct file_system_type *fs_type, int flags, void *data,
  626. int (*fill_super)(struct super_block *, void *, int),
  627. struct vfsmount *mnt)
  628. {
  629. struct super_block *sb;
  630. sb = sget(fs_type, ns_test_super, ns_set_super, data);
  631. if (IS_ERR(sb))
  632. return PTR_ERR(sb);
  633. if (!sb->s_root) {
  634. int err;
  635. sb->s_flags = flags;
  636. err = fill_super(sb, data, flags & MS_SILENT ? 1 : 0);
  637. if (err) {
  638. deactivate_locked_super(sb);
  639. return err;
  640. }
  641. sb->s_flags |= MS_ACTIVE;
  642. }
  643. simple_set_mnt(mnt, sb);
  644. return 0;
  645. }
  646. EXPORT_SYMBOL(get_sb_ns);
  647. #ifdef CONFIG_BLOCK
  648. static int set_bdev_super(struct super_block *s, void *data)
  649. {
  650. s->s_bdev = data;
  651. s->s_dev = s->s_bdev->bd_dev;
  652. /*
  653. * We set the bdi here to the queue backing, file systems can
  654. * overwrite this in ->fill_super()
  655. */
  656. s->s_bdi = &bdev_get_queue(s->s_bdev)->backing_dev_info;
  657. return 0;
  658. }
  659. static int test_bdev_super(struct super_block *s, void *data)
  660. {
  661. return (void *)s->s_bdev == data;
  662. }
  663. int get_sb_bdev(struct file_system_type *fs_type,
  664. int flags, const char *dev_name, void *data,
  665. int (*fill_super)(struct super_block *, void *, int),
  666. struct vfsmount *mnt)
  667. {
  668. struct block_device *bdev;
  669. struct super_block *s;
  670. fmode_t mode = FMODE_READ;
  671. int error = 0;
  672. if (!(flags & MS_RDONLY))
  673. mode |= FMODE_WRITE;
  674. bdev = open_bdev_exclusive(dev_name, mode, fs_type);
  675. if (IS_ERR(bdev))
  676. return PTR_ERR(bdev);
  677. /*
  678. * once the super is inserted into the list by sget, s_umount
  679. * will protect the lockfs code from trying to start a snapshot
  680. * while we are mounting
  681. */
  682. mutex_lock(&bdev->bd_fsfreeze_mutex);
  683. if (bdev->bd_fsfreeze_count > 0) {
  684. mutex_unlock(&bdev->bd_fsfreeze_mutex);
  685. error = -EBUSY;
  686. goto error_bdev;
  687. }
  688. s = sget(fs_type, test_bdev_super, set_bdev_super, bdev);
  689. mutex_unlock(&bdev->bd_fsfreeze_mutex);
  690. if (IS_ERR(s))
  691. goto error_s;
  692. if (s->s_root) {
  693. if ((flags ^ s->s_flags) & MS_RDONLY) {
  694. deactivate_locked_super(s);
  695. error = -EBUSY;
  696. goto error_bdev;
  697. }
  698. /*
  699. * s_umount nests inside bd_mutex during
  700. * __invalidate_device(). close_bdev_exclusive()
  701. * acquires bd_mutex and can't be called under
  702. * s_umount. Drop s_umount temporarily. This is safe
  703. * as we're holding an active reference.
  704. */
  705. up_write(&s->s_umount);
  706. close_bdev_exclusive(bdev, mode);
  707. down_write(&s->s_umount);
  708. } else {
  709. char b[BDEVNAME_SIZE];
  710. s->s_flags = flags;
  711. s->s_mode = mode;
  712. strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id));
  713. sb_set_blocksize(s, block_size(bdev));
  714. error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
  715. if (error) {
  716. deactivate_locked_super(s);
  717. goto error;
  718. }
  719. s->s_flags |= MS_ACTIVE;
  720. bdev->bd_super = s;
  721. }
  722. simple_set_mnt(mnt, s);
  723. return 0;
  724. error_s:
  725. error = PTR_ERR(s);
  726. error_bdev:
  727. close_bdev_exclusive(bdev, mode);
  728. error:
  729. return error;
  730. }
  731. EXPORT_SYMBOL(get_sb_bdev);
  732. void kill_block_super(struct super_block *sb)
  733. {
  734. struct block_device *bdev = sb->s_bdev;
  735. fmode_t mode = sb->s_mode;
  736. bdev->bd_super = NULL;
  737. generic_shutdown_super(sb);
  738. sync_blockdev(bdev);
  739. close_bdev_exclusive(bdev, mode);
  740. }
  741. EXPORT_SYMBOL(kill_block_super);
  742. #endif
  743. int get_sb_nodev(struct file_system_type *fs_type,
  744. int flags, void *data,
  745. int (*fill_super)(struct super_block *, void *, int),
  746. struct vfsmount *mnt)
  747. {
  748. int error;
  749. struct super_block *s = sget(fs_type, NULL, set_anon_super, NULL);
  750. if (IS_ERR(s))
  751. return PTR_ERR(s);
  752. s->s_flags = flags;
  753. error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
  754. if (error) {
  755. deactivate_locked_super(s);
  756. return error;
  757. }
  758. s->s_flags |= MS_ACTIVE;
  759. simple_set_mnt(mnt, s);
  760. return 0;
  761. }
  762. EXPORT_SYMBOL(get_sb_nodev);
  763. static int compare_single(struct super_block *s, void *p)
  764. {
  765. return 1;
  766. }
  767. int get_sb_single(struct file_system_type *fs_type,
  768. int flags, void *data,
  769. int (*fill_super)(struct super_block *, void *, int),
  770. struct vfsmount *mnt)
  771. {
  772. struct super_block *s;
  773. int error;
  774. s = sget(fs_type, compare_single, set_anon_super, NULL);
  775. if (IS_ERR(s))
  776. return PTR_ERR(s);
  777. if (!s->s_root) {
  778. s->s_flags = flags;
  779. error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
  780. if (error) {
  781. deactivate_locked_super(s);
  782. return error;
  783. }
  784. s->s_flags |= MS_ACTIVE;
  785. } else {
  786. do_remount_sb(s, flags, data, 0);
  787. }
  788. simple_set_mnt(mnt, s);
  789. return 0;
  790. }
  791. EXPORT_SYMBOL(get_sb_single);
  792. struct vfsmount *
  793. vfs_kern_mount(struct file_system_type *type, int flags, const char *name, void *data)
  794. {
  795. struct vfsmount *mnt;
  796. char *secdata = NULL;
  797. int error;
  798. if (!type)
  799. return ERR_PTR(-ENODEV);
  800. error = -ENOMEM;
  801. mnt = alloc_vfsmnt(name);
  802. if (!mnt)
  803. goto out;
  804. if (flags & MS_KERNMOUNT)
  805. mnt->mnt_flags = MNT_INTERNAL;
  806. if (data && !(type->fs_flags & FS_BINARY_MOUNTDATA)) {
  807. secdata = alloc_secdata();
  808. if (!secdata)
  809. goto out_mnt;
  810. error = security_sb_copy_data(data, secdata);
  811. if (error)
  812. goto out_free_secdata;
  813. }
  814. error = type->get_sb(type, flags, name, data, mnt);
  815. if (error < 0)
  816. goto out_free_secdata;
  817. BUG_ON(!mnt->mnt_sb);
  818. WARN_ON(!mnt->mnt_sb->s_bdi);
  819. mnt->mnt_sb->s_flags |= MS_BORN;
  820. error = security_sb_kern_mount(mnt->mnt_sb, flags, secdata);
  821. if (error)
  822. goto out_sb;
  823. /*
  824. * filesystems should never set s_maxbytes larger than MAX_LFS_FILESIZE
  825. * but s_maxbytes was an unsigned long long for many releases. Throw
  826. * this warning for a little while to try and catch filesystems that
  827. * violate this rule. This warning should be either removed or
  828. * converted to a BUG() in 2.6.34.
  829. */
  830. WARN((mnt->mnt_sb->s_maxbytes < 0), "%s set sb->s_maxbytes to "
  831. "negative value (%lld)\n", type->name, mnt->mnt_sb->s_maxbytes);
  832. mnt->mnt_mountpoint = mnt->mnt_root;
  833. mnt->mnt_parent = mnt;
  834. up_write(&mnt->mnt_sb->s_umount);
  835. free_secdata(secdata);
  836. return mnt;
  837. out_sb:
  838. dput(mnt->mnt_root);
  839. deactivate_locked_super(mnt->mnt_sb);
  840. out_free_secdata:
  841. free_secdata(secdata);
  842. out_mnt:
  843. free_vfsmnt(mnt);
  844. out:
  845. return ERR_PTR(error);
  846. }
  847. EXPORT_SYMBOL_GPL(vfs_kern_mount);
  848. /**
  849. * freeze_super - lock the filesystem and force it into a consistent state
  850. * @sb: the super to lock
  851. *
  852. * Syncs the super to make sure the filesystem is consistent and calls the fs's
  853. * freeze_fs. Subsequent calls to this without first thawing the fs will return
  854. * -EBUSY.
  855. */
  856. int freeze_super(struct super_block *sb)
  857. {
  858. int ret;
  859. atomic_inc(&sb->s_active);
  860. down_write(&sb->s_umount);
  861. if (sb->s_frozen) {
  862. deactivate_locked_super(sb);
  863. return -EBUSY;
  864. }
  865. if (sb->s_flags & MS_RDONLY) {
  866. sb->s_frozen = SB_FREEZE_TRANS;
  867. smp_wmb();
  868. up_write(&sb->s_umount);
  869. return 0;
  870. }
  871. sb->s_frozen = SB_FREEZE_WRITE;
  872. smp_wmb();
  873. sync_filesystem(sb);
  874. sb->s_frozen = SB_FREEZE_TRANS;
  875. smp_wmb();
  876. sync_blockdev(sb->s_bdev);
  877. if (sb->s_op->freeze_fs) {
  878. ret = sb->s_op->freeze_fs(sb);
  879. if (ret) {
  880. printk(KERN_ERR
  881. "VFS:Filesystem freeze failed\n");
  882. sb->s_frozen = SB_UNFROZEN;
  883. deactivate_locked_super(sb);
  884. return ret;
  885. }
  886. }
  887. up_write(&sb->s_umount);
  888. return 0;
  889. }
  890. EXPORT_SYMBOL(freeze_super);
  891. /**
  892. * thaw_super -- unlock filesystem
  893. * @sb: the super to thaw
  894. *
  895. * Unlocks the filesystem and marks it writeable again after freeze_super().
  896. */
  897. int thaw_super(struct super_block *sb)
  898. {
  899. int error;
  900. down_write(&sb->s_umount);
  901. if (sb->s_frozen == SB_UNFROZEN) {
  902. up_write(&sb->s_umount);
  903. return -EINVAL;
  904. }
  905. if (sb->s_flags & MS_RDONLY)
  906. goto out;
  907. if (sb->s_op->unfreeze_fs) {
  908. error = sb->s_op->unfreeze_fs(sb);
  909. if (error) {
  910. printk(KERN_ERR
  911. "VFS:Filesystem thaw failed\n");
  912. sb->s_frozen = SB_FREEZE_TRANS;
  913. up_write(&sb->s_umount);
  914. return error;
  915. }
  916. }
  917. out:
  918. sb->s_frozen = SB_UNFROZEN;
  919. smp_wmb();
  920. wake_up(&sb->s_wait_unfrozen);
  921. deactivate_locked_super(sb);
  922. return 0;
  923. }
  924. EXPORT_SYMBOL(thaw_super);
  925. static struct vfsmount *fs_set_subtype(struct vfsmount *mnt, const char *fstype)
  926. {
  927. int err;
  928. const char *subtype = strchr(fstype, '.');
  929. if (subtype) {
  930. subtype++;
  931. err = -EINVAL;
  932. if (!subtype[0])
  933. goto err;
  934. } else
  935. subtype = "";
  936. mnt->mnt_sb->s_subtype = kstrdup(subtype, GFP_KERNEL);
  937. err = -ENOMEM;
  938. if (!mnt->mnt_sb->s_subtype)
  939. goto err;
  940. return mnt;
  941. err:
  942. mntput(mnt);
  943. return ERR_PTR(err);
  944. }
  945. struct vfsmount *
  946. do_kern_mount(const char *fstype, int flags, const char *name, void *data)
  947. {
  948. struct file_system_type *type = get_fs_type(fstype);
  949. struct vfsmount *mnt;
  950. if (!type)
  951. return ERR_PTR(-ENODEV);
  952. mnt = vfs_kern_mount(type, flags, name, data);
  953. if (!IS_ERR(mnt) && (type->fs_flags & FS_HAS_SUBTYPE) &&
  954. !mnt->mnt_sb->s_subtype)
  955. mnt = fs_set_subtype(mnt, fstype);
  956. put_filesystem(type);
  957. return mnt;
  958. }
  959. EXPORT_SYMBOL_GPL(do_kern_mount);
  960. struct vfsmount *kern_mount_data(struct file_system_type *type, void *data)
  961. {
  962. return vfs_kern_mount(type, MS_KERNMOUNT, type->name, data);
  963. }
  964. EXPORT_SYMBOL_GPL(kern_mount_data);