xfs_super.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965
  1. /*
  2. * Copyright (c) 2000-2006 Silicon Graphics, Inc.
  3. * All Rights Reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it would be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write the Free Software Foundation,
  16. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "xfs.h"
  19. #include "xfs_bit.h"
  20. #include "xfs_log.h"
  21. #include "xfs_clnt.h"
  22. #include "xfs_inum.h"
  23. #include "xfs_trans.h"
  24. #include "xfs_sb.h"
  25. #include "xfs_ag.h"
  26. #include "xfs_dir2.h"
  27. #include "xfs_alloc.h"
  28. #include "xfs_dmapi.h"
  29. #include "xfs_quota.h"
  30. #include "xfs_mount.h"
  31. #include "xfs_bmap_btree.h"
  32. #include "xfs_alloc_btree.h"
  33. #include "xfs_ialloc_btree.h"
  34. #include "xfs_dir2_sf.h"
  35. #include "xfs_attr_sf.h"
  36. #include "xfs_dinode.h"
  37. #include "xfs_inode.h"
  38. #include "xfs_btree.h"
  39. #include "xfs_ialloc.h"
  40. #include "xfs_bmap.h"
  41. #include "xfs_rtalloc.h"
  42. #include "xfs_error.h"
  43. #include "xfs_itable.h"
  44. #include "xfs_rw.h"
  45. #include "xfs_acl.h"
  46. #include "xfs_attr.h"
  47. #include "xfs_buf_item.h"
  48. #include "xfs_utils.h"
  49. #include "xfs_vnodeops.h"
  50. #include "xfs_vfsops.h"
  51. #include "xfs_version.h"
  52. #include <linux/namei.h>
  53. #include <linux/init.h>
  54. #include <linux/mount.h>
  55. #include <linux/mempool.h>
  56. #include <linux/writeback.h>
  57. #include <linux/kthread.h>
  58. #include <linux/freezer.h>
  59. static struct quotactl_ops xfs_quotactl_operations;
  60. static struct super_operations xfs_super_operations;
  61. static kmem_zone_t *xfs_vnode_zone;
  62. static kmem_zone_t *xfs_ioend_zone;
  63. mempool_t *xfs_ioend_pool;
  64. STATIC struct xfs_mount_args *
  65. xfs_args_allocate(
  66. struct super_block *sb,
  67. int silent)
  68. {
  69. struct xfs_mount_args *args;
  70. args = kmem_zalloc(sizeof(struct xfs_mount_args), KM_SLEEP);
  71. args->logbufs = args->logbufsize = -1;
  72. strncpy(args->fsname, sb->s_id, MAXNAMELEN);
  73. /* Copy the already-parsed mount(2) flags we're interested in */
  74. if (sb->s_flags & MS_DIRSYNC)
  75. args->flags |= XFSMNT_DIRSYNC;
  76. if (sb->s_flags & MS_SYNCHRONOUS)
  77. args->flags |= XFSMNT_WSYNC;
  78. if (silent)
  79. args->flags |= XFSMNT_QUIET;
  80. args->flags |= XFSMNT_32BITINODES;
  81. return args;
  82. }
  83. __uint64_t
  84. xfs_max_file_offset(
  85. unsigned int blockshift)
  86. {
  87. unsigned int pagefactor = 1;
  88. unsigned int bitshift = BITS_PER_LONG - 1;
  89. /* Figure out maximum filesize, on Linux this can depend on
  90. * the filesystem blocksize (on 32 bit platforms).
  91. * __block_prepare_write does this in an [unsigned] long...
  92. * page->index << (PAGE_CACHE_SHIFT - bbits)
  93. * So, for page sized blocks (4K on 32 bit platforms),
  94. * this wraps at around 8Tb (hence MAX_LFS_FILESIZE which is
  95. * (((u64)PAGE_CACHE_SIZE << (BITS_PER_LONG-1))-1)
  96. * but for smaller blocksizes it is less (bbits = log2 bsize).
  97. * Note1: get_block_t takes a long (implicit cast from above)
  98. * Note2: The Large Block Device (LBD and HAVE_SECTOR_T) patch
  99. * can optionally convert the [unsigned] long from above into
  100. * an [unsigned] long long.
  101. */
  102. #if BITS_PER_LONG == 32
  103. # if defined(CONFIG_LBD)
  104. ASSERT(sizeof(sector_t) == 8);
  105. pagefactor = PAGE_CACHE_SIZE;
  106. bitshift = BITS_PER_LONG;
  107. # else
  108. pagefactor = PAGE_CACHE_SIZE >> (PAGE_CACHE_SHIFT - blockshift);
  109. # endif
  110. #endif
  111. return (((__uint64_t)pagefactor) << bitshift) - 1;
  112. }
  113. STATIC_INLINE void
  114. xfs_set_inodeops(
  115. struct inode *inode)
  116. {
  117. switch (inode->i_mode & S_IFMT) {
  118. case S_IFREG:
  119. inode->i_op = &xfs_inode_operations;
  120. inode->i_fop = &xfs_file_operations;
  121. inode->i_mapping->a_ops = &xfs_address_space_operations;
  122. break;
  123. case S_IFDIR:
  124. inode->i_op = &xfs_dir_inode_operations;
  125. inode->i_fop = &xfs_dir_file_operations;
  126. break;
  127. case S_IFLNK:
  128. inode->i_op = &xfs_symlink_inode_operations;
  129. if (inode->i_blocks)
  130. inode->i_mapping->a_ops = &xfs_address_space_operations;
  131. break;
  132. default:
  133. inode->i_op = &xfs_inode_operations;
  134. init_special_inode(inode, inode->i_mode, inode->i_rdev);
  135. break;
  136. }
  137. }
  138. STATIC_INLINE void
  139. xfs_revalidate_inode(
  140. xfs_mount_t *mp,
  141. bhv_vnode_t *vp,
  142. xfs_inode_t *ip)
  143. {
  144. struct inode *inode = vn_to_inode(vp);
  145. inode->i_mode = ip->i_d.di_mode;
  146. inode->i_nlink = ip->i_d.di_nlink;
  147. inode->i_uid = ip->i_d.di_uid;
  148. inode->i_gid = ip->i_d.di_gid;
  149. switch (inode->i_mode & S_IFMT) {
  150. case S_IFBLK:
  151. case S_IFCHR:
  152. inode->i_rdev =
  153. MKDEV(sysv_major(ip->i_df.if_u2.if_rdev) & 0x1ff,
  154. sysv_minor(ip->i_df.if_u2.if_rdev));
  155. break;
  156. default:
  157. inode->i_rdev = 0;
  158. break;
  159. }
  160. inode->i_generation = ip->i_d.di_gen;
  161. i_size_write(inode, ip->i_d.di_size);
  162. inode->i_blocks =
  163. XFS_FSB_TO_BB(mp, ip->i_d.di_nblocks + ip->i_delayed_blks);
  164. inode->i_atime.tv_sec = ip->i_d.di_atime.t_sec;
  165. inode->i_atime.tv_nsec = ip->i_d.di_atime.t_nsec;
  166. inode->i_mtime.tv_sec = ip->i_d.di_mtime.t_sec;
  167. inode->i_mtime.tv_nsec = ip->i_d.di_mtime.t_nsec;
  168. inode->i_ctime.tv_sec = ip->i_d.di_ctime.t_sec;
  169. inode->i_ctime.tv_nsec = ip->i_d.di_ctime.t_nsec;
  170. if (ip->i_d.di_flags & XFS_DIFLAG_IMMUTABLE)
  171. inode->i_flags |= S_IMMUTABLE;
  172. else
  173. inode->i_flags &= ~S_IMMUTABLE;
  174. if (ip->i_d.di_flags & XFS_DIFLAG_APPEND)
  175. inode->i_flags |= S_APPEND;
  176. else
  177. inode->i_flags &= ~S_APPEND;
  178. if (ip->i_d.di_flags & XFS_DIFLAG_SYNC)
  179. inode->i_flags |= S_SYNC;
  180. else
  181. inode->i_flags &= ~S_SYNC;
  182. if (ip->i_d.di_flags & XFS_DIFLAG_NOATIME)
  183. inode->i_flags |= S_NOATIME;
  184. else
  185. inode->i_flags &= ~S_NOATIME;
  186. xfs_iflags_clear(ip, XFS_IMODIFIED);
  187. }
  188. void
  189. xfs_initialize_vnode(
  190. struct xfs_mount *mp,
  191. bhv_vnode_t *vp,
  192. struct xfs_inode *ip)
  193. {
  194. struct inode *inode = vn_to_inode(vp);
  195. if (!ip->i_vnode) {
  196. ip->i_vnode = vp;
  197. inode->i_private = ip;
  198. }
  199. /*
  200. * We need to set the ops vectors, and unlock the inode, but if
  201. * we have been called during the new inode create process, it is
  202. * too early to fill in the Linux inode. We will get called a
  203. * second time once the inode is properly set up, and then we can
  204. * finish our work.
  205. */
  206. if (ip->i_d.di_mode != 0 && (inode->i_state & I_NEW)) {
  207. xfs_revalidate_inode(mp, vp, ip);
  208. xfs_set_inodeops(inode);
  209. xfs_iflags_clear(ip, XFS_INEW);
  210. barrier();
  211. unlock_new_inode(inode);
  212. }
  213. }
  214. int
  215. xfs_blkdev_get(
  216. xfs_mount_t *mp,
  217. const char *name,
  218. struct block_device **bdevp)
  219. {
  220. int error = 0;
  221. *bdevp = open_bdev_excl(name, 0, mp);
  222. if (IS_ERR(*bdevp)) {
  223. error = PTR_ERR(*bdevp);
  224. printk("XFS: Invalid device [%s], error=%d\n", name, error);
  225. }
  226. return -error;
  227. }
  228. void
  229. xfs_blkdev_put(
  230. struct block_device *bdev)
  231. {
  232. if (bdev)
  233. close_bdev_excl(bdev);
  234. }
  235. /*
  236. * Try to write out the superblock using barriers.
  237. */
  238. STATIC int
  239. xfs_barrier_test(
  240. xfs_mount_t *mp)
  241. {
  242. xfs_buf_t *sbp = xfs_getsb(mp, 0);
  243. int error;
  244. XFS_BUF_UNDONE(sbp);
  245. XFS_BUF_UNREAD(sbp);
  246. XFS_BUF_UNDELAYWRITE(sbp);
  247. XFS_BUF_WRITE(sbp);
  248. XFS_BUF_UNASYNC(sbp);
  249. XFS_BUF_ORDERED(sbp);
  250. xfsbdstrat(mp, sbp);
  251. error = xfs_iowait(sbp);
  252. /*
  253. * Clear all the flags we set and possible error state in the
  254. * buffer. We only did the write to try out whether barriers
  255. * worked and shouldn't leave any traces in the superblock
  256. * buffer.
  257. */
  258. XFS_BUF_DONE(sbp);
  259. XFS_BUF_ERROR(sbp, 0);
  260. XFS_BUF_UNORDERED(sbp);
  261. xfs_buf_relse(sbp);
  262. return error;
  263. }
  264. void
  265. xfs_mountfs_check_barriers(xfs_mount_t *mp)
  266. {
  267. int error;
  268. if (mp->m_logdev_targp != mp->m_ddev_targp) {
  269. xfs_fs_cmn_err(CE_NOTE, mp,
  270. "Disabling barriers, not supported with external log device");
  271. mp->m_flags &= ~XFS_MOUNT_BARRIER;
  272. return;
  273. }
  274. if (mp->m_ddev_targp->bt_bdev->bd_disk->queue->ordered ==
  275. QUEUE_ORDERED_NONE) {
  276. xfs_fs_cmn_err(CE_NOTE, mp,
  277. "Disabling barriers, not supported by the underlying device");
  278. mp->m_flags &= ~XFS_MOUNT_BARRIER;
  279. return;
  280. }
  281. if (xfs_readonly_buftarg(mp->m_ddev_targp)) {
  282. xfs_fs_cmn_err(CE_NOTE, mp,
  283. "Disabling barriers, underlying device is readonly");
  284. mp->m_flags &= ~XFS_MOUNT_BARRIER;
  285. return;
  286. }
  287. error = xfs_barrier_test(mp);
  288. if (error) {
  289. xfs_fs_cmn_err(CE_NOTE, mp,
  290. "Disabling barriers, trial barrier write failed");
  291. mp->m_flags &= ~XFS_MOUNT_BARRIER;
  292. return;
  293. }
  294. }
  295. void
  296. xfs_blkdev_issue_flush(
  297. xfs_buftarg_t *buftarg)
  298. {
  299. blkdev_issue_flush(buftarg->bt_bdev, NULL);
  300. }
  301. STATIC struct inode *
  302. xfs_fs_alloc_inode(
  303. struct super_block *sb)
  304. {
  305. bhv_vnode_t *vp;
  306. vp = kmem_zone_alloc(xfs_vnode_zone, KM_SLEEP);
  307. if (unlikely(!vp))
  308. return NULL;
  309. return vn_to_inode(vp);
  310. }
  311. STATIC void
  312. xfs_fs_destroy_inode(
  313. struct inode *inode)
  314. {
  315. kmem_zone_free(xfs_vnode_zone, vn_from_inode(inode));
  316. }
  317. STATIC void
  318. xfs_fs_inode_init_once(
  319. void *vnode,
  320. kmem_zone_t *zonep,
  321. unsigned long flags)
  322. {
  323. inode_init_once(vn_to_inode((bhv_vnode_t *)vnode));
  324. }
  325. STATIC int
  326. xfs_init_zones(void)
  327. {
  328. xfs_vnode_zone = kmem_zone_init_flags(sizeof(bhv_vnode_t), "xfs_vnode",
  329. KM_ZONE_HWALIGN | KM_ZONE_RECLAIM |
  330. KM_ZONE_SPREAD,
  331. xfs_fs_inode_init_once);
  332. if (!xfs_vnode_zone)
  333. goto out;
  334. xfs_ioend_zone = kmem_zone_init(sizeof(xfs_ioend_t), "xfs_ioend");
  335. if (!xfs_ioend_zone)
  336. goto out_destroy_vnode_zone;
  337. xfs_ioend_pool = mempool_create_slab_pool(4 * MAX_BUF_PER_PAGE,
  338. xfs_ioend_zone);
  339. if (!xfs_ioend_pool)
  340. goto out_free_ioend_zone;
  341. return 0;
  342. out_free_ioend_zone:
  343. kmem_zone_destroy(xfs_ioend_zone);
  344. out_destroy_vnode_zone:
  345. kmem_zone_destroy(xfs_vnode_zone);
  346. out:
  347. return -ENOMEM;
  348. }
  349. STATIC void
  350. xfs_destroy_zones(void)
  351. {
  352. mempool_destroy(xfs_ioend_pool);
  353. kmem_zone_destroy(xfs_vnode_zone);
  354. kmem_zone_destroy(xfs_ioend_zone);
  355. }
  356. /*
  357. * Attempt to flush the inode, this will actually fail
  358. * if the inode is pinned, but we dirty the inode again
  359. * at the point when it is unpinned after a log write,
  360. * since this is when the inode itself becomes flushable.
  361. */
  362. STATIC int
  363. xfs_fs_write_inode(
  364. struct inode *inode,
  365. int sync)
  366. {
  367. int error = 0, flags = FLUSH_INODE;
  368. vn_trace_entry(XFS_I(inode), __FUNCTION__,
  369. (inst_t *)__return_address);
  370. if (sync) {
  371. filemap_fdatawait(inode->i_mapping);
  372. flags |= FLUSH_SYNC;
  373. }
  374. error = xfs_inode_flush(XFS_I(inode), flags);
  375. /*
  376. * if we failed to write out the inode then mark
  377. * it dirty again so we'll try again later.
  378. */
  379. if (error)
  380. mark_inode_dirty_sync(inode);
  381. return -error;
  382. }
  383. STATIC void
  384. xfs_fs_clear_inode(
  385. struct inode *inode)
  386. {
  387. xfs_inode_t *ip = XFS_I(inode);
  388. /*
  389. * ip can be null when xfs_iget_core calls xfs_idestroy if we
  390. * find an inode with di_mode == 0 but without IGET_CREATE set.
  391. */
  392. if (ip) {
  393. vn_trace_entry(ip, __FUNCTION__, (inst_t *)__return_address);
  394. XFS_STATS_INC(vn_rele);
  395. XFS_STATS_INC(vn_remove);
  396. XFS_STATS_INC(vn_reclaim);
  397. XFS_STATS_DEC(vn_active);
  398. xfs_inactive(ip);
  399. xfs_iflags_clear(ip, XFS_IMODIFIED);
  400. if (xfs_reclaim(ip))
  401. panic("%s: cannot reclaim 0x%p\n", __FUNCTION__, inode);
  402. }
  403. ASSERT(XFS_I(inode) == NULL);
  404. }
  405. /*
  406. * Enqueue a work item to be picked up by the vfs xfssyncd thread.
  407. * Doing this has two advantages:
  408. * - It saves on stack space, which is tight in certain situations
  409. * - It can be used (with care) as a mechanism to avoid deadlocks.
  410. * Flushing while allocating in a full filesystem requires both.
  411. */
  412. STATIC void
  413. xfs_syncd_queue_work(
  414. struct xfs_mount *mp,
  415. void *data,
  416. void (*syncer)(struct xfs_mount *, void *))
  417. {
  418. struct bhv_vfs_sync_work *work;
  419. work = kmem_alloc(sizeof(struct bhv_vfs_sync_work), KM_SLEEP);
  420. INIT_LIST_HEAD(&work->w_list);
  421. work->w_syncer = syncer;
  422. work->w_data = data;
  423. work->w_mount = mp;
  424. spin_lock(&mp->m_sync_lock);
  425. list_add_tail(&work->w_list, &mp->m_sync_list);
  426. spin_unlock(&mp->m_sync_lock);
  427. wake_up_process(mp->m_sync_task);
  428. }
  429. /*
  430. * Flush delayed allocate data, attempting to free up reserved space
  431. * from existing allocations. At this point a new allocation attempt
  432. * has failed with ENOSPC and we are in the process of scratching our
  433. * heads, looking about for more room...
  434. */
  435. STATIC void
  436. xfs_flush_inode_work(
  437. struct xfs_mount *mp,
  438. void *arg)
  439. {
  440. struct inode *inode = arg;
  441. filemap_flush(inode->i_mapping);
  442. iput(inode);
  443. }
  444. void
  445. xfs_flush_inode(
  446. xfs_inode_t *ip)
  447. {
  448. struct inode *inode = ip->i_vnode;
  449. igrab(inode);
  450. xfs_syncd_queue_work(ip->i_mount, inode, xfs_flush_inode_work);
  451. delay(msecs_to_jiffies(500));
  452. }
  453. /*
  454. * This is the "bigger hammer" version of xfs_flush_inode_work...
  455. * (IOW, "If at first you don't succeed, use a Bigger Hammer").
  456. */
  457. STATIC void
  458. xfs_flush_device_work(
  459. struct xfs_mount *mp,
  460. void *arg)
  461. {
  462. struct inode *inode = arg;
  463. sync_blockdev(mp->m_super->s_bdev);
  464. iput(inode);
  465. }
  466. void
  467. xfs_flush_device(
  468. xfs_inode_t *ip)
  469. {
  470. struct inode *inode = vn_to_inode(XFS_ITOV(ip));
  471. igrab(inode);
  472. xfs_syncd_queue_work(ip->i_mount, inode, xfs_flush_device_work);
  473. delay(msecs_to_jiffies(500));
  474. xfs_log_force(ip->i_mount, (xfs_lsn_t)0, XFS_LOG_FORCE|XFS_LOG_SYNC);
  475. }
  476. STATIC void
  477. xfs_sync_worker(
  478. struct xfs_mount *mp,
  479. void *unused)
  480. {
  481. int error;
  482. if (!(mp->m_flags & XFS_MOUNT_RDONLY))
  483. error = xfs_sync(mp, SYNC_FSDATA | SYNC_BDFLUSH | SYNC_ATTR |
  484. SYNC_REFCACHE | SYNC_SUPER);
  485. mp->m_sync_seq++;
  486. wake_up(&mp->m_wait_single_sync_task);
  487. }
  488. STATIC int
  489. xfssyncd(
  490. void *arg)
  491. {
  492. struct xfs_mount *mp = arg;
  493. long timeleft;
  494. bhv_vfs_sync_work_t *work, *n;
  495. LIST_HEAD (tmp);
  496. set_freezable();
  497. timeleft = xfs_syncd_centisecs * msecs_to_jiffies(10);
  498. for (;;) {
  499. timeleft = schedule_timeout_interruptible(timeleft);
  500. /* swsusp */
  501. try_to_freeze();
  502. if (kthread_should_stop() && list_empty(&mp->m_sync_list))
  503. break;
  504. spin_lock(&mp->m_sync_lock);
  505. /*
  506. * We can get woken by laptop mode, to do a sync -
  507. * that's the (only!) case where the list would be
  508. * empty with time remaining.
  509. */
  510. if (!timeleft || list_empty(&mp->m_sync_list)) {
  511. if (!timeleft)
  512. timeleft = xfs_syncd_centisecs *
  513. msecs_to_jiffies(10);
  514. INIT_LIST_HEAD(&mp->m_sync_work.w_list);
  515. list_add_tail(&mp->m_sync_work.w_list,
  516. &mp->m_sync_list);
  517. }
  518. list_for_each_entry_safe(work, n, &mp->m_sync_list, w_list)
  519. list_move(&work->w_list, &tmp);
  520. spin_unlock(&mp->m_sync_lock);
  521. list_for_each_entry_safe(work, n, &tmp, w_list) {
  522. (*work->w_syncer)(mp, work->w_data);
  523. list_del(&work->w_list);
  524. if (work == &mp->m_sync_work)
  525. continue;
  526. kmem_free(work, sizeof(struct bhv_vfs_sync_work));
  527. }
  528. }
  529. return 0;
  530. }
  531. STATIC void
  532. xfs_fs_put_super(
  533. struct super_block *sb)
  534. {
  535. struct xfs_mount *mp = XFS_M(sb);
  536. int error;
  537. kthread_stop(mp->m_sync_task);
  538. xfs_sync(mp, SYNC_ATTR | SYNC_DELWRI);
  539. error = xfs_unmount(mp, 0, NULL);
  540. if (error)
  541. printk("XFS: unmount got error=%d\n", error);
  542. }
  543. STATIC void
  544. xfs_fs_write_super(
  545. struct super_block *sb)
  546. {
  547. if (!(sb->s_flags & MS_RDONLY))
  548. xfs_sync(XFS_M(sb), SYNC_FSDATA);
  549. sb->s_dirt = 0;
  550. }
  551. STATIC int
  552. xfs_fs_sync_super(
  553. struct super_block *sb,
  554. int wait)
  555. {
  556. struct xfs_mount *mp = XFS_M(sb);
  557. int error;
  558. int flags;
  559. /*
  560. * Treat a sync operation like a freeze. This is to work
  561. * around a race in sync_inodes() which works in two phases
  562. * - an asynchronous flush, which can write out an inode
  563. * without waiting for file size updates to complete, and a
  564. * synchronous flush, which wont do anything because the
  565. * async flush removed the inode's dirty flag. Also
  566. * sync_inodes() will not see any files that just have
  567. * outstanding transactions to be flushed because we don't
  568. * dirty the Linux inode until after the transaction I/O
  569. * completes.
  570. */
  571. if (wait || unlikely(sb->s_frozen == SB_FREEZE_WRITE)) {
  572. /*
  573. * First stage of freeze - no more writers will make progress
  574. * now we are here, so we flush delwri and delalloc buffers
  575. * here, then wait for all I/O to complete. Data is frozen at
  576. * that point. Metadata is not frozen, transactions can still
  577. * occur here so don't bother flushing the buftarg (i.e
  578. * SYNC_QUIESCE) because it'll just get dirty again.
  579. */
  580. flags = SYNC_DATA_QUIESCE;
  581. } else
  582. flags = SYNC_FSDATA;
  583. error = xfs_sync(mp, flags);
  584. sb->s_dirt = 0;
  585. if (unlikely(laptop_mode)) {
  586. int prev_sync_seq = mp->m_sync_seq;
  587. /*
  588. * The disk must be active because we're syncing.
  589. * We schedule xfssyncd now (now that the disk is
  590. * active) instead of later (when it might not be).
  591. */
  592. wake_up_process(mp->m_sync_task);
  593. /*
  594. * We have to wait for the sync iteration to complete.
  595. * If we don't, the disk activity caused by the sync
  596. * will come after the sync is completed, and that
  597. * triggers another sync from laptop mode.
  598. */
  599. wait_event(mp->m_wait_single_sync_task,
  600. mp->m_sync_seq != prev_sync_seq);
  601. }
  602. return -error;
  603. }
  604. STATIC int
  605. xfs_fs_statfs(
  606. struct dentry *dentry,
  607. struct kstatfs *statp)
  608. {
  609. return -xfs_statvfs(XFS_M(dentry->d_sb), statp,
  610. vn_from_inode(dentry->d_inode));
  611. }
  612. STATIC int
  613. xfs_fs_remount(
  614. struct super_block *sb,
  615. int *flags,
  616. char *options)
  617. {
  618. struct xfs_mount *mp = XFS_M(sb);
  619. struct xfs_mount_args *args = xfs_args_allocate(sb, 0);
  620. int error;
  621. error = xfs_parseargs(mp, options, args, 1);
  622. if (!error)
  623. error = xfs_mntupdate(mp, flags, args);
  624. kmem_free(args, sizeof(*args));
  625. return -error;
  626. }
  627. STATIC void
  628. xfs_fs_lockfs(
  629. struct super_block *sb)
  630. {
  631. xfs_freeze(XFS_M(sb));
  632. }
  633. STATIC int
  634. xfs_fs_show_options(
  635. struct seq_file *m,
  636. struct vfsmount *mnt)
  637. {
  638. return -xfs_showargs(XFS_M(mnt->mnt_sb), m);
  639. }
  640. STATIC int
  641. xfs_fs_quotasync(
  642. struct super_block *sb,
  643. int type)
  644. {
  645. return -XFS_QM_QUOTACTL(XFS_M(sb), Q_XQUOTASYNC, 0, NULL);
  646. }
  647. STATIC int
  648. xfs_fs_getxstate(
  649. struct super_block *sb,
  650. struct fs_quota_stat *fqs)
  651. {
  652. return -XFS_QM_QUOTACTL(XFS_M(sb), Q_XGETQSTAT, 0, (caddr_t)fqs);
  653. }
  654. STATIC int
  655. xfs_fs_setxstate(
  656. struct super_block *sb,
  657. unsigned int flags,
  658. int op)
  659. {
  660. return -XFS_QM_QUOTACTL(XFS_M(sb), op, 0, (caddr_t)&flags);
  661. }
  662. STATIC int
  663. xfs_fs_getxquota(
  664. struct super_block *sb,
  665. int type,
  666. qid_t id,
  667. struct fs_disk_quota *fdq)
  668. {
  669. return -XFS_QM_QUOTACTL(XFS_M(sb),
  670. (type == USRQUOTA) ? Q_XGETQUOTA :
  671. ((type == GRPQUOTA) ? Q_XGETGQUOTA :
  672. Q_XGETPQUOTA), id, (caddr_t)fdq);
  673. }
  674. STATIC int
  675. xfs_fs_setxquota(
  676. struct super_block *sb,
  677. int type,
  678. qid_t id,
  679. struct fs_disk_quota *fdq)
  680. {
  681. return -XFS_QM_QUOTACTL(XFS_M(sb),
  682. (type == USRQUOTA) ? Q_XSETQLIM :
  683. ((type == GRPQUOTA) ? Q_XSETGQLIM :
  684. Q_XSETPQLIM), id, (caddr_t)fdq);
  685. }
  686. STATIC int
  687. xfs_fs_fill_super(
  688. struct super_block *sb,
  689. void *data,
  690. int silent)
  691. {
  692. struct inode *rootvp;
  693. struct xfs_mount *mp = NULL;
  694. struct xfs_mount_args *args = xfs_args_allocate(sb, silent);
  695. struct kstatfs statvfs;
  696. int error;
  697. mp = xfs_mount_init();
  698. INIT_LIST_HEAD(&mp->m_sync_list);
  699. spin_lock_init(&mp->m_sync_lock);
  700. init_waitqueue_head(&mp->m_wait_single_sync_task);
  701. mp->m_super = sb;
  702. sb->s_fs_info = mp;
  703. if (sb->s_flags & MS_RDONLY)
  704. mp->m_flags |= XFS_MOUNT_RDONLY;
  705. error = xfs_parseargs(mp, (char *)data, args, 0);
  706. if (error)
  707. goto fail_vfsop;
  708. sb_min_blocksize(sb, BBSIZE);
  709. sb->s_export_op = &xfs_export_operations;
  710. sb->s_qcop = &xfs_quotactl_operations;
  711. sb->s_op = &xfs_super_operations;
  712. error = xfs_mount(mp, args, NULL);
  713. if (error)
  714. goto fail_vfsop;
  715. error = xfs_statvfs(mp, &statvfs, NULL);
  716. if (error)
  717. goto fail_unmount;
  718. sb->s_dirt = 1;
  719. sb->s_magic = statvfs.f_type;
  720. sb->s_blocksize = statvfs.f_bsize;
  721. sb->s_blocksize_bits = ffs(statvfs.f_bsize) - 1;
  722. sb->s_maxbytes = xfs_max_file_offset(sb->s_blocksize_bits);
  723. sb->s_time_gran = 1;
  724. set_posix_acl_flag(sb);
  725. error = xfs_root(mp, &rootvp);
  726. if (error)
  727. goto fail_unmount;
  728. sb->s_root = d_alloc_root(vn_to_inode(rootvp));
  729. if (!sb->s_root) {
  730. error = ENOMEM;
  731. goto fail_vnrele;
  732. }
  733. if (is_bad_inode(sb->s_root->d_inode)) {
  734. error = EINVAL;
  735. goto fail_vnrele;
  736. }
  737. mp->m_sync_work.w_syncer = xfs_sync_worker;
  738. mp->m_sync_work.w_mount = mp;
  739. mp->m_sync_task = kthread_run(xfssyncd, mp, "xfssyncd");
  740. if (IS_ERR(mp->m_sync_task)) {
  741. error = -PTR_ERR(mp->m_sync_task);
  742. goto fail_vnrele;
  743. }
  744. vn_trace_exit(XFS_I(sb->s_root->d_inode), __FUNCTION__,
  745. (inst_t *)__return_address);
  746. kmem_free(args, sizeof(*args));
  747. return 0;
  748. fail_vnrele:
  749. if (sb->s_root) {
  750. dput(sb->s_root);
  751. sb->s_root = NULL;
  752. } else {
  753. VN_RELE(rootvp);
  754. }
  755. fail_unmount:
  756. xfs_unmount(mp, 0, NULL);
  757. fail_vfsop:
  758. kmem_free(args, sizeof(*args));
  759. return -error;
  760. }
  761. STATIC int
  762. xfs_fs_get_sb(
  763. struct file_system_type *fs_type,
  764. int flags,
  765. const char *dev_name,
  766. void *data,
  767. struct vfsmount *mnt)
  768. {
  769. return get_sb_bdev(fs_type, flags, dev_name, data, xfs_fs_fill_super,
  770. mnt);
  771. }
  772. static struct super_operations xfs_super_operations = {
  773. .alloc_inode = xfs_fs_alloc_inode,
  774. .destroy_inode = xfs_fs_destroy_inode,
  775. .write_inode = xfs_fs_write_inode,
  776. .clear_inode = xfs_fs_clear_inode,
  777. .put_super = xfs_fs_put_super,
  778. .write_super = xfs_fs_write_super,
  779. .sync_fs = xfs_fs_sync_super,
  780. .write_super_lockfs = xfs_fs_lockfs,
  781. .statfs = xfs_fs_statfs,
  782. .remount_fs = xfs_fs_remount,
  783. .show_options = xfs_fs_show_options,
  784. };
  785. static struct quotactl_ops xfs_quotactl_operations = {
  786. .quota_sync = xfs_fs_quotasync,
  787. .get_xstate = xfs_fs_getxstate,
  788. .set_xstate = xfs_fs_setxstate,
  789. .get_xquota = xfs_fs_getxquota,
  790. .set_xquota = xfs_fs_setxquota,
  791. };
  792. static struct file_system_type xfs_fs_type = {
  793. .owner = THIS_MODULE,
  794. .name = "xfs",
  795. .get_sb = xfs_fs_get_sb,
  796. .kill_sb = kill_block_super,
  797. .fs_flags = FS_REQUIRES_DEV,
  798. };
  799. STATIC int __init
  800. init_xfs_fs( void )
  801. {
  802. int error;
  803. static char message[] __initdata = KERN_INFO \
  804. XFS_VERSION_STRING " with " XFS_BUILD_OPTIONS " enabled\n";
  805. printk(message);
  806. ktrace_init(64);
  807. error = xfs_init_zones();
  808. if (error < 0)
  809. goto undo_zones;
  810. error = xfs_buf_init();
  811. if (error < 0)
  812. goto undo_buffers;
  813. vn_init();
  814. xfs_init();
  815. uuid_init();
  816. vfs_initquota();
  817. error = register_filesystem(&xfs_fs_type);
  818. if (error)
  819. goto undo_register;
  820. return 0;
  821. undo_register:
  822. xfs_buf_terminate();
  823. undo_buffers:
  824. xfs_destroy_zones();
  825. undo_zones:
  826. return error;
  827. }
  828. STATIC void __exit
  829. exit_xfs_fs( void )
  830. {
  831. vfs_exitquota();
  832. unregister_filesystem(&xfs_fs_type);
  833. xfs_cleanup();
  834. xfs_buf_terminate();
  835. xfs_destroy_zones();
  836. ktrace_uninit();
  837. }
  838. module_init(init_xfs_fs);
  839. module_exit(exit_xfs_fs);
  840. MODULE_AUTHOR("Silicon Graphics, Inc.");
  841. MODULE_DESCRIPTION(XFS_VERSION_STRING " with " XFS_BUILD_OPTIONS " enabled");
  842. MODULE_LICENSE("GPL");