xfs_super.c 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001
  1. /*
  2. * Copyright (c) 2000-2006 Silicon Graphics, Inc.
  3. * All Rights Reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it would be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write the Free Software Foundation,
  16. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "xfs.h"
  19. #include "xfs_bit.h"
  20. #include "xfs_log.h"
  21. #include "xfs_clnt.h"
  22. #include "xfs_inum.h"
  23. #include "xfs_trans.h"
  24. #include "xfs_sb.h"
  25. #include "xfs_ag.h"
  26. #include "xfs_dir2.h"
  27. #include "xfs_alloc.h"
  28. #include "xfs_dmapi.h"
  29. #include "xfs_quota.h"
  30. #include "xfs_mount.h"
  31. #include "xfs_bmap_btree.h"
  32. #include "xfs_alloc_btree.h"
  33. #include "xfs_ialloc_btree.h"
  34. #include "xfs_dir2_sf.h"
  35. #include "xfs_attr_sf.h"
  36. #include "xfs_dinode.h"
  37. #include "xfs_inode.h"
  38. #include "xfs_btree.h"
  39. #include "xfs_ialloc.h"
  40. #include "xfs_bmap.h"
  41. #include "xfs_rtalloc.h"
  42. #include "xfs_error.h"
  43. #include "xfs_itable.h"
  44. #include "xfs_fsops.h"
  45. #include "xfs_rw.h"
  46. #include "xfs_acl.h"
  47. #include "xfs_attr.h"
  48. #include "xfs_buf_item.h"
  49. #include "xfs_utils.h"
  50. #include "xfs_vnodeops.h"
  51. #include "xfs_vfsops.h"
  52. #include "xfs_version.h"
  53. #include <linux/namei.h>
  54. #include <linux/init.h>
  55. #include <linux/mount.h>
  56. #include <linux/mempool.h>
  57. #include <linux/writeback.h>
  58. #include <linux/kthread.h>
  59. #include <linux/freezer.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. bhv_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_generation = ip->i_d.di_gen;
  162. i_size_write(inode, ip->i_d.di_size);
  163. inode->i_blocks =
  164. XFS_FSB_TO_BB(mp, ip->i_d.di_nblocks + ip->i_delayed_blks);
  165. inode->i_atime.tv_sec = ip->i_d.di_atime.t_sec;
  166. inode->i_atime.tv_nsec = ip->i_d.di_atime.t_nsec;
  167. inode->i_mtime.tv_sec = ip->i_d.di_mtime.t_sec;
  168. inode->i_mtime.tv_nsec = ip->i_d.di_mtime.t_nsec;
  169. inode->i_ctime.tv_sec = ip->i_d.di_ctime.t_sec;
  170. inode->i_ctime.tv_nsec = ip->i_d.di_ctime.t_nsec;
  171. if (ip->i_d.di_flags & XFS_DIFLAG_IMMUTABLE)
  172. inode->i_flags |= S_IMMUTABLE;
  173. else
  174. inode->i_flags &= ~S_IMMUTABLE;
  175. if (ip->i_d.di_flags & XFS_DIFLAG_APPEND)
  176. inode->i_flags |= S_APPEND;
  177. else
  178. inode->i_flags &= ~S_APPEND;
  179. if (ip->i_d.di_flags & XFS_DIFLAG_SYNC)
  180. inode->i_flags |= S_SYNC;
  181. else
  182. inode->i_flags &= ~S_SYNC;
  183. if (ip->i_d.di_flags & XFS_DIFLAG_NOATIME)
  184. inode->i_flags |= S_NOATIME;
  185. else
  186. inode->i_flags &= ~S_NOATIME;
  187. xfs_iflags_clear(ip, XFS_IMODIFIED);
  188. }
  189. void
  190. xfs_initialize_vnode(
  191. struct xfs_mount *mp,
  192. bhv_vnode_t *vp,
  193. struct xfs_inode *ip)
  194. {
  195. struct inode *inode = vn_to_inode(vp);
  196. if (!ip->i_vnode) {
  197. ip->i_vnode = vp;
  198. inode->i_private = ip;
  199. }
  200. /*
  201. * We need to set the ops vectors, and unlock the inode, but if
  202. * we have been called during the new inode create process, it is
  203. * too early to fill in the Linux inode. We will get called a
  204. * second time once the inode is properly set up, and then we can
  205. * finish our work.
  206. */
  207. if (ip->i_d.di_mode != 0 && (inode->i_state & I_NEW)) {
  208. xfs_revalidate_inode(mp, vp, ip);
  209. xfs_set_inodeops(inode);
  210. xfs_iflags_clear(ip, XFS_INEW);
  211. barrier();
  212. unlock_new_inode(inode);
  213. }
  214. }
  215. int
  216. xfs_blkdev_get(
  217. xfs_mount_t *mp,
  218. const char *name,
  219. struct block_device **bdevp)
  220. {
  221. int error = 0;
  222. *bdevp = open_bdev_excl(name, 0, mp);
  223. if (IS_ERR(*bdevp)) {
  224. error = PTR_ERR(*bdevp);
  225. printk("XFS: Invalid device [%s], error=%d\n", name, error);
  226. }
  227. return -error;
  228. }
  229. void
  230. xfs_blkdev_put(
  231. struct block_device *bdev)
  232. {
  233. if (bdev)
  234. close_bdev_excl(bdev);
  235. }
  236. /*
  237. * Try to write out the superblock using barriers.
  238. */
  239. STATIC int
  240. xfs_barrier_test(
  241. xfs_mount_t *mp)
  242. {
  243. xfs_buf_t *sbp = xfs_getsb(mp, 0);
  244. int error;
  245. XFS_BUF_UNDONE(sbp);
  246. XFS_BUF_UNREAD(sbp);
  247. XFS_BUF_UNDELAYWRITE(sbp);
  248. XFS_BUF_WRITE(sbp);
  249. XFS_BUF_UNASYNC(sbp);
  250. XFS_BUF_ORDERED(sbp);
  251. xfsbdstrat(mp, sbp);
  252. error = xfs_iowait(sbp);
  253. /*
  254. * Clear all the flags we set and possible error state in the
  255. * buffer. We only did the write to try out whether barriers
  256. * worked and shouldn't leave any traces in the superblock
  257. * buffer.
  258. */
  259. XFS_BUF_DONE(sbp);
  260. XFS_BUF_ERROR(sbp, 0);
  261. XFS_BUF_UNORDERED(sbp);
  262. xfs_buf_relse(sbp);
  263. return error;
  264. }
  265. void
  266. xfs_mountfs_check_barriers(xfs_mount_t *mp)
  267. {
  268. int error;
  269. if (mp->m_logdev_targp != mp->m_ddev_targp) {
  270. xfs_fs_cmn_err(CE_NOTE, mp,
  271. "Disabling barriers, not supported with external log device");
  272. mp->m_flags &= ~XFS_MOUNT_BARRIER;
  273. return;
  274. }
  275. if (mp->m_ddev_targp->bt_bdev->bd_disk->queue->ordered ==
  276. QUEUE_ORDERED_NONE) {
  277. xfs_fs_cmn_err(CE_NOTE, mp,
  278. "Disabling barriers, not supported by the underlying device");
  279. mp->m_flags &= ~XFS_MOUNT_BARRIER;
  280. return;
  281. }
  282. if (xfs_readonly_buftarg(mp->m_ddev_targp)) {
  283. xfs_fs_cmn_err(CE_NOTE, mp,
  284. "Disabling barriers, underlying device is readonly");
  285. mp->m_flags &= ~XFS_MOUNT_BARRIER;
  286. return;
  287. }
  288. error = xfs_barrier_test(mp);
  289. if (error) {
  290. xfs_fs_cmn_err(CE_NOTE, mp,
  291. "Disabling barriers, trial barrier write failed");
  292. mp->m_flags &= ~XFS_MOUNT_BARRIER;
  293. return;
  294. }
  295. }
  296. void
  297. xfs_blkdev_issue_flush(
  298. xfs_buftarg_t *buftarg)
  299. {
  300. blkdev_issue_flush(buftarg->bt_bdev, NULL);
  301. }
  302. STATIC struct inode *
  303. xfs_fs_alloc_inode(
  304. struct super_block *sb)
  305. {
  306. bhv_vnode_t *vp;
  307. vp = kmem_zone_alloc(xfs_vnode_zone, KM_SLEEP);
  308. if (unlikely(!vp))
  309. return NULL;
  310. return vn_to_inode(vp);
  311. }
  312. STATIC void
  313. xfs_fs_destroy_inode(
  314. struct inode *inode)
  315. {
  316. kmem_zone_free(xfs_vnode_zone, vn_from_inode(inode));
  317. }
  318. STATIC void
  319. xfs_fs_inode_init_once(
  320. kmem_zone_t *zonep,
  321. void *vnode)
  322. {
  323. inode_init_once(vn_to_inode((bhv_vnode_t *)vnode));
  324. }
  325. STATIC int
  326. xfs_init_zones(void)
  327. {
  328. xfs_vnode_zone = kmem_zone_init_flags(sizeof(bhv_vnode_t), "xfs_vnode",
  329. KM_ZONE_HWALIGN | KM_ZONE_RECLAIM |
  330. KM_ZONE_SPREAD,
  331. xfs_fs_inode_init_once);
  332. if (!xfs_vnode_zone)
  333. goto out;
  334. xfs_ioend_zone = kmem_zone_init(sizeof(xfs_ioend_t), "xfs_ioend");
  335. if (!xfs_ioend_zone)
  336. goto out_destroy_vnode_zone;
  337. xfs_ioend_pool = mempool_create_slab_pool(4 * MAX_BUF_PER_PAGE,
  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. int error = 0, flags = FLUSH_INODE;
  368. xfs_itrace_entry(XFS_I(inode));
  369. if (sync) {
  370. filemap_fdatawait(inode->i_mapping);
  371. flags |= FLUSH_SYNC;
  372. }
  373. error = xfs_inode_flush(XFS_I(inode), flags);
  374. /*
  375. * if we failed to write out the inode then mark
  376. * it dirty again so we'll try again later.
  377. */
  378. if (error)
  379. mark_inode_dirty_sync(inode);
  380. return -error;
  381. }
  382. STATIC void
  383. xfs_fs_clear_inode(
  384. struct inode *inode)
  385. {
  386. xfs_inode_t *ip = XFS_I(inode);
  387. /*
  388. * ip can be null when xfs_iget_core calls xfs_idestroy if we
  389. * find an inode with di_mode == 0 but without IGET_CREATE set.
  390. */
  391. if (ip) {
  392. xfs_itrace_entry(ip);
  393. XFS_STATS_INC(vn_rele);
  394. XFS_STATS_INC(vn_remove);
  395. XFS_STATS_INC(vn_reclaim);
  396. XFS_STATS_DEC(vn_active);
  397. xfs_inactive(ip);
  398. xfs_iflags_clear(ip, XFS_IMODIFIED);
  399. if (xfs_reclaim(ip))
  400. panic("%s: cannot reclaim 0x%p\n", __FUNCTION__, inode);
  401. }
  402. ASSERT(XFS_I(inode) == NULL);
  403. }
  404. /*
  405. * Enqueue a work item to be picked up by the vfs xfssyncd thread.
  406. * Doing this has two advantages:
  407. * - It saves on stack space, which is tight in certain situations
  408. * - It can be used (with care) as a mechanism to avoid deadlocks.
  409. * Flushing while allocating in a full filesystem requires both.
  410. */
  411. STATIC void
  412. xfs_syncd_queue_work(
  413. struct xfs_mount *mp,
  414. void *data,
  415. void (*syncer)(struct xfs_mount *, void *))
  416. {
  417. struct bhv_vfs_sync_work *work;
  418. work = kmem_alloc(sizeof(struct bhv_vfs_sync_work), KM_SLEEP);
  419. INIT_LIST_HEAD(&work->w_list);
  420. work->w_syncer = syncer;
  421. work->w_data = data;
  422. work->w_mount = mp;
  423. spin_lock(&mp->m_sync_lock);
  424. list_add_tail(&work->w_list, &mp->m_sync_list);
  425. spin_unlock(&mp->m_sync_lock);
  426. wake_up_process(mp->m_sync_task);
  427. }
  428. /*
  429. * Flush delayed allocate data, attempting to free up reserved space
  430. * from existing allocations. At this point a new allocation attempt
  431. * has failed with ENOSPC and we are in the process of scratching our
  432. * heads, looking about for more room...
  433. */
  434. STATIC void
  435. xfs_flush_inode_work(
  436. struct xfs_mount *mp,
  437. void *arg)
  438. {
  439. struct inode *inode = arg;
  440. filemap_flush(inode->i_mapping);
  441. iput(inode);
  442. }
  443. void
  444. xfs_flush_inode(
  445. xfs_inode_t *ip)
  446. {
  447. struct inode *inode = ip->i_vnode;
  448. igrab(inode);
  449. xfs_syncd_queue_work(ip->i_mount, inode, xfs_flush_inode_work);
  450. delay(msecs_to_jiffies(500));
  451. }
  452. /*
  453. * This is the "bigger hammer" version of xfs_flush_inode_work...
  454. * (IOW, "If at first you don't succeed, use a Bigger Hammer").
  455. */
  456. STATIC void
  457. xfs_flush_device_work(
  458. struct xfs_mount *mp,
  459. void *arg)
  460. {
  461. struct inode *inode = arg;
  462. sync_blockdev(mp->m_super->s_bdev);
  463. iput(inode);
  464. }
  465. void
  466. xfs_flush_device(
  467. xfs_inode_t *ip)
  468. {
  469. struct inode *inode = vn_to_inode(XFS_ITOV(ip));
  470. igrab(inode);
  471. xfs_syncd_queue_work(ip->i_mount, inode, xfs_flush_device_work);
  472. delay(msecs_to_jiffies(500));
  473. xfs_log_force(ip->i_mount, (xfs_lsn_t)0, XFS_LOG_FORCE|XFS_LOG_SYNC);
  474. }
  475. STATIC void
  476. xfs_sync_worker(
  477. struct xfs_mount *mp,
  478. void *unused)
  479. {
  480. int error;
  481. if (!(mp->m_flags & XFS_MOUNT_RDONLY))
  482. error = xfs_sync(mp, SYNC_FSDATA | SYNC_BDFLUSH | SYNC_ATTR |
  483. SYNC_REFCACHE | SYNC_SUPER);
  484. mp->m_sync_seq++;
  485. wake_up(&mp->m_wait_single_sync_task);
  486. }
  487. STATIC int
  488. xfssyncd(
  489. void *arg)
  490. {
  491. struct xfs_mount *mp = arg;
  492. long timeleft;
  493. bhv_vfs_sync_work_t *work, *n;
  494. LIST_HEAD (tmp);
  495. set_freezable();
  496. timeleft = xfs_syncd_centisecs * msecs_to_jiffies(10);
  497. for (;;) {
  498. timeleft = schedule_timeout_interruptible(timeleft);
  499. /* swsusp */
  500. try_to_freeze();
  501. if (kthread_should_stop() && list_empty(&mp->m_sync_list))
  502. break;
  503. spin_lock(&mp->m_sync_lock);
  504. /*
  505. * We can get woken by laptop mode, to do a sync -
  506. * that's the (only!) case where the list would be
  507. * empty with time remaining.
  508. */
  509. if (!timeleft || list_empty(&mp->m_sync_list)) {
  510. if (!timeleft)
  511. timeleft = xfs_syncd_centisecs *
  512. msecs_to_jiffies(10);
  513. INIT_LIST_HEAD(&mp->m_sync_work.w_list);
  514. list_add_tail(&mp->m_sync_work.w_list,
  515. &mp->m_sync_list);
  516. }
  517. list_for_each_entry_safe(work, n, &mp->m_sync_list, w_list)
  518. list_move(&work->w_list, &tmp);
  519. spin_unlock(&mp->m_sync_lock);
  520. list_for_each_entry_safe(work, n, &tmp, w_list) {
  521. (*work->w_syncer)(mp, work->w_data);
  522. list_del(&work->w_list);
  523. if (work == &mp->m_sync_work)
  524. continue;
  525. kmem_free(work, sizeof(struct bhv_vfs_sync_work));
  526. }
  527. }
  528. return 0;
  529. }
  530. STATIC void
  531. xfs_fs_put_super(
  532. struct super_block *sb)
  533. {
  534. struct xfs_mount *mp = XFS_M(sb);
  535. int error;
  536. kthread_stop(mp->m_sync_task);
  537. xfs_sync(mp, SYNC_ATTR | SYNC_DELWRI);
  538. error = xfs_unmount(mp, 0, NULL);
  539. if (error)
  540. printk("XFS: unmount got error=%d\n", error);
  541. }
  542. STATIC void
  543. xfs_fs_write_super(
  544. struct super_block *sb)
  545. {
  546. if (!(sb->s_flags & MS_RDONLY))
  547. xfs_sync(XFS_M(sb), SYNC_FSDATA);
  548. sb->s_dirt = 0;
  549. }
  550. STATIC int
  551. xfs_fs_sync_super(
  552. struct super_block *sb,
  553. int wait)
  554. {
  555. struct xfs_mount *mp = XFS_M(sb);
  556. int error;
  557. int flags;
  558. /*
  559. * Treat a sync operation like a freeze. This is to work
  560. * around a race in sync_inodes() which works in two phases
  561. * - an asynchronous flush, which can write out an inode
  562. * without waiting for file size updates to complete, and a
  563. * synchronous flush, which wont do anything because the
  564. * async flush removed the inode's dirty flag. Also
  565. * sync_inodes() will not see any files that just have
  566. * outstanding transactions to be flushed because we don't
  567. * dirty the Linux inode until after the transaction I/O
  568. * completes.
  569. */
  570. if (wait || unlikely(sb->s_frozen == SB_FREEZE_WRITE)) {
  571. /*
  572. * First stage of freeze - no more writers will make progress
  573. * now we are here, so we flush delwri and delalloc buffers
  574. * here, then wait for all I/O to complete. Data is frozen at
  575. * that point. Metadata is not frozen, transactions can still
  576. * occur here so don't bother flushing the buftarg (i.e
  577. * SYNC_QUIESCE) because it'll just get dirty again.
  578. */
  579. flags = SYNC_DATA_QUIESCE;
  580. } else
  581. flags = SYNC_FSDATA;
  582. error = xfs_sync(mp, flags);
  583. sb->s_dirt = 0;
  584. if (unlikely(laptop_mode)) {
  585. int prev_sync_seq = mp->m_sync_seq;
  586. /*
  587. * The disk must be active because we're syncing.
  588. * We schedule xfssyncd now (now that the disk is
  589. * active) instead of later (when it might not be).
  590. */
  591. wake_up_process(mp->m_sync_task);
  592. /*
  593. * We have to wait for the sync iteration to complete.
  594. * If we don't, the disk activity caused by the sync
  595. * will come after the sync is completed, and that
  596. * triggers another sync from laptop mode.
  597. */
  598. wait_event(mp->m_wait_single_sync_task,
  599. mp->m_sync_seq != prev_sync_seq);
  600. }
  601. return -error;
  602. }
  603. STATIC int
  604. xfs_fs_statfs(
  605. struct dentry *dentry,
  606. struct kstatfs *statp)
  607. {
  608. struct xfs_mount *mp = XFS_M(dentry->d_sb);
  609. xfs_sb_t *sbp = &mp->m_sb;
  610. __uint64_t fakeinos, id;
  611. xfs_extlen_t lsize;
  612. statp->f_type = XFS_SB_MAGIC;
  613. statp->f_namelen = MAXNAMELEN - 1;
  614. id = huge_encode_dev(mp->m_ddev_targp->bt_dev);
  615. statp->f_fsid.val[0] = (u32)id;
  616. statp->f_fsid.val[1] = (u32)(id >> 32);
  617. xfs_icsb_sync_counters_flags(mp, XFS_ICSB_LAZY_COUNT);
  618. spin_lock(&mp->m_sb_lock);
  619. statp->f_bsize = sbp->sb_blocksize;
  620. lsize = sbp->sb_logstart ? sbp->sb_logblocks : 0;
  621. statp->f_blocks = sbp->sb_dblocks - lsize;
  622. statp->f_bfree = statp->f_bavail =
  623. sbp->sb_fdblocks - XFS_ALLOC_SET_ASIDE(mp);
  624. fakeinos = statp->f_bfree << sbp->sb_inopblog;
  625. #if XFS_BIG_INUMS
  626. fakeinos += mp->m_inoadd;
  627. #endif
  628. statp->f_files =
  629. MIN(sbp->sb_icount + fakeinos, (__uint64_t)XFS_MAXINUMBER);
  630. if (mp->m_maxicount)
  631. #if XFS_BIG_INUMS
  632. if (!mp->m_inoadd)
  633. #endif
  634. statp->f_files = min_t(typeof(statp->f_files),
  635. statp->f_files,
  636. mp->m_maxicount);
  637. statp->f_ffree = statp->f_files - (sbp->sb_icount - sbp->sb_ifree);
  638. spin_unlock(&mp->m_sb_lock);
  639. XFS_QM_DQSTATVFS(XFS_I(dentry->d_inode), statp);
  640. return 0;
  641. }
  642. STATIC int
  643. xfs_fs_remount(
  644. struct super_block *sb,
  645. int *flags,
  646. char *options)
  647. {
  648. struct xfs_mount *mp = XFS_M(sb);
  649. struct xfs_mount_args *args = xfs_args_allocate(sb, 0);
  650. int error;
  651. error = xfs_parseargs(mp, options, args, 1);
  652. if (!error)
  653. error = xfs_mntupdate(mp, flags, args);
  654. kmem_free(args, sizeof(*args));
  655. return -error;
  656. }
  657. /*
  658. * Second stage of a freeze. The data is already frozen so we only
  659. * need to take care of themetadata. Once that's done write a dummy
  660. * record to dirty the log in case of a crash while frozen.
  661. */
  662. STATIC void
  663. xfs_fs_lockfs(
  664. struct super_block *sb)
  665. {
  666. struct xfs_mount *mp = XFS_M(sb);
  667. xfs_attr_quiesce(mp);
  668. xfs_fs_log_dummy(mp);
  669. }
  670. STATIC int
  671. xfs_fs_show_options(
  672. struct seq_file *m,
  673. struct vfsmount *mnt)
  674. {
  675. return -xfs_showargs(XFS_M(mnt->mnt_sb), m);
  676. }
  677. STATIC int
  678. xfs_fs_quotasync(
  679. struct super_block *sb,
  680. int type)
  681. {
  682. return -XFS_QM_QUOTACTL(XFS_M(sb), Q_XQUOTASYNC, 0, NULL);
  683. }
  684. STATIC int
  685. xfs_fs_getxstate(
  686. struct super_block *sb,
  687. struct fs_quota_stat *fqs)
  688. {
  689. return -XFS_QM_QUOTACTL(XFS_M(sb), Q_XGETQSTAT, 0, (caddr_t)fqs);
  690. }
  691. STATIC int
  692. xfs_fs_setxstate(
  693. struct super_block *sb,
  694. unsigned int flags,
  695. int op)
  696. {
  697. return -XFS_QM_QUOTACTL(XFS_M(sb), op, 0, (caddr_t)&flags);
  698. }
  699. STATIC int
  700. xfs_fs_getxquota(
  701. struct super_block *sb,
  702. int type,
  703. qid_t id,
  704. struct fs_disk_quota *fdq)
  705. {
  706. return -XFS_QM_QUOTACTL(XFS_M(sb),
  707. (type == USRQUOTA) ? Q_XGETQUOTA :
  708. ((type == GRPQUOTA) ? Q_XGETGQUOTA :
  709. Q_XGETPQUOTA), id, (caddr_t)fdq);
  710. }
  711. STATIC int
  712. xfs_fs_setxquota(
  713. struct super_block *sb,
  714. int type,
  715. qid_t id,
  716. struct fs_disk_quota *fdq)
  717. {
  718. return -XFS_QM_QUOTACTL(XFS_M(sb),
  719. (type == USRQUOTA) ? Q_XSETQLIM :
  720. ((type == GRPQUOTA) ? Q_XSETGQLIM :
  721. Q_XSETPQLIM), id, (caddr_t)fdq);
  722. }
  723. STATIC int
  724. xfs_fs_fill_super(
  725. struct super_block *sb,
  726. void *data,
  727. int silent)
  728. {
  729. struct inode *rootvp;
  730. struct xfs_mount *mp = NULL;
  731. struct xfs_mount_args *args = xfs_args_allocate(sb, silent);
  732. int error;
  733. mp = xfs_mount_init();
  734. INIT_LIST_HEAD(&mp->m_sync_list);
  735. spin_lock_init(&mp->m_sync_lock);
  736. init_waitqueue_head(&mp->m_wait_single_sync_task);
  737. mp->m_super = sb;
  738. sb->s_fs_info = mp;
  739. if (sb->s_flags & MS_RDONLY)
  740. mp->m_flags |= XFS_MOUNT_RDONLY;
  741. error = xfs_parseargs(mp, (char *)data, args, 0);
  742. if (error)
  743. goto fail_vfsop;
  744. sb_min_blocksize(sb, BBSIZE);
  745. sb->s_export_op = &xfs_export_operations;
  746. sb->s_qcop = &xfs_quotactl_operations;
  747. sb->s_op = &xfs_super_operations;
  748. error = xfs_mount(mp, args, NULL);
  749. if (error)
  750. goto fail_vfsop;
  751. sb->s_dirt = 1;
  752. sb->s_magic = XFS_SB_MAGIC;
  753. sb->s_blocksize = mp->m_sb.sb_blocksize;
  754. sb->s_blocksize_bits = ffs(sb->s_blocksize) - 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. error = xfs_root(mp, &rootvp);
  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. mp->m_sync_work.w_syncer = xfs_sync_worker;
  771. mp->m_sync_work.w_mount = mp;
  772. mp->m_sync_task = kthread_run(xfssyncd, mp, "xfssyncd");
  773. if (IS_ERR(mp->m_sync_task)) {
  774. error = -PTR_ERR(mp->m_sync_task);
  775. goto fail_vnrele;
  776. }
  777. xfs_itrace_exit(XFS_I(sb->s_root->d_inode));
  778. kmem_free(args, sizeof(*args));
  779. return 0;
  780. fail_vnrele:
  781. if (sb->s_root) {
  782. dput(sb->s_root);
  783. sb->s_root = NULL;
  784. } else {
  785. VN_RELE(rootvp);
  786. }
  787. fail_unmount:
  788. xfs_unmount(mp, 0, NULL);
  789. fail_vfsop:
  790. kmem_free(args, sizeof(*args));
  791. return -error;
  792. }
  793. STATIC int
  794. xfs_fs_get_sb(
  795. struct file_system_type *fs_type,
  796. int flags,
  797. const char *dev_name,
  798. void *data,
  799. struct vfsmount *mnt)
  800. {
  801. return get_sb_bdev(fs_type, flags, dev_name, data, xfs_fs_fill_super,
  802. mnt);
  803. }
  804. static struct super_operations xfs_super_operations = {
  805. .alloc_inode = xfs_fs_alloc_inode,
  806. .destroy_inode = xfs_fs_destroy_inode,
  807. .write_inode = xfs_fs_write_inode,
  808. .clear_inode = xfs_fs_clear_inode,
  809. .put_super = xfs_fs_put_super,
  810. .write_super = xfs_fs_write_super,
  811. .sync_fs = xfs_fs_sync_super,
  812. .write_super_lockfs = xfs_fs_lockfs,
  813. .statfs = xfs_fs_statfs,
  814. .remount_fs = xfs_fs_remount,
  815. .show_options = xfs_fs_show_options,
  816. };
  817. static struct quotactl_ops xfs_quotactl_operations = {
  818. .quota_sync = xfs_fs_quotasync,
  819. .get_xstate = xfs_fs_getxstate,
  820. .set_xstate = xfs_fs_setxstate,
  821. .get_xquota = xfs_fs_getxquota,
  822. .set_xquota = xfs_fs_setxquota,
  823. };
  824. static struct file_system_type xfs_fs_type = {
  825. .owner = THIS_MODULE,
  826. .name = "xfs",
  827. .get_sb = xfs_fs_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. static char message[] __initdata = KERN_INFO \
  836. XFS_VERSION_STRING " with " XFS_BUILD_OPTIONS " enabled\n";
  837. printk(message);
  838. ktrace_init(64);
  839. error = xfs_init_zones();
  840. if (error < 0)
  841. goto undo_zones;
  842. error = xfs_buf_init();
  843. if (error < 0)
  844. goto undo_buffers;
  845. vn_init();
  846. xfs_init();
  847. uuid_init();
  848. vfs_initquota();
  849. error = register_filesystem(&xfs_fs_type);
  850. if (error)
  851. goto undo_register;
  852. return 0;
  853. undo_register:
  854. xfs_buf_terminate();
  855. undo_buffers:
  856. xfs_destroy_zones();
  857. undo_zones:
  858. return error;
  859. }
  860. STATIC void __exit
  861. exit_xfs_fs( void )
  862. {
  863. vfs_exitquota();
  864. unregister_filesystem(&xfs_fs_type);
  865. xfs_cleanup();
  866. xfs_buf_terminate();
  867. xfs_destroy_zones();
  868. ktrace_uninit();
  869. }
  870. module_init(init_xfs_fs);
  871. module_exit(exit_xfs_fs);
  872. MODULE_AUTHOR("Silicon Graphics, Inc.");
  873. MODULE_DESCRIPTION(XFS_VERSION_STRING " with " XFS_BUILD_OPTIONS " enabled");
  874. MODULE_LICENSE("GPL");