xfs_super.c 22 KB

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