super.c 25 KB

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