xfs_inode.c 114 KB

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