xfs_vfsops.c 34 KB

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