xfs_vfsops.c 35 KB

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