xfs_inode.c 112 KB

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