xfs_inode.c 113 KB

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