super.c 28 KB

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