xfs_super.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964
  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_version.h"
  51. #include <linux/namei.h>
  52. #include <linux/init.h>
  53. #include <linux/mount.h>
  54. #include <linux/mempool.h>
  55. #include <linux/writeback.h>
  56. #include <linux/kthread.h>
  57. #include <linux/freezer.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_generation = ip->i_d.di_gen;
  160. i_size_write(inode, ip->i_d.di_size);
  161. inode->i_blocks =
  162. XFS_FSB_TO_BB(mp, ip->i_d.di_nblocks + ip->i_delayed_blks);
  163. inode->i_atime.tv_sec = ip->i_d.di_atime.t_sec;
  164. inode->i_atime.tv_nsec = ip->i_d.di_atime.t_nsec;
  165. inode->i_mtime.tv_sec = ip->i_d.di_mtime.t_sec;
  166. inode->i_mtime.tv_nsec = ip->i_d.di_mtime.t_nsec;
  167. inode->i_ctime.tv_sec = ip->i_d.di_ctime.t_sec;
  168. inode->i_ctime.tv_nsec = ip->i_d.di_ctime.t_nsec;
  169. if (ip->i_d.di_flags & XFS_DIFLAG_IMMUTABLE)
  170. inode->i_flags |= S_IMMUTABLE;
  171. else
  172. inode->i_flags &= ~S_IMMUTABLE;
  173. if (ip->i_d.di_flags & XFS_DIFLAG_APPEND)
  174. inode->i_flags |= S_APPEND;
  175. else
  176. inode->i_flags &= ~S_APPEND;
  177. if (ip->i_d.di_flags & XFS_DIFLAG_SYNC)
  178. inode->i_flags |= S_SYNC;
  179. else
  180. inode->i_flags &= ~S_SYNC;
  181. if (ip->i_d.di_flags & XFS_DIFLAG_NOATIME)
  182. inode->i_flags |= S_NOATIME;
  183. else
  184. inode->i_flags &= ~S_NOATIME;
  185. vp->v_flag &= ~VMODIFIED;
  186. }
  187. void
  188. xfs_initialize_vnode(
  189. bhv_desc_t *bdp,
  190. bhv_vnode_t *vp,
  191. struct xfs_inode *ip,
  192. int unlock)
  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 && unlock && (inode->i_state & I_NEW)) {
  207. xfs_revalidate_inode(XFS_BHVTOM(bdp), 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 (xfs_readonly_buftarg(mp->m_ddev_targp)) {
  275. xfs_fs_cmn_err(CE_NOTE, mp,
  276. "Disabling barriers, underlying device is readonly");
  277. mp->m_flags &= ~XFS_MOUNT_BARRIER;
  278. return;
  279. }
  280. error = xfs_barrier_test(mp);
  281. if (error) {
  282. xfs_fs_cmn_err(CE_NOTE, mp,
  283. "Disabling barriers, trial barrier write failed");
  284. mp->m_flags &= ~XFS_MOUNT_BARRIER;
  285. return;
  286. }
  287. }
  288. void
  289. xfs_blkdev_issue_flush(
  290. xfs_buftarg_t *buftarg)
  291. {
  292. blkdev_issue_flush(buftarg->bt_bdev, NULL);
  293. }
  294. STATIC struct inode *
  295. xfs_fs_alloc_inode(
  296. struct super_block *sb)
  297. {
  298. bhv_vnode_t *vp;
  299. vp = kmem_zone_alloc(xfs_vnode_zone, KM_SLEEP);
  300. if (unlikely(!vp))
  301. return NULL;
  302. return vn_to_inode(vp);
  303. }
  304. STATIC void
  305. xfs_fs_destroy_inode(
  306. struct inode *inode)
  307. {
  308. kmem_zone_free(xfs_vnode_zone, vn_from_inode(inode));
  309. }
  310. STATIC void
  311. xfs_fs_inode_init_once(
  312. void *vnode,
  313. kmem_zone_t *zonep,
  314. unsigned long flags)
  315. {
  316. inode_init_once(vn_to_inode((bhv_vnode_t *)vnode));
  317. }
  318. STATIC int
  319. xfs_init_zones(void)
  320. {
  321. xfs_vnode_zone = kmem_zone_init_flags(sizeof(bhv_vnode_t), "xfs_vnode",
  322. KM_ZONE_HWALIGN | KM_ZONE_RECLAIM |
  323. KM_ZONE_SPREAD,
  324. xfs_fs_inode_init_once);
  325. if (!xfs_vnode_zone)
  326. goto out;
  327. xfs_ioend_zone = kmem_zone_init(sizeof(xfs_ioend_t), "xfs_ioend");
  328. if (!xfs_ioend_zone)
  329. goto out_destroy_vnode_zone;
  330. xfs_ioend_pool = mempool_create_slab_pool(4 * MAX_BUF_PER_PAGE,
  331. xfs_ioend_zone);
  332. if (!xfs_ioend_pool)
  333. goto out_free_ioend_zone;
  334. return 0;
  335. out_free_ioend_zone:
  336. kmem_zone_destroy(xfs_ioend_zone);
  337. out_destroy_vnode_zone:
  338. kmem_zone_destroy(xfs_vnode_zone);
  339. out:
  340. return -ENOMEM;
  341. }
  342. STATIC void
  343. xfs_destroy_zones(void)
  344. {
  345. mempool_destroy(xfs_ioend_pool);
  346. kmem_zone_destroy(xfs_vnode_zone);
  347. kmem_zone_destroy(xfs_ioend_zone);
  348. }
  349. /*
  350. * Attempt to flush the inode, this will actually fail
  351. * if the inode is pinned, but we dirty the inode again
  352. * at the point when it is unpinned after a log write,
  353. * since this is when the inode itself becomes flushable.
  354. */
  355. STATIC int
  356. xfs_fs_write_inode(
  357. struct inode *inode,
  358. int sync)
  359. {
  360. int error = 0, flags = FLUSH_INODE;
  361. vn_trace_entry(vn_from_inode(inode), __FUNCTION__,
  362. (inst_t *)__return_address);
  363. if (sync) {
  364. filemap_fdatawait(inode->i_mapping);
  365. flags |= FLUSH_SYNC;
  366. }
  367. error = xfs_inode_flush(XFS_I(inode), flags);
  368. if (error == EAGAIN) {
  369. if (sync)
  370. error = xfs_inode_flush(XFS_I(inode),
  371. flags | FLUSH_LOG);
  372. else
  373. error = 0;
  374. }
  375. return -error;
  376. }
  377. STATIC void
  378. xfs_fs_clear_inode(
  379. struct inode *inode)
  380. {
  381. bhv_vnode_t *vp = vn_from_inode(inode);
  382. vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
  383. XFS_STATS_INC(vn_rele);
  384. XFS_STATS_INC(vn_remove);
  385. XFS_STATS_INC(vn_reclaim);
  386. XFS_STATS_DEC(vn_active);
  387. /*
  388. * This can happen because xfs_iget_core calls xfs_idestroy if we
  389. * find an inode with di_mode == 0 but without IGET_CREATE set.
  390. */
  391. if (XFS_I(inode))
  392. xfs_inactive(XFS_I(inode));
  393. VN_LOCK(vp);
  394. vp->v_flag &= ~VMODIFIED;
  395. VN_UNLOCK(vp, 0);
  396. if (XFS_I(inode))
  397. if (xfs_reclaim(XFS_I(inode)))
  398. panic("%s: cannot reclaim 0x%p\n", __FUNCTION__, vp);
  399. ASSERT(XFS_I(inode) == NULL);
  400. #ifdef XFS_VNODE_TRACE
  401. ktrace_free(vp->v_trace);
  402. #endif
  403. }
  404. /*
  405. * Enqueue a work item to be picked up by the vfs xfssyncd thread.
  406. * Doing this has two advantages:
  407. * - It saves on stack space, which is tight in certain situations
  408. * - It can be used (with care) as a mechanism to avoid deadlocks.
  409. * Flushing while allocating in a full filesystem requires both.
  410. */
  411. STATIC void
  412. xfs_syncd_queue_work(
  413. struct bhv_vfs *vfs,
  414. void *data,
  415. void (*syncer)(bhv_vfs_t *, void *))
  416. {
  417. struct bhv_vfs_sync_work *work;
  418. work = kmem_alloc(sizeof(struct bhv_vfs_sync_work), KM_SLEEP);
  419. INIT_LIST_HEAD(&work->w_list);
  420. work->w_syncer = syncer;
  421. work->w_data = data;
  422. work->w_vfs = vfs;
  423. spin_lock(&vfs->vfs_sync_lock);
  424. list_add_tail(&work->w_list, &vfs->vfs_sync_list);
  425. spin_unlock(&vfs->vfs_sync_lock);
  426. wake_up_process(vfs->vfs_sync_task);
  427. }
  428. /*
  429. * Flush delayed allocate data, attempting to free up reserved space
  430. * from existing allocations. At this point a new allocation attempt
  431. * has failed with ENOSPC and we are in the process of scratching our
  432. * heads, looking about for more room...
  433. */
  434. STATIC void
  435. xfs_flush_inode_work(
  436. bhv_vfs_t *vfs,
  437. void *inode)
  438. {
  439. filemap_flush(((struct inode *)inode)->i_mapping);
  440. iput((struct inode *)inode);
  441. }
  442. void
  443. xfs_flush_inode(
  444. xfs_inode_t *ip)
  445. {
  446. struct inode *inode = vn_to_inode(XFS_ITOV(ip));
  447. struct bhv_vfs *vfs = XFS_MTOVFS(ip->i_mount);
  448. igrab(inode);
  449. xfs_syncd_queue_work(vfs, inode, xfs_flush_inode_work);
  450. delay(msecs_to_jiffies(500));
  451. }
  452. /*
  453. * This is the "bigger hammer" version of xfs_flush_inode_work...
  454. * (IOW, "If at first you don't succeed, use a Bigger Hammer").
  455. */
  456. STATIC void
  457. xfs_flush_device_work(
  458. bhv_vfs_t *vfs,
  459. void *inode)
  460. {
  461. sync_blockdev(vfs->vfs_super->s_bdev);
  462. iput((struct inode *)inode);
  463. }
  464. void
  465. xfs_flush_device(
  466. xfs_inode_t *ip)
  467. {
  468. struct inode *inode = vn_to_inode(XFS_ITOV(ip));
  469. struct bhv_vfs *vfs = XFS_MTOVFS(ip->i_mount);
  470. igrab(inode);
  471. xfs_syncd_queue_work(vfs, inode, xfs_flush_device_work);
  472. delay(msecs_to_jiffies(500));
  473. xfs_log_force(ip->i_mount, (xfs_lsn_t)0, XFS_LOG_FORCE|XFS_LOG_SYNC);
  474. }
  475. STATIC void
  476. vfs_sync_worker(
  477. bhv_vfs_t *vfsp,
  478. void *unused)
  479. {
  480. int error;
  481. if (!(vfsp->vfs_flag & VFS_RDONLY))
  482. error = bhv_vfs_sync(vfsp, SYNC_FSDATA | SYNC_BDFLUSH | \
  483. SYNC_ATTR | SYNC_REFCACHE | SYNC_SUPER,
  484. NULL);
  485. vfsp->vfs_sync_seq++;
  486. wake_up(&vfsp->vfs_wait_single_sync_task);
  487. }
  488. STATIC int
  489. xfssyncd(
  490. void *arg)
  491. {
  492. long timeleft;
  493. bhv_vfs_t *vfsp = (bhv_vfs_t *) arg;
  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(&vfsp->vfs_sync_list))
  503. break;
  504. spin_lock(&vfsp->vfs_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(&vfsp->vfs_sync_list)) {
  511. if (!timeleft)
  512. timeleft = xfs_syncd_centisecs *
  513. msecs_to_jiffies(10);
  514. INIT_LIST_HEAD(&vfsp->vfs_sync_work.w_list);
  515. list_add_tail(&vfsp->vfs_sync_work.w_list,
  516. &vfsp->vfs_sync_list);
  517. }
  518. list_for_each_entry_safe(work, n, &vfsp->vfs_sync_list, w_list)
  519. list_move(&work->w_list, &tmp);
  520. spin_unlock(&vfsp->vfs_sync_lock);
  521. list_for_each_entry_safe(work, n, &tmp, w_list) {
  522. (*work->w_syncer)(vfsp, work->w_data);
  523. list_del(&work->w_list);
  524. if (work == &vfsp->vfs_sync_work)
  525. continue;
  526. kmem_free(work, sizeof(struct bhv_vfs_sync_work));
  527. }
  528. }
  529. return 0;
  530. }
  531. STATIC int
  532. xfs_fs_start_syncd(
  533. bhv_vfs_t *vfsp)
  534. {
  535. vfsp->vfs_sync_work.w_syncer = vfs_sync_worker;
  536. vfsp->vfs_sync_work.w_vfs = vfsp;
  537. vfsp->vfs_sync_task = kthread_run(xfssyncd, vfsp, "xfssyncd");
  538. if (IS_ERR(vfsp->vfs_sync_task))
  539. return -PTR_ERR(vfsp->vfs_sync_task);
  540. return 0;
  541. }
  542. STATIC void
  543. xfs_fs_stop_syncd(
  544. bhv_vfs_t *vfsp)
  545. {
  546. kthread_stop(vfsp->vfs_sync_task);
  547. }
  548. STATIC void
  549. xfs_fs_put_super(
  550. struct super_block *sb)
  551. {
  552. bhv_vfs_t *vfsp = vfs_from_sb(sb);
  553. int error;
  554. xfs_fs_stop_syncd(vfsp);
  555. bhv_vfs_sync(vfsp, SYNC_ATTR | SYNC_DELWRI, NULL);
  556. error = bhv_vfs_unmount(vfsp, 0, NULL);
  557. if (error) {
  558. printk("XFS: unmount got error=%d\n", error);
  559. printk("%s: vfs=0x%p left dangling!\n", __FUNCTION__, vfsp);
  560. } else {
  561. vfs_deallocate(vfsp);
  562. }
  563. }
  564. STATIC void
  565. xfs_fs_write_super(
  566. struct super_block *sb)
  567. {
  568. if (!(sb->s_flags & MS_RDONLY))
  569. bhv_vfs_sync(vfs_from_sb(sb), SYNC_FSDATA, NULL);
  570. sb->s_dirt = 0;
  571. }
  572. STATIC int
  573. xfs_fs_sync_super(
  574. struct super_block *sb,
  575. int wait)
  576. {
  577. bhv_vfs_t *vfsp = vfs_from_sb(sb);
  578. int error;
  579. int flags;
  580. if (unlikely(sb->s_frozen == SB_FREEZE_WRITE)) {
  581. /*
  582. * First stage of freeze - no more writers will make progress
  583. * now we are here, so we flush delwri and delalloc buffers
  584. * here, then wait for all I/O to complete. Data is frozen at
  585. * that point. Metadata is not frozen, transactions can still
  586. * occur here so don't bother flushing the buftarg (i.e
  587. * SYNC_QUIESCE) because it'll just get dirty again.
  588. */
  589. flags = SYNC_DATA_QUIESCE;
  590. } else
  591. flags = SYNC_FSDATA | (wait ? SYNC_WAIT : 0);
  592. error = bhv_vfs_sync(vfsp, flags, NULL);
  593. sb->s_dirt = 0;
  594. if (unlikely(laptop_mode)) {
  595. int prev_sync_seq = vfsp->vfs_sync_seq;
  596. /*
  597. * The disk must be active because we're syncing.
  598. * We schedule xfssyncd now (now that the disk is
  599. * active) instead of later (when it might not be).
  600. */
  601. wake_up_process(vfsp->vfs_sync_task);
  602. /*
  603. * We have to wait for the sync iteration to complete.
  604. * If we don't, the disk activity caused by the sync
  605. * will come after the sync is completed, and that
  606. * triggers another sync from laptop mode.
  607. */
  608. wait_event(vfsp->vfs_wait_single_sync_task,
  609. vfsp->vfs_sync_seq != prev_sync_seq);
  610. }
  611. return -error;
  612. }
  613. STATIC int
  614. xfs_fs_statfs(
  615. struct dentry *dentry,
  616. struct kstatfs *statp)
  617. {
  618. return -bhv_vfs_statvfs(vfs_from_sb(dentry->d_sb), statp,
  619. vn_from_inode(dentry->d_inode));
  620. }
  621. STATIC int
  622. xfs_fs_remount(
  623. struct super_block *sb,
  624. int *flags,
  625. char *options)
  626. {
  627. bhv_vfs_t *vfsp = vfs_from_sb(sb);
  628. struct xfs_mount_args *args = xfs_args_allocate(sb, 0);
  629. int error;
  630. error = bhv_vfs_parseargs(vfsp, options, args, 1);
  631. if (!error)
  632. error = bhv_vfs_mntupdate(vfsp, flags, args);
  633. kmem_free(args, sizeof(*args));
  634. return -error;
  635. }
  636. STATIC void
  637. xfs_fs_lockfs(
  638. struct super_block *sb)
  639. {
  640. bhv_vfs_freeze(vfs_from_sb(sb));
  641. }
  642. STATIC int
  643. xfs_fs_show_options(
  644. struct seq_file *m,
  645. struct vfsmount *mnt)
  646. {
  647. return -bhv_vfs_showargs(vfs_from_sb(mnt->mnt_sb), m);
  648. }
  649. STATIC int
  650. xfs_fs_quotasync(
  651. struct super_block *sb,
  652. int type)
  653. {
  654. return -bhv_vfs_quotactl(vfs_from_sb(sb), Q_XQUOTASYNC, 0, NULL);
  655. }
  656. STATIC int
  657. xfs_fs_getxstate(
  658. struct super_block *sb,
  659. struct fs_quota_stat *fqs)
  660. {
  661. return -bhv_vfs_quotactl(vfs_from_sb(sb), Q_XGETQSTAT, 0, (caddr_t)fqs);
  662. }
  663. STATIC int
  664. xfs_fs_setxstate(
  665. struct super_block *sb,
  666. unsigned int flags,
  667. int op)
  668. {
  669. return -bhv_vfs_quotactl(vfs_from_sb(sb), op, 0, (caddr_t)&flags);
  670. }
  671. STATIC int
  672. xfs_fs_getxquota(
  673. struct super_block *sb,
  674. int type,
  675. qid_t id,
  676. struct fs_disk_quota *fdq)
  677. {
  678. return -bhv_vfs_quotactl(vfs_from_sb(sb),
  679. (type == USRQUOTA) ? Q_XGETQUOTA :
  680. ((type == GRPQUOTA) ? Q_XGETGQUOTA :
  681. Q_XGETPQUOTA), id, (caddr_t)fdq);
  682. }
  683. STATIC int
  684. xfs_fs_setxquota(
  685. struct super_block *sb,
  686. int type,
  687. qid_t id,
  688. struct fs_disk_quota *fdq)
  689. {
  690. return -bhv_vfs_quotactl(vfs_from_sb(sb),
  691. (type == USRQUOTA) ? Q_XSETQLIM :
  692. ((type == GRPQUOTA) ? Q_XSETGQLIM :
  693. Q_XSETPQLIM), id, (caddr_t)fdq);
  694. }
  695. STATIC int
  696. xfs_fs_fill_super(
  697. struct super_block *sb,
  698. void *data,
  699. int silent)
  700. {
  701. struct bhv_vnode *rootvp;
  702. struct bhv_vfs *vfsp = vfs_allocate(sb);
  703. struct xfs_mount_args *args = xfs_args_allocate(sb, silent);
  704. struct kstatfs statvfs;
  705. int error;
  706. bhv_insert_all_vfsops(vfsp);
  707. error = bhv_vfs_parseargs(vfsp, (char *)data, args, 0);
  708. if (error) {
  709. bhv_remove_all_vfsops(vfsp, 1);
  710. goto fail_vfsop;
  711. }
  712. sb_min_blocksize(sb, BBSIZE);
  713. sb->s_export_op = &xfs_export_operations;
  714. sb->s_qcop = &xfs_quotactl_operations;
  715. sb->s_op = &xfs_super_operations;
  716. error = bhv_vfs_mount(vfsp, args, NULL);
  717. if (error) {
  718. bhv_remove_all_vfsops(vfsp, 1);
  719. goto fail_vfsop;
  720. }
  721. error = bhv_vfs_statvfs(vfsp, &statvfs, NULL);
  722. if (error)
  723. goto fail_unmount;
  724. sb->s_dirt = 1;
  725. sb->s_magic = statvfs.f_type;
  726. sb->s_blocksize = statvfs.f_bsize;
  727. sb->s_blocksize_bits = ffs(statvfs.f_bsize) - 1;
  728. sb->s_maxbytes = xfs_max_file_offset(sb->s_blocksize_bits);
  729. sb->s_time_gran = 1;
  730. set_posix_acl_flag(sb);
  731. error = bhv_vfs_root(vfsp, &rootvp);
  732. if (error)
  733. goto fail_unmount;
  734. sb->s_root = d_alloc_root(vn_to_inode(rootvp));
  735. if (!sb->s_root) {
  736. error = ENOMEM;
  737. goto fail_vnrele;
  738. }
  739. if (is_bad_inode(sb->s_root->d_inode)) {
  740. error = EINVAL;
  741. goto fail_vnrele;
  742. }
  743. if ((error = xfs_fs_start_syncd(vfsp)))
  744. goto fail_vnrele;
  745. vn_trace_exit(rootvp, __FUNCTION__, (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. bhv_vfs_unmount(vfsp, 0, NULL);
  757. fail_vfsop:
  758. vfs_deallocate(vfsp);
  759. kmem_free(args, sizeof(*args));
  760. return -error;
  761. }
  762. STATIC int
  763. xfs_fs_get_sb(
  764. struct file_system_type *fs_type,
  765. int flags,
  766. const char *dev_name,
  767. void *data,
  768. struct vfsmount *mnt)
  769. {
  770. return get_sb_bdev(fs_type, flags, dev_name, data, xfs_fs_fill_super,
  771. mnt);
  772. }
  773. static struct super_operations xfs_super_operations = {
  774. .alloc_inode = xfs_fs_alloc_inode,
  775. .destroy_inode = xfs_fs_destroy_inode,
  776. .write_inode = xfs_fs_write_inode,
  777. .clear_inode = xfs_fs_clear_inode,
  778. .put_super = xfs_fs_put_super,
  779. .write_super = xfs_fs_write_super,
  780. .sync_fs = xfs_fs_sync_super,
  781. .write_super_lockfs = xfs_fs_lockfs,
  782. .statfs = xfs_fs_statfs,
  783. .remount_fs = xfs_fs_remount,
  784. .show_options = xfs_fs_show_options,
  785. };
  786. static struct quotactl_ops xfs_quotactl_operations = {
  787. .quota_sync = xfs_fs_quotasync,
  788. .get_xstate = xfs_fs_getxstate,
  789. .set_xstate = xfs_fs_setxstate,
  790. .get_xquota = xfs_fs_getxquota,
  791. .set_xquota = xfs_fs_setxquota,
  792. };
  793. static struct file_system_type xfs_fs_type = {
  794. .owner = THIS_MODULE,
  795. .name = "xfs",
  796. .get_sb = xfs_fs_get_sb,
  797. .kill_sb = kill_block_super,
  798. .fs_flags = FS_REQUIRES_DEV,
  799. };
  800. STATIC int __init
  801. init_xfs_fs( void )
  802. {
  803. int error;
  804. static char message[] __initdata = KERN_INFO \
  805. XFS_VERSION_STRING " with " XFS_BUILD_OPTIONS " enabled\n";
  806. printk(message);
  807. ktrace_init(64);
  808. error = xfs_init_zones();
  809. if (error < 0)
  810. goto undo_zones;
  811. error = xfs_buf_init();
  812. if (error < 0)
  813. goto undo_buffers;
  814. vn_init();
  815. xfs_init();
  816. uuid_init();
  817. vfs_initquota();
  818. error = register_filesystem(&xfs_fs_type);
  819. if (error)
  820. goto undo_register;
  821. return 0;
  822. undo_register:
  823. xfs_buf_terminate();
  824. undo_buffers:
  825. xfs_destroy_zones();
  826. undo_zones:
  827. return error;
  828. }
  829. STATIC void __exit
  830. exit_xfs_fs( void )
  831. {
  832. vfs_exitquota();
  833. unregister_filesystem(&xfs_fs_type);
  834. xfs_cleanup();
  835. xfs_buf_terminate();
  836. xfs_destroy_zones();
  837. ktrace_uninit();
  838. }
  839. module_init(init_xfs_fs);
  840. module_exit(exit_xfs_fs);
  841. MODULE_AUTHOR("Silicon Graphics, Inc.");
  842. MODULE_DESCRIPTION(XFS_VERSION_STRING " with " XFS_BUILD_OPTIONS " enabled");
  843. MODULE_LICENSE("GPL");