super.c 25 KB

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