xfs_inode.c 105 KB

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