xfs_super.c 22 KB

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