super.c 24 KB

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