xfs_inode.c 108 KB

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