xfs_inode.c 113 KB

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