super.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067
  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/init.h>
  25. #include <linux/smp_lock.h>
  26. #include <linux/acct.h>
  27. #include <linux/blkdev.h>
  28. #include <linux/quotaops.h>
  29. #include <linux/namei.h>
  30. #include <linux/buffer_head.h> /* for fsync_super() */
  31. #include <linux/mount.h>
  32. #include <linux/security.h>
  33. #include <linux/syscalls.h>
  34. #include <linux/vfs.h>
  35. #include <linux/writeback.h> /* for the emergency remount stuff */
  36. #include <linux/idr.h>
  37. #include <linux/kobject.h>
  38. #include <linux/mutex.h>
  39. #include <linux/file.h>
  40. #include <asm/uaccess.h>
  41. #include "internal.h"
  42. LIST_HEAD(super_blocks);
  43. DEFINE_SPINLOCK(sb_lock);
  44. /**
  45. * alloc_super - create new superblock
  46. * @type: filesystem type superblock should belong to
  47. *
  48. * Allocates and initializes a new &struct super_block. alloc_super()
  49. * returns a pointer new superblock or %NULL if allocation had failed.
  50. */
  51. static struct super_block *alloc_super(struct file_system_type *type)
  52. {
  53. struct super_block *s = kzalloc(sizeof(struct super_block), GFP_USER);
  54. static struct super_operations default_op;
  55. if (s) {
  56. if (security_sb_alloc(s)) {
  57. kfree(s);
  58. s = NULL;
  59. goto out;
  60. }
  61. INIT_LIST_HEAD(&s->s_dirty);
  62. INIT_LIST_HEAD(&s->s_io);
  63. INIT_LIST_HEAD(&s->s_more_io);
  64. INIT_LIST_HEAD(&s->s_files);
  65. INIT_LIST_HEAD(&s->s_instances);
  66. INIT_HLIST_HEAD(&s->s_anon);
  67. INIT_LIST_HEAD(&s->s_inodes);
  68. INIT_LIST_HEAD(&s->s_dentry_lru);
  69. init_rwsem(&s->s_umount);
  70. mutex_init(&s->s_lock);
  71. lockdep_set_class(&s->s_umount, &type->s_umount_key);
  72. /*
  73. * The locking rules for s_lock are up to the
  74. * filesystem. For example ext3fs has different
  75. * lock ordering than usbfs:
  76. */
  77. lockdep_set_class(&s->s_lock, &type->s_lock_key);
  78. /*
  79. * sget() can have s_umount recursion.
  80. *
  81. * When it cannot find a suitable sb, it allocates a new
  82. * one (this one), and tries again to find a suitable old
  83. * one.
  84. *
  85. * In case that succeeds, it will acquire the s_umount
  86. * lock of the old one. Since these are clearly distrinct
  87. * locks, and this object isn't exposed yet, there's no
  88. * risk of deadlocks.
  89. *
  90. * Annotate this by putting this lock in a different
  91. * subclass.
  92. */
  93. down_write_nested(&s->s_umount, SINGLE_DEPTH_NESTING);
  94. s->s_count = S_BIAS;
  95. atomic_set(&s->s_active, 1);
  96. mutex_init(&s->s_vfs_rename_mutex);
  97. mutex_init(&s->s_dquot.dqio_mutex);
  98. mutex_init(&s->s_dquot.dqonoff_mutex);
  99. init_rwsem(&s->s_dquot.dqptr_sem);
  100. init_waitqueue_head(&s->s_wait_unfrozen);
  101. s->s_maxbytes = MAX_NON_LFS;
  102. s->dq_op = sb_dquot_ops;
  103. s->s_qcop = sb_quotactl_ops;
  104. s->s_op = &default_op;
  105. s->s_time_gran = 1000000000;
  106. }
  107. out:
  108. return s;
  109. }
  110. /**
  111. * destroy_super - frees a superblock
  112. * @s: superblock to free
  113. *
  114. * Frees a superblock.
  115. */
  116. static inline void destroy_super(struct super_block *s)
  117. {
  118. security_sb_free(s);
  119. kfree(s->s_subtype);
  120. kfree(s->s_options);
  121. kfree(s);
  122. }
  123. /* Superblock refcounting */
  124. /*
  125. * Drop a superblock's refcount. Returns non-zero if the superblock was
  126. * destroyed. The caller must hold sb_lock.
  127. */
  128. static int __put_super(struct super_block *sb)
  129. {
  130. int ret = 0;
  131. if (!--sb->s_count) {
  132. destroy_super(sb);
  133. ret = 1;
  134. }
  135. return ret;
  136. }
  137. /*
  138. * Drop a superblock's refcount.
  139. * Returns non-zero if the superblock is about to be destroyed and
  140. * at least is already removed from super_blocks list, so if we are
  141. * making a loop through super blocks then we need to restart.
  142. * The caller must hold sb_lock.
  143. */
  144. int __put_super_and_need_restart(struct super_block *sb)
  145. {
  146. /* check for race with generic_shutdown_super() */
  147. if (list_empty(&sb->s_list)) {
  148. /* super block is removed, need to restart... */
  149. __put_super(sb);
  150. return 1;
  151. }
  152. /* can't be the last, since s_list is still in use */
  153. sb->s_count--;
  154. BUG_ON(sb->s_count == 0);
  155. return 0;
  156. }
  157. /**
  158. * put_super - drop a temporary reference to superblock
  159. * @sb: superblock in question
  160. *
  161. * Drops a temporary reference, frees superblock if there's no
  162. * references left.
  163. */
  164. static void put_super(struct super_block *sb)
  165. {
  166. spin_lock(&sb_lock);
  167. __put_super(sb);
  168. spin_unlock(&sb_lock);
  169. }
  170. /**
  171. * deactivate_super - drop an active reference to superblock
  172. * @s: superblock to deactivate
  173. *
  174. * Drops an active reference to superblock, acquiring a temprory one if
  175. * there is no active references left. In that case we lock superblock,
  176. * tell fs driver to shut it down and drop the temporary reference we
  177. * had just acquired.
  178. */
  179. void deactivate_super(struct super_block *s)
  180. {
  181. struct file_system_type *fs = s->s_type;
  182. if (atomic_dec_and_lock(&s->s_active, &sb_lock)) {
  183. s->s_count -= S_BIAS-1;
  184. spin_unlock(&sb_lock);
  185. vfs_dq_off(s, 0);
  186. down_write(&s->s_umount);
  187. fs->kill_sb(s);
  188. put_filesystem(fs);
  189. put_super(s);
  190. }
  191. }
  192. EXPORT_SYMBOL(deactivate_super);
  193. /**
  194. * deactivate_locked_super - drop an active reference to superblock
  195. * @s: superblock to deactivate
  196. *
  197. * Equivalent of up_write(&s->s_umount); deactivate_super(s);, except that
  198. * it does not unlock it until it's all over. As the result, it's safe to
  199. * use to dispose of new superblock on ->get_sb() failure exits - nobody
  200. * will see the sucker until it's all over. Equivalent using up_write +
  201. * deactivate_super is safe for that purpose only if superblock is either
  202. * safe to use or has NULL ->s_root when we unlock.
  203. */
  204. void deactivate_locked_super(struct super_block *s)
  205. {
  206. struct file_system_type *fs = s->s_type;
  207. if (atomic_dec_and_lock(&s->s_active, &sb_lock)) {
  208. s->s_count -= S_BIAS-1;
  209. spin_unlock(&sb_lock);
  210. vfs_dq_off(s, 0);
  211. fs->kill_sb(s);
  212. put_filesystem(fs);
  213. put_super(s);
  214. } else {
  215. up_write(&s->s_umount);
  216. }
  217. }
  218. EXPORT_SYMBOL(deactivate_locked_super);
  219. /**
  220. * grab_super - acquire an active reference
  221. * @s: reference we are trying to make active
  222. *
  223. * Tries to acquire an active reference. grab_super() is used when we
  224. * had just found a superblock in super_blocks or fs_type->fs_supers
  225. * and want to turn it into a full-blown active reference. grab_super()
  226. * is called with sb_lock held and drops it. Returns 1 in case of
  227. * success, 0 if we had failed (superblock contents was already dead or
  228. * dying when grab_super() had been called).
  229. */
  230. static int grab_super(struct super_block *s) __releases(sb_lock)
  231. {
  232. s->s_count++;
  233. spin_unlock(&sb_lock);
  234. down_write(&s->s_umount);
  235. if (s->s_root) {
  236. spin_lock(&sb_lock);
  237. if (s->s_count > S_BIAS) {
  238. atomic_inc(&s->s_active);
  239. s->s_count--;
  240. spin_unlock(&sb_lock);
  241. return 1;
  242. }
  243. spin_unlock(&sb_lock);
  244. }
  245. up_write(&s->s_umount);
  246. put_super(s);
  247. yield();
  248. return 0;
  249. }
  250. /*
  251. * Superblock locking. We really ought to get rid of these two.
  252. */
  253. void lock_super(struct super_block * sb)
  254. {
  255. get_fs_excl();
  256. mutex_lock(&sb->s_lock);
  257. }
  258. void unlock_super(struct super_block * sb)
  259. {
  260. put_fs_excl();
  261. mutex_unlock(&sb->s_lock);
  262. }
  263. EXPORT_SYMBOL(lock_super);
  264. EXPORT_SYMBOL(unlock_super);
  265. /*
  266. * Write out and wait upon all dirty data associated with this
  267. * superblock. Filesystem data as well as the underlying block
  268. * device. Takes the superblock lock. Requires a second blkdev
  269. * flush by the caller to complete the operation.
  270. */
  271. void __fsync_super(struct super_block *sb)
  272. {
  273. sync_inodes_sb(sb, 0);
  274. vfs_dq_sync(sb);
  275. sync_inodes_sb(sb, 1);
  276. lock_super(sb);
  277. if (sb->s_dirt && sb->s_op->write_super)
  278. sb->s_op->write_super(sb);
  279. unlock_super(sb);
  280. if (sb->s_op->sync_fs)
  281. sb->s_op->sync_fs(sb, 1);
  282. sync_blockdev(sb->s_bdev);
  283. }
  284. /*
  285. * Write out and wait upon all dirty data associated with this
  286. * superblock. Filesystem data as well as the underlying block
  287. * device. Takes the superblock lock.
  288. */
  289. int fsync_super(struct super_block *sb)
  290. {
  291. __fsync_super(sb);
  292. return sync_blockdev(sb->s_bdev);
  293. }
  294. EXPORT_SYMBOL_GPL(fsync_super);
  295. /**
  296. * generic_shutdown_super - common helper for ->kill_sb()
  297. * @sb: superblock to kill
  298. *
  299. * generic_shutdown_super() does all fs-independent work on superblock
  300. * shutdown. Typical ->kill_sb() should pick all fs-specific objects
  301. * that need destruction out of superblock, call generic_shutdown_super()
  302. * and release aforementioned objects. Note: dentries and inodes _are_
  303. * taken care of and do not need specific handling.
  304. *
  305. * Upon calling this function, the filesystem may no longer alter or
  306. * rearrange the set of dentries belonging to this super_block, nor may it
  307. * change the attachments of dentries to inodes.
  308. */
  309. void generic_shutdown_super(struct super_block *sb)
  310. {
  311. const struct super_operations *sop = sb->s_op;
  312. if (sb->s_root) {
  313. shrink_dcache_for_umount(sb);
  314. fsync_super(sb);
  315. lock_super(sb);
  316. sb->s_flags &= ~MS_ACTIVE;
  317. /* bad name - it should be evict_inodes() */
  318. invalidate_inodes(sb);
  319. lock_kernel();
  320. if (sop->write_super && sb->s_dirt)
  321. sop->write_super(sb);
  322. if (sop->put_super)
  323. sop->put_super(sb);
  324. /* Forget any remaining inodes */
  325. if (invalidate_inodes(sb)) {
  326. printk("VFS: Busy inodes after unmount of %s. "
  327. "Self-destruct in 5 seconds. Have a nice day...\n",
  328. sb->s_id);
  329. }
  330. unlock_kernel();
  331. unlock_super(sb);
  332. }
  333. spin_lock(&sb_lock);
  334. /* should be initialized for __put_super_and_need_restart() */
  335. list_del_init(&sb->s_list);
  336. list_del(&sb->s_instances);
  337. spin_unlock(&sb_lock);
  338. up_write(&sb->s_umount);
  339. }
  340. EXPORT_SYMBOL(generic_shutdown_super);
  341. /**
  342. * sget - find or create a superblock
  343. * @type: filesystem type superblock should belong to
  344. * @test: comparison callback
  345. * @set: setup callback
  346. * @data: argument to each of them
  347. */
  348. struct super_block *sget(struct file_system_type *type,
  349. int (*test)(struct super_block *,void *),
  350. int (*set)(struct super_block *,void *),
  351. void *data)
  352. {
  353. struct super_block *s = NULL;
  354. struct super_block *old;
  355. int err;
  356. retry:
  357. spin_lock(&sb_lock);
  358. if (test) {
  359. list_for_each_entry(old, &type->fs_supers, s_instances) {
  360. if (!test(old, data))
  361. continue;
  362. if (!grab_super(old))
  363. goto retry;
  364. if (s) {
  365. up_write(&s->s_umount);
  366. destroy_super(s);
  367. }
  368. return old;
  369. }
  370. }
  371. if (!s) {
  372. spin_unlock(&sb_lock);
  373. s = alloc_super(type);
  374. if (!s)
  375. return ERR_PTR(-ENOMEM);
  376. goto retry;
  377. }
  378. err = set(s, data);
  379. if (err) {
  380. spin_unlock(&sb_lock);
  381. up_write(&s->s_umount);
  382. destroy_super(s);
  383. return ERR_PTR(err);
  384. }
  385. s->s_type = type;
  386. strlcpy(s->s_id, type->name, sizeof(s->s_id));
  387. list_add_tail(&s->s_list, &super_blocks);
  388. list_add(&s->s_instances, &type->fs_supers);
  389. spin_unlock(&sb_lock);
  390. get_filesystem(type);
  391. return s;
  392. }
  393. EXPORT_SYMBOL(sget);
  394. void drop_super(struct super_block *sb)
  395. {
  396. up_read(&sb->s_umount);
  397. put_super(sb);
  398. }
  399. EXPORT_SYMBOL(drop_super);
  400. static inline void write_super(struct super_block *sb)
  401. {
  402. lock_super(sb);
  403. if (sb->s_root && sb->s_dirt)
  404. if (sb->s_op->write_super)
  405. sb->s_op->write_super(sb);
  406. unlock_super(sb);
  407. }
  408. /*
  409. * Note: check the dirty flag before waiting, so we don't
  410. * hold up the sync while mounting a device. (The newly
  411. * mounted device won't need syncing.)
  412. */
  413. void sync_supers(void)
  414. {
  415. struct super_block *sb;
  416. spin_lock(&sb_lock);
  417. restart:
  418. list_for_each_entry(sb, &super_blocks, s_list) {
  419. if (sb->s_dirt) {
  420. sb->s_count++;
  421. spin_unlock(&sb_lock);
  422. down_read(&sb->s_umount);
  423. write_super(sb);
  424. up_read(&sb->s_umount);
  425. spin_lock(&sb_lock);
  426. if (__put_super_and_need_restart(sb))
  427. goto restart;
  428. }
  429. }
  430. spin_unlock(&sb_lock);
  431. }
  432. /*
  433. * Call the ->sync_fs super_op against all filesystems which are r/w and
  434. * which implement it.
  435. *
  436. * This operation is careful to avoid the livelock which could easily happen
  437. * if two or more filesystems are being continuously dirtied. s_need_sync_fs
  438. * is used only here. We set it against all filesystems and then clear it as
  439. * we sync them. So redirtied filesystems are skipped.
  440. *
  441. * But if process A is currently running sync_filesystems and then process B
  442. * calls sync_filesystems as well, process B will set all the s_need_sync_fs
  443. * flags again, which will cause process A to resync everything. Fix that with
  444. * a local mutex.
  445. *
  446. * (Fabian) Avoid sync_fs with clean fs & wait mode 0
  447. */
  448. void sync_filesystems(int wait)
  449. {
  450. struct super_block *sb;
  451. static DEFINE_MUTEX(mutex);
  452. mutex_lock(&mutex); /* Could be down_interruptible */
  453. spin_lock(&sb_lock);
  454. list_for_each_entry(sb, &super_blocks, s_list) {
  455. if (!sb->s_op->sync_fs)
  456. continue;
  457. if (sb->s_flags & MS_RDONLY)
  458. continue;
  459. sb->s_need_sync_fs = 1;
  460. }
  461. restart:
  462. list_for_each_entry(sb, &super_blocks, s_list) {
  463. if (!sb->s_need_sync_fs)
  464. continue;
  465. sb->s_need_sync_fs = 0;
  466. if (sb->s_flags & MS_RDONLY)
  467. continue; /* hm. Was remounted r/o meanwhile */
  468. sb->s_count++;
  469. spin_unlock(&sb_lock);
  470. down_read(&sb->s_umount);
  471. if (sb->s_root && (wait || sb->s_dirt))
  472. sb->s_op->sync_fs(sb, wait);
  473. up_read(&sb->s_umount);
  474. /* restart only when sb is no longer on the list */
  475. spin_lock(&sb_lock);
  476. if (__put_super_and_need_restart(sb))
  477. goto restart;
  478. }
  479. spin_unlock(&sb_lock);
  480. mutex_unlock(&mutex);
  481. }
  482. #ifdef CONFIG_BLOCK
  483. /*
  484. * Sync all block devices underlying some superblock
  485. */
  486. void sync_blockdevs(void)
  487. {
  488. struct super_block *sb;
  489. spin_lock(&sb_lock);
  490. restart:
  491. list_for_each_entry(sb, &super_blocks, s_list) {
  492. if (!sb->s_bdev)
  493. continue;
  494. sb->s_count++;
  495. spin_unlock(&sb_lock);
  496. down_read(&sb->s_umount);
  497. if (sb->s_root)
  498. sync_blockdev(sb->s_bdev);
  499. up_read(&sb->s_umount);
  500. spin_lock(&sb_lock);
  501. if (__put_super_and_need_restart(sb))
  502. goto restart;
  503. }
  504. spin_unlock(&sb_lock);
  505. }
  506. #endif
  507. /**
  508. * get_super - get the superblock of a device
  509. * @bdev: device to get the superblock for
  510. *
  511. * Scans the superblock list and finds the superblock of the file system
  512. * mounted on the device given. %NULL is returned if no match is found.
  513. */
  514. struct super_block * get_super(struct block_device *bdev)
  515. {
  516. struct super_block *sb;
  517. if (!bdev)
  518. return NULL;
  519. spin_lock(&sb_lock);
  520. rescan:
  521. list_for_each_entry(sb, &super_blocks, s_list) {
  522. if (sb->s_bdev == bdev) {
  523. sb->s_count++;
  524. spin_unlock(&sb_lock);
  525. down_read(&sb->s_umount);
  526. if (sb->s_root)
  527. return sb;
  528. up_read(&sb->s_umount);
  529. /* restart only when sb is no longer on the list */
  530. spin_lock(&sb_lock);
  531. if (__put_super_and_need_restart(sb))
  532. goto rescan;
  533. }
  534. }
  535. spin_unlock(&sb_lock);
  536. return NULL;
  537. }
  538. EXPORT_SYMBOL(get_super);
  539. struct super_block * user_get_super(dev_t dev)
  540. {
  541. struct super_block *sb;
  542. spin_lock(&sb_lock);
  543. rescan:
  544. list_for_each_entry(sb, &super_blocks, s_list) {
  545. if (sb->s_dev == dev) {
  546. sb->s_count++;
  547. spin_unlock(&sb_lock);
  548. down_read(&sb->s_umount);
  549. if (sb->s_root)
  550. return sb;
  551. up_read(&sb->s_umount);
  552. /* restart only when sb is no longer on the list */
  553. spin_lock(&sb_lock);
  554. if (__put_super_and_need_restart(sb))
  555. goto rescan;
  556. }
  557. }
  558. spin_unlock(&sb_lock);
  559. return NULL;
  560. }
  561. SYSCALL_DEFINE2(ustat, unsigned, dev, struct ustat __user *, ubuf)
  562. {
  563. struct super_block *s;
  564. struct ustat tmp;
  565. struct kstatfs sbuf;
  566. int err = -EINVAL;
  567. s = user_get_super(new_decode_dev(dev));
  568. if (s == NULL)
  569. goto out;
  570. err = vfs_statfs(s->s_root, &sbuf);
  571. drop_super(s);
  572. if (err)
  573. goto out;
  574. memset(&tmp,0,sizeof(struct ustat));
  575. tmp.f_tfree = sbuf.f_bfree;
  576. tmp.f_tinode = sbuf.f_ffree;
  577. err = copy_to_user(ubuf,&tmp,sizeof(struct ustat)) ? -EFAULT : 0;
  578. out:
  579. return err;
  580. }
  581. /**
  582. * do_remount_sb - asks filesystem to change mount options.
  583. * @sb: superblock in question
  584. * @flags: numeric part of options
  585. * @data: the rest of options
  586. * @force: whether or not to force the change
  587. *
  588. * Alters the mount options of a mounted file system.
  589. */
  590. int do_remount_sb(struct super_block *sb, int flags, void *data, int force)
  591. {
  592. int retval;
  593. int remount_rw;
  594. #ifdef CONFIG_BLOCK
  595. if (!(flags & MS_RDONLY) && bdev_read_only(sb->s_bdev))
  596. return -EACCES;
  597. #endif
  598. if (flags & MS_RDONLY)
  599. acct_auto_close(sb);
  600. shrink_dcache_sb(sb);
  601. fsync_super(sb);
  602. /* If we are remounting RDONLY and current sb is read/write,
  603. make sure there are no rw files opened */
  604. if ((flags & MS_RDONLY) && !(sb->s_flags & MS_RDONLY)) {
  605. if (force)
  606. mark_files_ro(sb);
  607. else if (!fs_may_remount_ro(sb))
  608. return -EBUSY;
  609. retval = vfs_dq_off(sb, 1);
  610. if (retval < 0 && retval != -ENOSYS)
  611. return -EBUSY;
  612. }
  613. remount_rw = !(flags & MS_RDONLY) && (sb->s_flags & MS_RDONLY);
  614. if (sb->s_op->remount_fs) {
  615. lock_super(sb);
  616. retval = sb->s_op->remount_fs(sb, &flags, data);
  617. unlock_super(sb);
  618. if (retval)
  619. return retval;
  620. }
  621. sb->s_flags = (sb->s_flags & ~MS_RMT_MASK) | (flags & MS_RMT_MASK);
  622. if (remount_rw)
  623. vfs_dq_quota_on_remount(sb);
  624. return 0;
  625. }
  626. static void do_emergency_remount(struct work_struct *work)
  627. {
  628. struct super_block *sb;
  629. spin_lock(&sb_lock);
  630. list_for_each_entry(sb, &super_blocks, s_list) {
  631. sb->s_count++;
  632. spin_unlock(&sb_lock);
  633. down_read(&sb->s_umount);
  634. if (sb->s_root && sb->s_bdev && !(sb->s_flags & MS_RDONLY)) {
  635. /*
  636. * ->remount_fs needs lock_kernel().
  637. *
  638. * What lock protects sb->s_flags??
  639. */
  640. lock_kernel();
  641. do_remount_sb(sb, MS_RDONLY, NULL, 1);
  642. unlock_kernel();
  643. }
  644. drop_super(sb);
  645. spin_lock(&sb_lock);
  646. }
  647. spin_unlock(&sb_lock);
  648. kfree(work);
  649. printk("Emergency Remount complete\n");
  650. }
  651. void emergency_remount(void)
  652. {
  653. struct work_struct *work;
  654. work = kmalloc(sizeof(*work), GFP_ATOMIC);
  655. if (work) {
  656. INIT_WORK(work, do_emergency_remount);
  657. schedule_work(work);
  658. }
  659. }
  660. /*
  661. * Unnamed block devices are dummy devices used by virtual
  662. * filesystems which don't use real block-devices. -- jrs
  663. */
  664. static DEFINE_IDA(unnamed_dev_ida);
  665. static DEFINE_SPINLOCK(unnamed_dev_lock);/* protects the above */
  666. int set_anon_super(struct super_block *s, void *data)
  667. {
  668. int dev;
  669. int error;
  670. retry:
  671. if (ida_pre_get(&unnamed_dev_ida, GFP_ATOMIC) == 0)
  672. return -ENOMEM;
  673. spin_lock(&unnamed_dev_lock);
  674. error = ida_get_new(&unnamed_dev_ida, &dev);
  675. spin_unlock(&unnamed_dev_lock);
  676. if (error == -EAGAIN)
  677. /* We raced and lost with another CPU. */
  678. goto retry;
  679. else if (error)
  680. return -EAGAIN;
  681. if ((dev & MAX_ID_MASK) == (1 << MINORBITS)) {
  682. spin_lock(&unnamed_dev_lock);
  683. ida_remove(&unnamed_dev_ida, dev);
  684. spin_unlock(&unnamed_dev_lock);
  685. return -EMFILE;
  686. }
  687. s->s_dev = MKDEV(0, dev & MINORMASK);
  688. return 0;
  689. }
  690. EXPORT_SYMBOL(set_anon_super);
  691. void kill_anon_super(struct super_block *sb)
  692. {
  693. int slot = MINOR(sb->s_dev);
  694. generic_shutdown_super(sb);
  695. spin_lock(&unnamed_dev_lock);
  696. ida_remove(&unnamed_dev_ida, slot);
  697. spin_unlock(&unnamed_dev_lock);
  698. }
  699. EXPORT_SYMBOL(kill_anon_super);
  700. void kill_litter_super(struct super_block *sb)
  701. {
  702. if (sb->s_root)
  703. d_genocide(sb->s_root);
  704. kill_anon_super(sb);
  705. }
  706. EXPORT_SYMBOL(kill_litter_super);
  707. static int ns_test_super(struct super_block *sb, void *data)
  708. {
  709. return sb->s_fs_info == data;
  710. }
  711. static int ns_set_super(struct super_block *sb, void *data)
  712. {
  713. sb->s_fs_info = data;
  714. return set_anon_super(sb, NULL);
  715. }
  716. int get_sb_ns(struct file_system_type *fs_type, int flags, void *data,
  717. int (*fill_super)(struct super_block *, void *, int),
  718. struct vfsmount *mnt)
  719. {
  720. struct super_block *sb;
  721. sb = sget(fs_type, ns_test_super, ns_set_super, data);
  722. if (IS_ERR(sb))
  723. return PTR_ERR(sb);
  724. if (!sb->s_root) {
  725. int err;
  726. sb->s_flags = flags;
  727. err = fill_super(sb, data, flags & MS_SILENT ? 1 : 0);
  728. if (err) {
  729. deactivate_locked_super(sb);
  730. return err;
  731. }
  732. sb->s_flags |= MS_ACTIVE;
  733. }
  734. simple_set_mnt(mnt, sb);
  735. return 0;
  736. }
  737. EXPORT_SYMBOL(get_sb_ns);
  738. #ifdef CONFIG_BLOCK
  739. static int set_bdev_super(struct super_block *s, void *data)
  740. {
  741. s->s_bdev = data;
  742. s->s_dev = s->s_bdev->bd_dev;
  743. return 0;
  744. }
  745. static int test_bdev_super(struct super_block *s, void *data)
  746. {
  747. return (void *)s->s_bdev == data;
  748. }
  749. int get_sb_bdev(struct file_system_type *fs_type,
  750. int flags, const char *dev_name, void *data,
  751. int (*fill_super)(struct super_block *, void *, int),
  752. struct vfsmount *mnt)
  753. {
  754. struct block_device *bdev;
  755. struct super_block *s;
  756. fmode_t mode = FMODE_READ;
  757. int error = 0;
  758. if (!(flags & MS_RDONLY))
  759. mode |= FMODE_WRITE;
  760. bdev = open_bdev_exclusive(dev_name, mode, fs_type);
  761. if (IS_ERR(bdev))
  762. return PTR_ERR(bdev);
  763. /*
  764. * once the super is inserted into the list by sget, s_umount
  765. * will protect the lockfs code from trying to start a snapshot
  766. * while we are mounting
  767. */
  768. down(&bdev->bd_mount_sem);
  769. s = sget(fs_type, test_bdev_super, set_bdev_super, bdev);
  770. up(&bdev->bd_mount_sem);
  771. if (IS_ERR(s))
  772. goto error_s;
  773. if (s->s_root) {
  774. if ((flags ^ s->s_flags) & MS_RDONLY) {
  775. deactivate_locked_super(s);
  776. error = -EBUSY;
  777. goto error_bdev;
  778. }
  779. close_bdev_exclusive(bdev, mode);
  780. } else {
  781. char b[BDEVNAME_SIZE];
  782. s->s_flags = flags;
  783. s->s_mode = mode;
  784. strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id));
  785. sb_set_blocksize(s, block_size(bdev));
  786. error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
  787. if (error) {
  788. deactivate_locked_super(s);
  789. goto error;
  790. }
  791. s->s_flags |= MS_ACTIVE;
  792. bdev->bd_super = s;
  793. }
  794. simple_set_mnt(mnt, s);
  795. return 0;
  796. error_s:
  797. error = PTR_ERR(s);
  798. error_bdev:
  799. close_bdev_exclusive(bdev, mode);
  800. error:
  801. return error;
  802. }
  803. EXPORT_SYMBOL(get_sb_bdev);
  804. void kill_block_super(struct super_block *sb)
  805. {
  806. struct block_device *bdev = sb->s_bdev;
  807. fmode_t mode = sb->s_mode;
  808. bdev->bd_super = NULL;
  809. generic_shutdown_super(sb);
  810. sync_blockdev(bdev);
  811. close_bdev_exclusive(bdev, mode);
  812. }
  813. EXPORT_SYMBOL(kill_block_super);
  814. #endif
  815. int get_sb_nodev(struct file_system_type *fs_type,
  816. int flags, void *data,
  817. int (*fill_super)(struct super_block *, void *, int),
  818. struct vfsmount *mnt)
  819. {
  820. int error;
  821. struct super_block *s = sget(fs_type, NULL, set_anon_super, NULL);
  822. if (IS_ERR(s))
  823. return PTR_ERR(s);
  824. s->s_flags = flags;
  825. error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
  826. if (error) {
  827. deactivate_locked_super(s);
  828. return error;
  829. }
  830. s->s_flags |= MS_ACTIVE;
  831. simple_set_mnt(mnt, s);
  832. return 0;
  833. }
  834. EXPORT_SYMBOL(get_sb_nodev);
  835. static int compare_single(struct super_block *s, void *p)
  836. {
  837. return 1;
  838. }
  839. int get_sb_single(struct file_system_type *fs_type,
  840. int flags, void *data,
  841. int (*fill_super)(struct super_block *, void *, int),
  842. struct vfsmount *mnt)
  843. {
  844. struct super_block *s;
  845. int error;
  846. s = sget(fs_type, compare_single, set_anon_super, NULL);
  847. if (IS_ERR(s))
  848. return PTR_ERR(s);
  849. if (!s->s_root) {
  850. s->s_flags = flags;
  851. error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
  852. if (error) {
  853. deactivate_locked_super(s);
  854. return error;
  855. }
  856. s->s_flags |= MS_ACTIVE;
  857. }
  858. do_remount_sb(s, flags, data, 0);
  859. simple_set_mnt(mnt, s);
  860. return 0;
  861. }
  862. EXPORT_SYMBOL(get_sb_single);
  863. struct vfsmount *
  864. vfs_kern_mount(struct file_system_type *type, int flags, const char *name, void *data)
  865. {
  866. struct vfsmount *mnt;
  867. char *secdata = NULL;
  868. int error;
  869. if (!type)
  870. return ERR_PTR(-ENODEV);
  871. error = -ENOMEM;
  872. mnt = alloc_vfsmnt(name);
  873. if (!mnt)
  874. goto out;
  875. if (data && !(type->fs_flags & FS_BINARY_MOUNTDATA)) {
  876. secdata = alloc_secdata();
  877. if (!secdata)
  878. goto out_mnt;
  879. error = security_sb_copy_data(data, secdata);
  880. if (error)
  881. goto out_free_secdata;
  882. }
  883. error = type->get_sb(type, flags, name, data, mnt);
  884. if (error < 0)
  885. goto out_free_secdata;
  886. BUG_ON(!mnt->mnt_sb);
  887. error = security_sb_kern_mount(mnt->mnt_sb, flags, secdata);
  888. if (error)
  889. goto out_sb;
  890. mnt->mnt_mountpoint = mnt->mnt_root;
  891. mnt->mnt_parent = mnt;
  892. up_write(&mnt->mnt_sb->s_umount);
  893. free_secdata(secdata);
  894. return mnt;
  895. out_sb:
  896. dput(mnt->mnt_root);
  897. deactivate_locked_super(mnt->mnt_sb);
  898. out_free_secdata:
  899. free_secdata(secdata);
  900. out_mnt:
  901. free_vfsmnt(mnt);
  902. out:
  903. return ERR_PTR(error);
  904. }
  905. EXPORT_SYMBOL_GPL(vfs_kern_mount);
  906. static struct vfsmount *fs_set_subtype(struct vfsmount *mnt, const char *fstype)
  907. {
  908. int err;
  909. const char *subtype = strchr(fstype, '.');
  910. if (subtype) {
  911. subtype++;
  912. err = -EINVAL;
  913. if (!subtype[0])
  914. goto err;
  915. } else
  916. subtype = "";
  917. mnt->mnt_sb->s_subtype = kstrdup(subtype, GFP_KERNEL);
  918. err = -ENOMEM;
  919. if (!mnt->mnt_sb->s_subtype)
  920. goto err;
  921. return mnt;
  922. err:
  923. mntput(mnt);
  924. return ERR_PTR(err);
  925. }
  926. struct vfsmount *
  927. do_kern_mount(const char *fstype, int flags, const char *name, void *data)
  928. {
  929. struct file_system_type *type = get_fs_type(fstype);
  930. struct vfsmount *mnt;
  931. if (!type)
  932. return ERR_PTR(-ENODEV);
  933. mnt = vfs_kern_mount(type, flags, name, data);
  934. if (!IS_ERR(mnt) && (type->fs_flags & FS_HAS_SUBTYPE) &&
  935. !mnt->mnt_sb->s_subtype)
  936. mnt = fs_set_subtype(mnt, fstype);
  937. put_filesystem(type);
  938. return mnt;
  939. }
  940. EXPORT_SYMBOL_GPL(do_kern_mount);
  941. struct vfsmount *kern_mount_data(struct file_system_type *type, void *data)
  942. {
  943. return vfs_kern_mount(type, MS_KERNMOUNT, type->name, data);
  944. }
  945. EXPORT_SYMBOL_GPL(kern_mount_data);