xfs_super.c 22 KB

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