xfs_vfsops.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364
  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_fs.h"
  20. #include "xfs_types.h"
  21. #include "xfs_bit.h"
  22. #include "xfs_log.h"
  23. #include "xfs_inum.h"
  24. #include "xfs_trans.h"
  25. #include "xfs_sb.h"
  26. #include "xfs_ag.h"
  27. #include "xfs_dir2.h"
  28. #include "xfs_dmapi.h"
  29. #include "xfs_mount.h"
  30. #include "xfs_da_btree.h"
  31. #include "xfs_bmap_btree.h"
  32. #include "xfs_ialloc_btree.h"
  33. #include "xfs_alloc_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_inode_item.h"
  39. #include "xfs_btree.h"
  40. #include "xfs_alloc.h"
  41. #include "xfs_ialloc.h"
  42. #include "xfs_quota.h"
  43. #include "xfs_error.h"
  44. #include "xfs_bmap.h"
  45. #include "xfs_rw.h"
  46. #include "xfs_buf_item.h"
  47. #include "xfs_log_priv.h"
  48. #include "xfs_dir2_trace.h"
  49. #include "xfs_extfree_item.h"
  50. #include "xfs_acl.h"
  51. #include "xfs_attr.h"
  52. #include "xfs_clnt.h"
  53. #include "xfs_mru_cache.h"
  54. #include "xfs_filestream.h"
  55. #include "xfs_fsops.h"
  56. #include "xfs_vnodeops.h"
  57. #include "xfs_vfsops.h"
  58. int __init
  59. xfs_init(void)
  60. {
  61. #ifdef XFS_DABUF_DEBUG
  62. extern spinlock_t xfs_dabuf_global_lock;
  63. spin_lock_init(&xfs_dabuf_global_lock);
  64. #endif
  65. /*
  66. * Initialize all of the zone allocators we use.
  67. */
  68. xfs_bmap_free_item_zone = kmem_zone_init(sizeof(xfs_bmap_free_item_t),
  69. "xfs_bmap_free_item");
  70. xfs_btree_cur_zone = kmem_zone_init(sizeof(xfs_btree_cur_t),
  71. "xfs_btree_cur");
  72. xfs_trans_zone = kmem_zone_init(sizeof(xfs_trans_t), "xfs_trans");
  73. xfs_da_state_zone =
  74. kmem_zone_init(sizeof(xfs_da_state_t), "xfs_da_state");
  75. xfs_dabuf_zone = kmem_zone_init(sizeof(xfs_dabuf_t), "xfs_dabuf");
  76. xfs_ifork_zone = kmem_zone_init(sizeof(xfs_ifork_t), "xfs_ifork");
  77. xfs_acl_zone_init(xfs_acl_zone, "xfs_acl");
  78. xfs_mru_cache_init();
  79. xfs_filestream_init();
  80. /*
  81. * The size of the zone allocated buf log item is the maximum
  82. * size possible under XFS. This wastes a little bit of memory,
  83. * but it is much faster.
  84. */
  85. xfs_buf_item_zone =
  86. kmem_zone_init((sizeof(xfs_buf_log_item_t) +
  87. (((XFS_MAX_BLOCKSIZE / XFS_BLI_CHUNK) /
  88. NBWORD) * sizeof(int))),
  89. "xfs_buf_item");
  90. xfs_efd_zone =
  91. kmem_zone_init((sizeof(xfs_efd_log_item_t) +
  92. ((XFS_EFD_MAX_FAST_EXTENTS - 1) *
  93. sizeof(xfs_extent_t))),
  94. "xfs_efd_item");
  95. xfs_efi_zone =
  96. kmem_zone_init((sizeof(xfs_efi_log_item_t) +
  97. ((XFS_EFI_MAX_FAST_EXTENTS - 1) *
  98. sizeof(xfs_extent_t))),
  99. "xfs_efi_item");
  100. /*
  101. * These zones warrant special memory allocator hints
  102. */
  103. xfs_inode_zone =
  104. kmem_zone_init_flags(sizeof(xfs_inode_t), "xfs_inode",
  105. KM_ZONE_HWALIGN | KM_ZONE_RECLAIM |
  106. KM_ZONE_SPREAD, NULL);
  107. xfs_ili_zone =
  108. kmem_zone_init_flags(sizeof(xfs_inode_log_item_t), "xfs_ili",
  109. KM_ZONE_SPREAD, NULL);
  110. /*
  111. * Allocate global trace buffers.
  112. */
  113. #ifdef XFS_ALLOC_TRACE
  114. xfs_alloc_trace_buf = ktrace_alloc(XFS_ALLOC_TRACE_SIZE, KM_SLEEP);
  115. #endif
  116. #ifdef XFS_BMAP_TRACE
  117. xfs_bmap_trace_buf = ktrace_alloc(XFS_BMAP_TRACE_SIZE, KM_SLEEP);
  118. #endif
  119. #ifdef XFS_BMBT_TRACE
  120. xfs_bmbt_trace_buf = ktrace_alloc(XFS_BMBT_TRACE_SIZE, KM_SLEEP);
  121. #endif
  122. #ifdef XFS_ATTR_TRACE
  123. xfs_attr_trace_buf = ktrace_alloc(XFS_ATTR_TRACE_SIZE, KM_SLEEP);
  124. #endif
  125. #ifdef XFS_DIR2_TRACE
  126. xfs_dir2_trace_buf = ktrace_alloc(XFS_DIR2_GTRACE_SIZE, KM_SLEEP);
  127. #endif
  128. xfs_dir_startup();
  129. #if (defined(DEBUG) || defined(INDUCE_IO_ERROR))
  130. xfs_error_test_init();
  131. #endif /* DEBUG || INDUCE_IO_ERROR */
  132. xfs_init_procfs();
  133. xfs_sysctl_register();
  134. return 0;
  135. }
  136. void __exit
  137. xfs_cleanup(void)
  138. {
  139. extern kmem_zone_t *xfs_inode_zone;
  140. extern kmem_zone_t *xfs_efd_zone;
  141. extern kmem_zone_t *xfs_efi_zone;
  142. xfs_cleanup_procfs();
  143. xfs_sysctl_unregister();
  144. xfs_filestream_uninit();
  145. xfs_mru_cache_uninit();
  146. xfs_acl_zone_destroy(xfs_acl_zone);
  147. #ifdef XFS_DIR2_TRACE
  148. ktrace_free(xfs_dir2_trace_buf);
  149. #endif
  150. #ifdef XFS_ATTR_TRACE
  151. ktrace_free(xfs_attr_trace_buf);
  152. #endif
  153. #ifdef XFS_BMBT_TRACE
  154. ktrace_free(xfs_bmbt_trace_buf);
  155. #endif
  156. #ifdef XFS_BMAP_TRACE
  157. ktrace_free(xfs_bmap_trace_buf);
  158. #endif
  159. #ifdef XFS_ALLOC_TRACE
  160. ktrace_free(xfs_alloc_trace_buf);
  161. #endif
  162. kmem_zone_destroy(xfs_bmap_free_item_zone);
  163. kmem_zone_destroy(xfs_btree_cur_zone);
  164. kmem_zone_destroy(xfs_inode_zone);
  165. kmem_zone_destroy(xfs_trans_zone);
  166. kmem_zone_destroy(xfs_da_state_zone);
  167. kmem_zone_destroy(xfs_dabuf_zone);
  168. kmem_zone_destroy(xfs_buf_item_zone);
  169. kmem_zone_destroy(xfs_efd_zone);
  170. kmem_zone_destroy(xfs_efi_zone);
  171. kmem_zone_destroy(xfs_ifork_zone);
  172. kmem_zone_destroy(xfs_ili_zone);
  173. }
  174. /*
  175. * xfs_start_flags
  176. *
  177. * This function fills in xfs_mount_t fields based on mount args.
  178. * Note: the superblock has _not_ yet been read in.
  179. */
  180. STATIC int
  181. xfs_start_flags(
  182. struct xfs_mount_args *ap,
  183. struct xfs_mount *mp)
  184. {
  185. /* Values are in BBs */
  186. if ((ap->flags & XFSMNT_NOALIGN) != XFSMNT_NOALIGN) {
  187. /*
  188. * At this point the superblock has not been read
  189. * in, therefore we do not know the block size.
  190. * Before the mount call ends we will convert
  191. * these to FSBs.
  192. */
  193. mp->m_dalign = ap->sunit;
  194. mp->m_swidth = ap->swidth;
  195. }
  196. if (ap->logbufs != -1 &&
  197. ap->logbufs != 0 &&
  198. (ap->logbufs < XLOG_MIN_ICLOGS ||
  199. ap->logbufs > XLOG_MAX_ICLOGS)) {
  200. cmn_err(CE_WARN,
  201. "XFS: invalid logbufs value: %d [not %d-%d]",
  202. ap->logbufs, XLOG_MIN_ICLOGS, XLOG_MAX_ICLOGS);
  203. return XFS_ERROR(EINVAL);
  204. }
  205. mp->m_logbufs = ap->logbufs;
  206. if (ap->logbufsize != -1 &&
  207. ap->logbufsize != 0 &&
  208. (ap->logbufsize < XLOG_MIN_RECORD_BSIZE ||
  209. ap->logbufsize > XLOG_MAX_RECORD_BSIZE ||
  210. !is_power_of_2(ap->logbufsize))) {
  211. cmn_err(CE_WARN,
  212. "XFS: invalid logbufsize: %d [not 16k,32k,64k,128k or 256k]",
  213. ap->logbufsize);
  214. return XFS_ERROR(EINVAL);
  215. }
  216. mp->m_logbsize = ap->logbufsize;
  217. mp->m_fsname_len = strlen(ap->fsname) + 1;
  218. mp->m_fsname = kmem_alloc(mp->m_fsname_len, KM_SLEEP);
  219. strcpy(mp->m_fsname, ap->fsname);
  220. if (ap->rtname[0]) {
  221. mp->m_rtname = kmem_alloc(strlen(ap->rtname) + 1, KM_SLEEP);
  222. strcpy(mp->m_rtname, ap->rtname);
  223. }
  224. if (ap->logname[0]) {
  225. mp->m_logname = kmem_alloc(strlen(ap->logname) + 1, KM_SLEEP);
  226. strcpy(mp->m_logname, ap->logname);
  227. }
  228. if (ap->flags & XFSMNT_WSYNC)
  229. mp->m_flags |= XFS_MOUNT_WSYNC;
  230. #if XFS_BIG_INUMS
  231. if (ap->flags & XFSMNT_INO64) {
  232. mp->m_flags |= XFS_MOUNT_INO64;
  233. mp->m_inoadd = XFS_INO64_OFFSET;
  234. }
  235. #endif
  236. if (ap->flags & XFSMNT_RETERR)
  237. mp->m_flags |= XFS_MOUNT_RETERR;
  238. if (ap->flags & XFSMNT_NOALIGN)
  239. mp->m_flags |= XFS_MOUNT_NOALIGN;
  240. if (ap->flags & XFSMNT_SWALLOC)
  241. mp->m_flags |= XFS_MOUNT_SWALLOC;
  242. if (ap->flags & XFSMNT_OSYNCISOSYNC)
  243. mp->m_flags |= XFS_MOUNT_OSYNCISOSYNC;
  244. if (ap->flags & XFSMNT_32BITINODES)
  245. mp->m_flags |= XFS_MOUNT_32BITINODES;
  246. if (ap->flags & XFSMNT_IOSIZE) {
  247. if (ap->iosizelog > XFS_MAX_IO_LOG ||
  248. ap->iosizelog < XFS_MIN_IO_LOG) {
  249. cmn_err(CE_WARN,
  250. "XFS: invalid log iosize: %d [not %d-%d]",
  251. ap->iosizelog, XFS_MIN_IO_LOG,
  252. XFS_MAX_IO_LOG);
  253. return XFS_ERROR(EINVAL);
  254. }
  255. mp->m_flags |= XFS_MOUNT_DFLT_IOSIZE;
  256. mp->m_readio_log = mp->m_writeio_log = ap->iosizelog;
  257. }
  258. if (ap->flags & XFSMNT_IKEEP)
  259. mp->m_flags |= XFS_MOUNT_IKEEP;
  260. if (ap->flags & XFSMNT_DIRSYNC)
  261. mp->m_flags |= XFS_MOUNT_DIRSYNC;
  262. if (ap->flags & XFSMNT_ATTR2)
  263. mp->m_flags |= XFS_MOUNT_ATTR2;
  264. if (ap->flags2 & XFSMNT2_COMPAT_IOSIZE)
  265. mp->m_flags |= XFS_MOUNT_COMPAT_IOSIZE;
  266. /*
  267. * no recovery flag requires a read-only mount
  268. */
  269. if (ap->flags & XFSMNT_NORECOVERY) {
  270. if (!(mp->m_flags & XFS_MOUNT_RDONLY)) {
  271. cmn_err(CE_WARN,
  272. "XFS: tried to mount a FS read-write without recovery!");
  273. return XFS_ERROR(EINVAL);
  274. }
  275. mp->m_flags |= XFS_MOUNT_NORECOVERY;
  276. }
  277. if (ap->flags & XFSMNT_NOUUID)
  278. mp->m_flags |= XFS_MOUNT_NOUUID;
  279. if (ap->flags & XFSMNT_BARRIER)
  280. mp->m_flags |= XFS_MOUNT_BARRIER;
  281. else
  282. mp->m_flags &= ~XFS_MOUNT_BARRIER;
  283. if (ap->flags2 & XFSMNT2_FILESTREAMS)
  284. mp->m_flags |= XFS_MOUNT_FILESTREAMS;
  285. if (ap->flags & XFSMNT_DMAPI)
  286. mp->m_flags |= XFS_MOUNT_DMAPI;
  287. return 0;
  288. }
  289. /*
  290. * This function fills in xfs_mount_t fields based on mount args.
  291. * Note: the superblock _has_ now been read in.
  292. */
  293. STATIC int
  294. xfs_finish_flags(
  295. struct xfs_mount_args *ap,
  296. struct xfs_mount *mp)
  297. {
  298. int ronly = (mp->m_flags & XFS_MOUNT_RDONLY);
  299. /* Fail a mount where the logbuf is smaller then the log stripe */
  300. if (xfs_sb_version_haslogv2(&mp->m_sb)) {
  301. if ((ap->logbufsize <= 0) &&
  302. (mp->m_sb.sb_logsunit > XLOG_BIG_RECORD_BSIZE)) {
  303. mp->m_logbsize = mp->m_sb.sb_logsunit;
  304. } else if (ap->logbufsize > 0 &&
  305. ap->logbufsize < mp->m_sb.sb_logsunit) {
  306. cmn_err(CE_WARN,
  307. "XFS: logbuf size must be greater than or equal to log stripe size");
  308. return XFS_ERROR(EINVAL);
  309. }
  310. } else {
  311. /* Fail a mount if the logbuf is larger than 32K */
  312. if (ap->logbufsize > XLOG_BIG_RECORD_BSIZE) {
  313. cmn_err(CE_WARN,
  314. "XFS: logbuf size for version 1 logs must be 16K or 32K");
  315. return XFS_ERROR(EINVAL);
  316. }
  317. }
  318. if (xfs_sb_version_hasattr2(&mp->m_sb))
  319. mp->m_flags |= XFS_MOUNT_ATTR2;
  320. /*
  321. * prohibit r/w mounts of read-only filesystems
  322. */
  323. if ((mp->m_sb.sb_flags & XFS_SBF_READONLY) && !ronly) {
  324. cmn_err(CE_WARN,
  325. "XFS: cannot mount a read-only filesystem as read-write");
  326. return XFS_ERROR(EROFS);
  327. }
  328. /*
  329. * check for shared mount.
  330. */
  331. if (ap->flags & XFSMNT_SHARED) {
  332. if (!xfs_sb_version_hasshared(&mp->m_sb))
  333. return XFS_ERROR(EINVAL);
  334. /*
  335. * For IRIX 6.5, shared mounts must have the shared
  336. * version bit set, have the persistent readonly
  337. * field set, must be version 0 and can only be mounted
  338. * read-only.
  339. */
  340. if (!ronly || !(mp->m_sb.sb_flags & XFS_SBF_READONLY) ||
  341. (mp->m_sb.sb_shared_vn != 0))
  342. return XFS_ERROR(EINVAL);
  343. mp->m_flags |= XFS_MOUNT_SHARED;
  344. /*
  345. * Shared XFS V0 can't deal with DMI. Return EINVAL.
  346. */
  347. if (mp->m_sb.sb_shared_vn == 0 && (ap->flags & XFSMNT_DMAPI))
  348. return XFS_ERROR(EINVAL);
  349. }
  350. if (ap->flags & XFSMNT_UQUOTA) {
  351. mp->m_qflags |= (XFS_UQUOTA_ACCT | XFS_UQUOTA_ACTIVE);
  352. if (ap->flags & XFSMNT_UQUOTAENF)
  353. mp->m_qflags |= XFS_UQUOTA_ENFD;
  354. }
  355. if (ap->flags & XFSMNT_GQUOTA) {
  356. mp->m_qflags |= (XFS_GQUOTA_ACCT | XFS_GQUOTA_ACTIVE);
  357. if (ap->flags & XFSMNT_GQUOTAENF)
  358. mp->m_qflags |= XFS_OQUOTA_ENFD;
  359. } else if (ap->flags & XFSMNT_PQUOTA) {
  360. mp->m_qflags |= (XFS_PQUOTA_ACCT | XFS_PQUOTA_ACTIVE);
  361. if (ap->flags & XFSMNT_PQUOTAENF)
  362. mp->m_qflags |= XFS_OQUOTA_ENFD;
  363. }
  364. return 0;
  365. }
  366. /*
  367. * xfs_mount
  368. *
  369. * The file system configurations are:
  370. * (1) device (partition) with data and internal log
  371. * (2) logical volume with data and log subvolumes.
  372. * (3) logical volume with data, log, and realtime subvolumes.
  373. *
  374. * We only have to handle opening the log and realtime volumes here if
  375. * they are present. The data subvolume has already been opened by
  376. * get_sb_bdev() and is stored in vfsp->vfs_super->s_bdev.
  377. */
  378. int
  379. xfs_mount(
  380. struct xfs_mount *mp,
  381. struct xfs_mount_args *args,
  382. cred_t *credp)
  383. {
  384. struct block_device *ddev, *logdev, *rtdev;
  385. int flags = 0, error;
  386. ddev = mp->m_super->s_bdev;
  387. logdev = rtdev = NULL;
  388. error = xfs_dmops_get(mp, args);
  389. if (error)
  390. return error;
  391. error = xfs_qmops_get(mp, args);
  392. if (error)
  393. return error;
  394. if (args->flags & XFSMNT_QUIET)
  395. flags |= XFS_MFSI_QUIET;
  396. /*
  397. * Open real time and log devices - order is important.
  398. */
  399. if (args->logname[0]) {
  400. error = xfs_blkdev_get(mp, args->logname, &logdev);
  401. if (error)
  402. return error;
  403. }
  404. if (args->rtname[0]) {
  405. error = xfs_blkdev_get(mp, args->rtname, &rtdev);
  406. if (error) {
  407. xfs_blkdev_put(logdev);
  408. return error;
  409. }
  410. if (rtdev == ddev || rtdev == logdev) {
  411. cmn_err(CE_WARN,
  412. "XFS: Cannot mount filesystem with identical rtdev and ddev/logdev.");
  413. xfs_blkdev_put(logdev);
  414. xfs_blkdev_put(rtdev);
  415. return EINVAL;
  416. }
  417. }
  418. /*
  419. * Setup xfs_mount buffer target pointers
  420. */
  421. error = ENOMEM;
  422. mp->m_ddev_targp = xfs_alloc_buftarg(ddev, 0);
  423. if (!mp->m_ddev_targp) {
  424. xfs_blkdev_put(logdev);
  425. xfs_blkdev_put(rtdev);
  426. return error;
  427. }
  428. if (rtdev) {
  429. mp->m_rtdev_targp = xfs_alloc_buftarg(rtdev, 1);
  430. if (!mp->m_rtdev_targp) {
  431. xfs_blkdev_put(logdev);
  432. xfs_blkdev_put(rtdev);
  433. goto error0;
  434. }
  435. }
  436. mp->m_logdev_targp = (logdev && logdev != ddev) ?
  437. xfs_alloc_buftarg(logdev, 1) : mp->m_ddev_targp;
  438. if (!mp->m_logdev_targp) {
  439. xfs_blkdev_put(logdev);
  440. xfs_blkdev_put(rtdev);
  441. goto error0;
  442. }
  443. /*
  444. * Setup flags based on mount(2) options and then the superblock
  445. */
  446. error = xfs_start_flags(args, mp);
  447. if (error)
  448. goto error1;
  449. error = xfs_readsb(mp, flags);
  450. if (error)
  451. goto error1;
  452. error = xfs_finish_flags(args, mp);
  453. if (error)
  454. goto error2;
  455. /*
  456. * Setup xfs_mount buffer target pointers based on superblock
  457. */
  458. error = xfs_setsize_buftarg(mp->m_ddev_targp, mp->m_sb.sb_blocksize,
  459. mp->m_sb.sb_sectsize);
  460. if (!error && logdev && logdev != ddev) {
  461. unsigned int log_sector_size = BBSIZE;
  462. if (xfs_sb_version_hassector(&mp->m_sb))
  463. log_sector_size = mp->m_sb.sb_logsectsize;
  464. error = xfs_setsize_buftarg(mp->m_logdev_targp,
  465. mp->m_sb.sb_blocksize,
  466. log_sector_size);
  467. }
  468. if (!error && rtdev)
  469. error = xfs_setsize_buftarg(mp->m_rtdev_targp,
  470. mp->m_sb.sb_blocksize,
  471. mp->m_sb.sb_sectsize);
  472. if (error)
  473. goto error2;
  474. if (mp->m_flags & XFS_MOUNT_BARRIER)
  475. xfs_mountfs_check_barriers(mp);
  476. if ((error = xfs_filestream_mount(mp)))
  477. goto error2;
  478. error = xfs_mountfs(mp, flags);
  479. if (error)
  480. goto error2;
  481. XFS_SEND_MOUNT(mp, DM_RIGHT_NULL, args->mtpt, args->fsname);
  482. return 0;
  483. error2:
  484. if (mp->m_sb_bp)
  485. xfs_freesb(mp);
  486. error1:
  487. xfs_binval(mp->m_ddev_targp);
  488. if (logdev && logdev != ddev)
  489. xfs_binval(mp->m_logdev_targp);
  490. if (rtdev)
  491. xfs_binval(mp->m_rtdev_targp);
  492. error0:
  493. xfs_unmountfs_close(mp, credp);
  494. xfs_qmops_put(mp);
  495. xfs_dmops_put(mp);
  496. return error;
  497. }
  498. int
  499. xfs_unmount(
  500. xfs_mount_t *mp,
  501. int flags,
  502. cred_t *credp)
  503. {
  504. xfs_inode_t *rip;
  505. bhv_vnode_t *rvp;
  506. int unmount_event_wanted = 0;
  507. int unmount_event_flags = 0;
  508. int xfs_unmountfs_needed = 0;
  509. int error;
  510. rip = mp->m_rootip;
  511. rvp = XFS_ITOV(rip);
  512. #ifdef HAVE_DMAPI
  513. if (mp->m_flags & XFS_MOUNT_DMAPI) {
  514. error = XFS_SEND_PREUNMOUNT(mp,
  515. rip, DM_RIGHT_NULL, rip, DM_RIGHT_NULL,
  516. NULL, NULL, 0, 0,
  517. (mp->m_dmevmask & (1<<DM_EVENT_PREUNMOUNT))?
  518. 0:DM_FLAGS_UNWANTED);
  519. if (error)
  520. return XFS_ERROR(error);
  521. unmount_event_wanted = 1;
  522. unmount_event_flags = (mp->m_dmevmask & (1<<DM_EVENT_UNMOUNT))?
  523. 0 : DM_FLAGS_UNWANTED;
  524. }
  525. #endif
  526. /*
  527. * Blow away any referenced inode in the filestreams cache.
  528. * This can and will cause log traffic as inodes go inactive
  529. * here.
  530. */
  531. xfs_filestream_unmount(mp);
  532. XFS_bflush(mp->m_ddev_targp);
  533. error = xfs_unmount_flush(mp, 0);
  534. if (error)
  535. goto out;
  536. ASSERT(vn_count(rvp) == 1);
  537. /*
  538. * Drop the reference count
  539. */
  540. VN_RELE(rvp);
  541. /*
  542. * If we're forcing a shutdown, typically because of a media error,
  543. * we want to make sure we invalidate dirty pages that belong to
  544. * referenced vnodes as well.
  545. */
  546. if (XFS_FORCED_SHUTDOWN(mp)) {
  547. error = xfs_sync(mp, SYNC_WAIT | SYNC_CLOSE);
  548. ASSERT(error != EFSCORRUPTED);
  549. }
  550. xfs_unmountfs_needed = 1;
  551. out:
  552. /* Send DMAPI event, if required.
  553. * Then do xfs_unmountfs() if needed.
  554. * Then return error (or zero).
  555. */
  556. if (unmount_event_wanted) {
  557. /* Note: mp structure must still exist for
  558. * XFS_SEND_UNMOUNT() call.
  559. */
  560. XFS_SEND_UNMOUNT(mp, error == 0 ? rip : NULL,
  561. DM_RIGHT_NULL, 0, error, unmount_event_flags);
  562. }
  563. if (xfs_unmountfs_needed) {
  564. /*
  565. * Call common unmount function to flush to disk
  566. * and free the super block buffer & mount structures.
  567. */
  568. xfs_unmountfs(mp, credp);
  569. xfs_qmops_put(mp);
  570. xfs_dmops_put(mp);
  571. kmem_free(mp, sizeof(xfs_mount_t));
  572. }
  573. return XFS_ERROR(error);
  574. }
  575. STATIC int
  576. xfs_quiesce_fs(
  577. xfs_mount_t *mp)
  578. {
  579. int count = 0, pincount;
  580. xfs_flush_buftarg(mp->m_ddev_targp, 0);
  581. xfs_finish_reclaim_all(mp, 0);
  582. /* This loop must run at least twice.
  583. * The first instance of the loop will flush
  584. * most meta data but that will generate more
  585. * meta data (typically directory updates).
  586. * Which then must be flushed and logged before
  587. * we can write the unmount record.
  588. */
  589. do {
  590. xfs_syncsub(mp, SYNC_INODE_QUIESCE, NULL);
  591. pincount = xfs_flush_buftarg(mp->m_ddev_targp, 1);
  592. if (!pincount) {
  593. delay(50);
  594. count++;
  595. }
  596. } while (count < 2);
  597. return 0;
  598. }
  599. /*
  600. * Second stage of a quiesce. The data is already synced, now we have to take
  601. * care of the metadata. New transactions are already blocked, so we need to
  602. * wait for any remaining transactions to drain out before proceding.
  603. */
  604. void
  605. xfs_attr_quiesce(
  606. xfs_mount_t *mp)
  607. {
  608. /* wait for all modifications to complete */
  609. while (atomic_read(&mp->m_active_trans) > 0)
  610. delay(100);
  611. /* flush inodes and push all remaining buffers out to disk */
  612. xfs_quiesce_fs(mp);
  613. ASSERT_ALWAYS(atomic_read(&mp->m_active_trans) == 0);
  614. /* Push the superblock and write an unmount record */
  615. xfs_log_sbcount(mp, 1);
  616. xfs_log_unmount_write(mp);
  617. xfs_unmountfs_writesb(mp);
  618. }
  619. int
  620. xfs_mntupdate(
  621. struct xfs_mount *mp,
  622. int *flags,
  623. struct xfs_mount_args *args)
  624. {
  625. if (!(*flags & MS_RDONLY)) { /* rw/ro -> rw */
  626. if (mp->m_flags & XFS_MOUNT_RDONLY)
  627. mp->m_flags &= ~XFS_MOUNT_RDONLY;
  628. if (args->flags & XFSMNT_BARRIER) {
  629. mp->m_flags |= XFS_MOUNT_BARRIER;
  630. xfs_mountfs_check_barriers(mp);
  631. } else {
  632. mp->m_flags &= ~XFS_MOUNT_BARRIER;
  633. }
  634. } else if (!(mp->m_flags & XFS_MOUNT_RDONLY)) { /* rw -> ro */
  635. xfs_filestream_flush(mp);
  636. xfs_sync(mp, SYNC_DATA_QUIESCE);
  637. xfs_attr_quiesce(mp);
  638. mp->m_flags |= XFS_MOUNT_RDONLY;
  639. }
  640. return 0;
  641. }
  642. /*
  643. * xfs_unmount_flush implements a set of flush operation on special
  644. * inodes, which are needed as a separate set of operations so that
  645. * they can be called as part of relocation process.
  646. */
  647. int
  648. xfs_unmount_flush(
  649. xfs_mount_t *mp, /* Mount structure we are getting
  650. rid of. */
  651. int relocation) /* Called from vfs relocation. */
  652. {
  653. xfs_inode_t *rip = mp->m_rootip;
  654. xfs_inode_t *rbmip;
  655. xfs_inode_t *rsumip = NULL;
  656. bhv_vnode_t *rvp = XFS_ITOV(rip);
  657. int error;
  658. xfs_ilock(rip, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
  659. xfs_iflock(rip);
  660. /*
  661. * Flush out the real time inodes.
  662. */
  663. if ((rbmip = mp->m_rbmip) != NULL) {
  664. xfs_ilock(rbmip, XFS_ILOCK_EXCL);
  665. xfs_iflock(rbmip);
  666. error = xfs_iflush(rbmip, XFS_IFLUSH_SYNC);
  667. xfs_iunlock(rbmip, XFS_ILOCK_EXCL);
  668. if (error == EFSCORRUPTED)
  669. goto fscorrupt_out;
  670. ASSERT(vn_count(XFS_ITOV(rbmip)) == 1);
  671. rsumip = mp->m_rsumip;
  672. xfs_ilock(rsumip, XFS_ILOCK_EXCL);
  673. xfs_iflock(rsumip);
  674. error = xfs_iflush(rsumip, XFS_IFLUSH_SYNC);
  675. xfs_iunlock(rsumip, XFS_ILOCK_EXCL);
  676. if (error == EFSCORRUPTED)
  677. goto fscorrupt_out;
  678. ASSERT(vn_count(XFS_ITOV(rsumip)) == 1);
  679. }
  680. /*
  681. * Synchronously flush root inode to disk
  682. */
  683. error = xfs_iflush(rip, XFS_IFLUSH_SYNC);
  684. if (error == EFSCORRUPTED)
  685. goto fscorrupt_out2;
  686. if (vn_count(rvp) != 1 && !relocation) {
  687. xfs_iunlock(rip, XFS_ILOCK_EXCL);
  688. return XFS_ERROR(EBUSY);
  689. }
  690. /*
  691. * Release dquot that rootinode, rbmino and rsumino might be holding,
  692. * flush and purge the quota inodes.
  693. */
  694. error = XFS_QM_UNMOUNT(mp);
  695. if (error == EFSCORRUPTED)
  696. goto fscorrupt_out2;
  697. if (rbmip) {
  698. VN_RELE(XFS_ITOV(rbmip));
  699. VN_RELE(XFS_ITOV(rsumip));
  700. }
  701. xfs_iunlock(rip, XFS_ILOCK_EXCL);
  702. return 0;
  703. fscorrupt_out:
  704. xfs_ifunlock(rip);
  705. fscorrupt_out2:
  706. xfs_iunlock(rip, XFS_ILOCK_EXCL);
  707. return XFS_ERROR(EFSCORRUPTED);
  708. }
  709. /*
  710. * xfs_sync flushes any pending I/O to file system vfsp.
  711. *
  712. * This routine is called by vfs_sync() to make sure that things make it
  713. * out to disk eventually, on sync() system calls to flush out everything,
  714. * and when the file system is unmounted. For the vfs_sync() case, all
  715. * we really need to do is sync out the log to make all of our meta-data
  716. * updates permanent (except for timestamps). For calls from pflushd(),
  717. * dirty pages are kept moving by calling pdflush() on the inodes
  718. * containing them. We also flush the inodes that we can lock without
  719. * sleeping and the superblock if we can lock it without sleeping from
  720. * vfs_sync() so that items at the tail of the log are always moving out.
  721. *
  722. * Flags:
  723. * SYNC_BDFLUSH - We're being called from vfs_sync() so we don't want
  724. * to sleep if we can help it. All we really need
  725. * to do is ensure that the log is synced at least
  726. * periodically. We also push the inodes and
  727. * superblock if we can lock them without sleeping
  728. * and they are not pinned.
  729. * SYNC_ATTR - We need to flush the inodes. If SYNC_BDFLUSH is not
  730. * set, then we really want to lock each inode and flush
  731. * it.
  732. * SYNC_WAIT - All the flushes that take place in this call should
  733. * be synchronous.
  734. * SYNC_DELWRI - This tells us to push dirty pages associated with
  735. * inodes. SYNC_WAIT and SYNC_BDFLUSH are used to
  736. * determine if they should be flushed sync, async, or
  737. * delwri.
  738. * SYNC_CLOSE - This flag is passed when the system is being
  739. * unmounted. We should sync and invalidate everything.
  740. * SYNC_FSDATA - This indicates that the caller would like to make
  741. * sure the superblock is safe on disk. We can ensure
  742. * this by simply making sure the log gets flushed
  743. * if SYNC_BDFLUSH is set, and by actually writing it
  744. * out otherwise.
  745. * SYNC_IOWAIT - The caller wants us to wait for all data I/O to complete
  746. * before we return (including direct I/O). Forms the drain
  747. * side of the write barrier needed to safely quiesce the
  748. * filesystem.
  749. *
  750. */
  751. int
  752. xfs_sync(
  753. xfs_mount_t *mp,
  754. int flags)
  755. {
  756. int error;
  757. /*
  758. * Get the Quota Manager to flush the dquots.
  759. *
  760. * If XFS quota support is not enabled or this filesystem
  761. * instance does not use quotas XFS_QM_DQSYNC will always
  762. * return zero.
  763. */
  764. error = XFS_QM_DQSYNC(mp, flags);
  765. if (error) {
  766. /*
  767. * If we got an IO error, we will be shutting down.
  768. * So, there's nothing more for us to do here.
  769. */
  770. ASSERT(error != EIO || XFS_FORCED_SHUTDOWN(mp));
  771. if (XFS_FORCED_SHUTDOWN(mp))
  772. return XFS_ERROR(error);
  773. }
  774. if (flags & SYNC_IOWAIT)
  775. xfs_filestream_flush(mp);
  776. return xfs_syncsub(mp, flags, NULL);
  777. }
  778. /*
  779. * xfs sync routine for internal use
  780. *
  781. * This routine supports all of the flags defined for the generic vfs_sync
  782. * interface as explained above under xfs_sync.
  783. *
  784. */
  785. int
  786. xfs_sync_inodes(
  787. xfs_mount_t *mp,
  788. int flags,
  789. int *bypassed)
  790. {
  791. xfs_inode_t *ip = NULL;
  792. bhv_vnode_t *vp = NULL;
  793. int error;
  794. int last_error;
  795. uint64_t fflag;
  796. uint lock_flags;
  797. uint base_lock_flags;
  798. boolean_t mount_locked;
  799. boolean_t vnode_refed;
  800. int preempt;
  801. xfs_iptr_t *ipointer;
  802. #ifdef DEBUG
  803. boolean_t ipointer_in = B_FALSE;
  804. #define IPOINTER_SET ipointer_in = B_TRUE
  805. #define IPOINTER_CLR ipointer_in = B_FALSE
  806. #else
  807. #define IPOINTER_SET
  808. #define IPOINTER_CLR
  809. #endif
  810. /* Insert a marker record into the inode list after inode ip. The list
  811. * must be locked when this is called. After the call the list will no
  812. * longer be locked.
  813. */
  814. #define IPOINTER_INSERT(ip, mp) { \
  815. ASSERT(ipointer_in == B_FALSE); \
  816. ipointer->ip_mnext = ip->i_mnext; \
  817. ipointer->ip_mprev = ip; \
  818. ip->i_mnext = (xfs_inode_t *)ipointer; \
  819. ipointer->ip_mnext->i_mprev = (xfs_inode_t *)ipointer; \
  820. preempt = 0; \
  821. XFS_MOUNT_IUNLOCK(mp); \
  822. mount_locked = B_FALSE; \
  823. IPOINTER_SET; \
  824. }
  825. /* Remove the marker from the inode list. If the marker was the only item
  826. * in the list then there are no remaining inodes and we should zero out
  827. * the whole list. If we are the current head of the list then move the head
  828. * past us.
  829. */
  830. #define IPOINTER_REMOVE(ip, mp) { \
  831. ASSERT(ipointer_in == B_TRUE); \
  832. if (ipointer->ip_mnext != (xfs_inode_t *)ipointer) { \
  833. ip = ipointer->ip_mnext; \
  834. ip->i_mprev = ipointer->ip_mprev; \
  835. ipointer->ip_mprev->i_mnext = ip; \
  836. if (mp->m_inodes == (xfs_inode_t *)ipointer) { \
  837. mp->m_inodes = ip; \
  838. } \
  839. } else { \
  840. ASSERT(mp->m_inodes == (xfs_inode_t *)ipointer); \
  841. mp->m_inodes = NULL; \
  842. ip = NULL; \
  843. } \
  844. IPOINTER_CLR; \
  845. }
  846. #define XFS_PREEMPT_MASK 0x7f
  847. ASSERT(!(flags & SYNC_BDFLUSH));
  848. if (bypassed)
  849. *bypassed = 0;
  850. if (mp->m_flags & XFS_MOUNT_RDONLY)
  851. return 0;
  852. error = 0;
  853. last_error = 0;
  854. preempt = 0;
  855. /* Allocate a reference marker */
  856. ipointer = (xfs_iptr_t *)kmem_zalloc(sizeof(xfs_iptr_t), KM_SLEEP);
  857. fflag = XFS_B_ASYNC; /* default is don't wait */
  858. if (flags & SYNC_DELWRI)
  859. fflag = XFS_B_DELWRI;
  860. if (flags & SYNC_WAIT)
  861. fflag = 0; /* synchronous overrides all */
  862. base_lock_flags = XFS_ILOCK_SHARED;
  863. if (flags & (SYNC_DELWRI | SYNC_CLOSE)) {
  864. /*
  865. * We need the I/O lock if we're going to call any of
  866. * the flush/inval routines.
  867. */
  868. base_lock_flags |= XFS_IOLOCK_SHARED;
  869. }
  870. XFS_MOUNT_ILOCK(mp);
  871. ip = mp->m_inodes;
  872. mount_locked = B_TRUE;
  873. vnode_refed = B_FALSE;
  874. IPOINTER_CLR;
  875. do {
  876. ASSERT(ipointer_in == B_FALSE);
  877. ASSERT(vnode_refed == B_FALSE);
  878. lock_flags = base_lock_flags;
  879. /*
  880. * There were no inodes in the list, just break out
  881. * of the loop.
  882. */
  883. if (ip == NULL) {
  884. break;
  885. }
  886. /*
  887. * We found another sync thread marker - skip it
  888. */
  889. if (ip->i_mount == NULL) {
  890. ip = ip->i_mnext;
  891. continue;
  892. }
  893. vp = XFS_ITOV_NULL(ip);
  894. /*
  895. * If the vnode is gone then this is being torn down,
  896. * call reclaim if it is flushed, else let regular flush
  897. * code deal with it later in the loop.
  898. */
  899. if (vp == NULL) {
  900. /* Skip ones already in reclaim */
  901. if (ip->i_flags & XFS_IRECLAIM) {
  902. ip = ip->i_mnext;
  903. continue;
  904. }
  905. if (xfs_ilock_nowait(ip, XFS_ILOCK_EXCL) == 0) {
  906. ip = ip->i_mnext;
  907. } else if ((xfs_ipincount(ip) == 0) &&
  908. xfs_iflock_nowait(ip)) {
  909. IPOINTER_INSERT(ip, mp);
  910. xfs_finish_reclaim(ip, 1,
  911. XFS_IFLUSH_DELWRI_ELSE_ASYNC);
  912. XFS_MOUNT_ILOCK(mp);
  913. mount_locked = B_TRUE;
  914. IPOINTER_REMOVE(ip, mp);
  915. } else {
  916. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  917. ip = ip->i_mnext;
  918. }
  919. continue;
  920. }
  921. if (VN_BAD(vp)) {
  922. ip = ip->i_mnext;
  923. continue;
  924. }
  925. if (XFS_FORCED_SHUTDOWN(mp) && !(flags & SYNC_CLOSE)) {
  926. XFS_MOUNT_IUNLOCK(mp);
  927. kmem_free(ipointer, sizeof(xfs_iptr_t));
  928. return 0;
  929. }
  930. /*
  931. * Try to lock without sleeping. We're out of order with
  932. * the inode list lock here, so if we fail we need to drop
  933. * the mount lock and try again. If we're called from
  934. * bdflush() here, then don't bother.
  935. *
  936. * The inode lock here actually coordinates with the
  937. * almost spurious inode lock in xfs_ireclaim() to prevent
  938. * the vnode we handle here without a reference from
  939. * being freed while we reference it. If we lock the inode
  940. * while it's on the mount list here, then the spurious inode
  941. * lock in xfs_ireclaim() after the inode is pulled from
  942. * the mount list will sleep until we release it here.
  943. * This keeps the vnode from being freed while we reference
  944. * it.
  945. */
  946. if (xfs_ilock_nowait(ip, lock_flags) == 0) {
  947. if (vp == NULL) {
  948. ip = ip->i_mnext;
  949. continue;
  950. }
  951. vp = vn_grab(vp);
  952. if (vp == NULL) {
  953. ip = ip->i_mnext;
  954. continue;
  955. }
  956. IPOINTER_INSERT(ip, mp);
  957. xfs_ilock(ip, lock_flags);
  958. ASSERT(vp == XFS_ITOV(ip));
  959. ASSERT(ip->i_mount == mp);
  960. vnode_refed = B_TRUE;
  961. }
  962. /* From here on in the loop we may have a marker record
  963. * in the inode list.
  964. */
  965. /*
  966. * If we have to flush data or wait for I/O completion
  967. * we need to drop the ilock that we currently hold.
  968. * If we need to drop the lock, insert a marker if we
  969. * have not already done so.
  970. */
  971. if ((flags & (SYNC_CLOSE|SYNC_IOWAIT)) ||
  972. ((flags & SYNC_DELWRI) && VN_DIRTY(vp))) {
  973. if (mount_locked) {
  974. IPOINTER_INSERT(ip, mp);
  975. }
  976. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  977. if (flags & SYNC_CLOSE) {
  978. /* Shutdown case. Flush and invalidate. */
  979. if (XFS_FORCED_SHUTDOWN(mp))
  980. xfs_tosspages(ip, 0, -1,
  981. FI_REMAPF);
  982. else
  983. error = xfs_flushinval_pages(ip,
  984. 0, -1, FI_REMAPF);
  985. } else if ((flags & SYNC_DELWRI) && VN_DIRTY(vp)) {
  986. error = xfs_flush_pages(ip, 0,
  987. -1, fflag, FI_NONE);
  988. }
  989. /*
  990. * When freezing, we need to wait ensure all I/O (including direct
  991. * I/O) is complete to ensure no further data modification can take
  992. * place after this point
  993. */
  994. if (flags & SYNC_IOWAIT)
  995. vn_iowait(ip);
  996. xfs_ilock(ip, XFS_ILOCK_SHARED);
  997. }
  998. if ((flags & SYNC_ATTR) &&
  999. (ip->i_update_core ||
  1000. (ip->i_itemp && ip->i_itemp->ili_format.ilf_fields))) {
  1001. if (mount_locked)
  1002. IPOINTER_INSERT(ip, mp);
  1003. if (flags & SYNC_WAIT) {
  1004. xfs_iflock(ip);
  1005. error = xfs_iflush(ip, XFS_IFLUSH_SYNC);
  1006. /*
  1007. * If we can't acquire the flush lock, then the inode
  1008. * is already being flushed so don't bother waiting.
  1009. *
  1010. * If we can lock it then do a delwri flush so we can
  1011. * combine multiple inode flushes in each disk write.
  1012. */
  1013. } else if (xfs_iflock_nowait(ip)) {
  1014. error = xfs_iflush(ip, XFS_IFLUSH_DELWRI);
  1015. } else if (bypassed) {
  1016. (*bypassed)++;
  1017. }
  1018. }
  1019. if (lock_flags != 0) {
  1020. xfs_iunlock(ip, lock_flags);
  1021. }
  1022. if (vnode_refed) {
  1023. /*
  1024. * If we had to take a reference on the vnode
  1025. * above, then wait until after we've unlocked
  1026. * the inode to release the reference. This is
  1027. * because we can be already holding the inode
  1028. * lock when VN_RELE() calls xfs_inactive().
  1029. *
  1030. * Make sure to drop the mount lock before calling
  1031. * VN_RELE() so that we don't trip over ourselves if
  1032. * we have to go for the mount lock again in the
  1033. * inactive code.
  1034. */
  1035. if (mount_locked) {
  1036. IPOINTER_INSERT(ip, mp);
  1037. }
  1038. VN_RELE(vp);
  1039. vnode_refed = B_FALSE;
  1040. }
  1041. if (error) {
  1042. last_error = error;
  1043. }
  1044. /*
  1045. * bail out if the filesystem is corrupted.
  1046. */
  1047. if (error == EFSCORRUPTED) {
  1048. if (!mount_locked) {
  1049. XFS_MOUNT_ILOCK(mp);
  1050. IPOINTER_REMOVE(ip, mp);
  1051. }
  1052. XFS_MOUNT_IUNLOCK(mp);
  1053. ASSERT(ipointer_in == B_FALSE);
  1054. kmem_free(ipointer, sizeof(xfs_iptr_t));
  1055. return XFS_ERROR(error);
  1056. }
  1057. /* Let other threads have a chance at the mount lock
  1058. * if we have looped many times without dropping the
  1059. * lock.
  1060. */
  1061. if ((++preempt & XFS_PREEMPT_MASK) == 0) {
  1062. if (mount_locked) {
  1063. IPOINTER_INSERT(ip, mp);
  1064. }
  1065. }
  1066. if (mount_locked == B_FALSE) {
  1067. XFS_MOUNT_ILOCK(mp);
  1068. mount_locked = B_TRUE;
  1069. IPOINTER_REMOVE(ip, mp);
  1070. continue;
  1071. }
  1072. ASSERT(ipointer_in == B_FALSE);
  1073. ip = ip->i_mnext;
  1074. } while (ip != mp->m_inodes);
  1075. XFS_MOUNT_IUNLOCK(mp);
  1076. ASSERT(ipointer_in == B_FALSE);
  1077. kmem_free(ipointer, sizeof(xfs_iptr_t));
  1078. return XFS_ERROR(last_error);
  1079. }
  1080. /*
  1081. * xfs sync routine for internal use
  1082. *
  1083. * This routine supports all of the flags defined for the generic vfs_sync
  1084. * interface as explained above under xfs_sync.
  1085. *
  1086. */
  1087. int
  1088. xfs_syncsub(
  1089. xfs_mount_t *mp,
  1090. int flags,
  1091. int *bypassed)
  1092. {
  1093. int error = 0;
  1094. int last_error = 0;
  1095. uint log_flags = XFS_LOG_FORCE;
  1096. xfs_buf_t *bp;
  1097. xfs_buf_log_item_t *bip;
  1098. /*
  1099. * Sync out the log. This ensures that the log is periodically
  1100. * flushed even if there is not enough activity to fill it up.
  1101. */
  1102. if (flags & SYNC_WAIT)
  1103. log_flags |= XFS_LOG_SYNC;
  1104. xfs_log_force(mp, (xfs_lsn_t)0, log_flags);
  1105. if (flags & (SYNC_ATTR|SYNC_DELWRI)) {
  1106. if (flags & SYNC_BDFLUSH)
  1107. xfs_finish_reclaim_all(mp, 1);
  1108. else
  1109. error = xfs_sync_inodes(mp, flags, bypassed);
  1110. }
  1111. /*
  1112. * Flushing out dirty data above probably generated more
  1113. * log activity, so if this isn't vfs_sync() then flush
  1114. * the log again.
  1115. */
  1116. if (flags & SYNC_DELWRI) {
  1117. xfs_log_force(mp, (xfs_lsn_t)0, log_flags);
  1118. }
  1119. if (flags & SYNC_FSDATA) {
  1120. /*
  1121. * If this is vfs_sync() then only sync the superblock
  1122. * if we can lock it without sleeping and it is not pinned.
  1123. */
  1124. if (flags & SYNC_BDFLUSH) {
  1125. bp = xfs_getsb(mp, XFS_BUF_TRYLOCK);
  1126. if (bp != NULL) {
  1127. bip = XFS_BUF_FSPRIVATE(bp,xfs_buf_log_item_t*);
  1128. if ((bip != NULL) &&
  1129. xfs_buf_item_dirty(bip)) {
  1130. if (!(XFS_BUF_ISPINNED(bp))) {
  1131. XFS_BUF_ASYNC(bp);
  1132. error = xfs_bwrite(mp, bp);
  1133. } else {
  1134. xfs_buf_relse(bp);
  1135. }
  1136. } else {
  1137. xfs_buf_relse(bp);
  1138. }
  1139. }
  1140. } else {
  1141. bp = xfs_getsb(mp, 0);
  1142. /*
  1143. * If the buffer is pinned then push on the log so
  1144. * we won't get stuck waiting in the write for
  1145. * someone, maybe ourselves, to flush the log.
  1146. * Even though we just pushed the log above, we
  1147. * did not have the superblock buffer locked at
  1148. * that point so it can become pinned in between
  1149. * there and here.
  1150. */
  1151. if (XFS_BUF_ISPINNED(bp))
  1152. xfs_log_force(mp, (xfs_lsn_t)0, XFS_LOG_FORCE);
  1153. if (flags & SYNC_WAIT)
  1154. XFS_BUF_UNASYNC(bp);
  1155. else
  1156. XFS_BUF_ASYNC(bp);
  1157. error = xfs_bwrite(mp, bp);
  1158. }
  1159. if (error) {
  1160. last_error = error;
  1161. }
  1162. }
  1163. /*
  1164. * If asked, update the disk superblock with incore counter values if we
  1165. * are using non-persistent counters so that they don't get too far out
  1166. * of sync if we crash or get a forced shutdown. We don't want to force
  1167. * this to disk, just get a transaction into the iclogs....
  1168. */
  1169. if (flags & SYNC_SUPER)
  1170. xfs_log_sbcount(mp, 0);
  1171. /*
  1172. * Now check to see if the log needs a "dummy" transaction.
  1173. */
  1174. if (!(flags & SYNC_REMOUNT) && xfs_log_need_covered(mp)) {
  1175. xfs_trans_t *tp;
  1176. xfs_inode_t *ip;
  1177. /*
  1178. * Put a dummy transaction in the log to tell
  1179. * recovery that all others are OK.
  1180. */
  1181. tp = xfs_trans_alloc(mp, XFS_TRANS_DUMMY1);
  1182. if ((error = xfs_trans_reserve(tp, 0,
  1183. XFS_ICHANGE_LOG_RES(mp),
  1184. 0, 0, 0))) {
  1185. xfs_trans_cancel(tp, 0);
  1186. return error;
  1187. }
  1188. ip = mp->m_rootip;
  1189. xfs_ilock(ip, XFS_ILOCK_EXCL);
  1190. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  1191. xfs_trans_ihold(tp, ip);
  1192. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  1193. error = xfs_trans_commit(tp, 0);
  1194. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1195. xfs_log_force(mp, (xfs_lsn_t)0, log_flags);
  1196. }
  1197. /*
  1198. * When shutting down, we need to insure that the AIL is pushed
  1199. * to disk or the filesystem can appear corrupt from the PROM.
  1200. */
  1201. if ((flags & (SYNC_CLOSE|SYNC_WAIT)) == (SYNC_CLOSE|SYNC_WAIT)) {
  1202. XFS_bflush(mp->m_ddev_targp);
  1203. if (mp->m_rtdev_targp) {
  1204. XFS_bflush(mp->m_rtdev_targp);
  1205. }
  1206. }
  1207. return XFS_ERROR(last_error);
  1208. }