xfs_super.c 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013
  1. /*
  2. * Copyright (c) 2000-2005 Silicon Graphics, Inc. All Rights Reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of version 2 of the GNU General Public License as
  6. * published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it would be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. *
  12. * Further, this software is distributed without any warranty that it is
  13. * free of the rightful claim of any third person regarding infringement
  14. * or the like. Any license provided herein, whether implied or
  15. * otherwise, applies only to this software file. Patent licenses, if
  16. * any, provided herein do not apply to combinations of this program with
  17. * other software, or any other product whatsoever.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, write the Free Software Foundation, Inc., 59
  21. * Temple Place - Suite 330, Boston MA 02111-1307, USA.
  22. *
  23. * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
  24. * Mountain View, CA 94043, or:
  25. *
  26. * http://www.sgi.com
  27. *
  28. * For further information regarding this notice, see:
  29. *
  30. * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
  31. */
  32. #include "xfs.h"
  33. #include "xfs_inum.h"
  34. #include "xfs_log.h"
  35. #include "xfs_clnt.h"
  36. #include "xfs_trans.h"
  37. #include "xfs_sb.h"
  38. #include "xfs_dir.h"
  39. #include "xfs_dir2.h"
  40. #include "xfs_alloc.h"
  41. #include "xfs_dmapi.h"
  42. #include "xfs_quota.h"
  43. #include "xfs_mount.h"
  44. #include "xfs_alloc_btree.h"
  45. #include "xfs_bmap_btree.h"
  46. #include "xfs_ialloc_btree.h"
  47. #include "xfs_btree.h"
  48. #include "xfs_ialloc.h"
  49. #include "xfs_attr_sf.h"
  50. #include "xfs_dir_sf.h"
  51. #include "xfs_dir2_sf.h"
  52. #include "xfs_dinode.h"
  53. #include "xfs_inode.h"
  54. #include "xfs_bmap.h"
  55. #include "xfs_bit.h"
  56. #include "xfs_rtalloc.h"
  57. #include "xfs_error.h"
  58. #include "xfs_itable.h"
  59. #include "xfs_rw.h"
  60. #include "xfs_acl.h"
  61. #include "xfs_cap.h"
  62. #include "xfs_mac.h"
  63. #include "xfs_attr.h"
  64. #include "xfs_buf_item.h"
  65. #include "xfs_utils.h"
  66. #include "xfs_version.h"
  67. #include <linux/namei.h>
  68. #include <linux/init.h>
  69. #include <linux/mount.h>
  70. #include <linux/mempool.h>
  71. #include <linux/writeback.h>
  72. #include <linux/kthread.h>
  73. STATIC struct quotactl_ops linvfs_qops;
  74. STATIC struct super_operations linvfs_sops;
  75. STATIC kmem_zone_t *xfs_vnode_zone;
  76. STATIC kmem_zone_t *xfs_ioend_zone;
  77. mempool_t *xfs_ioend_pool;
  78. STATIC struct xfs_mount_args *
  79. xfs_args_allocate(
  80. struct super_block *sb)
  81. {
  82. struct xfs_mount_args *args;
  83. args = kmem_zalloc(sizeof(struct xfs_mount_args), KM_SLEEP);
  84. args->logbufs = args->logbufsize = -1;
  85. strncpy(args->fsname, sb->s_id, MAXNAMELEN);
  86. /* Copy the already-parsed mount(2) flags we're interested in */
  87. if (sb->s_flags & MS_NOATIME)
  88. args->flags |= XFSMNT_NOATIME;
  89. if (sb->s_flags & MS_DIRSYNC)
  90. args->flags |= XFSMNT_DIRSYNC;
  91. if (sb->s_flags & MS_SYNCHRONOUS)
  92. args->flags |= XFSMNT_WSYNC;
  93. /* Default to 32 bit inodes on Linux all the time */
  94. args->flags |= XFSMNT_32BITINODES;
  95. return args;
  96. }
  97. __uint64_t
  98. xfs_max_file_offset(
  99. unsigned int blockshift)
  100. {
  101. unsigned int pagefactor = 1;
  102. unsigned int bitshift = BITS_PER_LONG - 1;
  103. /* Figure out maximum filesize, on Linux this can depend on
  104. * the filesystem blocksize (on 32 bit platforms).
  105. * __block_prepare_write does this in an [unsigned] long...
  106. * page->index << (PAGE_CACHE_SHIFT - bbits)
  107. * So, for page sized blocks (4K on 32 bit platforms),
  108. * this wraps at around 8Tb (hence MAX_LFS_FILESIZE which is
  109. * (((u64)PAGE_CACHE_SIZE << (BITS_PER_LONG-1))-1)
  110. * but for smaller blocksizes it is less (bbits = log2 bsize).
  111. * Note1: get_block_t takes a long (implicit cast from above)
  112. * Note2: The Large Block Device (LBD and HAVE_SECTOR_T) patch
  113. * can optionally convert the [unsigned] long from above into
  114. * an [unsigned] long long.
  115. */
  116. #if BITS_PER_LONG == 32
  117. # if defined(CONFIG_LBD)
  118. ASSERT(sizeof(sector_t) == 8);
  119. pagefactor = PAGE_CACHE_SIZE;
  120. bitshift = BITS_PER_LONG;
  121. # else
  122. pagefactor = PAGE_CACHE_SIZE >> (PAGE_CACHE_SHIFT - blockshift);
  123. # endif
  124. #endif
  125. return (((__uint64_t)pagefactor) << bitshift) - 1;
  126. }
  127. STATIC __inline__ void
  128. xfs_set_inodeops(
  129. struct inode *inode)
  130. {
  131. switch (inode->i_mode & S_IFMT) {
  132. case S_IFREG:
  133. inode->i_op = &linvfs_file_inode_operations;
  134. inode->i_fop = &linvfs_file_operations;
  135. inode->i_mapping->a_ops = &linvfs_aops;
  136. break;
  137. case S_IFDIR:
  138. inode->i_op = &linvfs_dir_inode_operations;
  139. inode->i_fop = &linvfs_dir_operations;
  140. break;
  141. case S_IFLNK:
  142. inode->i_op = &linvfs_symlink_inode_operations;
  143. if (inode->i_blocks)
  144. inode->i_mapping->a_ops = &linvfs_aops;
  145. break;
  146. default:
  147. inode->i_op = &linvfs_file_inode_operations;
  148. init_special_inode(inode, inode->i_mode, inode->i_rdev);
  149. break;
  150. }
  151. }
  152. STATIC __inline__ void
  153. xfs_revalidate_inode(
  154. xfs_mount_t *mp,
  155. vnode_t *vp,
  156. xfs_inode_t *ip)
  157. {
  158. struct inode *inode = LINVFS_GET_IP(vp);
  159. inode->i_mode = ip->i_d.di_mode;
  160. inode->i_nlink = ip->i_d.di_nlink;
  161. inode->i_uid = ip->i_d.di_uid;
  162. inode->i_gid = ip->i_d.di_gid;
  163. switch (inode->i_mode & S_IFMT) {
  164. case S_IFBLK:
  165. case S_IFCHR:
  166. inode->i_rdev =
  167. MKDEV(sysv_major(ip->i_df.if_u2.if_rdev) & 0x1ff,
  168. sysv_minor(ip->i_df.if_u2.if_rdev));
  169. break;
  170. default:
  171. inode->i_rdev = 0;
  172. break;
  173. }
  174. inode->i_blksize = PAGE_CACHE_SIZE;
  175. inode->i_generation = ip->i_d.di_gen;
  176. i_size_write(inode, ip->i_d.di_size);
  177. inode->i_blocks =
  178. XFS_FSB_TO_BB(mp, ip->i_d.di_nblocks + ip->i_delayed_blks);
  179. inode->i_atime.tv_sec = ip->i_d.di_atime.t_sec;
  180. inode->i_atime.tv_nsec = ip->i_d.di_atime.t_nsec;
  181. inode->i_mtime.tv_sec = ip->i_d.di_mtime.t_sec;
  182. inode->i_mtime.tv_nsec = ip->i_d.di_mtime.t_nsec;
  183. inode->i_ctime.tv_sec = ip->i_d.di_ctime.t_sec;
  184. inode->i_ctime.tv_nsec = ip->i_d.di_ctime.t_nsec;
  185. if (ip->i_d.di_flags & XFS_DIFLAG_IMMUTABLE)
  186. inode->i_flags |= S_IMMUTABLE;
  187. else
  188. inode->i_flags &= ~S_IMMUTABLE;
  189. if (ip->i_d.di_flags & XFS_DIFLAG_APPEND)
  190. inode->i_flags |= S_APPEND;
  191. else
  192. inode->i_flags &= ~S_APPEND;
  193. if (ip->i_d.di_flags & XFS_DIFLAG_SYNC)
  194. inode->i_flags |= S_SYNC;
  195. else
  196. inode->i_flags &= ~S_SYNC;
  197. if (ip->i_d.di_flags & XFS_DIFLAG_NOATIME)
  198. inode->i_flags |= S_NOATIME;
  199. else
  200. inode->i_flags &= ~S_NOATIME;
  201. vp->v_flag &= ~VMODIFIED;
  202. }
  203. void
  204. xfs_initialize_vnode(
  205. bhv_desc_t *bdp,
  206. vnode_t *vp,
  207. bhv_desc_t *inode_bhv,
  208. int unlock)
  209. {
  210. xfs_inode_t *ip = XFS_BHVTOI(inode_bhv);
  211. struct inode *inode = LINVFS_GET_IP(vp);
  212. if (!inode_bhv->bd_vobj) {
  213. vp->v_vfsp = bhvtovfs(bdp);
  214. bhv_desc_init(inode_bhv, ip, vp, &xfs_vnodeops);
  215. bhv_insert(VN_BHV_HEAD(vp), inode_bhv);
  216. }
  217. /*
  218. * We need to set the ops vectors, and unlock the inode, but if
  219. * we have been called during the new inode create process, it is
  220. * too early to fill in the Linux inode. We will get called a
  221. * second time once the inode is properly set up, and then we can
  222. * finish our work.
  223. */
  224. if (ip->i_d.di_mode != 0 && unlock && (inode->i_state & I_NEW)) {
  225. xfs_revalidate_inode(XFS_BHVTOM(bdp), vp, ip);
  226. xfs_set_inodeops(inode);
  227. ip->i_flags &= ~XFS_INEW;
  228. barrier();
  229. unlock_new_inode(inode);
  230. }
  231. }
  232. int
  233. xfs_blkdev_get(
  234. xfs_mount_t *mp,
  235. const char *name,
  236. struct block_device **bdevp)
  237. {
  238. int error = 0;
  239. *bdevp = open_bdev_excl(name, 0, mp);
  240. if (IS_ERR(*bdevp)) {
  241. error = PTR_ERR(*bdevp);
  242. printk("XFS: Invalid device [%s], error=%d\n", name, error);
  243. }
  244. return -error;
  245. }
  246. void
  247. xfs_blkdev_put(
  248. struct block_device *bdev)
  249. {
  250. if (bdev)
  251. close_bdev_excl(bdev);
  252. }
  253. /*
  254. * Try to write out the superblock using barriers.
  255. */
  256. STATIC int
  257. xfs_barrier_test(
  258. xfs_mount_t *mp)
  259. {
  260. xfs_buf_t *sbp = xfs_getsb(mp, 0);
  261. int error;
  262. XFS_BUF_UNDONE(sbp);
  263. XFS_BUF_UNREAD(sbp);
  264. XFS_BUF_UNDELAYWRITE(sbp);
  265. XFS_BUF_WRITE(sbp);
  266. XFS_BUF_UNASYNC(sbp);
  267. XFS_BUF_ORDERED(sbp);
  268. xfsbdstrat(mp, sbp);
  269. error = xfs_iowait(sbp);
  270. /*
  271. * Clear all the flags we set and possible error state in the
  272. * buffer. We only did the write to try out whether barriers
  273. * worked and shouldn't leave any traces in the superblock
  274. * buffer.
  275. */
  276. XFS_BUF_DONE(sbp);
  277. XFS_BUF_ERROR(sbp, 0);
  278. XFS_BUF_UNORDERED(sbp);
  279. xfs_buf_relse(sbp);
  280. return error;
  281. }
  282. void
  283. xfs_mountfs_check_barriers(xfs_mount_t *mp)
  284. {
  285. int error;
  286. if (mp->m_logdev_targp != mp->m_ddev_targp) {
  287. xfs_fs_cmn_err(CE_NOTE, mp,
  288. "Disabling barriers, not supported with external log device");
  289. mp->m_flags &= ~XFS_MOUNT_BARRIER;
  290. }
  291. if (mp->m_ddev_targp->pbr_bdev->bd_disk->queue->ordered ==
  292. QUEUE_ORDERED_NONE) {
  293. xfs_fs_cmn_err(CE_NOTE, mp,
  294. "Disabling barriers, not supported by the underlying device");
  295. mp->m_flags &= ~XFS_MOUNT_BARRIER;
  296. }
  297. error = xfs_barrier_test(mp);
  298. if (error) {
  299. xfs_fs_cmn_err(CE_NOTE, mp,
  300. "Disabling barriers, trial barrier write failed");
  301. mp->m_flags &= ~XFS_MOUNT_BARRIER;
  302. }
  303. }
  304. void
  305. xfs_blkdev_issue_flush(
  306. xfs_buftarg_t *buftarg)
  307. {
  308. blkdev_issue_flush(buftarg->pbr_bdev, NULL);
  309. }
  310. STATIC struct inode *
  311. linvfs_alloc_inode(
  312. struct super_block *sb)
  313. {
  314. vnode_t *vp;
  315. vp = kmem_cache_alloc(xfs_vnode_zone, kmem_flags_convert(KM_SLEEP));
  316. if (!vp)
  317. return NULL;
  318. return LINVFS_GET_IP(vp);
  319. }
  320. STATIC void
  321. linvfs_destroy_inode(
  322. struct inode *inode)
  323. {
  324. kmem_zone_free(xfs_vnode_zone, LINVFS_GET_VP(inode));
  325. }
  326. STATIC void
  327. linvfs_inode_init_once(
  328. void *data,
  329. kmem_cache_t *cachep,
  330. unsigned long flags)
  331. {
  332. vnode_t *vp = (vnode_t *)data;
  333. if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
  334. SLAB_CTOR_CONSTRUCTOR)
  335. inode_init_once(LINVFS_GET_IP(vp));
  336. }
  337. STATIC int
  338. linvfs_init_zones(void)
  339. {
  340. xfs_vnode_zone = kmem_cache_create("xfs_vnode",
  341. sizeof(vnode_t), 0, SLAB_RECLAIM_ACCOUNT,
  342. linvfs_inode_init_once, NULL);
  343. if (!xfs_vnode_zone)
  344. goto out;
  345. xfs_ioend_zone = kmem_zone_init(sizeof(xfs_ioend_t), "xfs_ioend");
  346. if (!xfs_ioend_zone)
  347. goto out_destroy_vnode_zone;
  348. xfs_ioend_pool = mempool_create(4 * MAX_BUF_PER_PAGE,
  349. mempool_alloc_slab, mempool_free_slab,
  350. xfs_ioend_zone);
  351. if (!xfs_ioend_pool)
  352. goto out_free_ioend_zone;
  353. return 0;
  354. out_free_ioend_zone:
  355. kmem_zone_destroy(xfs_ioend_zone);
  356. out_destroy_vnode_zone:
  357. kmem_zone_destroy(xfs_vnode_zone);
  358. out:
  359. return -ENOMEM;
  360. }
  361. STATIC void
  362. linvfs_destroy_zones(void)
  363. {
  364. mempool_destroy(xfs_ioend_pool);
  365. kmem_zone_destroy(xfs_vnode_zone);
  366. kmem_zone_destroy(xfs_ioend_zone);
  367. }
  368. /*
  369. * Attempt to flush the inode, this will actually fail
  370. * if the inode is pinned, but we dirty the inode again
  371. * at the point when it is unpinned after a log write,
  372. * since this is when the inode itself becomes flushable.
  373. */
  374. STATIC int
  375. linvfs_write_inode(
  376. struct inode *inode,
  377. int sync)
  378. {
  379. vnode_t *vp = LINVFS_GET_VP(inode);
  380. int error = 0, flags = FLUSH_INODE;
  381. if (vp) {
  382. vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
  383. if (sync)
  384. flags |= FLUSH_SYNC;
  385. VOP_IFLUSH(vp, flags, error);
  386. if (error == EAGAIN) {
  387. if (sync)
  388. VOP_IFLUSH(vp, flags | FLUSH_LOG, error);
  389. else
  390. error = 0;
  391. }
  392. }
  393. return -error;
  394. }
  395. STATIC void
  396. linvfs_clear_inode(
  397. struct inode *inode)
  398. {
  399. vnode_t *vp = LINVFS_GET_VP(inode);
  400. int error, cache;
  401. vn_trace_entry(vp, "clear_inode", (inst_t *)__return_address);
  402. XFS_STATS_INC(vn_rele);
  403. XFS_STATS_INC(vn_remove);
  404. XFS_STATS_INC(vn_reclaim);
  405. XFS_STATS_DEC(vn_active);
  406. /*
  407. * This can happen because xfs_iget_core calls xfs_idestroy if we
  408. * find an inode with di_mode == 0 but without IGET_CREATE set.
  409. */
  410. if (vp->v_fbhv)
  411. VOP_INACTIVE(vp, NULL, cache);
  412. VN_LOCK(vp);
  413. vp->v_flag &= ~VMODIFIED;
  414. VN_UNLOCK(vp, 0);
  415. if (vp->v_fbhv) {
  416. VOP_RECLAIM(vp, error);
  417. if (error)
  418. panic("vn_purge: cannot reclaim");
  419. }
  420. ASSERT(vp->v_fbhv == NULL);
  421. #ifdef XFS_VNODE_TRACE
  422. ktrace_free(vp->v_trace);
  423. #endif
  424. }
  425. /*
  426. * Enqueue a work item to be picked up by the vfs xfssyncd thread.
  427. * Doing this has two advantages:
  428. * - It saves on stack space, which is tight in certain situations
  429. * - It can be used (with care) as a mechanism to avoid deadlocks.
  430. * Flushing while allocating in a full filesystem requires both.
  431. */
  432. STATIC void
  433. xfs_syncd_queue_work(
  434. struct vfs *vfs,
  435. void *data,
  436. void (*syncer)(vfs_t *, void *))
  437. {
  438. vfs_sync_work_t *work;
  439. work = kmem_alloc(sizeof(struct vfs_sync_work), KM_SLEEP);
  440. INIT_LIST_HEAD(&work->w_list);
  441. work->w_syncer = syncer;
  442. work->w_data = data;
  443. work->w_vfs = vfs;
  444. spin_lock(&vfs->vfs_sync_lock);
  445. list_add_tail(&work->w_list, &vfs->vfs_sync_list);
  446. spin_unlock(&vfs->vfs_sync_lock);
  447. wake_up_process(vfs->vfs_sync_task);
  448. }
  449. /*
  450. * Flush delayed allocate data, attempting to free up reserved space
  451. * from existing allocations. At this point a new allocation attempt
  452. * has failed with ENOSPC and we are in the process of scratching our
  453. * heads, looking about for more room...
  454. */
  455. STATIC void
  456. xfs_flush_inode_work(
  457. vfs_t *vfs,
  458. void *inode)
  459. {
  460. filemap_flush(((struct inode *)inode)->i_mapping);
  461. iput((struct inode *)inode);
  462. }
  463. void
  464. xfs_flush_inode(
  465. xfs_inode_t *ip)
  466. {
  467. struct inode *inode = LINVFS_GET_IP(XFS_ITOV(ip));
  468. struct vfs *vfs = XFS_MTOVFS(ip->i_mount);
  469. igrab(inode);
  470. xfs_syncd_queue_work(vfs, inode, xfs_flush_inode_work);
  471. delay(msecs_to_jiffies(500));
  472. }
  473. /*
  474. * This is the "bigger hammer" version of xfs_flush_inode_work...
  475. * (IOW, "If at first you don't succeed, use a Bigger Hammer").
  476. */
  477. STATIC void
  478. xfs_flush_device_work(
  479. vfs_t *vfs,
  480. void *inode)
  481. {
  482. sync_blockdev(vfs->vfs_super->s_bdev);
  483. iput((struct inode *)inode);
  484. }
  485. void
  486. xfs_flush_device(
  487. xfs_inode_t *ip)
  488. {
  489. struct inode *inode = LINVFS_GET_IP(XFS_ITOV(ip));
  490. struct vfs *vfs = XFS_MTOVFS(ip->i_mount);
  491. igrab(inode);
  492. xfs_syncd_queue_work(vfs, inode, xfs_flush_device_work);
  493. delay(msecs_to_jiffies(500));
  494. xfs_log_force(ip->i_mount, (xfs_lsn_t)0, XFS_LOG_FORCE|XFS_LOG_SYNC);
  495. }
  496. #define SYNCD_FLAGS (SYNC_FSDATA|SYNC_BDFLUSH|SYNC_ATTR)
  497. STATIC void
  498. vfs_sync_worker(
  499. vfs_t *vfsp,
  500. void *unused)
  501. {
  502. int error;
  503. if (!(vfsp->vfs_flag & VFS_RDONLY))
  504. VFS_SYNC(vfsp, SYNCD_FLAGS, NULL, error);
  505. vfsp->vfs_sync_seq++;
  506. wmb();
  507. wake_up(&vfsp->vfs_wait_single_sync_task);
  508. }
  509. STATIC int
  510. xfssyncd(
  511. void *arg)
  512. {
  513. long timeleft;
  514. vfs_t *vfsp = (vfs_t *) arg;
  515. struct vfs_sync_work *work, *n;
  516. LIST_HEAD (tmp);
  517. timeleft = xfs_syncd_centisecs * msecs_to_jiffies(10);
  518. for (;;) {
  519. timeleft = schedule_timeout_interruptible(timeleft);
  520. /* swsusp */
  521. try_to_freeze();
  522. if (kthread_should_stop())
  523. break;
  524. spin_lock(&vfsp->vfs_sync_lock);
  525. /*
  526. * We can get woken by laptop mode, to do a sync -
  527. * that's the (only!) case where the list would be
  528. * empty with time remaining.
  529. */
  530. if (!timeleft || list_empty(&vfsp->vfs_sync_list)) {
  531. if (!timeleft)
  532. timeleft = xfs_syncd_centisecs *
  533. msecs_to_jiffies(10);
  534. INIT_LIST_HEAD(&vfsp->vfs_sync_work.w_list);
  535. list_add_tail(&vfsp->vfs_sync_work.w_list,
  536. &vfsp->vfs_sync_list);
  537. }
  538. list_for_each_entry_safe(work, n, &vfsp->vfs_sync_list, w_list)
  539. list_move(&work->w_list, &tmp);
  540. spin_unlock(&vfsp->vfs_sync_lock);
  541. list_for_each_entry_safe(work, n, &tmp, w_list) {
  542. (*work->w_syncer)(vfsp, work->w_data);
  543. list_del(&work->w_list);
  544. if (work == &vfsp->vfs_sync_work)
  545. continue;
  546. kmem_free(work, sizeof(struct vfs_sync_work));
  547. }
  548. }
  549. return 0;
  550. }
  551. STATIC int
  552. linvfs_start_syncd(
  553. vfs_t *vfsp)
  554. {
  555. vfsp->vfs_sync_work.w_syncer = vfs_sync_worker;
  556. vfsp->vfs_sync_work.w_vfs = vfsp;
  557. vfsp->vfs_sync_task = kthread_run(xfssyncd, vfsp, "xfssyncd");
  558. if (IS_ERR(vfsp->vfs_sync_task))
  559. return -PTR_ERR(vfsp->vfs_sync_task);
  560. return 0;
  561. }
  562. STATIC void
  563. linvfs_stop_syncd(
  564. vfs_t *vfsp)
  565. {
  566. kthread_stop(vfsp->vfs_sync_task);
  567. }
  568. STATIC void
  569. linvfs_put_super(
  570. struct super_block *sb)
  571. {
  572. vfs_t *vfsp = LINVFS_GET_VFS(sb);
  573. int error;
  574. linvfs_stop_syncd(vfsp);
  575. VFS_SYNC(vfsp, SYNC_ATTR|SYNC_DELWRI, NULL, error);
  576. if (!error)
  577. VFS_UNMOUNT(vfsp, 0, NULL, error);
  578. if (error) {
  579. printk("XFS unmount got error %d\n", error);
  580. printk("%s: vfsp/0x%p left dangling!\n", __FUNCTION__, vfsp);
  581. return;
  582. }
  583. vfs_deallocate(vfsp);
  584. }
  585. STATIC void
  586. linvfs_write_super(
  587. struct super_block *sb)
  588. {
  589. vfs_t *vfsp = LINVFS_GET_VFS(sb);
  590. int error;
  591. if (sb->s_flags & MS_RDONLY) {
  592. sb->s_dirt = 0; /* paranoia */
  593. return;
  594. }
  595. /* Push the log and superblock a little */
  596. VFS_SYNC(vfsp, SYNC_FSDATA, NULL, error);
  597. sb->s_dirt = 0;
  598. }
  599. STATIC int
  600. linvfs_sync_super(
  601. struct super_block *sb,
  602. int wait)
  603. {
  604. vfs_t *vfsp = LINVFS_GET_VFS(sb);
  605. int error;
  606. int flags = SYNC_FSDATA;
  607. if (unlikely(sb->s_frozen == SB_FREEZE_WRITE))
  608. flags = SYNC_QUIESCE;
  609. else
  610. flags = SYNC_FSDATA | (wait ? SYNC_WAIT : 0);
  611. VFS_SYNC(vfsp, flags, NULL, error);
  612. sb->s_dirt = 0;
  613. if (unlikely(laptop_mode)) {
  614. int prev_sync_seq = vfsp->vfs_sync_seq;
  615. /*
  616. * The disk must be active because we're syncing.
  617. * We schedule xfssyncd now (now that the disk is
  618. * active) instead of later (when it might not be).
  619. */
  620. wake_up_process(vfsp->vfs_sync_task);
  621. /*
  622. * We have to wait for the sync iteration to complete.
  623. * If we don't, the disk activity caused by the sync
  624. * will come after the sync is completed, and that
  625. * triggers another sync from laptop mode.
  626. */
  627. wait_event(vfsp->vfs_wait_single_sync_task,
  628. vfsp->vfs_sync_seq != prev_sync_seq);
  629. }
  630. return -error;
  631. }
  632. STATIC int
  633. linvfs_statfs(
  634. struct super_block *sb,
  635. struct kstatfs *statp)
  636. {
  637. vfs_t *vfsp = LINVFS_GET_VFS(sb);
  638. int error;
  639. VFS_STATVFS(vfsp, statp, NULL, error);
  640. return -error;
  641. }
  642. STATIC int
  643. linvfs_remount(
  644. struct super_block *sb,
  645. int *flags,
  646. char *options)
  647. {
  648. vfs_t *vfsp = LINVFS_GET_VFS(sb);
  649. struct xfs_mount_args *args = xfs_args_allocate(sb);
  650. int error;
  651. VFS_PARSEARGS(vfsp, options, args, 1, error);
  652. if (!error)
  653. VFS_MNTUPDATE(vfsp, flags, args, error);
  654. kmem_free(args, sizeof(*args));
  655. return -error;
  656. }
  657. STATIC void
  658. linvfs_freeze_fs(
  659. struct super_block *sb)
  660. {
  661. VFS_FREEZE(LINVFS_GET_VFS(sb));
  662. }
  663. STATIC int
  664. linvfs_show_options(
  665. struct seq_file *m,
  666. struct vfsmount *mnt)
  667. {
  668. struct vfs *vfsp = LINVFS_GET_VFS(mnt->mnt_sb);
  669. int error;
  670. VFS_SHOWARGS(vfsp, m, 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. .get_xstate = linvfs_getxstate,
  819. .set_xstate = linvfs_setxstate,
  820. .get_xquota = linvfs_getxquota,
  821. .set_xquota = linvfs_setxquota,
  822. };
  823. STATIC struct file_system_type xfs_fs_type = {
  824. .owner = THIS_MODULE,
  825. .name = "xfs",
  826. .get_sb = linvfs_get_sb,
  827. .kill_sb = kill_block_super,
  828. .fs_flags = FS_REQUIRES_DEV,
  829. };
  830. STATIC int __init
  831. init_xfs_fs( void )
  832. {
  833. int error;
  834. struct sysinfo si;
  835. static char message[] __initdata = KERN_INFO \
  836. XFS_VERSION_STRING " with " XFS_BUILD_OPTIONS " enabled\n";
  837. printk(message);
  838. si_meminfo(&si);
  839. xfs_physmem = si.totalram;
  840. ktrace_init(64);
  841. error = linvfs_init_zones();
  842. if (error < 0)
  843. goto undo_zones;
  844. error = pagebuf_init();
  845. if (error < 0)
  846. goto undo_pagebuf;
  847. vn_init();
  848. xfs_init();
  849. uuid_init();
  850. vfs_initquota();
  851. error = register_filesystem(&xfs_fs_type);
  852. if (error)
  853. goto undo_register;
  854. XFS_DM_INIT(&xfs_fs_type);
  855. return 0;
  856. undo_register:
  857. pagebuf_terminate();
  858. undo_pagebuf:
  859. linvfs_destroy_zones();
  860. undo_zones:
  861. return error;
  862. }
  863. STATIC void __exit
  864. exit_xfs_fs( void )
  865. {
  866. vfs_exitquota();
  867. XFS_DM_EXIT(&xfs_fs_type);
  868. unregister_filesystem(&xfs_fs_type);
  869. xfs_cleanup();
  870. pagebuf_terminate();
  871. linvfs_destroy_zones();
  872. ktrace_uninit();
  873. }
  874. module_init(init_xfs_fs);
  875. module_exit(exit_xfs_fs);
  876. MODULE_AUTHOR("Silicon Graphics, Inc.");
  877. MODULE_DESCRIPTION(XFS_VERSION_STRING " with " XFS_BUILD_OPTIONS " enabled");
  878. MODULE_LICENSE("GPL");