xfs_inode.c 131 KB

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