xfs_inode.c 113 KB

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