super.c 28 KB

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