xfs_inode.c 106 KB

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