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