super.c 25 KB

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