super.c 25 KB

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