xfs_vfsops.c 34 KB

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