super.c 29 KB

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