xfs_vfsops.c 35 KB

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