xfs_vfsops.c 34 KB

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