xfs_vfsops.c 36 KB

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