xfs_vfsops.c 34 KB

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