xfs_super.c 22 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003
  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 xfs_quotactl_operations;
  61. STATIC struct super_operations xfs_super_operations;
  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_DIRSYNC)
  75. args->flags |= XFSMNT_DIRSYNC;
  76. if (sb->s_flags & MS_SYNCHRONOUS)
  77. args->flags |= XFSMNT_WSYNC;
  78. /* Default to 32 bit inodes on Linux all the time */
  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. 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. 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. error = xfs_barrier_test(mp);
  285. if (error) {
  286. xfs_fs_cmn_err(CE_NOTE, mp,
  287. "Disabling barriers, trial barrier write failed");
  288. mp->m_flags &= ~XFS_MOUNT_BARRIER;
  289. return;
  290. }
  291. }
  292. void
  293. xfs_blkdev_issue_flush(
  294. xfs_buftarg_t *buftarg)
  295. {
  296. blkdev_issue_flush(buftarg->bt_bdev, NULL);
  297. }
  298. STATIC struct inode *
  299. xfs_fs_alloc_inode(
  300. struct super_block *sb)
  301. {
  302. vnode_t *vp;
  303. vp = kmem_zone_alloc(xfs_vnode_zone, KM_SLEEP);
  304. if (unlikely(!vp))
  305. return NULL;
  306. return vn_to_inode(vp);
  307. }
  308. STATIC void
  309. xfs_fs_destroy_inode(
  310. struct inode *inode)
  311. {
  312. kmem_zone_free(xfs_vnode_zone, vn_from_inode(inode));
  313. }
  314. STATIC void
  315. xfs_fs_inode_init_once(
  316. void *vnode,
  317. kmem_zone_t *zonep,
  318. unsigned long flags)
  319. {
  320. if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
  321. SLAB_CTOR_CONSTRUCTOR)
  322. inode_init_once(vn_to_inode((vnode_t *)vnode));
  323. }
  324. STATIC int
  325. xfs_init_zones(void)
  326. {
  327. xfs_vnode_zone = kmem_zone_init_flags(sizeof(vnode_t), "xfs_vnode_t",
  328. KM_ZONE_HWALIGN | KM_ZONE_RECLAIM |
  329. KM_ZONE_SPREAD,
  330. xfs_fs_inode_init_once);
  331. if (!xfs_vnode_zone)
  332. goto out;
  333. xfs_ioend_zone = kmem_zone_init(sizeof(xfs_ioend_t), "xfs_ioend");
  334. if (!xfs_ioend_zone)
  335. goto out_destroy_vnode_zone;
  336. xfs_ioend_pool = mempool_create(4 * MAX_BUF_PER_PAGE,
  337. mempool_alloc_slab, mempool_free_slab,
  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. 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. flags |= FLUSH_SYNC;
  373. VOP_IFLUSH(vp, flags, error);
  374. if (error == EAGAIN) {
  375. if (sync)
  376. VOP_IFLUSH(vp, flags | FLUSH_LOG, error);
  377. else
  378. error = 0;
  379. }
  380. }
  381. return -error;
  382. }
  383. STATIC void
  384. xfs_fs_clear_inode(
  385. struct inode *inode)
  386. {
  387. vnode_t *vp = vn_from_inode(inode);
  388. int error, cache;
  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 (vp->v_fbhv)
  399. VOP_INACTIVE(vp, NULL, cache);
  400. VN_LOCK(vp);
  401. vp->v_flag &= ~VMODIFIED;
  402. VN_UNLOCK(vp, 0);
  403. if (vp->v_fbhv) {
  404. VOP_RECLAIM(vp, error);
  405. if (error)
  406. panic("vn_purge: cannot reclaim");
  407. }
  408. ASSERT(vp->v_fbhv == NULL);
  409. #ifdef XFS_VNODE_TRACE
  410. ktrace_free(vp->v_trace);
  411. #endif
  412. }
  413. /*
  414. * Enqueue a work item to be picked up by the vfs xfssyncd thread.
  415. * Doing this has two advantages:
  416. * - It saves on stack space, which is tight in certain situations
  417. * - It can be used (with care) as a mechanism to avoid deadlocks.
  418. * Flushing while allocating in a full filesystem requires both.
  419. */
  420. STATIC void
  421. xfs_syncd_queue_work(
  422. struct vfs *vfs,
  423. void *data,
  424. void (*syncer)(vfs_t *, void *))
  425. {
  426. vfs_sync_work_t *work;
  427. work = kmem_alloc(sizeof(struct vfs_sync_work), KM_SLEEP);
  428. INIT_LIST_HEAD(&work->w_list);
  429. work->w_syncer = syncer;
  430. work->w_data = data;
  431. work->w_vfs = vfs;
  432. spin_lock(&vfs->vfs_sync_lock);
  433. list_add_tail(&work->w_list, &vfs->vfs_sync_list);
  434. spin_unlock(&vfs->vfs_sync_lock);
  435. wake_up_process(vfs->vfs_sync_task);
  436. }
  437. /*
  438. * Flush delayed allocate data, attempting to free up reserved space
  439. * from existing allocations. At this point a new allocation attempt
  440. * has failed with ENOSPC and we are in the process of scratching our
  441. * heads, looking about for more room...
  442. */
  443. STATIC void
  444. xfs_flush_inode_work(
  445. vfs_t *vfs,
  446. void *inode)
  447. {
  448. filemap_flush(((struct inode *)inode)->i_mapping);
  449. iput((struct inode *)inode);
  450. }
  451. void
  452. xfs_flush_inode(
  453. xfs_inode_t *ip)
  454. {
  455. struct inode *inode = vn_to_inode(XFS_ITOV(ip));
  456. struct vfs *vfs = XFS_MTOVFS(ip->i_mount);
  457. igrab(inode);
  458. xfs_syncd_queue_work(vfs, inode, xfs_flush_inode_work);
  459. delay(msecs_to_jiffies(500));
  460. }
  461. /*
  462. * This is the "bigger hammer" version of xfs_flush_inode_work...
  463. * (IOW, "If at first you don't succeed, use a Bigger Hammer").
  464. */
  465. STATIC void
  466. xfs_flush_device_work(
  467. vfs_t *vfs,
  468. void *inode)
  469. {
  470. sync_blockdev(vfs->vfs_super->s_bdev);
  471. iput((struct inode *)inode);
  472. }
  473. void
  474. xfs_flush_device(
  475. xfs_inode_t *ip)
  476. {
  477. struct inode *inode = vn_to_inode(XFS_ITOV(ip));
  478. struct vfs *vfs = XFS_MTOVFS(ip->i_mount);
  479. igrab(inode);
  480. xfs_syncd_queue_work(vfs, inode, xfs_flush_device_work);
  481. delay(msecs_to_jiffies(500));
  482. xfs_log_force(ip->i_mount, (xfs_lsn_t)0, XFS_LOG_FORCE|XFS_LOG_SYNC);
  483. }
  484. #define SYNCD_FLAGS (SYNC_FSDATA|SYNC_BDFLUSH|SYNC_ATTR|SYNC_REFCACHE)
  485. STATIC void
  486. vfs_sync_worker(
  487. vfs_t *vfsp,
  488. void *unused)
  489. {
  490. int error;
  491. if (!(vfsp->vfs_flag & VFS_RDONLY))
  492. VFS_SYNC(vfsp, SYNCD_FLAGS, NULL, error);
  493. vfsp->vfs_sync_seq++;
  494. wmb();
  495. wake_up(&vfsp->vfs_wait_single_sync_task);
  496. }
  497. STATIC int
  498. xfssyncd(
  499. void *arg)
  500. {
  501. long timeleft;
  502. vfs_t *vfsp = (vfs_t *) arg;
  503. struct vfs_sync_work *work, *n;
  504. LIST_HEAD (tmp);
  505. timeleft = xfs_syncd_centisecs * msecs_to_jiffies(10);
  506. for (;;) {
  507. timeleft = schedule_timeout_interruptible(timeleft);
  508. /* swsusp */
  509. try_to_freeze();
  510. if (kthread_should_stop() && list_empty(&vfsp->vfs_sync_list))
  511. break;
  512. spin_lock(&vfsp->vfs_sync_lock);
  513. /*
  514. * We can get woken by laptop mode, to do a sync -
  515. * that's the (only!) case where the list would be
  516. * empty with time remaining.
  517. */
  518. if (!timeleft || list_empty(&vfsp->vfs_sync_list)) {
  519. if (!timeleft)
  520. timeleft = xfs_syncd_centisecs *
  521. msecs_to_jiffies(10);
  522. INIT_LIST_HEAD(&vfsp->vfs_sync_work.w_list);
  523. list_add_tail(&vfsp->vfs_sync_work.w_list,
  524. &vfsp->vfs_sync_list);
  525. }
  526. list_for_each_entry_safe(work, n, &vfsp->vfs_sync_list, w_list)
  527. list_move(&work->w_list, &tmp);
  528. spin_unlock(&vfsp->vfs_sync_lock);
  529. list_for_each_entry_safe(work, n, &tmp, w_list) {
  530. (*work->w_syncer)(vfsp, work->w_data);
  531. list_del(&work->w_list);
  532. if (work == &vfsp->vfs_sync_work)
  533. continue;
  534. kmem_free(work, sizeof(struct vfs_sync_work));
  535. }
  536. }
  537. return 0;
  538. }
  539. STATIC int
  540. xfs_fs_start_syncd(
  541. vfs_t *vfsp)
  542. {
  543. vfsp->vfs_sync_work.w_syncer = vfs_sync_worker;
  544. vfsp->vfs_sync_work.w_vfs = vfsp;
  545. vfsp->vfs_sync_task = kthread_run(xfssyncd, vfsp, "xfssyncd");
  546. if (IS_ERR(vfsp->vfs_sync_task))
  547. return -PTR_ERR(vfsp->vfs_sync_task);
  548. return 0;
  549. }
  550. STATIC void
  551. xfs_fs_stop_syncd(
  552. vfs_t *vfsp)
  553. {
  554. kthread_stop(vfsp->vfs_sync_task);
  555. }
  556. STATIC void
  557. xfs_fs_put_super(
  558. struct super_block *sb)
  559. {
  560. vfs_t *vfsp = vfs_from_sb(sb);
  561. int error;
  562. xfs_fs_stop_syncd(vfsp);
  563. VFS_SYNC(vfsp, SYNC_ATTR|SYNC_DELWRI, NULL, error);
  564. if (!error)
  565. VFS_UNMOUNT(vfsp, 0, NULL, error);
  566. if (error) {
  567. printk("XFS unmount got error %d\n", error);
  568. printk("%s: vfsp/0x%p left dangling!\n", __FUNCTION__, vfsp);
  569. return;
  570. }
  571. vfs_deallocate(vfsp);
  572. }
  573. STATIC void
  574. xfs_fs_write_super(
  575. struct super_block *sb)
  576. {
  577. vfs_t *vfsp = vfs_from_sb(sb);
  578. int error;
  579. if (sb->s_flags & MS_RDONLY) {
  580. sb->s_dirt = 0; /* paranoia */
  581. return;
  582. }
  583. /* Push the log and superblock a little */
  584. VFS_SYNC(vfsp, SYNC_FSDATA, NULL, error);
  585. sb->s_dirt = 0;
  586. }
  587. STATIC int
  588. xfs_fs_sync_super(
  589. struct super_block *sb,
  590. int wait)
  591. {
  592. vfs_t *vfsp = vfs_from_sb(sb);
  593. int error;
  594. int flags = SYNC_FSDATA;
  595. if (unlikely(sb->s_frozen == SB_FREEZE_WRITE))
  596. flags = SYNC_QUIESCE;
  597. else
  598. flags = SYNC_FSDATA | (wait ? SYNC_WAIT : 0);
  599. VFS_SYNC(vfsp, flags, NULL, error);
  600. sb->s_dirt = 0;
  601. if (unlikely(laptop_mode)) {
  602. int prev_sync_seq = vfsp->vfs_sync_seq;
  603. /*
  604. * The disk must be active because we're syncing.
  605. * We schedule xfssyncd now (now that the disk is
  606. * active) instead of later (when it might not be).
  607. */
  608. wake_up_process(vfsp->vfs_sync_task);
  609. /*
  610. * We have to wait for the sync iteration to complete.
  611. * If we don't, the disk activity caused by the sync
  612. * will come after the sync is completed, and that
  613. * triggers another sync from laptop mode.
  614. */
  615. wait_event(vfsp->vfs_wait_single_sync_task,
  616. vfsp->vfs_sync_seq != prev_sync_seq);
  617. }
  618. return -error;
  619. }
  620. STATIC int
  621. xfs_fs_statfs(
  622. struct super_block *sb,
  623. struct kstatfs *statp)
  624. {
  625. vfs_t *vfsp = vfs_from_sb(sb);
  626. int error;
  627. VFS_STATVFS(vfsp, statp, NULL, error);
  628. return -error;
  629. }
  630. STATIC int
  631. xfs_fs_remount(
  632. struct super_block *sb,
  633. int *flags,
  634. char *options)
  635. {
  636. vfs_t *vfsp = vfs_from_sb(sb);
  637. struct xfs_mount_args *args = xfs_args_allocate(sb);
  638. int error;
  639. VFS_PARSEARGS(vfsp, options, args, 1, error);
  640. if (!error)
  641. VFS_MNTUPDATE(vfsp, flags, args, error);
  642. kmem_free(args, sizeof(*args));
  643. return -error;
  644. }
  645. STATIC void
  646. xfs_fs_lockfs(
  647. struct super_block *sb)
  648. {
  649. VFS_FREEZE(vfs_from_sb(sb));
  650. }
  651. STATIC int
  652. xfs_fs_show_options(
  653. struct seq_file *m,
  654. struct vfsmount *mnt)
  655. {
  656. struct vfs *vfsp = vfs_from_sb(mnt->mnt_sb);
  657. int error;
  658. VFS_SHOWARGS(vfsp, m, error);
  659. return error;
  660. }
  661. STATIC int
  662. xfs_fs_quotasync(
  663. struct super_block *sb,
  664. int type)
  665. {
  666. struct vfs *vfsp = vfs_from_sb(sb);
  667. int error;
  668. VFS_QUOTACTL(vfsp, Q_XQUOTASYNC, 0, (caddr_t)NULL, error);
  669. return -error;
  670. }
  671. STATIC int
  672. xfs_fs_getxstate(
  673. struct super_block *sb,
  674. struct fs_quota_stat *fqs)
  675. {
  676. struct vfs *vfsp = vfs_from_sb(sb);
  677. int error;
  678. VFS_QUOTACTL(vfsp, Q_XGETQSTAT, 0, (caddr_t)fqs, error);
  679. return -error;
  680. }
  681. STATIC int
  682. xfs_fs_setxstate(
  683. struct super_block *sb,
  684. unsigned int flags,
  685. int op)
  686. {
  687. struct vfs *vfsp = vfs_from_sb(sb);
  688. int error;
  689. VFS_QUOTACTL(vfsp, op, 0, (caddr_t)&flags, error);
  690. return -error;
  691. }
  692. STATIC int
  693. xfs_fs_getxquota(
  694. struct super_block *sb,
  695. int type,
  696. qid_t id,
  697. struct fs_disk_quota *fdq)
  698. {
  699. struct vfs *vfsp = vfs_from_sb(sb);
  700. int error, getmode;
  701. getmode = (type == USRQUOTA) ? Q_XGETQUOTA :
  702. ((type == GRPQUOTA) ? Q_XGETGQUOTA : Q_XGETPQUOTA);
  703. VFS_QUOTACTL(vfsp, getmode, id, (caddr_t)fdq, error);
  704. return -error;
  705. }
  706. STATIC int
  707. xfs_fs_setxquota(
  708. struct super_block *sb,
  709. int type,
  710. qid_t id,
  711. struct fs_disk_quota *fdq)
  712. {
  713. struct vfs *vfsp = vfs_from_sb(sb);
  714. int error, setmode;
  715. setmode = (type == USRQUOTA) ? Q_XSETQLIM :
  716. ((type == GRPQUOTA) ? Q_XSETGQLIM : Q_XSETPQLIM);
  717. VFS_QUOTACTL(vfsp, setmode, id, (caddr_t)fdq, error);
  718. return -error;
  719. }
  720. STATIC int
  721. xfs_fs_fill_super(
  722. struct super_block *sb,
  723. void *data,
  724. int silent)
  725. {
  726. vnode_t *rootvp;
  727. struct vfs *vfsp = vfs_allocate(sb);
  728. struct xfs_mount_args *args = xfs_args_allocate(sb);
  729. struct kstatfs statvfs;
  730. int error, error2;
  731. bhv_insert_all_vfsops(vfsp);
  732. VFS_PARSEARGS(vfsp, (char *)data, args, 0, error);
  733. if (error) {
  734. bhv_remove_all_vfsops(vfsp, 1);
  735. goto fail_vfsop;
  736. }
  737. sb_min_blocksize(sb, BBSIZE);
  738. #ifdef CONFIG_XFS_EXPORT
  739. sb->s_export_op = &xfs_export_operations;
  740. #endif
  741. sb->s_qcop = &xfs_quotactl_operations;
  742. sb->s_op = &xfs_super_operations;
  743. VFS_MOUNT(vfsp, args, NULL, error);
  744. if (error) {
  745. bhv_remove_all_vfsops(vfsp, 1);
  746. goto fail_vfsop;
  747. }
  748. VFS_STATVFS(vfsp, &statvfs, NULL, error);
  749. if (error)
  750. goto fail_unmount;
  751. sb->s_dirt = 1;
  752. sb->s_magic = statvfs.f_type;
  753. sb->s_blocksize = statvfs.f_bsize;
  754. sb->s_blocksize_bits = ffs(statvfs.f_bsize) - 1;
  755. sb->s_maxbytes = xfs_max_file_offset(sb->s_blocksize_bits);
  756. sb->s_time_gran = 1;
  757. set_posix_acl_flag(sb);
  758. VFS_ROOT(vfsp, &rootvp, error);
  759. if (error)
  760. goto fail_unmount;
  761. sb->s_root = d_alloc_root(vn_to_inode(rootvp));
  762. if (!sb->s_root) {
  763. error = ENOMEM;
  764. goto fail_vnrele;
  765. }
  766. if (is_bad_inode(sb->s_root->d_inode)) {
  767. error = EINVAL;
  768. goto fail_vnrele;
  769. }
  770. if ((error = xfs_fs_start_syncd(vfsp)))
  771. goto fail_vnrele;
  772. vn_trace_exit(rootvp, __FUNCTION__, (inst_t *)__return_address);
  773. kmem_free(args, sizeof(*args));
  774. return 0;
  775. fail_vnrele:
  776. if (sb->s_root) {
  777. dput(sb->s_root);
  778. sb->s_root = NULL;
  779. } else {
  780. VN_RELE(rootvp);
  781. }
  782. fail_unmount:
  783. VFS_UNMOUNT(vfsp, 0, NULL, error2);
  784. fail_vfsop:
  785. vfs_deallocate(vfsp);
  786. kmem_free(args, sizeof(*args));
  787. return -error;
  788. }
  789. STATIC struct super_block *
  790. xfs_fs_get_sb(
  791. struct file_system_type *fs_type,
  792. int flags,
  793. const char *dev_name,
  794. void *data)
  795. {
  796. return get_sb_bdev(fs_type, flags, dev_name, data, xfs_fs_fill_super);
  797. }
  798. STATIC struct super_operations xfs_super_operations = {
  799. .alloc_inode = xfs_fs_alloc_inode,
  800. .destroy_inode = xfs_fs_destroy_inode,
  801. .write_inode = xfs_fs_write_inode,
  802. .clear_inode = xfs_fs_clear_inode,
  803. .put_super = xfs_fs_put_super,
  804. .write_super = xfs_fs_write_super,
  805. .sync_fs = xfs_fs_sync_super,
  806. .write_super_lockfs = xfs_fs_lockfs,
  807. .statfs = xfs_fs_statfs,
  808. .remount_fs = xfs_fs_remount,
  809. .show_options = xfs_fs_show_options,
  810. };
  811. STATIC struct quotactl_ops xfs_quotactl_operations = {
  812. .quota_sync = xfs_fs_quotasync,
  813. .get_xstate = xfs_fs_getxstate,
  814. .set_xstate = xfs_fs_setxstate,
  815. .get_xquota = xfs_fs_getxquota,
  816. .set_xquota = xfs_fs_setxquota,
  817. };
  818. STATIC struct file_system_type xfs_fs_type = {
  819. .owner = THIS_MODULE,
  820. .name = "xfs",
  821. .get_sb = xfs_fs_get_sb,
  822. .kill_sb = kill_block_super,
  823. .fs_flags = FS_REQUIRES_DEV,
  824. };
  825. STATIC int __init
  826. init_xfs_fs( void )
  827. {
  828. int error;
  829. struct sysinfo si;
  830. static char message[] __initdata = KERN_INFO \
  831. XFS_VERSION_STRING " with " XFS_BUILD_OPTIONS " enabled\n";
  832. printk(message);
  833. si_meminfo(&si);
  834. xfs_physmem = si.totalram;
  835. ktrace_init(64);
  836. error = xfs_init_zones();
  837. if (error < 0)
  838. goto undo_zones;
  839. error = xfs_buf_init();
  840. if (error < 0)
  841. goto undo_buffers;
  842. vn_init();
  843. xfs_init();
  844. uuid_init();
  845. vfs_initquota();
  846. error = register_filesystem(&xfs_fs_type);
  847. if (error)
  848. goto undo_register;
  849. return 0;
  850. undo_register:
  851. xfs_buf_terminate();
  852. undo_buffers:
  853. xfs_destroy_zones();
  854. undo_zones:
  855. return error;
  856. }
  857. STATIC void __exit
  858. exit_xfs_fs( void )
  859. {
  860. vfs_exitquota();
  861. unregister_filesystem(&xfs_fs_type);
  862. xfs_cleanup();
  863. xfs_buf_terminate();
  864. xfs_destroy_zones();
  865. ktrace_uninit();
  866. }
  867. module_init(init_xfs_fs);
  868. module_exit(exit_xfs_fs);
  869. MODULE_AUTHOR("Silicon Graphics, Inc.");
  870. MODULE_DESCRIPTION(XFS_VERSION_STRING " with " XFS_BUILD_OPTIONS " enabled");
  871. MODULE_LICENSE("GPL");