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