xfs_inode.c 107 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772
  1. /*
  2. * Copyright (c) 2000-2003 Silicon Graphics, Inc. All Rights Reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of version 2 of the GNU General Public License as
  6. * published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it would be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. *
  12. * Further, this software is distributed without any warranty that it is
  13. * free of the rightful claim of any third person regarding infringement
  14. * or the like. Any license provided herein, whether implied or
  15. * otherwise, applies only to this software file. Patent licenses, if
  16. * any, provided herein do not apply to combinations of this program with
  17. * other software, or any other product whatsoever.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, write the Free Software Foundation, Inc., 59
  21. * Temple Place - Suite 330, Boston MA 02111-1307, USA.
  22. *
  23. * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
  24. * Mountain View, CA 94043, or:
  25. *
  26. * http://www.sgi.com
  27. *
  28. * For further information regarding this notice, see:
  29. *
  30. * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
  31. */
  32. #include "xfs.h"
  33. #include "xfs_macros.h"
  34. #include "xfs_types.h"
  35. #include "xfs_inum.h"
  36. #include "xfs_log.h"
  37. #include "xfs_trans.h"
  38. #include "xfs_trans_priv.h"
  39. #include "xfs_sb.h"
  40. #include "xfs_ag.h"
  41. #include "xfs_dir.h"
  42. #include "xfs_dir2.h"
  43. #include "xfs_dmapi.h"
  44. #include "xfs_mount.h"
  45. #include "xfs_alloc_btree.h"
  46. #include "xfs_bmap_btree.h"
  47. #include "xfs_ialloc_btree.h"
  48. #include "xfs_btree.h"
  49. #include "xfs_imap.h"
  50. #include "xfs_alloc.h"
  51. #include "xfs_ialloc.h"
  52. #include "xfs_attr_sf.h"
  53. #include "xfs_dir_sf.h"
  54. #include "xfs_dir2_sf.h"
  55. #include "xfs_dinode.h"
  56. #include "xfs_inode_item.h"
  57. #include "xfs_inode.h"
  58. #include "xfs_bmap.h"
  59. #include "xfs_buf_item.h"
  60. #include "xfs_rw.h"
  61. #include "xfs_error.h"
  62. #include "xfs_bit.h"
  63. #include "xfs_utils.h"
  64. #include "xfs_dir2_trace.h"
  65. #include "xfs_quota.h"
  66. #include "xfs_mac.h"
  67. #include "xfs_acl.h"
  68. kmem_zone_t *xfs_ifork_zone;
  69. kmem_zone_t *xfs_inode_zone;
  70. kmem_zone_t *xfs_chashlist_zone;
  71. /*
  72. * Used in xfs_itruncate(). This is the maximum number of extents
  73. * freed from a file in a single transaction.
  74. */
  75. #define XFS_ITRUNC_MAX_EXTENTS 2
  76. STATIC int xfs_iflush_int(xfs_inode_t *, xfs_buf_t *);
  77. STATIC int xfs_iformat_local(xfs_inode_t *, xfs_dinode_t *, int, int);
  78. STATIC int xfs_iformat_extents(xfs_inode_t *, xfs_dinode_t *, int);
  79. STATIC int xfs_iformat_btree(xfs_inode_t *, xfs_dinode_t *, int);
  80. #ifdef DEBUG
  81. /*
  82. * Make sure that the extents in the given memory buffer
  83. * are valid.
  84. */
  85. STATIC void
  86. xfs_validate_extents(
  87. xfs_bmbt_rec_t *ep,
  88. int nrecs,
  89. int disk,
  90. xfs_exntfmt_t fmt)
  91. {
  92. xfs_bmbt_irec_t irec;
  93. xfs_bmbt_rec_t rec;
  94. int i;
  95. for (i = 0; i < nrecs; i++) {
  96. rec.l0 = get_unaligned((__uint64_t*)&ep->l0);
  97. rec.l1 = get_unaligned((__uint64_t*)&ep->l1);
  98. if (disk)
  99. xfs_bmbt_disk_get_all(&rec, &irec);
  100. else
  101. xfs_bmbt_get_all(&rec, &irec);
  102. if (fmt == XFS_EXTFMT_NOSTATE)
  103. ASSERT(irec.br_state == XFS_EXT_NORM);
  104. ep++;
  105. }
  106. }
  107. #else /* DEBUG */
  108. #define xfs_validate_extents(ep, nrecs, disk, fmt)
  109. #endif /* DEBUG */
  110. /*
  111. * Check that none of the inode's in the buffer have a next
  112. * unlinked field of 0.
  113. */
  114. #if defined(DEBUG)
  115. void
  116. xfs_inobp_check(
  117. xfs_mount_t *mp,
  118. xfs_buf_t *bp)
  119. {
  120. int i;
  121. int j;
  122. xfs_dinode_t *dip;
  123. j = mp->m_inode_cluster_size >> mp->m_sb.sb_inodelog;
  124. for (i = 0; i < j; i++) {
  125. dip = (xfs_dinode_t *)xfs_buf_offset(bp,
  126. i * mp->m_sb.sb_inodesize);
  127. if (!dip->di_next_unlinked) {
  128. xfs_fs_cmn_err(CE_ALERT, mp,
  129. "Detected a bogus zero next_unlinked field in incore inode buffer 0x%p. About to pop an ASSERT.",
  130. bp);
  131. ASSERT(dip->di_next_unlinked);
  132. }
  133. }
  134. }
  135. #endif
  136. /*
  137. * This routine is called to map an inode number within a file
  138. * system to the buffer containing the on-disk version of the
  139. * inode. It returns a pointer to the buffer containing the
  140. * on-disk inode in the bpp parameter, and in the dip parameter
  141. * it returns a pointer to the on-disk inode within that buffer.
  142. *
  143. * If a non-zero error is returned, then the contents of bpp and
  144. * dipp are undefined.
  145. *
  146. * Use xfs_imap() to determine the size and location of the
  147. * buffer to read from disk.
  148. */
  149. STATIC int
  150. xfs_inotobp(
  151. xfs_mount_t *mp,
  152. xfs_trans_t *tp,
  153. xfs_ino_t ino,
  154. xfs_dinode_t **dipp,
  155. xfs_buf_t **bpp,
  156. int *offset)
  157. {
  158. int di_ok;
  159. xfs_imap_t imap;
  160. xfs_buf_t *bp;
  161. int error;
  162. xfs_dinode_t *dip;
  163. /*
  164. * Call the space managment code to find the location of the
  165. * inode on disk.
  166. */
  167. imap.im_blkno = 0;
  168. error = xfs_imap(mp, tp, ino, &imap, XFS_IMAP_LOOKUP);
  169. if (error != 0) {
  170. cmn_err(CE_WARN,
  171. "xfs_inotobp: xfs_imap() returned an "
  172. "error %d on %s. Returning error.", error, mp->m_fsname);
  173. return error;
  174. }
  175. /*
  176. * If the inode number maps to a block outside the bounds of the
  177. * file system then return NULL rather than calling read_buf
  178. * and panicing when we get an error from the driver.
  179. */
  180. if ((imap.im_blkno + imap.im_len) >
  181. XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks)) {
  182. cmn_err(CE_WARN,
  183. "xfs_inotobp: inode number (%d + %d) maps to a block outside the bounds "
  184. "of the file system %s. Returning EINVAL.",
  185. imap.im_blkno, imap.im_len,mp->m_fsname);
  186. return XFS_ERROR(EINVAL);
  187. }
  188. /*
  189. * Read in the buffer. If tp is NULL, xfs_trans_read_buf() will
  190. * default to just a read_buf() call.
  191. */
  192. error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, imap.im_blkno,
  193. (int)imap.im_len, XFS_BUF_LOCK, &bp);
  194. if (error) {
  195. cmn_err(CE_WARN,
  196. "xfs_inotobp: xfs_trans_read_buf() returned an "
  197. "error %d on %s. Returning error.", error, mp->m_fsname);
  198. return error;
  199. }
  200. dip = (xfs_dinode_t *)xfs_buf_offset(bp, 0);
  201. di_ok =
  202. INT_GET(dip->di_core.di_magic, ARCH_CONVERT) == XFS_DINODE_MAGIC &&
  203. XFS_DINODE_GOOD_VERSION(INT_GET(dip->di_core.di_version, ARCH_CONVERT));
  204. if (unlikely(XFS_TEST_ERROR(!di_ok, mp, XFS_ERRTAG_ITOBP_INOTOBP,
  205. XFS_RANDOM_ITOBP_INOTOBP))) {
  206. XFS_CORRUPTION_ERROR("xfs_inotobp", XFS_ERRLEVEL_LOW, mp, dip);
  207. xfs_trans_brelse(tp, bp);
  208. cmn_err(CE_WARN,
  209. "xfs_inotobp: XFS_TEST_ERROR() returned an "
  210. "error on %s. Returning EFSCORRUPTED.", mp->m_fsname);
  211. return XFS_ERROR(EFSCORRUPTED);
  212. }
  213. xfs_inobp_check(mp, bp);
  214. /*
  215. * Set *dipp to point to the on-disk inode in the buffer.
  216. */
  217. *dipp = (xfs_dinode_t *)xfs_buf_offset(bp, imap.im_boffset);
  218. *bpp = bp;
  219. *offset = imap.im_boffset;
  220. return 0;
  221. }
  222. /*
  223. * This routine is called to map an inode to the buffer containing
  224. * the on-disk version of the inode. It returns a pointer to the
  225. * buffer containing the on-disk inode in the bpp parameter, and in
  226. * the dip parameter it returns a pointer to the on-disk inode within
  227. * that buffer.
  228. *
  229. * If a non-zero error is returned, then the contents of bpp and
  230. * dipp are undefined.
  231. *
  232. * If the inode is new and has not yet been initialized, use xfs_imap()
  233. * to determine the size and location of the buffer to read from disk.
  234. * If the inode has already been mapped to its buffer and read in once,
  235. * then use the mapping information stored in the inode rather than
  236. * calling xfs_imap(). This allows us to avoid the overhead of looking
  237. * at the inode btree for small block file systems (see xfs_dilocate()).
  238. * We can tell whether the inode has been mapped in before by comparing
  239. * its disk block address to 0. Only uninitialized inodes will have
  240. * 0 for the disk block address.
  241. */
  242. int
  243. xfs_itobp(
  244. xfs_mount_t *mp,
  245. xfs_trans_t *tp,
  246. xfs_inode_t *ip,
  247. xfs_dinode_t **dipp,
  248. xfs_buf_t **bpp,
  249. xfs_daddr_t bno)
  250. {
  251. xfs_buf_t *bp;
  252. int error;
  253. xfs_imap_t imap;
  254. #ifdef __KERNEL__
  255. int i;
  256. int ni;
  257. #endif
  258. if (ip->i_blkno == (xfs_daddr_t)0) {
  259. /*
  260. * Call the space management code to find the location of the
  261. * inode on disk.
  262. */
  263. imap.im_blkno = bno;
  264. error = xfs_imap(mp, tp, ip->i_ino, &imap, XFS_IMAP_LOOKUP);
  265. if (error != 0) {
  266. return error;
  267. }
  268. /*
  269. * If the inode number maps to a block outside the bounds
  270. * of the file system then return NULL rather than calling
  271. * read_buf and panicing when we get an error from the
  272. * driver.
  273. */
  274. if ((imap.im_blkno + imap.im_len) >
  275. XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks)) {
  276. #ifdef DEBUG
  277. xfs_fs_cmn_err(CE_ALERT, mp, "xfs_itobp: "
  278. "(imap.im_blkno (0x%llx) "
  279. "+ imap.im_len (0x%llx)) > "
  280. " XFS_FSB_TO_BB(mp, "
  281. "mp->m_sb.sb_dblocks) (0x%llx)",
  282. (unsigned long long) imap.im_blkno,
  283. (unsigned long long) imap.im_len,
  284. XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks));
  285. #endif /* DEBUG */
  286. return XFS_ERROR(EINVAL);
  287. }
  288. /*
  289. * Fill in the fields in the inode that will be used to
  290. * map the inode to its buffer from now on.
  291. */
  292. ip->i_blkno = imap.im_blkno;
  293. ip->i_len = imap.im_len;
  294. ip->i_boffset = imap.im_boffset;
  295. } else {
  296. /*
  297. * We've already mapped the inode once, so just use the
  298. * mapping that we saved the first time.
  299. */
  300. imap.im_blkno = ip->i_blkno;
  301. imap.im_len = ip->i_len;
  302. imap.im_boffset = ip->i_boffset;
  303. }
  304. ASSERT(bno == 0 || bno == imap.im_blkno);
  305. /*
  306. * Read in the buffer. If tp is NULL, xfs_trans_read_buf() will
  307. * default to just a read_buf() call.
  308. */
  309. error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, imap.im_blkno,
  310. (int)imap.im_len, XFS_BUF_LOCK, &bp);
  311. if (error) {
  312. #ifdef DEBUG
  313. xfs_fs_cmn_err(CE_ALERT, mp, "xfs_itobp: "
  314. "xfs_trans_read_buf() returned error %d, "
  315. "imap.im_blkno 0x%llx, imap.im_len 0x%llx",
  316. error, (unsigned long long) imap.im_blkno,
  317. (unsigned long long) imap.im_len);
  318. #endif /* DEBUG */
  319. return error;
  320. }
  321. #ifdef __KERNEL__
  322. /*
  323. * Validate the magic number and version of every inode in the buffer
  324. * (if DEBUG kernel) or the first inode in the buffer, otherwise.
  325. */
  326. #ifdef DEBUG
  327. ni = BBTOB(imap.im_len) >> mp->m_sb.sb_inodelog;
  328. #else
  329. ni = 1;
  330. #endif
  331. for (i = 0; i < ni; i++) {
  332. int di_ok;
  333. xfs_dinode_t *dip;
  334. dip = (xfs_dinode_t *)xfs_buf_offset(bp,
  335. (i << mp->m_sb.sb_inodelog));
  336. di_ok = INT_GET(dip->di_core.di_magic, ARCH_CONVERT) == XFS_DINODE_MAGIC &&
  337. XFS_DINODE_GOOD_VERSION(INT_GET(dip->di_core.di_version, ARCH_CONVERT));
  338. if (unlikely(XFS_TEST_ERROR(!di_ok, mp, XFS_ERRTAG_ITOBP_INOTOBP,
  339. XFS_RANDOM_ITOBP_INOTOBP))) {
  340. #ifdef DEBUG
  341. prdev("bad inode magic/vsn daddr %lld #%d (magic=%x)",
  342. mp->m_ddev_targp,
  343. (unsigned long long)imap.im_blkno, i,
  344. INT_GET(dip->di_core.di_magic, ARCH_CONVERT));
  345. #endif
  346. XFS_CORRUPTION_ERROR("xfs_itobp", XFS_ERRLEVEL_HIGH,
  347. mp, dip);
  348. xfs_trans_brelse(tp, bp);
  349. return XFS_ERROR(EFSCORRUPTED);
  350. }
  351. }
  352. #endif /* __KERNEL__ */
  353. xfs_inobp_check(mp, bp);
  354. /*
  355. * Mark the buffer as an inode buffer now that it looks good
  356. */
  357. XFS_BUF_SET_VTYPE(bp, B_FS_INO);
  358. /*
  359. * Set *dipp to point to the on-disk inode in the buffer.
  360. */
  361. *dipp = (xfs_dinode_t *)xfs_buf_offset(bp, imap.im_boffset);
  362. *bpp = bp;
  363. return 0;
  364. }
  365. /*
  366. * Move inode type and inode format specific information from the
  367. * on-disk inode to the in-core inode. For fifos, devs, and sockets
  368. * this means set if_rdev to the proper value. For files, directories,
  369. * and symlinks this means to bring in the in-line data or extent
  370. * pointers. For a file in B-tree format, only the root is immediately
  371. * brought in-core. The rest will be in-lined in if_extents when it
  372. * is first referenced (see xfs_iread_extents()).
  373. */
  374. STATIC int
  375. xfs_iformat(
  376. xfs_inode_t *ip,
  377. xfs_dinode_t *dip)
  378. {
  379. xfs_attr_shortform_t *atp;
  380. int size;
  381. int error;
  382. xfs_fsize_t di_size;
  383. ip->i_df.if_ext_max =
  384. XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
  385. error = 0;
  386. if (unlikely(
  387. INT_GET(dip->di_core.di_nextents, ARCH_CONVERT) +
  388. INT_GET(dip->di_core.di_anextents, ARCH_CONVERT) >
  389. INT_GET(dip->di_core.di_nblocks, ARCH_CONVERT))) {
  390. xfs_fs_cmn_err(CE_WARN, ip->i_mount,
  391. "corrupt dinode %Lu, extent total = %d, nblocks = %Lu."
  392. " Unmount and run xfs_repair.",
  393. (unsigned long long)ip->i_ino,
  394. (int)(INT_GET(dip->di_core.di_nextents, ARCH_CONVERT)
  395. + INT_GET(dip->di_core.di_anextents, ARCH_CONVERT)),
  396. (unsigned long long)
  397. INT_GET(dip->di_core.di_nblocks, ARCH_CONVERT));
  398. XFS_CORRUPTION_ERROR("xfs_iformat(1)", XFS_ERRLEVEL_LOW,
  399. ip->i_mount, dip);
  400. return XFS_ERROR(EFSCORRUPTED);
  401. }
  402. if (unlikely(INT_GET(dip->di_core.di_forkoff, ARCH_CONVERT) > ip->i_mount->m_sb.sb_inodesize)) {
  403. xfs_fs_cmn_err(CE_WARN, ip->i_mount,
  404. "corrupt dinode %Lu, forkoff = 0x%x."
  405. " Unmount and run xfs_repair.",
  406. (unsigned long long)ip->i_ino,
  407. (int)(INT_GET(dip->di_core.di_forkoff, ARCH_CONVERT)));
  408. XFS_CORRUPTION_ERROR("xfs_iformat(2)", XFS_ERRLEVEL_LOW,
  409. ip->i_mount, dip);
  410. return XFS_ERROR(EFSCORRUPTED);
  411. }
  412. switch (ip->i_d.di_mode & S_IFMT) {
  413. case S_IFIFO:
  414. case S_IFCHR:
  415. case S_IFBLK:
  416. case S_IFSOCK:
  417. if (unlikely(INT_GET(dip->di_core.di_format, ARCH_CONVERT) != XFS_DINODE_FMT_DEV)) {
  418. XFS_CORRUPTION_ERROR("xfs_iformat(3)", XFS_ERRLEVEL_LOW,
  419. ip->i_mount, dip);
  420. return XFS_ERROR(EFSCORRUPTED);
  421. }
  422. ip->i_d.di_size = 0;
  423. ip->i_df.if_u2.if_rdev = INT_GET(dip->di_u.di_dev, ARCH_CONVERT);
  424. break;
  425. case S_IFREG:
  426. case S_IFLNK:
  427. case S_IFDIR:
  428. switch (INT_GET(dip->di_core.di_format, ARCH_CONVERT)) {
  429. case XFS_DINODE_FMT_LOCAL:
  430. /*
  431. * no local regular files yet
  432. */
  433. if (unlikely((INT_GET(dip->di_core.di_mode, ARCH_CONVERT) & S_IFMT) == S_IFREG)) {
  434. xfs_fs_cmn_err(CE_WARN, ip->i_mount,
  435. "corrupt inode (local format for regular file) %Lu. Unmount and run xfs_repair.",
  436. (unsigned long long) ip->i_ino);
  437. XFS_CORRUPTION_ERROR("xfs_iformat(4)",
  438. XFS_ERRLEVEL_LOW,
  439. ip->i_mount, dip);
  440. return XFS_ERROR(EFSCORRUPTED);
  441. }
  442. di_size = INT_GET(dip->di_core.di_size, ARCH_CONVERT);
  443. if (unlikely(di_size > XFS_DFORK_DSIZE(dip, ip->i_mount))) {
  444. xfs_fs_cmn_err(CE_WARN, ip->i_mount,
  445. "corrupt inode %Lu (bad size %Ld for local inode). Unmount and run xfs_repair.",
  446. (unsigned long long) ip->i_ino,
  447. (long long) di_size);
  448. XFS_CORRUPTION_ERROR("xfs_iformat(5)",
  449. XFS_ERRLEVEL_LOW,
  450. ip->i_mount, dip);
  451. return XFS_ERROR(EFSCORRUPTED);
  452. }
  453. size = (int)di_size;
  454. error = xfs_iformat_local(ip, dip, XFS_DATA_FORK, size);
  455. break;
  456. case XFS_DINODE_FMT_EXTENTS:
  457. error = xfs_iformat_extents(ip, dip, XFS_DATA_FORK);
  458. break;
  459. case XFS_DINODE_FMT_BTREE:
  460. error = xfs_iformat_btree(ip, dip, XFS_DATA_FORK);
  461. break;
  462. default:
  463. XFS_ERROR_REPORT("xfs_iformat(6)", XFS_ERRLEVEL_LOW,
  464. ip->i_mount);
  465. return XFS_ERROR(EFSCORRUPTED);
  466. }
  467. break;
  468. default:
  469. XFS_ERROR_REPORT("xfs_iformat(7)", XFS_ERRLEVEL_LOW, ip->i_mount);
  470. return XFS_ERROR(EFSCORRUPTED);
  471. }
  472. if (error) {
  473. return error;
  474. }
  475. if (!XFS_DFORK_Q(dip))
  476. return 0;
  477. ASSERT(ip->i_afp == NULL);
  478. ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP);
  479. ip->i_afp->if_ext_max =
  480. XFS_IFORK_ASIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
  481. switch (INT_GET(dip->di_core.di_aformat, ARCH_CONVERT)) {
  482. case XFS_DINODE_FMT_LOCAL:
  483. atp = (xfs_attr_shortform_t *)XFS_DFORK_APTR(dip);
  484. size = (int)INT_GET(atp->hdr.totsize, ARCH_CONVERT);
  485. error = xfs_iformat_local(ip, dip, XFS_ATTR_FORK, size);
  486. break;
  487. case XFS_DINODE_FMT_EXTENTS:
  488. error = xfs_iformat_extents(ip, dip, XFS_ATTR_FORK);
  489. break;
  490. case XFS_DINODE_FMT_BTREE:
  491. error = xfs_iformat_btree(ip, dip, XFS_ATTR_FORK);
  492. break;
  493. default:
  494. error = XFS_ERROR(EFSCORRUPTED);
  495. break;
  496. }
  497. if (error) {
  498. kmem_zone_free(xfs_ifork_zone, ip->i_afp);
  499. ip->i_afp = NULL;
  500. xfs_idestroy_fork(ip, XFS_DATA_FORK);
  501. }
  502. return error;
  503. }
  504. /*
  505. * The file is in-lined in the on-disk inode.
  506. * If it fits into if_inline_data, then copy
  507. * it there, otherwise allocate a buffer for it
  508. * and copy the data there. Either way, set
  509. * if_data to point at the data.
  510. * If we allocate a buffer for the data, make
  511. * sure that its size is a multiple of 4 and
  512. * record the real size in i_real_bytes.
  513. */
  514. STATIC int
  515. xfs_iformat_local(
  516. xfs_inode_t *ip,
  517. xfs_dinode_t *dip,
  518. int whichfork,
  519. int size)
  520. {
  521. xfs_ifork_t *ifp;
  522. int real_size;
  523. /*
  524. * If the size is unreasonable, then something
  525. * is wrong and we just bail out rather than crash in
  526. * kmem_alloc() or memcpy() below.
  527. */
  528. if (unlikely(size > XFS_DFORK_SIZE(dip, ip->i_mount, whichfork))) {
  529. xfs_fs_cmn_err(CE_WARN, ip->i_mount,
  530. "corrupt inode %Lu (bad size %d for local fork, size = %d). Unmount and run xfs_repair.",
  531. (unsigned long long) ip->i_ino, size,
  532. XFS_DFORK_SIZE(dip, ip->i_mount, whichfork));
  533. XFS_CORRUPTION_ERROR("xfs_iformat_local", XFS_ERRLEVEL_LOW,
  534. ip->i_mount, dip);
  535. return XFS_ERROR(EFSCORRUPTED);
  536. }
  537. ifp = XFS_IFORK_PTR(ip, whichfork);
  538. real_size = 0;
  539. if (size == 0)
  540. ifp->if_u1.if_data = NULL;
  541. else if (size <= sizeof(ifp->if_u2.if_inline_data))
  542. ifp->if_u1.if_data = ifp->if_u2.if_inline_data;
  543. else {
  544. real_size = roundup(size, 4);
  545. ifp->if_u1.if_data = kmem_alloc(real_size, KM_SLEEP);
  546. }
  547. ifp->if_bytes = size;
  548. ifp->if_real_bytes = real_size;
  549. if (size)
  550. memcpy(ifp->if_u1.if_data, XFS_DFORK_PTR(dip, whichfork), size);
  551. ifp->if_flags &= ~XFS_IFEXTENTS;
  552. ifp->if_flags |= XFS_IFINLINE;
  553. return 0;
  554. }
  555. /*
  556. * The file consists of a set of extents all
  557. * of which fit into the on-disk inode.
  558. * If there are few enough extents to fit into
  559. * the if_inline_ext, then copy them there.
  560. * Otherwise allocate a buffer for them and copy
  561. * them into it. Either way, set if_extents
  562. * to point at the extents.
  563. */
  564. STATIC int
  565. xfs_iformat_extents(
  566. xfs_inode_t *ip,
  567. xfs_dinode_t *dip,
  568. int whichfork)
  569. {
  570. xfs_bmbt_rec_t *ep, *dp;
  571. xfs_ifork_t *ifp;
  572. int nex;
  573. int real_size;
  574. int size;
  575. int i;
  576. ifp = XFS_IFORK_PTR(ip, whichfork);
  577. nex = XFS_DFORK_NEXTENTS(dip, whichfork);
  578. size = nex * (uint)sizeof(xfs_bmbt_rec_t);
  579. /*
  580. * If the number of extents is unreasonable, then something
  581. * is wrong and we just bail out rather than crash in
  582. * kmem_alloc() or memcpy() below.
  583. */
  584. if (unlikely(size < 0 || size > XFS_DFORK_SIZE(dip, ip->i_mount, whichfork))) {
  585. xfs_fs_cmn_err(CE_WARN, ip->i_mount,
  586. "corrupt inode %Lu ((a)extents = %d). Unmount and run xfs_repair.",
  587. (unsigned long long) ip->i_ino, nex);
  588. XFS_CORRUPTION_ERROR("xfs_iformat_extents(1)", XFS_ERRLEVEL_LOW,
  589. ip->i_mount, dip);
  590. return XFS_ERROR(EFSCORRUPTED);
  591. }
  592. real_size = 0;
  593. if (nex == 0)
  594. ifp->if_u1.if_extents = NULL;
  595. else if (nex <= XFS_INLINE_EXTS)
  596. ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext;
  597. else {
  598. ifp->if_u1.if_extents = kmem_alloc(size, KM_SLEEP);
  599. ASSERT(ifp->if_u1.if_extents != NULL);
  600. real_size = size;
  601. }
  602. ifp->if_bytes = size;
  603. ifp->if_real_bytes = real_size;
  604. if (size) {
  605. dp = (xfs_bmbt_rec_t *) XFS_DFORK_PTR(dip, whichfork);
  606. xfs_validate_extents(dp, nex, 1, XFS_EXTFMT_INODE(ip));
  607. ep = ifp->if_u1.if_extents;
  608. for (i = 0; i < nex; i++, ep++, dp++) {
  609. ep->l0 = INT_GET(get_unaligned((__uint64_t*)&dp->l0),
  610. ARCH_CONVERT);
  611. ep->l1 = INT_GET(get_unaligned((__uint64_t*)&dp->l1),
  612. ARCH_CONVERT);
  613. }
  614. xfs_bmap_trace_exlist("xfs_iformat_extents", ip, nex,
  615. whichfork);
  616. if (whichfork != XFS_DATA_FORK ||
  617. XFS_EXTFMT_INODE(ip) == XFS_EXTFMT_NOSTATE)
  618. if (unlikely(xfs_check_nostate_extents(
  619. ifp->if_u1.if_extents, nex))) {
  620. XFS_ERROR_REPORT("xfs_iformat_extents(2)",
  621. XFS_ERRLEVEL_LOW,
  622. ip->i_mount);
  623. return XFS_ERROR(EFSCORRUPTED);
  624. }
  625. }
  626. ifp->if_flags |= XFS_IFEXTENTS;
  627. return 0;
  628. }
  629. /*
  630. * The file has too many extents to fit into
  631. * the inode, so they are in B-tree format.
  632. * Allocate a buffer for the root of the B-tree
  633. * and copy the root into it. The i_extents
  634. * field will remain NULL until all of the
  635. * extents are read in (when they are needed).
  636. */
  637. STATIC int
  638. xfs_iformat_btree(
  639. xfs_inode_t *ip,
  640. xfs_dinode_t *dip,
  641. int whichfork)
  642. {
  643. xfs_bmdr_block_t *dfp;
  644. xfs_ifork_t *ifp;
  645. /* REFERENCED */
  646. int nrecs;
  647. int size;
  648. ifp = XFS_IFORK_PTR(ip, whichfork);
  649. dfp = (xfs_bmdr_block_t *)XFS_DFORK_PTR(dip, whichfork);
  650. size = XFS_BMAP_BROOT_SPACE(dfp);
  651. nrecs = XFS_BMAP_BROOT_NUMRECS(dfp);
  652. /*
  653. * blow out if -- fork has less extents than can fit in
  654. * fork (fork shouldn't be a btree format), root btree
  655. * block has more records than can fit into the fork,
  656. * or the number of extents is greater than the number of
  657. * blocks.
  658. */
  659. if (unlikely(XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max
  660. || XFS_BMDR_SPACE_CALC(nrecs) >
  661. XFS_DFORK_SIZE(dip, ip->i_mount, whichfork)
  662. || XFS_IFORK_NEXTENTS(ip, whichfork) > ip->i_d.di_nblocks)) {
  663. xfs_fs_cmn_err(CE_WARN, ip->i_mount,
  664. "corrupt inode %Lu (btree). Unmount and run xfs_repair.",
  665. (unsigned long long) ip->i_ino);
  666. XFS_ERROR_REPORT("xfs_iformat_btree", XFS_ERRLEVEL_LOW,
  667. ip->i_mount);
  668. return XFS_ERROR(EFSCORRUPTED);
  669. }
  670. ifp->if_broot_bytes = size;
  671. ifp->if_broot = kmem_alloc(size, KM_SLEEP);
  672. ASSERT(ifp->if_broot != NULL);
  673. /*
  674. * Copy and convert from the on-disk structure
  675. * to the in-memory structure.
  676. */
  677. xfs_bmdr_to_bmbt(dfp, XFS_DFORK_SIZE(dip, ip->i_mount, whichfork),
  678. ifp->if_broot, size);
  679. ifp->if_flags &= ~XFS_IFEXTENTS;
  680. ifp->if_flags |= XFS_IFBROOT;
  681. return 0;
  682. }
  683. /*
  684. * xfs_xlate_dinode_core - translate an xfs_inode_core_t between ondisk
  685. * and native format
  686. *
  687. * buf = on-disk representation
  688. * dip = native representation
  689. * dir = direction - +ve -> disk to native
  690. * -ve -> native to disk
  691. */
  692. void
  693. xfs_xlate_dinode_core(
  694. xfs_caddr_t buf,
  695. xfs_dinode_core_t *dip,
  696. int dir)
  697. {
  698. xfs_dinode_core_t *buf_core = (xfs_dinode_core_t *)buf;
  699. xfs_dinode_core_t *mem_core = (xfs_dinode_core_t *)dip;
  700. xfs_arch_t arch = ARCH_CONVERT;
  701. ASSERT(dir);
  702. INT_XLATE(buf_core->di_magic, mem_core->di_magic, dir, arch);
  703. INT_XLATE(buf_core->di_mode, mem_core->di_mode, dir, arch);
  704. INT_XLATE(buf_core->di_version, mem_core->di_version, dir, arch);
  705. INT_XLATE(buf_core->di_format, mem_core->di_format, dir, arch);
  706. INT_XLATE(buf_core->di_onlink, mem_core->di_onlink, dir, arch);
  707. INT_XLATE(buf_core->di_uid, mem_core->di_uid, dir, arch);
  708. INT_XLATE(buf_core->di_gid, mem_core->di_gid, dir, arch);
  709. INT_XLATE(buf_core->di_nlink, mem_core->di_nlink, dir, arch);
  710. INT_XLATE(buf_core->di_projid, mem_core->di_projid, dir, arch);
  711. if (dir > 0) {
  712. memcpy(mem_core->di_pad, buf_core->di_pad,
  713. sizeof(buf_core->di_pad));
  714. } else {
  715. memcpy(buf_core->di_pad, mem_core->di_pad,
  716. sizeof(buf_core->di_pad));
  717. }
  718. INT_XLATE(buf_core->di_flushiter, mem_core->di_flushiter, dir, arch);
  719. INT_XLATE(buf_core->di_atime.t_sec, mem_core->di_atime.t_sec,
  720. dir, arch);
  721. INT_XLATE(buf_core->di_atime.t_nsec, mem_core->di_atime.t_nsec,
  722. dir, arch);
  723. INT_XLATE(buf_core->di_mtime.t_sec, mem_core->di_mtime.t_sec,
  724. dir, arch);
  725. INT_XLATE(buf_core->di_mtime.t_nsec, mem_core->di_mtime.t_nsec,
  726. dir, arch);
  727. INT_XLATE(buf_core->di_ctime.t_sec, mem_core->di_ctime.t_sec,
  728. dir, arch);
  729. INT_XLATE(buf_core->di_ctime.t_nsec, mem_core->di_ctime.t_nsec,
  730. dir, arch);
  731. INT_XLATE(buf_core->di_size, mem_core->di_size, dir, arch);
  732. INT_XLATE(buf_core->di_nblocks, mem_core->di_nblocks, dir, arch);
  733. INT_XLATE(buf_core->di_extsize, mem_core->di_extsize, dir, arch);
  734. INT_XLATE(buf_core->di_nextents, mem_core->di_nextents, dir, arch);
  735. INT_XLATE(buf_core->di_anextents, mem_core->di_anextents, dir, arch);
  736. INT_XLATE(buf_core->di_forkoff, mem_core->di_forkoff, dir, arch);
  737. INT_XLATE(buf_core->di_aformat, mem_core->di_aformat, dir, arch);
  738. INT_XLATE(buf_core->di_dmevmask, mem_core->di_dmevmask, dir, arch);
  739. INT_XLATE(buf_core->di_dmstate, mem_core->di_dmstate, dir, arch);
  740. INT_XLATE(buf_core->di_flags, mem_core->di_flags, dir, arch);
  741. INT_XLATE(buf_core->di_gen, mem_core->di_gen, dir, arch);
  742. }
  743. STATIC uint
  744. _xfs_dic2xflags(
  745. xfs_dinode_core_t *dic,
  746. __uint16_t di_flags)
  747. {
  748. uint flags = 0;
  749. if (di_flags & XFS_DIFLAG_ANY) {
  750. if (di_flags & XFS_DIFLAG_REALTIME)
  751. flags |= XFS_XFLAG_REALTIME;
  752. if (di_flags & XFS_DIFLAG_PREALLOC)
  753. flags |= XFS_XFLAG_PREALLOC;
  754. if (di_flags & XFS_DIFLAG_IMMUTABLE)
  755. flags |= XFS_XFLAG_IMMUTABLE;
  756. if (di_flags & XFS_DIFLAG_APPEND)
  757. flags |= XFS_XFLAG_APPEND;
  758. if (di_flags & XFS_DIFLAG_SYNC)
  759. flags |= XFS_XFLAG_SYNC;
  760. if (di_flags & XFS_DIFLAG_NOATIME)
  761. flags |= XFS_XFLAG_NOATIME;
  762. if (di_flags & XFS_DIFLAG_NODUMP)
  763. flags |= XFS_XFLAG_NODUMP;
  764. if (di_flags & XFS_DIFLAG_RTINHERIT)
  765. flags |= XFS_XFLAG_RTINHERIT;
  766. if (di_flags & XFS_DIFLAG_PROJINHERIT)
  767. flags |= XFS_XFLAG_PROJINHERIT;
  768. if (di_flags & XFS_DIFLAG_NOSYMLINKS)
  769. flags |= XFS_XFLAG_NOSYMLINKS;
  770. }
  771. return flags;
  772. }
  773. uint
  774. xfs_ip2xflags(
  775. xfs_inode_t *ip)
  776. {
  777. xfs_dinode_core_t *dic = &ip->i_d;
  778. return _xfs_dic2xflags(dic, dic->di_flags) |
  779. (XFS_CFORK_Q(dic) ? XFS_XFLAG_HASATTR : 0);
  780. }
  781. uint
  782. xfs_dic2xflags(
  783. xfs_dinode_core_t *dic)
  784. {
  785. return _xfs_dic2xflags(dic, INT_GET(dic->di_flags, ARCH_CONVERT)) |
  786. (XFS_CFORK_Q_DISK(dic) ? XFS_XFLAG_HASATTR : 0);
  787. }
  788. /*
  789. * Given a mount structure and an inode number, return a pointer
  790. * to a newly allocated in-core inode coresponding to the given
  791. * inode number.
  792. *
  793. * Initialize the inode's attributes and extent pointers if it
  794. * already has them (it will not if the inode has no links).
  795. */
  796. int
  797. xfs_iread(
  798. xfs_mount_t *mp,
  799. xfs_trans_t *tp,
  800. xfs_ino_t ino,
  801. xfs_inode_t **ipp,
  802. xfs_daddr_t bno)
  803. {
  804. xfs_buf_t *bp;
  805. xfs_dinode_t *dip;
  806. xfs_inode_t *ip;
  807. int error;
  808. ASSERT(xfs_inode_zone != NULL);
  809. ip = kmem_zone_zalloc(xfs_inode_zone, KM_SLEEP);
  810. ip->i_ino = ino;
  811. ip->i_mount = mp;
  812. /*
  813. * Get pointer's to the on-disk inode and the buffer containing it.
  814. * If the inode number refers to a block outside the file system
  815. * then xfs_itobp() will return NULL. In this case we should
  816. * return NULL as well. Set i_blkno to 0 so that xfs_itobp() will
  817. * know that this is a new incore inode.
  818. */
  819. error = xfs_itobp(mp, tp, ip, &dip, &bp, bno);
  820. if (error != 0) {
  821. kmem_zone_free(xfs_inode_zone, ip);
  822. return error;
  823. }
  824. /*
  825. * Initialize inode's trace buffers.
  826. * Do this before xfs_iformat in case it adds entries.
  827. */
  828. #ifdef XFS_BMAP_TRACE
  829. ip->i_xtrace = ktrace_alloc(XFS_BMAP_KTRACE_SIZE, KM_SLEEP);
  830. #endif
  831. #ifdef XFS_BMBT_TRACE
  832. ip->i_btrace = ktrace_alloc(XFS_BMBT_KTRACE_SIZE, KM_SLEEP);
  833. #endif
  834. #ifdef XFS_RW_TRACE
  835. ip->i_rwtrace = ktrace_alloc(XFS_RW_KTRACE_SIZE, KM_SLEEP);
  836. #endif
  837. #ifdef XFS_ILOCK_TRACE
  838. ip->i_lock_trace = ktrace_alloc(XFS_ILOCK_KTRACE_SIZE, KM_SLEEP);
  839. #endif
  840. #ifdef XFS_DIR2_TRACE
  841. ip->i_dir_trace = ktrace_alloc(XFS_DIR2_KTRACE_SIZE, KM_SLEEP);
  842. #endif
  843. /*
  844. * If we got something that isn't an inode it means someone
  845. * (nfs or dmi) has a stale handle.
  846. */
  847. if (INT_GET(dip->di_core.di_magic, ARCH_CONVERT) != XFS_DINODE_MAGIC) {
  848. kmem_zone_free(xfs_inode_zone, ip);
  849. xfs_trans_brelse(tp, bp);
  850. #ifdef DEBUG
  851. xfs_fs_cmn_err(CE_ALERT, mp, "xfs_iread: "
  852. "dip->di_core.di_magic (0x%x) != "
  853. "XFS_DINODE_MAGIC (0x%x)",
  854. INT_GET(dip->di_core.di_magic, ARCH_CONVERT),
  855. XFS_DINODE_MAGIC);
  856. #endif /* DEBUG */
  857. return XFS_ERROR(EINVAL);
  858. }
  859. /*
  860. * If the on-disk inode is already linked to a directory
  861. * entry, copy all of the inode into the in-core inode.
  862. * xfs_iformat() handles copying in the inode format
  863. * specific information.
  864. * Otherwise, just get the truly permanent information.
  865. */
  866. if (dip->di_core.di_mode) {
  867. xfs_xlate_dinode_core((xfs_caddr_t)&dip->di_core,
  868. &(ip->i_d), 1);
  869. error = xfs_iformat(ip, dip);
  870. if (error) {
  871. kmem_zone_free(xfs_inode_zone, ip);
  872. xfs_trans_brelse(tp, bp);
  873. #ifdef DEBUG
  874. xfs_fs_cmn_err(CE_ALERT, mp, "xfs_iread: "
  875. "xfs_iformat() returned error %d",
  876. error);
  877. #endif /* DEBUG */
  878. return error;
  879. }
  880. } else {
  881. ip->i_d.di_magic = INT_GET(dip->di_core.di_magic, ARCH_CONVERT);
  882. ip->i_d.di_version = INT_GET(dip->di_core.di_version, ARCH_CONVERT);
  883. ip->i_d.di_gen = INT_GET(dip->di_core.di_gen, ARCH_CONVERT);
  884. ip->i_d.di_flushiter = INT_GET(dip->di_core.di_flushiter, ARCH_CONVERT);
  885. /*
  886. * Make sure to pull in the mode here as well in
  887. * case the inode is released without being used.
  888. * This ensures that xfs_inactive() will see that
  889. * the inode is already free and not try to mess
  890. * with the uninitialized part of it.
  891. */
  892. ip->i_d.di_mode = 0;
  893. /*
  894. * Initialize the per-fork minima and maxima for a new
  895. * inode here. xfs_iformat will do it for old inodes.
  896. */
  897. ip->i_df.if_ext_max =
  898. XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
  899. }
  900. INIT_LIST_HEAD(&ip->i_reclaim);
  901. /*
  902. * The inode format changed when we moved the link count and
  903. * made it 32 bits long. If this is an old format inode,
  904. * convert it in memory to look like a new one. If it gets
  905. * flushed to disk we will convert back before flushing or
  906. * logging it. We zero out the new projid field and the old link
  907. * count field. We'll handle clearing the pad field (the remains
  908. * of the old uuid field) when we actually convert the inode to
  909. * the new format. We don't change the version number so that we
  910. * can distinguish this from a real new format inode.
  911. */
  912. if (ip->i_d.di_version == XFS_DINODE_VERSION_1) {
  913. ip->i_d.di_nlink = ip->i_d.di_onlink;
  914. ip->i_d.di_onlink = 0;
  915. ip->i_d.di_projid = 0;
  916. }
  917. ip->i_delayed_blks = 0;
  918. /*
  919. * Mark the buffer containing the inode as something to keep
  920. * around for a while. This helps to keep recently accessed
  921. * meta-data in-core longer.
  922. */
  923. XFS_BUF_SET_REF(bp, XFS_INO_REF);
  924. /*
  925. * Use xfs_trans_brelse() to release the buffer containing the
  926. * on-disk inode, because it was acquired with xfs_trans_read_buf()
  927. * in xfs_itobp() above. If tp is NULL, this is just a normal
  928. * brelse(). If we're within a transaction, then xfs_trans_brelse()
  929. * will only release the buffer if it is not dirty within the
  930. * transaction. It will be OK to release the buffer in this case,
  931. * because inodes on disk are never destroyed and we will be
  932. * locking the new in-core inode before putting it in the hash
  933. * table where other processes can find it. Thus we don't have
  934. * to worry about the inode being changed just because we released
  935. * the buffer.
  936. */
  937. xfs_trans_brelse(tp, bp);
  938. *ipp = ip;
  939. return 0;
  940. }
  941. /*
  942. * Read in extents from a btree-format inode.
  943. * Allocate and fill in if_extents. Real work is done in xfs_bmap.c.
  944. */
  945. int
  946. xfs_iread_extents(
  947. xfs_trans_t *tp,
  948. xfs_inode_t *ip,
  949. int whichfork)
  950. {
  951. int error;
  952. xfs_ifork_t *ifp;
  953. size_t size;
  954. if (unlikely(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
  955. XFS_ERROR_REPORT("xfs_iread_extents", XFS_ERRLEVEL_LOW,
  956. ip->i_mount);
  957. return XFS_ERROR(EFSCORRUPTED);
  958. }
  959. size = XFS_IFORK_NEXTENTS(ip, whichfork) * (uint)sizeof(xfs_bmbt_rec_t);
  960. ifp = XFS_IFORK_PTR(ip, whichfork);
  961. /*
  962. * We know that the size is valid (it's checked in iformat_btree)
  963. */
  964. ifp->if_u1.if_extents = kmem_alloc(size, KM_SLEEP);
  965. ASSERT(ifp->if_u1.if_extents != NULL);
  966. ifp->if_lastex = NULLEXTNUM;
  967. ifp->if_bytes = ifp->if_real_bytes = (int)size;
  968. ifp->if_flags |= XFS_IFEXTENTS;
  969. error = xfs_bmap_read_extents(tp, ip, whichfork);
  970. if (error) {
  971. kmem_free(ifp->if_u1.if_extents, size);
  972. ifp->if_u1.if_extents = NULL;
  973. ifp->if_bytes = ifp->if_real_bytes = 0;
  974. ifp->if_flags &= ~XFS_IFEXTENTS;
  975. return error;
  976. }
  977. xfs_validate_extents((xfs_bmbt_rec_t *)ifp->if_u1.if_extents,
  978. XFS_IFORK_NEXTENTS(ip, whichfork), 0, XFS_EXTFMT_INODE(ip));
  979. return 0;
  980. }
  981. /*
  982. * Allocate an inode on disk and return a copy of its in-core version.
  983. * The in-core inode is locked exclusively. Set mode, nlink, and rdev
  984. * appropriately within the inode. The uid and gid for the inode are
  985. * set according to the contents of the given cred structure.
  986. *
  987. * Use xfs_dialloc() to allocate the on-disk inode. If xfs_dialloc()
  988. * has a free inode available, call xfs_iget()
  989. * to obtain the in-core version of the allocated inode. Finally,
  990. * fill in the inode and log its initial contents. In this case,
  991. * ialloc_context would be set to NULL and call_again set to false.
  992. *
  993. * If xfs_dialloc() does not have an available inode,
  994. * it will replenish its supply by doing an allocation. Since we can
  995. * only do one allocation within a transaction without deadlocks, we
  996. * must commit the current transaction before returning the inode itself.
  997. * In this case, therefore, we will set call_again to true and return.
  998. * The caller should then commit the current transaction, start a new
  999. * transaction, and call xfs_ialloc() again to actually get the inode.
  1000. *
  1001. * To ensure that some other process does not grab the inode that
  1002. * was allocated during the first call to xfs_ialloc(), this routine
  1003. * also returns the [locked] bp pointing to the head of the freelist
  1004. * as ialloc_context. The caller should hold this buffer across
  1005. * the commit and pass it back into this routine on the second call.
  1006. */
  1007. int
  1008. xfs_ialloc(
  1009. xfs_trans_t *tp,
  1010. xfs_inode_t *pip,
  1011. mode_t mode,
  1012. xfs_nlink_t nlink,
  1013. xfs_dev_t rdev,
  1014. cred_t *cr,
  1015. xfs_prid_t prid,
  1016. int okalloc,
  1017. xfs_buf_t **ialloc_context,
  1018. boolean_t *call_again,
  1019. xfs_inode_t **ipp)
  1020. {
  1021. xfs_ino_t ino;
  1022. xfs_inode_t *ip;
  1023. vnode_t *vp;
  1024. uint flags;
  1025. int error;
  1026. /*
  1027. * Call the space management code to pick
  1028. * the on-disk inode to be allocated.
  1029. */
  1030. error = xfs_dialloc(tp, pip->i_ino, mode, okalloc,
  1031. ialloc_context, call_again, &ino);
  1032. if (error != 0) {
  1033. return error;
  1034. }
  1035. if (*call_again || ino == NULLFSINO) {
  1036. *ipp = NULL;
  1037. return 0;
  1038. }
  1039. ASSERT(*ialloc_context == NULL);
  1040. /*
  1041. * Get the in-core inode with the lock held exclusively.
  1042. * This is because we're setting fields here we need
  1043. * to prevent others from looking at until we're done.
  1044. */
  1045. error = xfs_trans_iget(tp->t_mountp, tp, ino,
  1046. IGET_CREATE, XFS_ILOCK_EXCL, &ip);
  1047. if (error != 0) {
  1048. return error;
  1049. }
  1050. ASSERT(ip != NULL);
  1051. vp = XFS_ITOV(ip);
  1052. ip->i_d.di_mode = (__uint16_t)mode;
  1053. ip->i_d.di_onlink = 0;
  1054. ip->i_d.di_nlink = nlink;
  1055. ASSERT(ip->i_d.di_nlink == nlink);
  1056. ip->i_d.di_uid = current_fsuid(cr);
  1057. ip->i_d.di_gid = current_fsgid(cr);
  1058. ip->i_d.di_projid = prid;
  1059. memset(&(ip->i_d.di_pad[0]), 0, sizeof(ip->i_d.di_pad));
  1060. /*
  1061. * If the superblock version is up to where we support new format
  1062. * inodes and this is currently an old format inode, then change
  1063. * the inode version number now. This way we only do the conversion
  1064. * here rather than here and in the flush/logging code.
  1065. */
  1066. if (XFS_SB_VERSION_HASNLINK(&tp->t_mountp->m_sb) &&
  1067. ip->i_d.di_version == XFS_DINODE_VERSION_1) {
  1068. ip->i_d.di_version = XFS_DINODE_VERSION_2;
  1069. /*
  1070. * We've already zeroed the old link count, the projid field,
  1071. * and the pad field.
  1072. */
  1073. }
  1074. /*
  1075. * Project ids won't be stored on disk if we are using a version 1 inode.
  1076. */
  1077. if ( (prid != 0) && (ip->i_d.di_version == XFS_DINODE_VERSION_1))
  1078. xfs_bump_ino_vers2(tp, ip);
  1079. if (XFS_INHERIT_GID(pip, vp->v_vfsp)) {
  1080. ip->i_d.di_gid = pip->i_d.di_gid;
  1081. if ((pip->i_d.di_mode & S_ISGID) && (mode & S_IFMT) == S_IFDIR) {
  1082. ip->i_d.di_mode |= S_ISGID;
  1083. }
  1084. }
  1085. /*
  1086. * If the group ID of the new file does not match the effective group
  1087. * ID or one of the supplementary group IDs, the S_ISGID bit is cleared
  1088. * (and only if the irix_sgid_inherit compatibility variable is set).
  1089. */
  1090. if ((irix_sgid_inherit) &&
  1091. (ip->i_d.di_mode & S_ISGID) &&
  1092. (!in_group_p((gid_t)ip->i_d.di_gid))) {
  1093. ip->i_d.di_mode &= ~S_ISGID;
  1094. }
  1095. ip->i_d.di_size = 0;
  1096. ip->i_d.di_nextents = 0;
  1097. ASSERT(ip->i_d.di_nblocks == 0);
  1098. xfs_ichgtime(ip, XFS_ICHGTIME_CHG|XFS_ICHGTIME_ACC|XFS_ICHGTIME_MOD);
  1099. /*
  1100. * di_gen will have been taken care of in xfs_iread.
  1101. */
  1102. ip->i_d.di_extsize = 0;
  1103. ip->i_d.di_dmevmask = 0;
  1104. ip->i_d.di_dmstate = 0;
  1105. ip->i_d.di_flags = 0;
  1106. flags = XFS_ILOG_CORE;
  1107. switch (mode & S_IFMT) {
  1108. case S_IFIFO:
  1109. case S_IFCHR:
  1110. case S_IFBLK:
  1111. case S_IFSOCK:
  1112. ip->i_d.di_format = XFS_DINODE_FMT_DEV;
  1113. ip->i_df.if_u2.if_rdev = rdev;
  1114. ip->i_df.if_flags = 0;
  1115. flags |= XFS_ILOG_DEV;
  1116. break;
  1117. case S_IFREG:
  1118. case S_IFDIR:
  1119. if (unlikely(pip->i_d.di_flags & XFS_DIFLAG_ANY)) {
  1120. uint di_flags = 0;
  1121. if ((mode & S_IFMT) == S_IFDIR) {
  1122. if (pip->i_d.di_flags & XFS_DIFLAG_RTINHERIT)
  1123. di_flags |= XFS_DIFLAG_RTINHERIT;
  1124. } else {
  1125. if (pip->i_d.di_flags & XFS_DIFLAG_RTINHERIT) {
  1126. di_flags |= XFS_DIFLAG_REALTIME;
  1127. ip->i_iocore.io_flags |= XFS_IOCORE_RT;
  1128. }
  1129. }
  1130. if ((pip->i_d.di_flags & XFS_DIFLAG_NOATIME) &&
  1131. xfs_inherit_noatime)
  1132. di_flags |= XFS_DIFLAG_NOATIME;
  1133. if ((pip->i_d.di_flags & XFS_DIFLAG_NODUMP) &&
  1134. xfs_inherit_nodump)
  1135. di_flags |= XFS_DIFLAG_NODUMP;
  1136. if ((pip->i_d.di_flags & XFS_DIFLAG_SYNC) &&
  1137. xfs_inherit_sync)
  1138. di_flags |= XFS_DIFLAG_SYNC;
  1139. if ((pip->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) &&
  1140. xfs_inherit_nosymlinks)
  1141. di_flags |= XFS_DIFLAG_NOSYMLINKS;
  1142. if (pip->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
  1143. di_flags |= XFS_DIFLAG_PROJINHERIT;
  1144. ip->i_d.di_flags |= di_flags;
  1145. }
  1146. /* FALLTHROUGH */
  1147. case S_IFLNK:
  1148. ip->i_d.di_format = XFS_DINODE_FMT_EXTENTS;
  1149. ip->i_df.if_flags = XFS_IFEXTENTS;
  1150. ip->i_df.if_bytes = ip->i_df.if_real_bytes = 0;
  1151. ip->i_df.if_u1.if_extents = NULL;
  1152. break;
  1153. default:
  1154. ASSERT(0);
  1155. }
  1156. /*
  1157. * Attribute fork settings for new inode.
  1158. */
  1159. ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
  1160. ip->i_d.di_anextents = 0;
  1161. /*
  1162. * Log the new values stuffed into the inode.
  1163. */
  1164. xfs_trans_log_inode(tp, ip, flags);
  1165. /* now that we have an i_mode we can set Linux inode ops (& unlock) */
  1166. VFS_INIT_VNODE(XFS_MTOVFS(tp->t_mountp), vp, XFS_ITOBHV(ip), 1);
  1167. *ipp = ip;
  1168. return 0;
  1169. }
  1170. /*
  1171. * Check to make sure that there are no blocks allocated to the
  1172. * file beyond the size of the file. We don't check this for
  1173. * files with fixed size extents or real time extents, but we
  1174. * at least do it for regular files.
  1175. */
  1176. #ifdef DEBUG
  1177. void
  1178. xfs_isize_check(
  1179. xfs_mount_t *mp,
  1180. xfs_inode_t *ip,
  1181. xfs_fsize_t isize)
  1182. {
  1183. xfs_fileoff_t map_first;
  1184. int nimaps;
  1185. xfs_bmbt_irec_t imaps[2];
  1186. if ((ip->i_d.di_mode & S_IFMT) != S_IFREG)
  1187. return;
  1188. if ( ip->i_d.di_flags & XFS_DIFLAG_REALTIME )
  1189. return;
  1190. nimaps = 2;
  1191. map_first = XFS_B_TO_FSB(mp, (xfs_ufsize_t)isize);
  1192. /*
  1193. * The filesystem could be shutting down, so bmapi may return
  1194. * an error.
  1195. */
  1196. if (xfs_bmapi(NULL, ip, map_first,
  1197. (XFS_B_TO_FSB(mp,
  1198. (xfs_ufsize_t)XFS_MAXIOFFSET(mp)) -
  1199. map_first),
  1200. XFS_BMAPI_ENTIRE, NULL, 0, imaps, &nimaps,
  1201. NULL))
  1202. return;
  1203. ASSERT(nimaps == 1);
  1204. ASSERT(imaps[0].br_startblock == HOLESTARTBLOCK);
  1205. }
  1206. #endif /* DEBUG */
  1207. /*
  1208. * Calculate the last possible buffered byte in a file. This must
  1209. * include data that was buffered beyond the EOF by the write code.
  1210. * This also needs to deal with overflowing the xfs_fsize_t type
  1211. * which can happen for sizes near the limit.
  1212. *
  1213. * We also need to take into account any blocks beyond the EOF. It
  1214. * may be the case that they were buffered by a write which failed.
  1215. * In that case the pages will still be in memory, but the inode size
  1216. * will never have been updated.
  1217. */
  1218. xfs_fsize_t
  1219. xfs_file_last_byte(
  1220. xfs_inode_t *ip)
  1221. {
  1222. xfs_mount_t *mp;
  1223. xfs_fsize_t last_byte;
  1224. xfs_fileoff_t last_block;
  1225. xfs_fileoff_t size_last_block;
  1226. int error;
  1227. ASSERT(ismrlocked(&(ip->i_iolock), MR_UPDATE | MR_ACCESS));
  1228. mp = ip->i_mount;
  1229. /*
  1230. * Only check for blocks beyond the EOF if the extents have
  1231. * been read in. This eliminates the need for the inode lock,
  1232. * and it also saves us from looking when it really isn't
  1233. * necessary.
  1234. */
  1235. if (ip->i_df.if_flags & XFS_IFEXTENTS) {
  1236. error = xfs_bmap_last_offset(NULL, ip, &last_block,
  1237. XFS_DATA_FORK);
  1238. if (error) {
  1239. last_block = 0;
  1240. }
  1241. } else {
  1242. last_block = 0;
  1243. }
  1244. size_last_block = XFS_B_TO_FSB(mp, (xfs_ufsize_t)ip->i_d.di_size);
  1245. last_block = XFS_FILEOFF_MAX(last_block, size_last_block);
  1246. last_byte = XFS_FSB_TO_B(mp, last_block);
  1247. if (last_byte < 0) {
  1248. return XFS_MAXIOFFSET(mp);
  1249. }
  1250. last_byte += (1 << mp->m_writeio_log);
  1251. if (last_byte < 0) {
  1252. return XFS_MAXIOFFSET(mp);
  1253. }
  1254. return last_byte;
  1255. }
  1256. #if defined(XFS_RW_TRACE)
  1257. STATIC void
  1258. xfs_itrunc_trace(
  1259. int tag,
  1260. xfs_inode_t *ip,
  1261. int flag,
  1262. xfs_fsize_t new_size,
  1263. xfs_off_t toss_start,
  1264. xfs_off_t toss_finish)
  1265. {
  1266. if (ip->i_rwtrace == NULL) {
  1267. return;
  1268. }
  1269. ktrace_enter(ip->i_rwtrace,
  1270. (void*)((long)tag),
  1271. (void*)ip,
  1272. (void*)(unsigned long)((ip->i_d.di_size >> 32) & 0xffffffff),
  1273. (void*)(unsigned long)(ip->i_d.di_size & 0xffffffff),
  1274. (void*)((long)flag),
  1275. (void*)(unsigned long)((new_size >> 32) & 0xffffffff),
  1276. (void*)(unsigned long)(new_size & 0xffffffff),
  1277. (void*)(unsigned long)((toss_start >> 32) & 0xffffffff),
  1278. (void*)(unsigned long)(toss_start & 0xffffffff),
  1279. (void*)(unsigned long)((toss_finish >> 32) & 0xffffffff),
  1280. (void*)(unsigned long)(toss_finish & 0xffffffff),
  1281. (void*)(unsigned long)current_cpu(),
  1282. (void*)0,
  1283. (void*)0,
  1284. (void*)0,
  1285. (void*)0);
  1286. }
  1287. #else
  1288. #define xfs_itrunc_trace(tag, ip, flag, new_size, toss_start, toss_finish)
  1289. #endif
  1290. /*
  1291. * Start the truncation of the file to new_size. The new size
  1292. * must be smaller than the current size. This routine will
  1293. * clear the buffer and page caches of file data in the removed
  1294. * range, and xfs_itruncate_finish() will remove the underlying
  1295. * disk blocks.
  1296. *
  1297. * The inode must have its I/O lock locked EXCLUSIVELY, and it
  1298. * must NOT have the inode lock held at all. This is because we're
  1299. * calling into the buffer/page cache code and we can't hold the
  1300. * inode lock when we do so.
  1301. *
  1302. * The flags parameter can have either the value XFS_ITRUNC_DEFINITE
  1303. * or XFS_ITRUNC_MAYBE. The XFS_ITRUNC_MAYBE value should be used
  1304. * in the case that the caller is locking things out of order and
  1305. * may not be able to call xfs_itruncate_finish() with the inode lock
  1306. * held without dropping the I/O lock. If the caller must drop the
  1307. * I/O lock before calling xfs_itruncate_finish(), then xfs_itruncate_start()
  1308. * must be called again with all the same restrictions as the initial
  1309. * call.
  1310. */
  1311. void
  1312. xfs_itruncate_start(
  1313. xfs_inode_t *ip,
  1314. uint flags,
  1315. xfs_fsize_t new_size)
  1316. {
  1317. xfs_fsize_t last_byte;
  1318. xfs_off_t toss_start;
  1319. xfs_mount_t *mp;
  1320. vnode_t *vp;
  1321. ASSERT(ismrlocked(&ip->i_iolock, MR_UPDATE) != 0);
  1322. ASSERT((new_size == 0) || (new_size <= ip->i_d.di_size));
  1323. ASSERT((flags == XFS_ITRUNC_DEFINITE) ||
  1324. (flags == XFS_ITRUNC_MAYBE));
  1325. mp = ip->i_mount;
  1326. vp = XFS_ITOV(ip);
  1327. /*
  1328. * Call VOP_TOSS_PAGES() or VOP_FLUSHINVAL_PAGES() to get rid of pages and buffers
  1329. * overlapping the region being removed. We have to use
  1330. * the less efficient VOP_FLUSHINVAL_PAGES() in the case that the
  1331. * caller may not be able to finish the truncate without
  1332. * dropping the inode's I/O lock. Make sure
  1333. * to catch any pages brought in by buffers overlapping
  1334. * the EOF by searching out beyond the isize by our
  1335. * block size. We round new_size up to a block boundary
  1336. * so that we don't toss things on the same block as
  1337. * new_size but before it.
  1338. *
  1339. * Before calling VOP_TOSS_PAGES() or VOP_FLUSHINVAL_PAGES(), make sure to
  1340. * call remapf() over the same region if the file is mapped.
  1341. * This frees up mapped file references to the pages in the
  1342. * given range and for the VOP_FLUSHINVAL_PAGES() case it ensures
  1343. * that we get the latest mapped changes flushed out.
  1344. */
  1345. toss_start = XFS_B_TO_FSB(mp, (xfs_ufsize_t)new_size);
  1346. toss_start = XFS_FSB_TO_B(mp, toss_start);
  1347. if (toss_start < 0) {
  1348. /*
  1349. * The place to start tossing is beyond our maximum
  1350. * file size, so there is no way that the data extended
  1351. * out there.
  1352. */
  1353. return;
  1354. }
  1355. last_byte = xfs_file_last_byte(ip);
  1356. xfs_itrunc_trace(XFS_ITRUNC_START, ip, flags, new_size, toss_start,
  1357. last_byte);
  1358. if (last_byte > toss_start) {
  1359. if (flags & XFS_ITRUNC_DEFINITE) {
  1360. VOP_TOSS_PAGES(vp, toss_start, -1, FI_REMAPF_LOCKED);
  1361. } else {
  1362. VOP_FLUSHINVAL_PAGES(vp, toss_start, -1, FI_REMAPF_LOCKED);
  1363. }
  1364. }
  1365. #ifdef DEBUG
  1366. if (new_size == 0) {
  1367. ASSERT(VN_CACHED(vp) == 0);
  1368. }
  1369. #endif
  1370. }
  1371. /*
  1372. * Shrink the file to the given new_size. The new
  1373. * size must be smaller than the current size.
  1374. * This will free up the underlying blocks
  1375. * in the removed range after a call to xfs_itruncate_start()
  1376. * or xfs_atruncate_start().
  1377. *
  1378. * The transaction passed to this routine must have made
  1379. * a permanent log reservation of at least XFS_ITRUNCATE_LOG_RES.
  1380. * This routine may commit the given transaction and
  1381. * start new ones, so make sure everything involved in
  1382. * the transaction is tidy before calling here.
  1383. * Some transaction will be returned to the caller to be
  1384. * committed. The incoming transaction must already include
  1385. * the inode, and both inode locks must be held exclusively.
  1386. * The inode must also be "held" within the transaction. On
  1387. * return the inode will be "held" within the returned transaction.
  1388. * This routine does NOT require any disk space to be reserved
  1389. * for it within the transaction.
  1390. *
  1391. * The fork parameter must be either xfs_attr_fork or xfs_data_fork,
  1392. * and it indicates the fork which is to be truncated. For the
  1393. * attribute fork we only support truncation to size 0.
  1394. *
  1395. * We use the sync parameter to indicate whether or not the first
  1396. * transaction we perform might have to be synchronous. For the attr fork,
  1397. * it needs to be so if the unlink of the inode is not yet known to be
  1398. * permanent in the log. This keeps us from freeing and reusing the
  1399. * blocks of the attribute fork before the unlink of the inode becomes
  1400. * permanent.
  1401. *
  1402. * For the data fork, we normally have to run synchronously if we're
  1403. * being called out of the inactive path or we're being called
  1404. * out of the create path where we're truncating an existing file.
  1405. * Either way, the truncate needs to be sync so blocks don't reappear
  1406. * in the file with altered data in case of a crash. wsync filesystems
  1407. * can run the first case async because anything that shrinks the inode
  1408. * has to run sync so by the time we're called here from inactive, the
  1409. * inode size is permanently set to 0.
  1410. *
  1411. * Calls from the truncate path always need to be sync unless we're
  1412. * in a wsync filesystem and the file has already been unlinked.
  1413. *
  1414. * The caller is responsible for correctly setting the sync parameter.
  1415. * It gets too hard for us to guess here which path we're being called
  1416. * out of just based on inode state.
  1417. */
  1418. int
  1419. xfs_itruncate_finish(
  1420. xfs_trans_t **tp,
  1421. xfs_inode_t *ip,
  1422. xfs_fsize_t new_size,
  1423. int fork,
  1424. int sync)
  1425. {
  1426. xfs_fsblock_t first_block;
  1427. xfs_fileoff_t first_unmap_block;
  1428. xfs_fileoff_t last_block;
  1429. xfs_filblks_t unmap_len=0;
  1430. xfs_mount_t *mp;
  1431. xfs_trans_t *ntp;
  1432. int done;
  1433. int committed;
  1434. xfs_bmap_free_t free_list;
  1435. int error;
  1436. ASSERT(ismrlocked(&ip->i_iolock, MR_UPDATE) != 0);
  1437. ASSERT(ismrlocked(&ip->i_lock, MR_UPDATE) != 0);
  1438. ASSERT((new_size == 0) || (new_size <= ip->i_d.di_size));
  1439. ASSERT(*tp != NULL);
  1440. ASSERT((*tp)->t_flags & XFS_TRANS_PERM_LOG_RES);
  1441. ASSERT(ip->i_transp == *tp);
  1442. ASSERT(ip->i_itemp != NULL);
  1443. ASSERT(ip->i_itemp->ili_flags & XFS_ILI_HOLD);
  1444. ntp = *tp;
  1445. mp = (ntp)->t_mountp;
  1446. ASSERT(! XFS_NOT_DQATTACHED(mp, ip));
  1447. /*
  1448. * We only support truncating the entire attribute fork.
  1449. */
  1450. if (fork == XFS_ATTR_FORK) {
  1451. new_size = 0LL;
  1452. }
  1453. first_unmap_block = XFS_B_TO_FSB(mp, (xfs_ufsize_t)new_size);
  1454. xfs_itrunc_trace(XFS_ITRUNC_FINISH1, ip, 0, new_size, 0, 0);
  1455. /*
  1456. * The first thing we do is set the size to new_size permanently
  1457. * on disk. This way we don't have to worry about anyone ever
  1458. * being able to look at the data being freed even in the face
  1459. * of a crash. What we're getting around here is the case where
  1460. * we free a block, it is allocated to another file, it is written
  1461. * to, and then we crash. If the new data gets written to the
  1462. * file but the log buffers containing the free and reallocation
  1463. * don't, then we'd end up with garbage in the blocks being freed.
  1464. * As long as we make the new_size permanent before actually
  1465. * freeing any blocks it doesn't matter if they get writtten to.
  1466. *
  1467. * The callers must signal into us whether or not the size
  1468. * setting here must be synchronous. There are a few cases
  1469. * where it doesn't have to be synchronous. Those cases
  1470. * occur if the file is unlinked and we know the unlink is
  1471. * permanent or if the blocks being truncated are guaranteed
  1472. * to be beyond the inode eof (regardless of the link count)
  1473. * and the eof value is permanent. Both of these cases occur
  1474. * only on wsync-mounted filesystems. In those cases, we're
  1475. * guaranteed that no user will ever see the data in the blocks
  1476. * that are being truncated so the truncate can run async.
  1477. * In the free beyond eof case, the file may wind up with
  1478. * more blocks allocated to it than it needs if we crash
  1479. * and that won't get fixed until the next time the file
  1480. * is re-opened and closed but that's ok as that shouldn't
  1481. * be too many blocks.
  1482. *
  1483. * However, we can't just make all wsync xactions run async
  1484. * because there's one call out of the create path that needs
  1485. * to run sync where it's truncating an existing file to size
  1486. * 0 whose size is > 0.
  1487. *
  1488. * It's probably possible to come up with a test in this
  1489. * routine that would correctly distinguish all the above
  1490. * cases from the values of the function parameters and the
  1491. * inode state but for sanity's sake, I've decided to let the
  1492. * layers above just tell us. It's simpler to correctly figure
  1493. * out in the layer above exactly under what conditions we
  1494. * can run async and I think it's easier for others read and
  1495. * follow the logic in case something has to be changed.
  1496. * cscope is your friend -- rcc.
  1497. *
  1498. * The attribute fork is much simpler.
  1499. *
  1500. * For the attribute fork we allow the caller to tell us whether
  1501. * the unlink of the inode that led to this call is yet permanent
  1502. * in the on disk log. If it is not and we will be freeing extents
  1503. * in this inode then we make the first transaction synchronous
  1504. * to make sure that the unlink is permanent by the time we free
  1505. * the blocks.
  1506. */
  1507. if (fork == XFS_DATA_FORK) {
  1508. if (ip->i_d.di_nextents > 0) {
  1509. ip->i_d.di_size = new_size;
  1510. xfs_trans_log_inode(ntp, ip, XFS_ILOG_CORE);
  1511. }
  1512. } else if (sync) {
  1513. ASSERT(!(mp->m_flags & XFS_MOUNT_WSYNC));
  1514. if (ip->i_d.di_anextents > 0)
  1515. xfs_trans_set_sync(ntp);
  1516. }
  1517. ASSERT(fork == XFS_DATA_FORK ||
  1518. (fork == XFS_ATTR_FORK &&
  1519. ((sync && !(mp->m_flags & XFS_MOUNT_WSYNC)) ||
  1520. (sync == 0 && (mp->m_flags & XFS_MOUNT_WSYNC)))));
  1521. /*
  1522. * Since it is possible for space to become allocated beyond
  1523. * the end of the file (in a crash where the space is allocated
  1524. * but the inode size is not yet updated), simply remove any
  1525. * blocks which show up between the new EOF and the maximum
  1526. * possible file size. If the first block to be removed is
  1527. * beyond the maximum file size (ie it is the same as last_block),
  1528. * then there is nothing to do.
  1529. */
  1530. last_block = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp));
  1531. ASSERT(first_unmap_block <= last_block);
  1532. done = 0;
  1533. if (last_block == first_unmap_block) {
  1534. done = 1;
  1535. } else {
  1536. unmap_len = last_block - first_unmap_block + 1;
  1537. }
  1538. while (!done) {
  1539. /*
  1540. * Free up up to XFS_ITRUNC_MAX_EXTENTS. xfs_bunmapi()
  1541. * will tell us whether it freed the entire range or
  1542. * not. If this is a synchronous mount (wsync),
  1543. * then we can tell bunmapi to keep all the
  1544. * transactions asynchronous since the unlink
  1545. * transaction that made this inode inactive has
  1546. * already hit the disk. There's no danger of
  1547. * the freed blocks being reused, there being a
  1548. * crash, and the reused blocks suddenly reappearing
  1549. * in this file with garbage in them once recovery
  1550. * runs.
  1551. */
  1552. XFS_BMAP_INIT(&free_list, &first_block);
  1553. error = xfs_bunmapi(ntp, ip, first_unmap_block,
  1554. unmap_len,
  1555. XFS_BMAPI_AFLAG(fork) |
  1556. (sync ? 0 : XFS_BMAPI_ASYNC),
  1557. XFS_ITRUNC_MAX_EXTENTS,
  1558. &first_block, &free_list, &done);
  1559. if (error) {
  1560. /*
  1561. * If the bunmapi call encounters an error,
  1562. * return to the caller where the transaction
  1563. * can be properly aborted. We just need to
  1564. * make sure we're not holding any resources
  1565. * that we were not when we came in.
  1566. */
  1567. xfs_bmap_cancel(&free_list);
  1568. return error;
  1569. }
  1570. /*
  1571. * Duplicate the transaction that has the permanent
  1572. * reservation and commit the old transaction.
  1573. */
  1574. error = xfs_bmap_finish(tp, &free_list, first_block,
  1575. &committed);
  1576. ntp = *tp;
  1577. if (error) {
  1578. /*
  1579. * If the bmap finish call encounters an error,
  1580. * return to the caller where the transaction
  1581. * can be properly aborted. We just need to
  1582. * make sure we're not holding any resources
  1583. * that we were not when we came in.
  1584. *
  1585. * Aborting from this point might lose some
  1586. * blocks in the file system, but oh well.
  1587. */
  1588. xfs_bmap_cancel(&free_list);
  1589. if (committed) {
  1590. /*
  1591. * If the passed in transaction committed
  1592. * in xfs_bmap_finish(), then we want to
  1593. * add the inode to this one before returning.
  1594. * This keeps things simple for the higher
  1595. * level code, because it always knows that
  1596. * the inode is locked and held in the
  1597. * transaction that returns to it whether
  1598. * errors occur or not. We don't mark the
  1599. * inode dirty so that this transaction can
  1600. * be easily aborted if possible.
  1601. */
  1602. xfs_trans_ijoin(ntp, ip,
  1603. XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
  1604. xfs_trans_ihold(ntp, ip);
  1605. }
  1606. return error;
  1607. }
  1608. if (committed) {
  1609. /*
  1610. * The first xact was committed,
  1611. * so add the inode to the new one.
  1612. * Mark it dirty so it will be logged
  1613. * and moved forward in the log as
  1614. * part of every commit.
  1615. */
  1616. xfs_trans_ijoin(ntp, ip,
  1617. XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
  1618. xfs_trans_ihold(ntp, ip);
  1619. xfs_trans_log_inode(ntp, ip, XFS_ILOG_CORE);
  1620. }
  1621. ntp = xfs_trans_dup(ntp);
  1622. (void) xfs_trans_commit(*tp, 0, NULL);
  1623. *tp = ntp;
  1624. error = xfs_trans_reserve(ntp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
  1625. XFS_TRANS_PERM_LOG_RES,
  1626. XFS_ITRUNCATE_LOG_COUNT);
  1627. /*
  1628. * Add the inode being truncated to the next chained
  1629. * transaction.
  1630. */
  1631. xfs_trans_ijoin(ntp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
  1632. xfs_trans_ihold(ntp, ip);
  1633. if (error)
  1634. return (error);
  1635. }
  1636. /*
  1637. * Only update the size in the case of the data fork, but
  1638. * always re-log the inode so that our permanent transaction
  1639. * can keep on rolling it forward in the log.
  1640. */
  1641. if (fork == XFS_DATA_FORK) {
  1642. xfs_isize_check(mp, ip, new_size);
  1643. ip->i_d.di_size = new_size;
  1644. }
  1645. xfs_trans_log_inode(ntp, ip, XFS_ILOG_CORE);
  1646. ASSERT((new_size != 0) ||
  1647. (fork == XFS_ATTR_FORK) ||
  1648. (ip->i_delayed_blks == 0));
  1649. ASSERT((new_size != 0) ||
  1650. (fork == XFS_ATTR_FORK) ||
  1651. (ip->i_d.di_nextents == 0));
  1652. xfs_itrunc_trace(XFS_ITRUNC_FINISH2, ip, 0, new_size, 0, 0);
  1653. return 0;
  1654. }
  1655. /*
  1656. * xfs_igrow_start
  1657. *
  1658. * Do the first part of growing a file: zero any data in the last
  1659. * block that is beyond the old EOF. We need to do this before
  1660. * the inode is joined to the transaction to modify the i_size.
  1661. * That way we can drop the inode lock and call into the buffer
  1662. * cache to get the buffer mapping the EOF.
  1663. */
  1664. int
  1665. xfs_igrow_start(
  1666. xfs_inode_t *ip,
  1667. xfs_fsize_t new_size,
  1668. cred_t *credp)
  1669. {
  1670. xfs_fsize_t isize;
  1671. int error;
  1672. ASSERT(ismrlocked(&(ip->i_lock), MR_UPDATE) != 0);
  1673. ASSERT(ismrlocked(&(ip->i_iolock), MR_UPDATE) != 0);
  1674. ASSERT(new_size > ip->i_d.di_size);
  1675. error = 0;
  1676. isize = ip->i_d.di_size;
  1677. /*
  1678. * Zero any pages that may have been created by
  1679. * xfs_write_file() beyond the end of the file
  1680. * and any blocks between the old and new file sizes.
  1681. */
  1682. error = xfs_zero_eof(XFS_ITOV(ip), &ip->i_iocore, new_size, isize,
  1683. new_size);
  1684. return error;
  1685. }
  1686. /*
  1687. * xfs_igrow_finish
  1688. *
  1689. * This routine is called to extend the size of a file.
  1690. * The inode must have both the iolock and the ilock locked
  1691. * for update and it must be a part of the current transaction.
  1692. * The xfs_igrow_start() function must have been called previously.
  1693. * If the change_flag is not zero, the inode change timestamp will
  1694. * be updated.
  1695. */
  1696. void
  1697. xfs_igrow_finish(
  1698. xfs_trans_t *tp,
  1699. xfs_inode_t *ip,
  1700. xfs_fsize_t new_size,
  1701. int change_flag)
  1702. {
  1703. ASSERT(ismrlocked(&(ip->i_lock), MR_UPDATE) != 0);
  1704. ASSERT(ismrlocked(&(ip->i_iolock), MR_UPDATE) != 0);
  1705. ASSERT(ip->i_transp == tp);
  1706. ASSERT(new_size > ip->i_d.di_size);
  1707. /*
  1708. * Update the file size. Update the inode change timestamp
  1709. * if change_flag set.
  1710. */
  1711. ip->i_d.di_size = new_size;
  1712. if (change_flag)
  1713. xfs_ichgtime(ip, XFS_ICHGTIME_CHG);
  1714. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  1715. }
  1716. /*
  1717. * This is called when the inode's link count goes to 0.
  1718. * We place the on-disk inode on a list in the AGI. It
  1719. * will be pulled from this list when the inode is freed.
  1720. */
  1721. int
  1722. xfs_iunlink(
  1723. xfs_trans_t *tp,
  1724. xfs_inode_t *ip)
  1725. {
  1726. xfs_mount_t *mp;
  1727. xfs_agi_t *agi;
  1728. xfs_dinode_t *dip;
  1729. xfs_buf_t *agibp;
  1730. xfs_buf_t *ibp;
  1731. xfs_agnumber_t agno;
  1732. xfs_daddr_t agdaddr;
  1733. xfs_agino_t agino;
  1734. short bucket_index;
  1735. int offset;
  1736. int error;
  1737. int agi_ok;
  1738. ASSERT(ip->i_d.di_nlink == 0);
  1739. ASSERT(ip->i_d.di_mode != 0);
  1740. ASSERT(ip->i_transp == tp);
  1741. mp = tp->t_mountp;
  1742. agno = XFS_INO_TO_AGNO(mp, ip->i_ino);
  1743. agdaddr = XFS_AG_DADDR(mp, agno, XFS_AGI_DADDR(mp));
  1744. /*
  1745. * Get the agi buffer first. It ensures lock ordering
  1746. * on the list.
  1747. */
  1748. error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, agdaddr,
  1749. XFS_FSS_TO_BB(mp, 1), 0, &agibp);
  1750. if (error) {
  1751. return error;
  1752. }
  1753. /*
  1754. * Validate the magic number of the agi block.
  1755. */
  1756. agi = XFS_BUF_TO_AGI(agibp);
  1757. agi_ok =
  1758. INT_GET(agi->agi_magicnum, ARCH_CONVERT) == XFS_AGI_MAGIC &&
  1759. XFS_AGI_GOOD_VERSION(INT_GET(agi->agi_versionnum, ARCH_CONVERT));
  1760. if (unlikely(XFS_TEST_ERROR(!agi_ok, mp, XFS_ERRTAG_IUNLINK,
  1761. XFS_RANDOM_IUNLINK))) {
  1762. XFS_CORRUPTION_ERROR("xfs_iunlink", XFS_ERRLEVEL_LOW, mp, agi);
  1763. xfs_trans_brelse(tp, agibp);
  1764. return XFS_ERROR(EFSCORRUPTED);
  1765. }
  1766. /*
  1767. * Get the index into the agi hash table for the
  1768. * list this inode will go on.
  1769. */
  1770. agino = XFS_INO_TO_AGINO(mp, ip->i_ino);
  1771. ASSERT(agino != 0);
  1772. bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS;
  1773. ASSERT(agi->agi_unlinked[bucket_index]);
  1774. ASSERT(INT_GET(agi->agi_unlinked[bucket_index], ARCH_CONVERT) != agino);
  1775. if (INT_GET(agi->agi_unlinked[bucket_index], ARCH_CONVERT) != NULLAGINO) {
  1776. /*
  1777. * There is already another inode in the bucket we need
  1778. * to add ourselves to. Add us at the front of the list.
  1779. * Here we put the head pointer into our next pointer,
  1780. * and then we fall through to point the head at us.
  1781. */
  1782. error = xfs_itobp(mp, tp, ip, &dip, &ibp, 0);
  1783. if (error) {
  1784. return error;
  1785. }
  1786. ASSERT(INT_GET(dip->di_next_unlinked, ARCH_CONVERT) == NULLAGINO);
  1787. ASSERT(dip->di_next_unlinked);
  1788. /* both on-disk, don't endian flip twice */
  1789. dip->di_next_unlinked = agi->agi_unlinked[bucket_index];
  1790. offset = ip->i_boffset +
  1791. offsetof(xfs_dinode_t, di_next_unlinked);
  1792. xfs_trans_inode_buf(tp, ibp);
  1793. xfs_trans_log_buf(tp, ibp, offset,
  1794. (offset + sizeof(xfs_agino_t) - 1));
  1795. xfs_inobp_check(mp, ibp);
  1796. }
  1797. /*
  1798. * Point the bucket head pointer at the inode being inserted.
  1799. */
  1800. ASSERT(agino != 0);
  1801. INT_SET(agi->agi_unlinked[bucket_index], ARCH_CONVERT, agino);
  1802. offset = offsetof(xfs_agi_t, agi_unlinked) +
  1803. (sizeof(xfs_agino_t) * bucket_index);
  1804. xfs_trans_log_buf(tp, agibp, offset,
  1805. (offset + sizeof(xfs_agino_t) - 1));
  1806. return 0;
  1807. }
  1808. /*
  1809. * Pull the on-disk inode from the AGI unlinked list.
  1810. */
  1811. STATIC int
  1812. xfs_iunlink_remove(
  1813. xfs_trans_t *tp,
  1814. xfs_inode_t *ip)
  1815. {
  1816. xfs_ino_t next_ino;
  1817. xfs_mount_t *mp;
  1818. xfs_agi_t *agi;
  1819. xfs_dinode_t *dip;
  1820. xfs_buf_t *agibp;
  1821. xfs_buf_t *ibp;
  1822. xfs_agnumber_t agno;
  1823. xfs_daddr_t agdaddr;
  1824. xfs_agino_t agino;
  1825. xfs_agino_t next_agino;
  1826. xfs_buf_t *last_ibp;
  1827. xfs_dinode_t *last_dip;
  1828. short bucket_index;
  1829. int offset, last_offset;
  1830. int error;
  1831. int agi_ok;
  1832. /*
  1833. * First pull the on-disk inode from the AGI unlinked list.
  1834. */
  1835. mp = tp->t_mountp;
  1836. agno = XFS_INO_TO_AGNO(mp, ip->i_ino);
  1837. agdaddr = XFS_AG_DADDR(mp, agno, XFS_AGI_DADDR(mp));
  1838. /*
  1839. * Get the agi buffer first. It ensures lock ordering
  1840. * on the list.
  1841. */
  1842. error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, agdaddr,
  1843. XFS_FSS_TO_BB(mp, 1), 0, &agibp);
  1844. if (error) {
  1845. cmn_err(CE_WARN,
  1846. "xfs_iunlink_remove: xfs_trans_read_buf() returned an error %d on %s. Returning error.",
  1847. error, mp->m_fsname);
  1848. return error;
  1849. }
  1850. /*
  1851. * Validate the magic number of the agi block.
  1852. */
  1853. agi = XFS_BUF_TO_AGI(agibp);
  1854. agi_ok =
  1855. INT_GET(agi->agi_magicnum, ARCH_CONVERT) == XFS_AGI_MAGIC &&
  1856. XFS_AGI_GOOD_VERSION(INT_GET(agi->agi_versionnum, ARCH_CONVERT));
  1857. if (unlikely(XFS_TEST_ERROR(!agi_ok, mp, XFS_ERRTAG_IUNLINK_REMOVE,
  1858. XFS_RANDOM_IUNLINK_REMOVE))) {
  1859. XFS_CORRUPTION_ERROR("xfs_iunlink_remove", XFS_ERRLEVEL_LOW,
  1860. mp, agi);
  1861. xfs_trans_brelse(tp, agibp);
  1862. cmn_err(CE_WARN,
  1863. "xfs_iunlink_remove: XFS_TEST_ERROR() returned an error on %s. Returning EFSCORRUPTED.",
  1864. mp->m_fsname);
  1865. return XFS_ERROR(EFSCORRUPTED);
  1866. }
  1867. /*
  1868. * Get the index into the agi hash table for the
  1869. * list this inode will go on.
  1870. */
  1871. agino = XFS_INO_TO_AGINO(mp, ip->i_ino);
  1872. ASSERT(agino != 0);
  1873. bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS;
  1874. ASSERT(INT_GET(agi->agi_unlinked[bucket_index], ARCH_CONVERT) != NULLAGINO);
  1875. ASSERT(agi->agi_unlinked[bucket_index]);
  1876. if (INT_GET(agi->agi_unlinked[bucket_index], ARCH_CONVERT) == agino) {
  1877. /*
  1878. * We're at the head of the list. Get the inode's
  1879. * on-disk buffer to see if there is anyone after us
  1880. * on the list. Only modify our next pointer if it
  1881. * is not already NULLAGINO. This saves us the overhead
  1882. * of dealing with the buffer when there is no need to
  1883. * change it.
  1884. */
  1885. error = xfs_itobp(mp, tp, ip, &dip, &ibp, 0);
  1886. if (error) {
  1887. cmn_err(CE_WARN,
  1888. "xfs_iunlink_remove: xfs_itobp() returned an error %d on %s. Returning error.",
  1889. error, mp->m_fsname);
  1890. return error;
  1891. }
  1892. next_agino = INT_GET(dip->di_next_unlinked, ARCH_CONVERT);
  1893. ASSERT(next_agino != 0);
  1894. if (next_agino != NULLAGINO) {
  1895. INT_SET(dip->di_next_unlinked, ARCH_CONVERT, NULLAGINO);
  1896. offset = ip->i_boffset +
  1897. offsetof(xfs_dinode_t, di_next_unlinked);
  1898. xfs_trans_inode_buf(tp, ibp);
  1899. xfs_trans_log_buf(tp, ibp, offset,
  1900. (offset + sizeof(xfs_agino_t) - 1));
  1901. xfs_inobp_check(mp, ibp);
  1902. } else {
  1903. xfs_trans_brelse(tp, ibp);
  1904. }
  1905. /*
  1906. * Point the bucket head pointer at the next inode.
  1907. */
  1908. ASSERT(next_agino != 0);
  1909. ASSERT(next_agino != agino);
  1910. INT_SET(agi->agi_unlinked[bucket_index], ARCH_CONVERT, next_agino);
  1911. offset = offsetof(xfs_agi_t, agi_unlinked) +
  1912. (sizeof(xfs_agino_t) * bucket_index);
  1913. xfs_trans_log_buf(tp, agibp, offset,
  1914. (offset + sizeof(xfs_agino_t) - 1));
  1915. } else {
  1916. /*
  1917. * We need to search the list for the inode being freed.
  1918. */
  1919. next_agino = INT_GET(agi->agi_unlinked[bucket_index], ARCH_CONVERT);
  1920. last_ibp = NULL;
  1921. while (next_agino != agino) {
  1922. /*
  1923. * If the last inode wasn't the one pointing to
  1924. * us, then release its buffer since we're not
  1925. * going to do anything with it.
  1926. */
  1927. if (last_ibp != NULL) {
  1928. xfs_trans_brelse(tp, last_ibp);
  1929. }
  1930. next_ino = XFS_AGINO_TO_INO(mp, agno, next_agino);
  1931. error = xfs_inotobp(mp, tp, next_ino, &last_dip,
  1932. &last_ibp, &last_offset);
  1933. if (error) {
  1934. cmn_err(CE_WARN,
  1935. "xfs_iunlink_remove: xfs_inotobp() returned an error %d on %s. Returning error.",
  1936. error, mp->m_fsname);
  1937. return error;
  1938. }
  1939. next_agino = INT_GET(last_dip->di_next_unlinked, ARCH_CONVERT);
  1940. ASSERT(next_agino != NULLAGINO);
  1941. ASSERT(next_agino != 0);
  1942. }
  1943. /*
  1944. * Now last_ibp points to the buffer previous to us on
  1945. * the unlinked list. Pull us from the list.
  1946. */
  1947. error = xfs_itobp(mp, tp, ip, &dip, &ibp, 0);
  1948. if (error) {
  1949. cmn_err(CE_WARN,
  1950. "xfs_iunlink_remove: xfs_itobp() returned an error %d on %s. Returning error.",
  1951. error, mp->m_fsname);
  1952. return error;
  1953. }
  1954. next_agino = INT_GET(dip->di_next_unlinked, ARCH_CONVERT);
  1955. ASSERT(next_agino != 0);
  1956. ASSERT(next_agino != agino);
  1957. if (next_agino != NULLAGINO) {
  1958. INT_SET(dip->di_next_unlinked, ARCH_CONVERT, NULLAGINO);
  1959. offset = ip->i_boffset +
  1960. offsetof(xfs_dinode_t, di_next_unlinked);
  1961. xfs_trans_inode_buf(tp, ibp);
  1962. xfs_trans_log_buf(tp, ibp, offset,
  1963. (offset + sizeof(xfs_agino_t) - 1));
  1964. xfs_inobp_check(mp, ibp);
  1965. } else {
  1966. xfs_trans_brelse(tp, ibp);
  1967. }
  1968. /*
  1969. * Point the previous inode on the list to the next inode.
  1970. */
  1971. INT_SET(last_dip->di_next_unlinked, ARCH_CONVERT, next_agino);
  1972. ASSERT(next_agino != 0);
  1973. offset = last_offset + offsetof(xfs_dinode_t, di_next_unlinked);
  1974. xfs_trans_inode_buf(tp, last_ibp);
  1975. xfs_trans_log_buf(tp, last_ibp, offset,
  1976. (offset + sizeof(xfs_agino_t) - 1));
  1977. xfs_inobp_check(mp, last_ibp);
  1978. }
  1979. return 0;
  1980. }
  1981. static __inline__ int xfs_inode_clean(xfs_inode_t *ip)
  1982. {
  1983. return (((ip->i_itemp == NULL) ||
  1984. !(ip->i_itemp->ili_format.ilf_fields & XFS_ILOG_ALL)) &&
  1985. (ip->i_update_core == 0));
  1986. }
  1987. STATIC void
  1988. xfs_ifree_cluster(
  1989. xfs_inode_t *free_ip,
  1990. xfs_trans_t *tp,
  1991. xfs_ino_t inum)
  1992. {
  1993. xfs_mount_t *mp = free_ip->i_mount;
  1994. int blks_per_cluster;
  1995. int nbufs;
  1996. int ninodes;
  1997. int i, j, found, pre_flushed;
  1998. xfs_daddr_t blkno;
  1999. xfs_buf_t *bp;
  2000. xfs_ihash_t *ih;
  2001. xfs_inode_t *ip, **ip_found;
  2002. xfs_inode_log_item_t *iip;
  2003. xfs_log_item_t *lip;
  2004. SPLDECL(s);
  2005. if (mp->m_sb.sb_blocksize >= XFS_INODE_CLUSTER_SIZE(mp)) {
  2006. blks_per_cluster = 1;
  2007. ninodes = mp->m_sb.sb_inopblock;
  2008. nbufs = XFS_IALLOC_BLOCKS(mp);
  2009. } else {
  2010. blks_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) /
  2011. mp->m_sb.sb_blocksize;
  2012. ninodes = blks_per_cluster * mp->m_sb.sb_inopblock;
  2013. nbufs = XFS_IALLOC_BLOCKS(mp) / blks_per_cluster;
  2014. }
  2015. ip_found = kmem_alloc(ninodes * sizeof(xfs_inode_t *), KM_NOFS);
  2016. for (j = 0; j < nbufs; j++, inum += ninodes) {
  2017. blkno = XFS_AGB_TO_DADDR(mp, XFS_INO_TO_AGNO(mp, inum),
  2018. XFS_INO_TO_AGBNO(mp, inum));
  2019. /*
  2020. * Look for each inode in memory and attempt to lock it,
  2021. * we can be racing with flush and tail pushing here.
  2022. * any inode we get the locks on, add to an array of
  2023. * inode items to process later.
  2024. *
  2025. * The get the buffer lock, we could beat a flush
  2026. * or tail pushing thread to the lock here, in which
  2027. * case they will go looking for the inode buffer
  2028. * and fail, we need some other form of interlock
  2029. * here.
  2030. */
  2031. found = 0;
  2032. for (i = 0; i < ninodes; i++) {
  2033. ih = XFS_IHASH(mp, inum + i);
  2034. read_lock(&ih->ih_lock);
  2035. for (ip = ih->ih_next; ip != NULL; ip = ip->i_next) {
  2036. if (ip->i_ino == inum + i)
  2037. break;
  2038. }
  2039. /* Inode not in memory or we found it already,
  2040. * nothing to do
  2041. */
  2042. if (!ip || (ip->i_flags & XFS_ISTALE)) {
  2043. read_unlock(&ih->ih_lock);
  2044. continue;
  2045. }
  2046. if (xfs_inode_clean(ip)) {
  2047. read_unlock(&ih->ih_lock);
  2048. continue;
  2049. }
  2050. /* If we can get the locks then add it to the
  2051. * list, otherwise by the time we get the bp lock
  2052. * below it will already be attached to the
  2053. * inode buffer.
  2054. */
  2055. /* This inode will already be locked - by us, lets
  2056. * keep it that way.
  2057. */
  2058. if (ip == free_ip) {
  2059. if (xfs_iflock_nowait(ip)) {
  2060. ip->i_flags |= XFS_ISTALE;
  2061. if (xfs_inode_clean(ip)) {
  2062. xfs_ifunlock(ip);
  2063. } else {
  2064. ip_found[found++] = ip;
  2065. }
  2066. }
  2067. read_unlock(&ih->ih_lock);
  2068. continue;
  2069. }
  2070. if (xfs_ilock_nowait(ip, XFS_ILOCK_EXCL)) {
  2071. if (xfs_iflock_nowait(ip)) {
  2072. ip->i_flags |= XFS_ISTALE;
  2073. if (xfs_inode_clean(ip)) {
  2074. xfs_ifunlock(ip);
  2075. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  2076. } else {
  2077. ip_found[found++] = ip;
  2078. }
  2079. } else {
  2080. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  2081. }
  2082. }
  2083. read_unlock(&ih->ih_lock);
  2084. }
  2085. bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, blkno,
  2086. mp->m_bsize * blks_per_cluster,
  2087. XFS_BUF_LOCK);
  2088. pre_flushed = 0;
  2089. lip = XFS_BUF_FSPRIVATE(bp, xfs_log_item_t *);
  2090. while (lip) {
  2091. if (lip->li_type == XFS_LI_INODE) {
  2092. iip = (xfs_inode_log_item_t *)lip;
  2093. ASSERT(iip->ili_logged == 1);
  2094. lip->li_cb = (void(*)(xfs_buf_t*,xfs_log_item_t*)) xfs_istale_done;
  2095. AIL_LOCK(mp,s);
  2096. iip->ili_flush_lsn = iip->ili_item.li_lsn;
  2097. AIL_UNLOCK(mp, s);
  2098. iip->ili_inode->i_flags |= XFS_ISTALE;
  2099. pre_flushed++;
  2100. }
  2101. lip = lip->li_bio_list;
  2102. }
  2103. for (i = 0; i < found; i++) {
  2104. ip = ip_found[i];
  2105. iip = ip->i_itemp;
  2106. if (!iip) {
  2107. ip->i_update_core = 0;
  2108. xfs_ifunlock(ip);
  2109. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  2110. continue;
  2111. }
  2112. iip->ili_last_fields = iip->ili_format.ilf_fields;
  2113. iip->ili_format.ilf_fields = 0;
  2114. iip->ili_logged = 1;
  2115. AIL_LOCK(mp,s);
  2116. iip->ili_flush_lsn = iip->ili_item.li_lsn;
  2117. AIL_UNLOCK(mp, s);
  2118. xfs_buf_attach_iodone(bp,
  2119. (void(*)(xfs_buf_t*,xfs_log_item_t*))
  2120. xfs_istale_done, (xfs_log_item_t *)iip);
  2121. if (ip != free_ip) {
  2122. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  2123. }
  2124. }
  2125. if (found || pre_flushed)
  2126. xfs_trans_stale_inode_buf(tp, bp);
  2127. xfs_trans_binval(tp, bp);
  2128. }
  2129. kmem_free(ip_found, ninodes * sizeof(xfs_inode_t *));
  2130. }
  2131. /*
  2132. * This is called to return an inode to the inode free list.
  2133. * The inode should already be truncated to 0 length and have
  2134. * no pages associated with it. This routine also assumes that
  2135. * the inode is already a part of the transaction.
  2136. *
  2137. * The on-disk copy of the inode will have been added to the list
  2138. * of unlinked inodes in the AGI. We need to remove the inode from
  2139. * that list atomically with respect to freeing it here.
  2140. */
  2141. int
  2142. xfs_ifree(
  2143. xfs_trans_t *tp,
  2144. xfs_inode_t *ip,
  2145. xfs_bmap_free_t *flist)
  2146. {
  2147. int error;
  2148. int delete;
  2149. xfs_ino_t first_ino;
  2150. ASSERT(ismrlocked(&ip->i_lock, MR_UPDATE));
  2151. ASSERT(ip->i_transp == tp);
  2152. ASSERT(ip->i_d.di_nlink == 0);
  2153. ASSERT(ip->i_d.di_nextents == 0);
  2154. ASSERT(ip->i_d.di_anextents == 0);
  2155. ASSERT((ip->i_d.di_size == 0) ||
  2156. ((ip->i_d.di_mode & S_IFMT) != S_IFREG));
  2157. ASSERT(ip->i_d.di_nblocks == 0);
  2158. /*
  2159. * Pull the on-disk inode from the AGI unlinked list.
  2160. */
  2161. error = xfs_iunlink_remove(tp, ip);
  2162. if (error != 0) {
  2163. return error;
  2164. }
  2165. error = xfs_difree(tp, ip->i_ino, flist, &delete, &first_ino);
  2166. if (error != 0) {
  2167. return error;
  2168. }
  2169. ip->i_d.di_mode = 0; /* mark incore inode as free */
  2170. ip->i_d.di_flags = 0;
  2171. ip->i_d.di_dmevmask = 0;
  2172. ip->i_d.di_forkoff = 0; /* mark the attr fork not in use */
  2173. ip->i_df.if_ext_max =
  2174. XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
  2175. ip->i_d.di_format = XFS_DINODE_FMT_EXTENTS;
  2176. ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
  2177. /*
  2178. * Bump the generation count so no one will be confused
  2179. * by reincarnations of this inode.
  2180. */
  2181. ip->i_d.di_gen++;
  2182. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  2183. if (delete) {
  2184. xfs_ifree_cluster(ip, tp, first_ino);
  2185. }
  2186. return 0;
  2187. }
  2188. /*
  2189. * Reallocate the space for if_broot based on the number of records
  2190. * being added or deleted as indicated in rec_diff. Move the records
  2191. * and pointers in if_broot to fit the new size. When shrinking this
  2192. * will eliminate holes between the records and pointers created by
  2193. * the caller. When growing this will create holes to be filled in
  2194. * by the caller.
  2195. *
  2196. * The caller must not request to add more records than would fit in
  2197. * the on-disk inode root. If the if_broot is currently NULL, then
  2198. * if we adding records one will be allocated. The caller must also
  2199. * not request that the number of records go below zero, although
  2200. * it can go to zero.
  2201. *
  2202. * ip -- the inode whose if_broot area is changing
  2203. * ext_diff -- the change in the number of records, positive or negative,
  2204. * requested for the if_broot array.
  2205. */
  2206. void
  2207. xfs_iroot_realloc(
  2208. xfs_inode_t *ip,
  2209. int rec_diff,
  2210. int whichfork)
  2211. {
  2212. int cur_max;
  2213. xfs_ifork_t *ifp;
  2214. xfs_bmbt_block_t *new_broot;
  2215. int new_max;
  2216. size_t new_size;
  2217. char *np;
  2218. char *op;
  2219. /*
  2220. * Handle the degenerate case quietly.
  2221. */
  2222. if (rec_diff == 0) {
  2223. return;
  2224. }
  2225. ifp = XFS_IFORK_PTR(ip, whichfork);
  2226. if (rec_diff > 0) {
  2227. /*
  2228. * If there wasn't any memory allocated before, just
  2229. * allocate it now and get out.
  2230. */
  2231. if (ifp->if_broot_bytes == 0) {
  2232. new_size = (size_t)XFS_BMAP_BROOT_SPACE_CALC(rec_diff);
  2233. ifp->if_broot = (xfs_bmbt_block_t*)kmem_alloc(new_size,
  2234. KM_SLEEP);
  2235. ifp->if_broot_bytes = (int)new_size;
  2236. return;
  2237. }
  2238. /*
  2239. * If there is already an existing if_broot, then we need
  2240. * to realloc() it and shift the pointers to their new
  2241. * location. The records don't change location because
  2242. * they are kept butted up against the btree block header.
  2243. */
  2244. cur_max = XFS_BMAP_BROOT_MAXRECS(ifp->if_broot_bytes);
  2245. new_max = cur_max + rec_diff;
  2246. new_size = (size_t)XFS_BMAP_BROOT_SPACE_CALC(new_max);
  2247. ifp->if_broot = (xfs_bmbt_block_t *)
  2248. kmem_realloc(ifp->if_broot,
  2249. new_size,
  2250. (size_t)XFS_BMAP_BROOT_SPACE_CALC(cur_max), /* old size */
  2251. KM_SLEEP);
  2252. op = (char *)XFS_BMAP_BROOT_PTR_ADDR(ifp->if_broot, 1,
  2253. ifp->if_broot_bytes);
  2254. np = (char *)XFS_BMAP_BROOT_PTR_ADDR(ifp->if_broot, 1,
  2255. (int)new_size);
  2256. ifp->if_broot_bytes = (int)new_size;
  2257. ASSERT(ifp->if_broot_bytes <=
  2258. XFS_IFORK_SIZE(ip, whichfork) + XFS_BROOT_SIZE_ADJ);
  2259. memmove(np, op, cur_max * (uint)sizeof(xfs_dfsbno_t));
  2260. return;
  2261. }
  2262. /*
  2263. * rec_diff is less than 0. In this case, we are shrinking the
  2264. * if_broot buffer. It must already exist. If we go to zero
  2265. * records, just get rid of the root and clear the status bit.
  2266. */
  2267. ASSERT((ifp->if_broot != NULL) && (ifp->if_broot_bytes > 0));
  2268. cur_max = XFS_BMAP_BROOT_MAXRECS(ifp->if_broot_bytes);
  2269. new_max = cur_max + rec_diff;
  2270. ASSERT(new_max >= 0);
  2271. if (new_max > 0)
  2272. new_size = (size_t)XFS_BMAP_BROOT_SPACE_CALC(new_max);
  2273. else
  2274. new_size = 0;
  2275. if (new_size > 0) {
  2276. new_broot = (xfs_bmbt_block_t *)kmem_alloc(new_size, KM_SLEEP);
  2277. /*
  2278. * First copy over the btree block header.
  2279. */
  2280. memcpy(new_broot, ifp->if_broot, sizeof(xfs_bmbt_block_t));
  2281. } else {
  2282. new_broot = NULL;
  2283. ifp->if_flags &= ~XFS_IFBROOT;
  2284. }
  2285. /*
  2286. * Only copy the records and pointers if there are any.
  2287. */
  2288. if (new_max > 0) {
  2289. /*
  2290. * First copy the records.
  2291. */
  2292. op = (char *)XFS_BMAP_BROOT_REC_ADDR(ifp->if_broot, 1,
  2293. ifp->if_broot_bytes);
  2294. np = (char *)XFS_BMAP_BROOT_REC_ADDR(new_broot, 1,
  2295. (int)new_size);
  2296. memcpy(np, op, new_max * (uint)sizeof(xfs_bmbt_rec_t));
  2297. /*
  2298. * Then copy the pointers.
  2299. */
  2300. op = (char *)XFS_BMAP_BROOT_PTR_ADDR(ifp->if_broot, 1,
  2301. ifp->if_broot_bytes);
  2302. np = (char *)XFS_BMAP_BROOT_PTR_ADDR(new_broot, 1,
  2303. (int)new_size);
  2304. memcpy(np, op, new_max * (uint)sizeof(xfs_dfsbno_t));
  2305. }
  2306. kmem_free(ifp->if_broot, ifp->if_broot_bytes);
  2307. ifp->if_broot = new_broot;
  2308. ifp->if_broot_bytes = (int)new_size;
  2309. ASSERT(ifp->if_broot_bytes <=
  2310. XFS_IFORK_SIZE(ip, whichfork) + XFS_BROOT_SIZE_ADJ);
  2311. return;
  2312. }
  2313. /*
  2314. * This is called when the amount of space needed for if_extents
  2315. * is increased or decreased. The change in size is indicated by
  2316. * the number of extents that need to be added or deleted in the
  2317. * ext_diff parameter.
  2318. *
  2319. * If the amount of space needed has decreased below the size of the
  2320. * inline buffer, then switch to using the inline buffer. Otherwise,
  2321. * use kmem_realloc() or kmem_alloc() to adjust the size of the buffer
  2322. * to what is needed.
  2323. *
  2324. * ip -- the inode whose if_extents area is changing
  2325. * ext_diff -- the change in the number of extents, positive or negative,
  2326. * requested for the if_extents array.
  2327. */
  2328. void
  2329. xfs_iext_realloc(
  2330. xfs_inode_t *ip,
  2331. int ext_diff,
  2332. int whichfork)
  2333. {
  2334. int byte_diff;
  2335. xfs_ifork_t *ifp;
  2336. int new_size;
  2337. uint rnew_size;
  2338. if (ext_diff == 0) {
  2339. return;
  2340. }
  2341. ifp = XFS_IFORK_PTR(ip, whichfork);
  2342. byte_diff = ext_diff * (uint)sizeof(xfs_bmbt_rec_t);
  2343. new_size = (int)ifp->if_bytes + byte_diff;
  2344. ASSERT(new_size >= 0);
  2345. if (new_size == 0) {
  2346. if (ifp->if_u1.if_extents != ifp->if_u2.if_inline_ext) {
  2347. ASSERT(ifp->if_real_bytes != 0);
  2348. kmem_free(ifp->if_u1.if_extents, ifp->if_real_bytes);
  2349. }
  2350. ifp->if_u1.if_extents = NULL;
  2351. rnew_size = 0;
  2352. } else if (new_size <= sizeof(ifp->if_u2.if_inline_ext)) {
  2353. /*
  2354. * If the valid extents can fit in if_inline_ext,
  2355. * copy them from the malloc'd vector and free it.
  2356. */
  2357. if (ifp->if_u1.if_extents != ifp->if_u2.if_inline_ext) {
  2358. /*
  2359. * For now, empty files are format EXTENTS,
  2360. * so the if_extents pointer is null.
  2361. */
  2362. if (ifp->if_u1.if_extents) {
  2363. memcpy(ifp->if_u2.if_inline_ext,
  2364. ifp->if_u1.if_extents, new_size);
  2365. kmem_free(ifp->if_u1.if_extents,
  2366. ifp->if_real_bytes);
  2367. }
  2368. ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext;
  2369. }
  2370. rnew_size = 0;
  2371. } else {
  2372. rnew_size = new_size;
  2373. if ((rnew_size & (rnew_size - 1)) != 0)
  2374. rnew_size = xfs_iroundup(rnew_size);
  2375. /*
  2376. * Stuck with malloc/realloc.
  2377. */
  2378. if (ifp->if_u1.if_extents == ifp->if_u2.if_inline_ext) {
  2379. ifp->if_u1.if_extents = (xfs_bmbt_rec_t *)
  2380. kmem_alloc(rnew_size, KM_SLEEP);
  2381. memcpy(ifp->if_u1.if_extents, ifp->if_u2.if_inline_ext,
  2382. sizeof(ifp->if_u2.if_inline_ext));
  2383. } else if (rnew_size != ifp->if_real_bytes) {
  2384. ifp->if_u1.if_extents = (xfs_bmbt_rec_t *)
  2385. kmem_realloc(ifp->if_u1.if_extents,
  2386. rnew_size,
  2387. ifp->if_real_bytes,
  2388. KM_NOFS);
  2389. }
  2390. }
  2391. ifp->if_real_bytes = rnew_size;
  2392. ifp->if_bytes = new_size;
  2393. }
  2394. /*
  2395. * This is called when the amount of space needed for if_data
  2396. * is increased or decreased. The change in size is indicated by
  2397. * the number of bytes that need to be added or deleted in the
  2398. * byte_diff parameter.
  2399. *
  2400. * If the amount of space needed has decreased below the size of the
  2401. * inline buffer, then switch to using the inline buffer. Otherwise,
  2402. * use kmem_realloc() or kmem_alloc() to adjust the size of the buffer
  2403. * to what is needed.
  2404. *
  2405. * ip -- the inode whose if_data area is changing
  2406. * byte_diff -- the change in the number of bytes, positive or negative,
  2407. * requested for the if_data array.
  2408. */
  2409. void
  2410. xfs_idata_realloc(
  2411. xfs_inode_t *ip,
  2412. int byte_diff,
  2413. int whichfork)
  2414. {
  2415. xfs_ifork_t *ifp;
  2416. int new_size;
  2417. int real_size;
  2418. if (byte_diff == 0) {
  2419. return;
  2420. }
  2421. ifp = XFS_IFORK_PTR(ip, whichfork);
  2422. new_size = (int)ifp->if_bytes + byte_diff;
  2423. ASSERT(new_size >= 0);
  2424. if (new_size == 0) {
  2425. if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) {
  2426. kmem_free(ifp->if_u1.if_data, ifp->if_real_bytes);
  2427. }
  2428. ifp->if_u1.if_data = NULL;
  2429. real_size = 0;
  2430. } else if (new_size <= sizeof(ifp->if_u2.if_inline_data)) {
  2431. /*
  2432. * If the valid extents/data can fit in if_inline_ext/data,
  2433. * copy them from the malloc'd vector and free it.
  2434. */
  2435. if (ifp->if_u1.if_data == NULL) {
  2436. ifp->if_u1.if_data = ifp->if_u2.if_inline_data;
  2437. } else if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) {
  2438. ASSERT(ifp->if_real_bytes != 0);
  2439. memcpy(ifp->if_u2.if_inline_data, ifp->if_u1.if_data,
  2440. new_size);
  2441. kmem_free(ifp->if_u1.if_data, ifp->if_real_bytes);
  2442. ifp->if_u1.if_data = ifp->if_u2.if_inline_data;
  2443. }
  2444. real_size = 0;
  2445. } else {
  2446. /*
  2447. * Stuck with malloc/realloc.
  2448. * For inline data, the underlying buffer must be
  2449. * a multiple of 4 bytes in size so that it can be
  2450. * logged and stay on word boundaries. We enforce
  2451. * that here.
  2452. */
  2453. real_size = roundup(new_size, 4);
  2454. if (ifp->if_u1.if_data == NULL) {
  2455. ASSERT(ifp->if_real_bytes == 0);
  2456. ifp->if_u1.if_data = kmem_alloc(real_size, KM_SLEEP);
  2457. } else if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) {
  2458. /*
  2459. * Only do the realloc if the underlying size
  2460. * is really changing.
  2461. */
  2462. if (ifp->if_real_bytes != real_size) {
  2463. ifp->if_u1.if_data =
  2464. kmem_realloc(ifp->if_u1.if_data,
  2465. real_size,
  2466. ifp->if_real_bytes,
  2467. KM_SLEEP);
  2468. }
  2469. } else {
  2470. ASSERT(ifp->if_real_bytes == 0);
  2471. ifp->if_u1.if_data = kmem_alloc(real_size, KM_SLEEP);
  2472. memcpy(ifp->if_u1.if_data, ifp->if_u2.if_inline_data,
  2473. ifp->if_bytes);
  2474. }
  2475. }
  2476. ifp->if_real_bytes = real_size;
  2477. ifp->if_bytes = new_size;
  2478. ASSERT(ifp->if_bytes <= XFS_IFORK_SIZE(ip, whichfork));
  2479. }
  2480. /*
  2481. * Map inode to disk block and offset.
  2482. *
  2483. * mp -- the mount point structure for the current file system
  2484. * tp -- the current transaction
  2485. * ino -- the inode number of the inode to be located
  2486. * imap -- this structure is filled in with the information necessary
  2487. * to retrieve the given inode from disk
  2488. * flags -- flags to pass to xfs_dilocate indicating whether or not
  2489. * lookups in the inode btree were OK or not
  2490. */
  2491. int
  2492. xfs_imap(
  2493. xfs_mount_t *mp,
  2494. xfs_trans_t *tp,
  2495. xfs_ino_t ino,
  2496. xfs_imap_t *imap,
  2497. uint flags)
  2498. {
  2499. xfs_fsblock_t fsbno;
  2500. int len;
  2501. int off;
  2502. int error;
  2503. fsbno = imap->im_blkno ?
  2504. XFS_DADDR_TO_FSB(mp, imap->im_blkno) : NULLFSBLOCK;
  2505. error = xfs_dilocate(mp, tp, ino, &fsbno, &len, &off, flags);
  2506. if (error != 0) {
  2507. return error;
  2508. }
  2509. imap->im_blkno = XFS_FSB_TO_DADDR(mp, fsbno);
  2510. imap->im_len = XFS_FSB_TO_BB(mp, len);
  2511. imap->im_agblkno = XFS_FSB_TO_AGBNO(mp, fsbno);
  2512. imap->im_ioffset = (ushort)off;
  2513. imap->im_boffset = (ushort)(off << mp->m_sb.sb_inodelog);
  2514. return 0;
  2515. }
  2516. void
  2517. xfs_idestroy_fork(
  2518. xfs_inode_t *ip,
  2519. int whichfork)
  2520. {
  2521. xfs_ifork_t *ifp;
  2522. ifp = XFS_IFORK_PTR(ip, whichfork);
  2523. if (ifp->if_broot != NULL) {
  2524. kmem_free(ifp->if_broot, ifp->if_broot_bytes);
  2525. ifp->if_broot = NULL;
  2526. }
  2527. /*
  2528. * If the format is local, then we can't have an extents
  2529. * array so just look for an inline data array. If we're
  2530. * not local then we may or may not have an extents list,
  2531. * so check and free it up if we do.
  2532. */
  2533. if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
  2534. if ((ifp->if_u1.if_data != ifp->if_u2.if_inline_data) &&
  2535. (ifp->if_u1.if_data != NULL)) {
  2536. ASSERT(ifp->if_real_bytes != 0);
  2537. kmem_free(ifp->if_u1.if_data, ifp->if_real_bytes);
  2538. ifp->if_u1.if_data = NULL;
  2539. ifp->if_real_bytes = 0;
  2540. }
  2541. } else if ((ifp->if_flags & XFS_IFEXTENTS) &&
  2542. (ifp->if_u1.if_extents != NULL) &&
  2543. (ifp->if_u1.if_extents != ifp->if_u2.if_inline_ext)) {
  2544. ASSERT(ifp->if_real_bytes != 0);
  2545. kmem_free(ifp->if_u1.if_extents, ifp->if_real_bytes);
  2546. ifp->if_u1.if_extents = NULL;
  2547. ifp->if_real_bytes = 0;
  2548. }
  2549. ASSERT(ifp->if_u1.if_extents == NULL ||
  2550. ifp->if_u1.if_extents == ifp->if_u2.if_inline_ext);
  2551. ASSERT(ifp->if_real_bytes == 0);
  2552. if (whichfork == XFS_ATTR_FORK) {
  2553. kmem_zone_free(xfs_ifork_zone, ip->i_afp);
  2554. ip->i_afp = NULL;
  2555. }
  2556. }
  2557. /*
  2558. * This is called free all the memory associated with an inode.
  2559. * It must free the inode itself and any buffers allocated for
  2560. * if_extents/if_data and if_broot. It must also free the lock
  2561. * associated with the inode.
  2562. */
  2563. void
  2564. xfs_idestroy(
  2565. xfs_inode_t *ip)
  2566. {
  2567. switch (ip->i_d.di_mode & S_IFMT) {
  2568. case S_IFREG:
  2569. case S_IFDIR:
  2570. case S_IFLNK:
  2571. xfs_idestroy_fork(ip, XFS_DATA_FORK);
  2572. break;
  2573. }
  2574. if (ip->i_afp)
  2575. xfs_idestroy_fork(ip, XFS_ATTR_FORK);
  2576. mrfree(&ip->i_lock);
  2577. mrfree(&ip->i_iolock);
  2578. freesema(&ip->i_flock);
  2579. #ifdef XFS_BMAP_TRACE
  2580. ktrace_free(ip->i_xtrace);
  2581. #endif
  2582. #ifdef XFS_BMBT_TRACE
  2583. ktrace_free(ip->i_btrace);
  2584. #endif
  2585. #ifdef XFS_RW_TRACE
  2586. ktrace_free(ip->i_rwtrace);
  2587. #endif
  2588. #ifdef XFS_ILOCK_TRACE
  2589. ktrace_free(ip->i_lock_trace);
  2590. #endif
  2591. #ifdef XFS_DIR2_TRACE
  2592. ktrace_free(ip->i_dir_trace);
  2593. #endif
  2594. if (ip->i_itemp) {
  2595. /* XXXdpd should be able to assert this but shutdown
  2596. * is leaving the AIL behind. */
  2597. ASSERT(((ip->i_itemp->ili_item.li_flags & XFS_LI_IN_AIL) == 0) ||
  2598. XFS_FORCED_SHUTDOWN(ip->i_mount));
  2599. xfs_inode_item_destroy(ip);
  2600. }
  2601. kmem_zone_free(xfs_inode_zone, ip);
  2602. }
  2603. /*
  2604. * Increment the pin count of the given buffer.
  2605. * This value is protected by ipinlock spinlock in the mount structure.
  2606. */
  2607. void
  2608. xfs_ipin(
  2609. xfs_inode_t *ip)
  2610. {
  2611. ASSERT(ismrlocked(&ip->i_lock, MR_UPDATE));
  2612. atomic_inc(&ip->i_pincount);
  2613. }
  2614. /*
  2615. * Decrement the pin count of the given inode, and wake up
  2616. * anyone in xfs_iwait_unpin() if the count goes to 0. The
  2617. * inode must have been previoulsy pinned with a call to xfs_ipin().
  2618. */
  2619. void
  2620. xfs_iunpin(
  2621. xfs_inode_t *ip)
  2622. {
  2623. ASSERT(atomic_read(&ip->i_pincount) > 0);
  2624. if (atomic_dec_and_test(&ip->i_pincount)) {
  2625. vnode_t *vp = XFS_ITOV_NULL(ip);
  2626. /* make sync come back and flush this inode */
  2627. if (vp) {
  2628. struct inode *inode = LINVFS_GET_IP(vp);
  2629. if (!(inode->i_state & I_NEW))
  2630. mark_inode_dirty_sync(inode);
  2631. }
  2632. wake_up(&ip->i_ipin_wait);
  2633. }
  2634. }
  2635. /*
  2636. * This is called to wait for the given inode to be unpinned.
  2637. * It will sleep until this happens. The caller must have the
  2638. * inode locked in at least shared mode so that the buffer cannot
  2639. * be subsequently pinned once someone is waiting for it to be
  2640. * unpinned.
  2641. */
  2642. STATIC void
  2643. xfs_iunpin_wait(
  2644. xfs_inode_t *ip)
  2645. {
  2646. xfs_inode_log_item_t *iip;
  2647. xfs_lsn_t lsn;
  2648. ASSERT(ismrlocked(&ip->i_lock, MR_UPDATE | MR_ACCESS));
  2649. if (atomic_read(&ip->i_pincount) == 0) {
  2650. return;
  2651. }
  2652. iip = ip->i_itemp;
  2653. if (iip && iip->ili_last_lsn) {
  2654. lsn = iip->ili_last_lsn;
  2655. } else {
  2656. lsn = (xfs_lsn_t)0;
  2657. }
  2658. /*
  2659. * Give the log a push so we don't wait here too long.
  2660. */
  2661. xfs_log_force(ip->i_mount, lsn, XFS_LOG_FORCE);
  2662. wait_event(ip->i_ipin_wait, (atomic_read(&ip->i_pincount) == 0));
  2663. }
  2664. /*
  2665. * xfs_iextents_copy()
  2666. *
  2667. * This is called to copy the REAL extents (as opposed to the delayed
  2668. * allocation extents) from the inode into the given buffer. It
  2669. * returns the number of bytes copied into the buffer.
  2670. *
  2671. * If there are no delayed allocation extents, then we can just
  2672. * memcpy() the extents into the buffer. Otherwise, we need to
  2673. * examine each extent in turn and skip those which are delayed.
  2674. */
  2675. int
  2676. xfs_iextents_copy(
  2677. xfs_inode_t *ip,
  2678. xfs_bmbt_rec_t *buffer,
  2679. int whichfork)
  2680. {
  2681. int copied;
  2682. xfs_bmbt_rec_t *dest_ep;
  2683. xfs_bmbt_rec_t *ep;
  2684. #ifdef XFS_BMAP_TRACE
  2685. static char fname[] = "xfs_iextents_copy";
  2686. #endif
  2687. int i;
  2688. xfs_ifork_t *ifp;
  2689. int nrecs;
  2690. xfs_fsblock_t start_block;
  2691. ifp = XFS_IFORK_PTR(ip, whichfork);
  2692. ASSERT(ismrlocked(&ip->i_lock, MR_UPDATE|MR_ACCESS));
  2693. ASSERT(ifp->if_bytes > 0);
  2694. nrecs = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  2695. xfs_bmap_trace_exlist(fname, ip, nrecs, whichfork);
  2696. ASSERT(nrecs > 0);
  2697. /*
  2698. * There are some delayed allocation extents in the
  2699. * inode, so copy the extents one at a time and skip
  2700. * the delayed ones. There must be at least one
  2701. * non-delayed extent.
  2702. */
  2703. ep = ifp->if_u1.if_extents;
  2704. dest_ep = buffer;
  2705. copied = 0;
  2706. for (i = 0; i < nrecs; i++) {
  2707. start_block = xfs_bmbt_get_startblock(ep);
  2708. if (ISNULLSTARTBLOCK(start_block)) {
  2709. /*
  2710. * It's a delayed allocation extent, so skip it.
  2711. */
  2712. ep++;
  2713. continue;
  2714. }
  2715. /* Translate to on disk format */
  2716. put_unaligned(INT_GET(ep->l0, ARCH_CONVERT),
  2717. (__uint64_t*)&dest_ep->l0);
  2718. put_unaligned(INT_GET(ep->l1, ARCH_CONVERT),
  2719. (__uint64_t*)&dest_ep->l1);
  2720. dest_ep++;
  2721. ep++;
  2722. copied++;
  2723. }
  2724. ASSERT(copied != 0);
  2725. xfs_validate_extents(buffer, copied, 1, XFS_EXTFMT_INODE(ip));
  2726. return (copied * (uint)sizeof(xfs_bmbt_rec_t));
  2727. }
  2728. /*
  2729. * Each of the following cases stores data into the same region
  2730. * of the on-disk inode, so only one of them can be valid at
  2731. * any given time. While it is possible to have conflicting formats
  2732. * and log flags, e.g. having XFS_ILOG_?DATA set when the fork is
  2733. * in EXTENTS format, this can only happen when the fork has
  2734. * changed formats after being modified but before being flushed.
  2735. * In these cases, the format always takes precedence, because the
  2736. * format indicates the current state of the fork.
  2737. */
  2738. /*ARGSUSED*/
  2739. STATIC int
  2740. xfs_iflush_fork(
  2741. xfs_inode_t *ip,
  2742. xfs_dinode_t *dip,
  2743. xfs_inode_log_item_t *iip,
  2744. int whichfork,
  2745. xfs_buf_t *bp)
  2746. {
  2747. char *cp;
  2748. xfs_ifork_t *ifp;
  2749. xfs_mount_t *mp;
  2750. #ifdef XFS_TRANS_DEBUG
  2751. int first;
  2752. #endif
  2753. static const short brootflag[2] =
  2754. { XFS_ILOG_DBROOT, XFS_ILOG_ABROOT };
  2755. static const short dataflag[2] =
  2756. { XFS_ILOG_DDATA, XFS_ILOG_ADATA };
  2757. static const short extflag[2] =
  2758. { XFS_ILOG_DEXT, XFS_ILOG_AEXT };
  2759. if (iip == NULL)
  2760. return 0;
  2761. ifp = XFS_IFORK_PTR(ip, whichfork);
  2762. /*
  2763. * This can happen if we gave up in iformat in an error path,
  2764. * for the attribute fork.
  2765. */
  2766. if (ifp == NULL) {
  2767. ASSERT(whichfork == XFS_ATTR_FORK);
  2768. return 0;
  2769. }
  2770. cp = XFS_DFORK_PTR(dip, whichfork);
  2771. mp = ip->i_mount;
  2772. switch (XFS_IFORK_FORMAT(ip, whichfork)) {
  2773. case XFS_DINODE_FMT_LOCAL:
  2774. if ((iip->ili_format.ilf_fields & dataflag[whichfork]) &&
  2775. (ifp->if_bytes > 0)) {
  2776. ASSERT(ifp->if_u1.if_data != NULL);
  2777. ASSERT(ifp->if_bytes <= XFS_IFORK_SIZE(ip, whichfork));
  2778. memcpy(cp, ifp->if_u1.if_data, ifp->if_bytes);
  2779. }
  2780. if (whichfork == XFS_DATA_FORK) {
  2781. if (unlikely(XFS_DIR_SHORTFORM_VALIDATE_ONDISK(mp, dip))) {
  2782. XFS_ERROR_REPORT("xfs_iflush_fork",
  2783. XFS_ERRLEVEL_LOW, mp);
  2784. return XFS_ERROR(EFSCORRUPTED);
  2785. }
  2786. }
  2787. break;
  2788. case XFS_DINODE_FMT_EXTENTS:
  2789. ASSERT((ifp->if_flags & XFS_IFEXTENTS) ||
  2790. !(iip->ili_format.ilf_fields & extflag[whichfork]));
  2791. ASSERT((ifp->if_u1.if_extents != NULL) || (ifp->if_bytes == 0));
  2792. ASSERT((ifp->if_u1.if_extents == NULL) || (ifp->if_bytes > 0));
  2793. if ((iip->ili_format.ilf_fields & extflag[whichfork]) &&
  2794. (ifp->if_bytes > 0)) {
  2795. ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) > 0);
  2796. (void)xfs_iextents_copy(ip, (xfs_bmbt_rec_t *)cp,
  2797. whichfork);
  2798. }
  2799. break;
  2800. case XFS_DINODE_FMT_BTREE:
  2801. if ((iip->ili_format.ilf_fields & brootflag[whichfork]) &&
  2802. (ifp->if_broot_bytes > 0)) {
  2803. ASSERT(ifp->if_broot != NULL);
  2804. ASSERT(ifp->if_broot_bytes <=
  2805. (XFS_IFORK_SIZE(ip, whichfork) +
  2806. XFS_BROOT_SIZE_ADJ));
  2807. xfs_bmbt_to_bmdr(ifp->if_broot, ifp->if_broot_bytes,
  2808. (xfs_bmdr_block_t *)cp,
  2809. XFS_DFORK_SIZE(dip, mp, whichfork));
  2810. }
  2811. break;
  2812. case XFS_DINODE_FMT_DEV:
  2813. if (iip->ili_format.ilf_fields & XFS_ILOG_DEV) {
  2814. ASSERT(whichfork == XFS_DATA_FORK);
  2815. INT_SET(dip->di_u.di_dev, ARCH_CONVERT, ip->i_df.if_u2.if_rdev);
  2816. }
  2817. break;
  2818. case XFS_DINODE_FMT_UUID:
  2819. if (iip->ili_format.ilf_fields & XFS_ILOG_UUID) {
  2820. ASSERT(whichfork == XFS_DATA_FORK);
  2821. memcpy(&dip->di_u.di_muuid, &ip->i_df.if_u2.if_uuid,
  2822. sizeof(uuid_t));
  2823. }
  2824. break;
  2825. default:
  2826. ASSERT(0);
  2827. break;
  2828. }
  2829. return 0;
  2830. }
  2831. /*
  2832. * xfs_iflush() will write a modified inode's changes out to the
  2833. * inode's on disk home. The caller must have the inode lock held
  2834. * in at least shared mode and the inode flush semaphore must be
  2835. * held as well. The inode lock will still be held upon return from
  2836. * the call and the caller is free to unlock it.
  2837. * The inode flush lock will be unlocked when the inode reaches the disk.
  2838. * The flags indicate how the inode's buffer should be written out.
  2839. */
  2840. int
  2841. xfs_iflush(
  2842. xfs_inode_t *ip,
  2843. uint flags)
  2844. {
  2845. xfs_inode_log_item_t *iip;
  2846. xfs_buf_t *bp;
  2847. xfs_dinode_t *dip;
  2848. xfs_mount_t *mp;
  2849. int error;
  2850. /* REFERENCED */
  2851. xfs_chash_t *ch;
  2852. xfs_inode_t *iq;
  2853. int clcount; /* count of inodes clustered */
  2854. int bufwasdelwri;
  2855. enum { INT_DELWRI = (1 << 0), INT_ASYNC = (1 << 1) };
  2856. SPLDECL(s);
  2857. XFS_STATS_INC(xs_iflush_count);
  2858. ASSERT(ismrlocked(&ip->i_lock, MR_UPDATE|MR_ACCESS));
  2859. ASSERT(valusema(&ip->i_flock) <= 0);
  2860. ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE ||
  2861. ip->i_d.di_nextents > ip->i_df.if_ext_max);
  2862. iip = ip->i_itemp;
  2863. mp = ip->i_mount;
  2864. /*
  2865. * If the inode isn't dirty, then just release the inode
  2866. * flush lock and do nothing.
  2867. */
  2868. if ((ip->i_update_core == 0) &&
  2869. ((iip == NULL) || !(iip->ili_format.ilf_fields & XFS_ILOG_ALL))) {
  2870. ASSERT((iip != NULL) ?
  2871. !(iip->ili_item.li_flags & XFS_LI_IN_AIL) : 1);
  2872. xfs_ifunlock(ip);
  2873. return 0;
  2874. }
  2875. /*
  2876. * We can't flush the inode until it is unpinned, so
  2877. * wait for it. We know noone new can pin it, because
  2878. * we are holding the inode lock shared and you need
  2879. * to hold it exclusively to pin the inode.
  2880. */
  2881. xfs_iunpin_wait(ip);
  2882. /*
  2883. * This may have been unpinned because the filesystem is shutting
  2884. * down forcibly. If that's the case we must not write this inode
  2885. * to disk, because the log record didn't make it to disk!
  2886. */
  2887. if (XFS_FORCED_SHUTDOWN(mp)) {
  2888. ip->i_update_core = 0;
  2889. if (iip)
  2890. iip->ili_format.ilf_fields = 0;
  2891. xfs_ifunlock(ip);
  2892. return XFS_ERROR(EIO);
  2893. }
  2894. /*
  2895. * Get the buffer containing the on-disk inode.
  2896. */
  2897. error = xfs_itobp(mp, NULL, ip, &dip, &bp, 0);
  2898. if (error != 0) {
  2899. xfs_ifunlock(ip);
  2900. return error;
  2901. }
  2902. /*
  2903. * Decide how buffer will be flushed out. This is done before
  2904. * the call to xfs_iflush_int because this field is zeroed by it.
  2905. */
  2906. if (iip != NULL && iip->ili_format.ilf_fields != 0) {
  2907. /*
  2908. * Flush out the inode buffer according to the directions
  2909. * of the caller. In the cases where the caller has given
  2910. * us a choice choose the non-delwri case. This is because
  2911. * the inode is in the AIL and we need to get it out soon.
  2912. */
  2913. switch (flags) {
  2914. case XFS_IFLUSH_SYNC:
  2915. case XFS_IFLUSH_DELWRI_ELSE_SYNC:
  2916. flags = 0;
  2917. break;
  2918. case XFS_IFLUSH_ASYNC:
  2919. case XFS_IFLUSH_DELWRI_ELSE_ASYNC:
  2920. flags = INT_ASYNC;
  2921. break;
  2922. case XFS_IFLUSH_DELWRI:
  2923. flags = INT_DELWRI;
  2924. break;
  2925. default:
  2926. ASSERT(0);
  2927. flags = 0;
  2928. break;
  2929. }
  2930. } else {
  2931. switch (flags) {
  2932. case XFS_IFLUSH_DELWRI_ELSE_SYNC:
  2933. case XFS_IFLUSH_DELWRI_ELSE_ASYNC:
  2934. case XFS_IFLUSH_DELWRI:
  2935. flags = INT_DELWRI;
  2936. break;
  2937. case XFS_IFLUSH_ASYNC:
  2938. flags = INT_ASYNC;
  2939. break;
  2940. case XFS_IFLUSH_SYNC:
  2941. flags = 0;
  2942. break;
  2943. default:
  2944. ASSERT(0);
  2945. flags = 0;
  2946. break;
  2947. }
  2948. }
  2949. /*
  2950. * First flush out the inode that xfs_iflush was called with.
  2951. */
  2952. error = xfs_iflush_int(ip, bp);
  2953. if (error) {
  2954. goto corrupt_out;
  2955. }
  2956. /*
  2957. * inode clustering:
  2958. * see if other inodes can be gathered into this write
  2959. */
  2960. ip->i_chash->chl_buf = bp;
  2961. ch = XFS_CHASH(mp, ip->i_blkno);
  2962. s = mutex_spinlock(&ch->ch_lock);
  2963. clcount = 0;
  2964. for (iq = ip->i_cnext; iq != ip; iq = iq->i_cnext) {
  2965. /*
  2966. * Do an un-protected check to see if the inode is dirty and
  2967. * is a candidate for flushing. These checks will be repeated
  2968. * later after the appropriate locks are acquired.
  2969. */
  2970. iip = iq->i_itemp;
  2971. if ((iq->i_update_core == 0) &&
  2972. ((iip == NULL) ||
  2973. !(iip->ili_format.ilf_fields & XFS_ILOG_ALL)) &&
  2974. xfs_ipincount(iq) == 0) {
  2975. continue;
  2976. }
  2977. /*
  2978. * Try to get locks. If any are unavailable,
  2979. * then this inode cannot be flushed and is skipped.
  2980. */
  2981. /* get inode locks (just i_lock) */
  2982. if (xfs_ilock_nowait(iq, XFS_ILOCK_SHARED)) {
  2983. /* get inode flush lock */
  2984. if (xfs_iflock_nowait(iq)) {
  2985. /* check if pinned */
  2986. if (xfs_ipincount(iq) == 0) {
  2987. /* arriving here means that
  2988. * this inode can be flushed.
  2989. * first re-check that it's
  2990. * dirty
  2991. */
  2992. iip = iq->i_itemp;
  2993. if ((iq->i_update_core != 0)||
  2994. ((iip != NULL) &&
  2995. (iip->ili_format.ilf_fields & XFS_ILOG_ALL))) {
  2996. clcount++;
  2997. error = xfs_iflush_int(iq, bp);
  2998. if (error) {
  2999. xfs_iunlock(iq,
  3000. XFS_ILOCK_SHARED);
  3001. goto cluster_corrupt_out;
  3002. }
  3003. } else {
  3004. xfs_ifunlock(iq);
  3005. }
  3006. } else {
  3007. xfs_ifunlock(iq);
  3008. }
  3009. }
  3010. xfs_iunlock(iq, XFS_ILOCK_SHARED);
  3011. }
  3012. }
  3013. mutex_spinunlock(&ch->ch_lock, s);
  3014. if (clcount) {
  3015. XFS_STATS_INC(xs_icluster_flushcnt);
  3016. XFS_STATS_ADD(xs_icluster_flushinode, clcount);
  3017. }
  3018. /*
  3019. * If the buffer is pinned then push on the log so we won't
  3020. * get stuck waiting in the write for too long.
  3021. */
  3022. if (XFS_BUF_ISPINNED(bp)){
  3023. xfs_log_force(mp, (xfs_lsn_t)0, XFS_LOG_FORCE);
  3024. }
  3025. if (flags & INT_DELWRI) {
  3026. xfs_bdwrite(mp, bp);
  3027. } else if (flags & INT_ASYNC) {
  3028. xfs_bawrite(mp, bp);
  3029. } else {
  3030. error = xfs_bwrite(mp, bp);
  3031. }
  3032. return error;
  3033. corrupt_out:
  3034. xfs_buf_relse(bp);
  3035. xfs_force_shutdown(mp, XFS_CORRUPT_INCORE);
  3036. xfs_iflush_abort(ip);
  3037. /*
  3038. * Unlocks the flush lock
  3039. */
  3040. return XFS_ERROR(EFSCORRUPTED);
  3041. cluster_corrupt_out:
  3042. /* Corruption detected in the clustering loop. Invalidate the
  3043. * inode buffer and shut down the filesystem.
  3044. */
  3045. mutex_spinunlock(&ch->ch_lock, s);
  3046. /*
  3047. * Clean up the buffer. If it was B_DELWRI, just release it --
  3048. * brelse can handle it with no problems. If not, shut down the
  3049. * filesystem before releasing the buffer.
  3050. */
  3051. if ((bufwasdelwri= XFS_BUF_ISDELAYWRITE(bp))) {
  3052. xfs_buf_relse(bp);
  3053. }
  3054. xfs_force_shutdown(mp, XFS_CORRUPT_INCORE);
  3055. if(!bufwasdelwri) {
  3056. /*
  3057. * Just like incore_relse: if we have b_iodone functions,
  3058. * mark the buffer as an error and call them. Otherwise
  3059. * mark it as stale and brelse.
  3060. */
  3061. if (XFS_BUF_IODONE_FUNC(bp)) {
  3062. XFS_BUF_CLR_BDSTRAT_FUNC(bp);
  3063. XFS_BUF_UNDONE(bp);
  3064. XFS_BUF_STALE(bp);
  3065. XFS_BUF_SHUT(bp);
  3066. XFS_BUF_ERROR(bp,EIO);
  3067. xfs_biodone(bp);
  3068. } else {
  3069. XFS_BUF_STALE(bp);
  3070. xfs_buf_relse(bp);
  3071. }
  3072. }
  3073. xfs_iflush_abort(iq);
  3074. /*
  3075. * Unlocks the flush lock
  3076. */
  3077. return XFS_ERROR(EFSCORRUPTED);
  3078. }
  3079. STATIC int
  3080. xfs_iflush_int(
  3081. xfs_inode_t *ip,
  3082. xfs_buf_t *bp)
  3083. {
  3084. xfs_inode_log_item_t *iip;
  3085. xfs_dinode_t *dip;
  3086. xfs_mount_t *mp;
  3087. #ifdef XFS_TRANS_DEBUG
  3088. int first;
  3089. #endif
  3090. SPLDECL(s);
  3091. ASSERT(ismrlocked(&ip->i_lock, MR_UPDATE|MR_ACCESS));
  3092. ASSERT(valusema(&ip->i_flock) <= 0);
  3093. ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE ||
  3094. ip->i_d.di_nextents > ip->i_df.if_ext_max);
  3095. iip = ip->i_itemp;
  3096. mp = ip->i_mount;
  3097. /*
  3098. * If the inode isn't dirty, then just release the inode
  3099. * flush lock and do nothing.
  3100. */
  3101. if ((ip->i_update_core == 0) &&
  3102. ((iip == NULL) || !(iip->ili_format.ilf_fields & XFS_ILOG_ALL))) {
  3103. xfs_ifunlock(ip);
  3104. return 0;
  3105. }
  3106. /* set *dip = inode's place in the buffer */
  3107. dip = (xfs_dinode_t *)xfs_buf_offset(bp, ip->i_boffset);
  3108. /*
  3109. * Clear i_update_core before copying out the data.
  3110. * This is for coordination with our timestamp updates
  3111. * that don't hold the inode lock. They will always
  3112. * update the timestamps BEFORE setting i_update_core,
  3113. * so if we clear i_update_core after they set it we
  3114. * are guaranteed to see their updates to the timestamps.
  3115. * I believe that this depends on strongly ordered memory
  3116. * semantics, but we have that. We use the SYNCHRONIZE
  3117. * macro to make sure that the compiler does not reorder
  3118. * the i_update_core access below the data copy below.
  3119. */
  3120. ip->i_update_core = 0;
  3121. SYNCHRONIZE();
  3122. if (XFS_TEST_ERROR(INT_GET(dip->di_core.di_magic,ARCH_CONVERT) != XFS_DINODE_MAGIC,
  3123. mp, XFS_ERRTAG_IFLUSH_1, XFS_RANDOM_IFLUSH_1)) {
  3124. xfs_cmn_err(XFS_PTAG_IFLUSH, CE_ALERT, mp,
  3125. "xfs_iflush: Bad inode %Lu magic number 0x%x, ptr 0x%p",
  3126. ip->i_ino, (int) INT_GET(dip->di_core.di_magic, ARCH_CONVERT), dip);
  3127. goto corrupt_out;
  3128. }
  3129. if (XFS_TEST_ERROR(ip->i_d.di_magic != XFS_DINODE_MAGIC,
  3130. mp, XFS_ERRTAG_IFLUSH_2, XFS_RANDOM_IFLUSH_2)) {
  3131. xfs_cmn_err(XFS_PTAG_IFLUSH, CE_ALERT, mp,
  3132. "xfs_iflush: Bad inode %Lu, ptr 0x%p, magic number 0x%x",
  3133. ip->i_ino, ip, ip->i_d.di_magic);
  3134. goto corrupt_out;
  3135. }
  3136. if ((ip->i_d.di_mode & S_IFMT) == S_IFREG) {
  3137. if (XFS_TEST_ERROR(
  3138. (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS) &&
  3139. (ip->i_d.di_format != XFS_DINODE_FMT_BTREE),
  3140. mp, XFS_ERRTAG_IFLUSH_3, XFS_RANDOM_IFLUSH_3)) {
  3141. xfs_cmn_err(XFS_PTAG_IFLUSH, CE_ALERT, mp,
  3142. "xfs_iflush: Bad regular inode %Lu, ptr 0x%p",
  3143. ip->i_ino, ip);
  3144. goto corrupt_out;
  3145. }
  3146. } else if ((ip->i_d.di_mode & S_IFMT) == S_IFDIR) {
  3147. if (XFS_TEST_ERROR(
  3148. (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS) &&
  3149. (ip->i_d.di_format != XFS_DINODE_FMT_BTREE) &&
  3150. (ip->i_d.di_format != XFS_DINODE_FMT_LOCAL),
  3151. mp, XFS_ERRTAG_IFLUSH_4, XFS_RANDOM_IFLUSH_4)) {
  3152. xfs_cmn_err(XFS_PTAG_IFLUSH, CE_ALERT, mp,
  3153. "xfs_iflush: Bad directory inode %Lu, ptr 0x%p",
  3154. ip->i_ino, ip);
  3155. goto corrupt_out;
  3156. }
  3157. }
  3158. if (XFS_TEST_ERROR(ip->i_d.di_nextents + ip->i_d.di_anextents >
  3159. ip->i_d.di_nblocks, mp, XFS_ERRTAG_IFLUSH_5,
  3160. XFS_RANDOM_IFLUSH_5)) {
  3161. xfs_cmn_err(XFS_PTAG_IFLUSH, CE_ALERT, mp,
  3162. "xfs_iflush: detected corrupt incore inode %Lu, total extents = %d, nblocks = %Ld, ptr 0x%p",
  3163. ip->i_ino,
  3164. ip->i_d.di_nextents + ip->i_d.di_anextents,
  3165. ip->i_d.di_nblocks,
  3166. ip);
  3167. goto corrupt_out;
  3168. }
  3169. if (XFS_TEST_ERROR(ip->i_d.di_forkoff > mp->m_sb.sb_inodesize,
  3170. mp, XFS_ERRTAG_IFLUSH_6, XFS_RANDOM_IFLUSH_6)) {
  3171. xfs_cmn_err(XFS_PTAG_IFLUSH, CE_ALERT, mp,
  3172. "xfs_iflush: bad inode %Lu, forkoff 0x%x, ptr 0x%p",
  3173. ip->i_ino, ip->i_d.di_forkoff, ip);
  3174. goto corrupt_out;
  3175. }
  3176. /*
  3177. * bump the flush iteration count, used to detect flushes which
  3178. * postdate a log record during recovery.
  3179. */
  3180. ip->i_d.di_flushiter++;
  3181. /*
  3182. * Copy the dirty parts of the inode into the on-disk
  3183. * inode. We always copy out the core of the inode,
  3184. * because if the inode is dirty at all the core must
  3185. * be.
  3186. */
  3187. xfs_xlate_dinode_core((xfs_caddr_t)&(dip->di_core), &(ip->i_d), -1);
  3188. /* Wrap, we never let the log put out DI_MAX_FLUSH */
  3189. if (ip->i_d.di_flushiter == DI_MAX_FLUSH)
  3190. ip->i_d.di_flushiter = 0;
  3191. /*
  3192. * If this is really an old format inode and the superblock version
  3193. * has not been updated to support only new format inodes, then
  3194. * convert back to the old inode format. If the superblock version
  3195. * has been updated, then make the conversion permanent.
  3196. */
  3197. ASSERT(ip->i_d.di_version == XFS_DINODE_VERSION_1 ||
  3198. XFS_SB_VERSION_HASNLINK(&mp->m_sb));
  3199. if (ip->i_d.di_version == XFS_DINODE_VERSION_1) {
  3200. if (!XFS_SB_VERSION_HASNLINK(&mp->m_sb)) {
  3201. /*
  3202. * Convert it back.
  3203. */
  3204. ASSERT(ip->i_d.di_nlink <= XFS_MAXLINK_1);
  3205. INT_SET(dip->di_core.di_onlink, ARCH_CONVERT, ip->i_d.di_nlink);
  3206. } else {
  3207. /*
  3208. * The superblock version has already been bumped,
  3209. * so just make the conversion to the new inode
  3210. * format permanent.
  3211. */
  3212. ip->i_d.di_version = XFS_DINODE_VERSION_2;
  3213. INT_SET(dip->di_core.di_version, ARCH_CONVERT, XFS_DINODE_VERSION_2);
  3214. ip->i_d.di_onlink = 0;
  3215. dip->di_core.di_onlink = 0;
  3216. memset(&(ip->i_d.di_pad[0]), 0, sizeof(ip->i_d.di_pad));
  3217. memset(&(dip->di_core.di_pad[0]), 0,
  3218. sizeof(dip->di_core.di_pad));
  3219. ASSERT(ip->i_d.di_projid == 0);
  3220. }
  3221. }
  3222. if (xfs_iflush_fork(ip, dip, iip, XFS_DATA_FORK, bp) == EFSCORRUPTED) {
  3223. goto corrupt_out;
  3224. }
  3225. if (XFS_IFORK_Q(ip)) {
  3226. /*
  3227. * The only error from xfs_iflush_fork is on the data fork.
  3228. */
  3229. (void) xfs_iflush_fork(ip, dip, iip, XFS_ATTR_FORK, bp);
  3230. }
  3231. xfs_inobp_check(mp, bp);
  3232. /*
  3233. * We've recorded everything logged in the inode, so we'd
  3234. * like to clear the ilf_fields bits so we don't log and
  3235. * flush things unnecessarily. However, we can't stop
  3236. * logging all this information until the data we've copied
  3237. * into the disk buffer is written to disk. If we did we might
  3238. * overwrite the copy of the inode in the log with all the
  3239. * data after re-logging only part of it, and in the face of
  3240. * a crash we wouldn't have all the data we need to recover.
  3241. *
  3242. * What we do is move the bits to the ili_last_fields field.
  3243. * When logging the inode, these bits are moved back to the
  3244. * ilf_fields field. In the xfs_iflush_done() routine we
  3245. * clear ili_last_fields, since we know that the information
  3246. * those bits represent is permanently on disk. As long as
  3247. * the flush completes before the inode is logged again, then
  3248. * both ilf_fields and ili_last_fields will be cleared.
  3249. *
  3250. * We can play with the ilf_fields bits here, because the inode
  3251. * lock must be held exclusively in order to set bits there
  3252. * and the flush lock protects the ili_last_fields bits.
  3253. * Set ili_logged so the flush done
  3254. * routine can tell whether or not to look in the AIL.
  3255. * Also, store the current LSN of the inode so that we can tell
  3256. * whether the item has moved in the AIL from xfs_iflush_done().
  3257. * In order to read the lsn we need the AIL lock, because
  3258. * it is a 64 bit value that cannot be read atomically.
  3259. */
  3260. if (iip != NULL && iip->ili_format.ilf_fields != 0) {
  3261. iip->ili_last_fields = iip->ili_format.ilf_fields;
  3262. iip->ili_format.ilf_fields = 0;
  3263. iip->ili_logged = 1;
  3264. ASSERT(sizeof(xfs_lsn_t) == 8); /* don't lock if it shrinks */
  3265. AIL_LOCK(mp,s);
  3266. iip->ili_flush_lsn = iip->ili_item.li_lsn;
  3267. AIL_UNLOCK(mp, s);
  3268. /*
  3269. * Attach the function xfs_iflush_done to the inode's
  3270. * buffer. This will remove the inode from the AIL
  3271. * and unlock the inode's flush lock when the inode is
  3272. * completely written to disk.
  3273. */
  3274. xfs_buf_attach_iodone(bp, (void(*)(xfs_buf_t*,xfs_log_item_t*))
  3275. xfs_iflush_done, (xfs_log_item_t *)iip);
  3276. ASSERT(XFS_BUF_FSPRIVATE(bp, void *) != NULL);
  3277. ASSERT(XFS_BUF_IODONE_FUNC(bp) != NULL);
  3278. } else {
  3279. /*
  3280. * We're flushing an inode which is not in the AIL and has
  3281. * not been logged but has i_update_core set. For this
  3282. * case we can use a B_DELWRI flush and immediately drop
  3283. * the inode flush lock because we can avoid the whole
  3284. * AIL state thing. It's OK to drop the flush lock now,
  3285. * because we've already locked the buffer and to do anything
  3286. * you really need both.
  3287. */
  3288. if (iip != NULL) {
  3289. ASSERT(iip->ili_logged == 0);
  3290. ASSERT(iip->ili_last_fields == 0);
  3291. ASSERT((iip->ili_item.li_flags & XFS_LI_IN_AIL) == 0);
  3292. }
  3293. xfs_ifunlock(ip);
  3294. }
  3295. return 0;
  3296. corrupt_out:
  3297. return XFS_ERROR(EFSCORRUPTED);
  3298. }
  3299. /*
  3300. * Flush all inactive inodes in mp.
  3301. */
  3302. void
  3303. xfs_iflush_all(
  3304. xfs_mount_t *mp)
  3305. {
  3306. xfs_inode_t *ip;
  3307. vnode_t *vp;
  3308. again:
  3309. XFS_MOUNT_ILOCK(mp);
  3310. ip = mp->m_inodes;
  3311. if (ip == NULL)
  3312. goto out;
  3313. do {
  3314. /* Make sure we skip markers inserted by sync */
  3315. if (ip->i_mount == NULL) {
  3316. ip = ip->i_mnext;
  3317. continue;
  3318. }
  3319. vp = XFS_ITOV_NULL(ip);
  3320. if (!vp) {
  3321. XFS_MOUNT_IUNLOCK(mp);
  3322. xfs_finish_reclaim(ip, 0, XFS_IFLUSH_ASYNC);
  3323. goto again;
  3324. }
  3325. ASSERT(vn_count(vp) == 0);
  3326. ip = ip->i_mnext;
  3327. } while (ip != mp->m_inodes);
  3328. out:
  3329. XFS_MOUNT_IUNLOCK(mp);
  3330. }
  3331. /*
  3332. * xfs_iaccess: check accessibility of inode for mode.
  3333. */
  3334. int
  3335. xfs_iaccess(
  3336. xfs_inode_t *ip,
  3337. mode_t mode,
  3338. cred_t *cr)
  3339. {
  3340. int error;
  3341. mode_t orgmode = mode;
  3342. struct inode *inode = LINVFS_GET_IP(XFS_ITOV(ip));
  3343. if (mode & S_IWUSR) {
  3344. umode_t imode = inode->i_mode;
  3345. if (IS_RDONLY(inode) &&
  3346. (S_ISREG(imode) || S_ISDIR(imode) || S_ISLNK(imode)))
  3347. return XFS_ERROR(EROFS);
  3348. if (IS_IMMUTABLE(inode))
  3349. return XFS_ERROR(EACCES);
  3350. }
  3351. /*
  3352. * If there's an Access Control List it's used instead of
  3353. * the mode bits.
  3354. */
  3355. if ((error = _ACL_XFS_IACCESS(ip, mode, cr)) != -1)
  3356. return error ? XFS_ERROR(error) : 0;
  3357. if (current_fsuid(cr) != ip->i_d.di_uid) {
  3358. mode >>= 3;
  3359. if (!in_group_p((gid_t)ip->i_d.di_gid))
  3360. mode >>= 3;
  3361. }
  3362. /*
  3363. * If the DACs are ok we don't need any capability check.
  3364. */
  3365. if ((ip->i_d.di_mode & mode) == mode)
  3366. return 0;
  3367. /*
  3368. * Read/write DACs are always overridable.
  3369. * Executable DACs are overridable if at least one exec bit is set.
  3370. */
  3371. if (!(orgmode & S_IXUSR) ||
  3372. (inode->i_mode & S_IXUGO) || S_ISDIR(inode->i_mode))
  3373. if (capable_cred(cr, CAP_DAC_OVERRIDE))
  3374. return 0;
  3375. if ((orgmode == S_IRUSR) ||
  3376. (S_ISDIR(inode->i_mode) && (!(orgmode & S_IWUSR)))) {
  3377. if (capable_cred(cr, CAP_DAC_READ_SEARCH))
  3378. return 0;
  3379. #ifdef NOISE
  3380. cmn_err(CE_NOTE, "Ick: mode=%o, orgmode=%o", mode, orgmode);
  3381. #endif /* NOISE */
  3382. return XFS_ERROR(EACCES);
  3383. }
  3384. return XFS_ERROR(EACCES);
  3385. }
  3386. /*
  3387. * xfs_iroundup: round up argument to next power of two
  3388. */
  3389. uint
  3390. xfs_iroundup(
  3391. uint v)
  3392. {
  3393. int i;
  3394. uint m;
  3395. if ((v & (v - 1)) == 0)
  3396. return v;
  3397. ASSERT((v & 0x80000000) == 0);
  3398. if ((v & (v + 1)) == 0)
  3399. return v + 1;
  3400. for (i = 0, m = 1; i < 31; i++, m <<= 1) {
  3401. if (v & m)
  3402. continue;
  3403. v |= m;
  3404. if ((v & (v + 1)) == 0)
  3405. return v + 1;
  3406. }
  3407. ASSERT(0);
  3408. return( 0 );
  3409. }
  3410. /*
  3411. * Change the requested timestamp in the given inode.
  3412. * We don't lock across timestamp updates, and we don't log them but
  3413. * we do record the fact that there is dirty information in core.
  3414. *
  3415. * NOTE -- callers MUST combine XFS_ICHGTIME_MOD or XFS_ICHGTIME_CHG
  3416. * with XFS_ICHGTIME_ACC to be sure that access time
  3417. * update will take. Calling first with XFS_ICHGTIME_ACC
  3418. * and then XFS_ICHGTIME_MOD may fail to modify the access
  3419. * timestamp if the filesystem is mounted noacctm.
  3420. */
  3421. void
  3422. xfs_ichgtime(xfs_inode_t *ip,
  3423. int flags)
  3424. {
  3425. timespec_t tv;
  3426. vnode_t *vp = XFS_ITOV(ip);
  3427. struct inode *inode = LINVFS_GET_IP(vp);
  3428. /*
  3429. * We're not supposed to change timestamps in readonly-mounted
  3430. * filesystems. Throw it away if anyone asks us.
  3431. */
  3432. if (unlikely(vp->v_vfsp->vfs_flag & VFS_RDONLY))
  3433. return;
  3434. /*
  3435. * Don't update access timestamps on reads if mounted "noatime"
  3436. * Throw it away if anyone asks us.
  3437. */
  3438. if ((ip->i_mount->m_flags & XFS_MOUNT_NOATIME || IS_NOATIME(inode)) &&
  3439. ((flags & (XFS_ICHGTIME_ACC|XFS_ICHGTIME_MOD|XFS_ICHGTIME_CHG))
  3440. == XFS_ICHGTIME_ACC))
  3441. return;
  3442. nanotime(&tv);
  3443. if (flags & XFS_ICHGTIME_MOD) {
  3444. VN_MTIMESET(vp, &tv);
  3445. ip->i_d.di_mtime.t_sec = (__int32_t)tv.tv_sec;
  3446. ip->i_d.di_mtime.t_nsec = (__int32_t)tv.tv_nsec;
  3447. }
  3448. if (flags & XFS_ICHGTIME_ACC) {
  3449. VN_ATIMESET(vp, &tv);
  3450. ip->i_d.di_atime.t_sec = (__int32_t)tv.tv_sec;
  3451. ip->i_d.di_atime.t_nsec = (__int32_t)tv.tv_nsec;
  3452. }
  3453. if (flags & XFS_ICHGTIME_CHG) {
  3454. VN_CTIMESET(vp, &tv);
  3455. ip->i_d.di_ctime.t_sec = (__int32_t)tv.tv_sec;
  3456. ip->i_d.di_ctime.t_nsec = (__int32_t)tv.tv_nsec;
  3457. }
  3458. /*
  3459. * We update the i_update_core field _after_ changing
  3460. * the timestamps in order to coordinate properly with
  3461. * xfs_iflush() so that we don't lose timestamp updates.
  3462. * This keeps us from having to hold the inode lock
  3463. * while doing this. We use the SYNCHRONIZE macro to
  3464. * ensure that the compiler does not reorder the update
  3465. * of i_update_core above the timestamp updates above.
  3466. */
  3467. SYNCHRONIZE();
  3468. ip->i_update_core = 1;
  3469. if (!(inode->i_state & I_LOCK))
  3470. mark_inode_dirty_sync(inode);
  3471. }
  3472. #ifdef XFS_ILOCK_TRACE
  3473. ktrace_t *xfs_ilock_trace_buf;
  3474. void
  3475. xfs_ilock_trace(xfs_inode_t *ip, int lock, unsigned int lockflags, inst_t *ra)
  3476. {
  3477. ktrace_enter(ip->i_lock_trace,
  3478. (void *)ip,
  3479. (void *)(unsigned long)lock, /* 1 = LOCK, 3=UNLOCK, etc */
  3480. (void *)(unsigned long)lockflags, /* XFS_ILOCK_EXCL etc */
  3481. (void *)ra, /* caller of ilock */
  3482. (void *)(unsigned long)current_cpu(),
  3483. (void *)(unsigned long)current_pid(),
  3484. NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL);
  3485. }
  3486. #endif