xfs_inode.c 105 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663
  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));
  1047. ASSERT(!atomic_read(&VFS_I(ip)->i_count) ||
  1048. xfs_isilocked(ip, XFS_IOLOCK_EXCL));
  1049. ASSERT(new_size <= XFS_ISIZE(ip));
  1050. ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
  1051. ASSERT(ip->i_itemp != NULL);
  1052. ASSERT(ip->i_itemp->ili_lock_flags == 0);
  1053. ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
  1054. trace_xfs_itruncate_extents_start(ip, new_size);
  1055. /*
  1056. * Since it is possible for space to become allocated beyond
  1057. * the end of the file (in a crash where the space is allocated
  1058. * but the inode size is not yet updated), simply remove any
  1059. * blocks which show up between the new EOF and the maximum
  1060. * possible file size. If the first block to be removed is
  1061. * beyond the maximum file size (ie it is the same as last_block),
  1062. * then there is nothing to do.
  1063. */
  1064. first_unmap_block = XFS_B_TO_FSB(mp, (xfs_ufsize_t)new_size);
  1065. last_block = XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes);
  1066. if (first_unmap_block == last_block)
  1067. return 0;
  1068. ASSERT(first_unmap_block < last_block);
  1069. unmap_len = last_block - first_unmap_block + 1;
  1070. while (!done) {
  1071. xfs_bmap_init(&free_list, &first_block);
  1072. error = xfs_bunmapi(tp, ip,
  1073. first_unmap_block, unmap_len,
  1074. xfs_bmapi_aflag(whichfork),
  1075. XFS_ITRUNC_MAX_EXTENTS,
  1076. &first_block, &free_list,
  1077. &done);
  1078. if (error)
  1079. goto out_bmap_cancel;
  1080. /*
  1081. * Duplicate the transaction that has the permanent
  1082. * reservation and commit the old transaction.
  1083. */
  1084. error = xfs_bmap_finish(&tp, &free_list, &committed);
  1085. if (committed)
  1086. xfs_trans_ijoin(tp, ip, 0);
  1087. if (error)
  1088. goto out_bmap_cancel;
  1089. if (committed) {
  1090. /*
  1091. * Mark the inode dirty so it will be logged and
  1092. * moved forward in the log as part of every commit.
  1093. */
  1094. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  1095. }
  1096. ntp = xfs_trans_dup(tp);
  1097. error = xfs_trans_commit(tp, 0);
  1098. tp = ntp;
  1099. xfs_trans_ijoin(tp, ip, 0);
  1100. if (error)
  1101. goto out;
  1102. /*
  1103. * Transaction commit worked ok so we can drop the extra ticket
  1104. * reference that we gained in xfs_trans_dup()
  1105. */
  1106. xfs_log_ticket_put(tp->t_ticket);
  1107. error = xfs_trans_reserve(tp, 0,
  1108. XFS_ITRUNCATE_LOG_RES(mp), 0,
  1109. XFS_TRANS_PERM_LOG_RES,
  1110. XFS_ITRUNCATE_LOG_COUNT);
  1111. if (error)
  1112. goto out;
  1113. }
  1114. /*
  1115. * Always re-log the inode so that our permanent transaction can keep
  1116. * on rolling it forward in the log.
  1117. */
  1118. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  1119. trace_xfs_itruncate_extents_end(ip, new_size);
  1120. out:
  1121. *tpp = tp;
  1122. return error;
  1123. out_bmap_cancel:
  1124. /*
  1125. * If the bunmapi call encounters an error, return to the caller where
  1126. * the transaction can be properly aborted. We just need to make sure
  1127. * we're not holding any resources that we were not when we came in.
  1128. */
  1129. xfs_bmap_cancel(&free_list);
  1130. goto out;
  1131. }
  1132. /*
  1133. * This is called when the inode's link count goes to 0.
  1134. * We place the on-disk inode on a list in the AGI. It
  1135. * will be pulled from this list when the inode is freed.
  1136. */
  1137. int
  1138. xfs_iunlink(
  1139. xfs_trans_t *tp,
  1140. xfs_inode_t *ip)
  1141. {
  1142. xfs_mount_t *mp;
  1143. xfs_agi_t *agi;
  1144. xfs_dinode_t *dip;
  1145. xfs_buf_t *agibp;
  1146. xfs_buf_t *ibp;
  1147. xfs_agino_t agino;
  1148. short bucket_index;
  1149. int offset;
  1150. int error;
  1151. ASSERT(ip->i_d.di_nlink == 0);
  1152. ASSERT(ip->i_d.di_mode != 0);
  1153. mp = tp->t_mountp;
  1154. /*
  1155. * Get the agi buffer first. It ensures lock ordering
  1156. * on the list.
  1157. */
  1158. error = xfs_read_agi(mp, tp, XFS_INO_TO_AGNO(mp, ip->i_ino), &agibp);
  1159. if (error)
  1160. return error;
  1161. agi = XFS_BUF_TO_AGI(agibp);
  1162. /*
  1163. * Get the index into the agi hash table for the
  1164. * list this inode will go on.
  1165. */
  1166. agino = XFS_INO_TO_AGINO(mp, ip->i_ino);
  1167. ASSERT(agino != 0);
  1168. bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS;
  1169. ASSERT(agi->agi_unlinked[bucket_index]);
  1170. ASSERT(be32_to_cpu(agi->agi_unlinked[bucket_index]) != agino);
  1171. if (agi->agi_unlinked[bucket_index] != cpu_to_be32(NULLAGINO)) {
  1172. /*
  1173. * There is already another inode in the bucket we need
  1174. * to add ourselves to. Add us at the front of the list.
  1175. * Here we put the head pointer into our next pointer,
  1176. * and then we fall through to point the head at us.
  1177. */
  1178. error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp,
  1179. 0, 0);
  1180. if (error)
  1181. return error;
  1182. ASSERT(dip->di_next_unlinked == cpu_to_be32(NULLAGINO));
  1183. dip->di_next_unlinked = agi->agi_unlinked[bucket_index];
  1184. offset = ip->i_imap.im_boffset +
  1185. offsetof(xfs_dinode_t, di_next_unlinked);
  1186. xfs_trans_inode_buf(tp, ibp);
  1187. xfs_trans_log_buf(tp, ibp, offset,
  1188. (offset + sizeof(xfs_agino_t) - 1));
  1189. xfs_inobp_check(mp, ibp);
  1190. }
  1191. /*
  1192. * Point the bucket head pointer at the inode being inserted.
  1193. */
  1194. ASSERT(agino != 0);
  1195. agi->agi_unlinked[bucket_index] = cpu_to_be32(agino);
  1196. offset = offsetof(xfs_agi_t, agi_unlinked) +
  1197. (sizeof(xfs_agino_t) * bucket_index);
  1198. xfs_trans_log_buf(tp, agibp, offset,
  1199. (offset + sizeof(xfs_agino_t) - 1));
  1200. return 0;
  1201. }
  1202. /*
  1203. * Pull the on-disk inode from the AGI unlinked list.
  1204. */
  1205. STATIC int
  1206. xfs_iunlink_remove(
  1207. xfs_trans_t *tp,
  1208. xfs_inode_t *ip)
  1209. {
  1210. xfs_ino_t next_ino;
  1211. xfs_mount_t *mp;
  1212. xfs_agi_t *agi;
  1213. xfs_dinode_t *dip;
  1214. xfs_buf_t *agibp;
  1215. xfs_buf_t *ibp;
  1216. xfs_agnumber_t agno;
  1217. xfs_agino_t agino;
  1218. xfs_agino_t next_agino;
  1219. xfs_buf_t *last_ibp;
  1220. xfs_dinode_t *last_dip = NULL;
  1221. short bucket_index;
  1222. int offset, last_offset = 0;
  1223. int error;
  1224. mp = tp->t_mountp;
  1225. agno = XFS_INO_TO_AGNO(mp, ip->i_ino);
  1226. /*
  1227. * Get the agi buffer first. It ensures lock ordering
  1228. * on the list.
  1229. */
  1230. error = xfs_read_agi(mp, tp, agno, &agibp);
  1231. if (error)
  1232. return error;
  1233. agi = XFS_BUF_TO_AGI(agibp);
  1234. /*
  1235. * Get the index into the agi hash table for the
  1236. * list this inode will go on.
  1237. */
  1238. agino = XFS_INO_TO_AGINO(mp, ip->i_ino);
  1239. ASSERT(agino != 0);
  1240. bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS;
  1241. ASSERT(agi->agi_unlinked[bucket_index] != cpu_to_be32(NULLAGINO));
  1242. ASSERT(agi->agi_unlinked[bucket_index]);
  1243. if (be32_to_cpu(agi->agi_unlinked[bucket_index]) == agino) {
  1244. /*
  1245. * We're at the head of the list. Get the inode's on-disk
  1246. * buffer to see if there is anyone after us on the list.
  1247. * Only modify our next pointer if it is not already NULLAGINO.
  1248. * This saves us the overhead of dealing with the buffer when
  1249. * there is no need to change it.
  1250. */
  1251. error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp,
  1252. 0, 0);
  1253. if (error) {
  1254. xfs_warn(mp, "%s: xfs_imap_to_bp returned error %d.",
  1255. __func__, error);
  1256. return error;
  1257. }
  1258. next_agino = be32_to_cpu(dip->di_next_unlinked);
  1259. ASSERT(next_agino != 0);
  1260. if (next_agino != NULLAGINO) {
  1261. dip->di_next_unlinked = cpu_to_be32(NULLAGINO);
  1262. offset = ip->i_imap.im_boffset +
  1263. offsetof(xfs_dinode_t, di_next_unlinked);
  1264. xfs_trans_inode_buf(tp, ibp);
  1265. xfs_trans_log_buf(tp, ibp, offset,
  1266. (offset + sizeof(xfs_agino_t) - 1));
  1267. xfs_inobp_check(mp, ibp);
  1268. } else {
  1269. xfs_trans_brelse(tp, ibp);
  1270. }
  1271. /*
  1272. * Point the bucket head pointer at the next inode.
  1273. */
  1274. ASSERT(next_agino != 0);
  1275. ASSERT(next_agino != agino);
  1276. agi->agi_unlinked[bucket_index] = cpu_to_be32(next_agino);
  1277. offset = offsetof(xfs_agi_t, agi_unlinked) +
  1278. (sizeof(xfs_agino_t) * bucket_index);
  1279. xfs_trans_log_buf(tp, agibp, offset,
  1280. (offset + sizeof(xfs_agino_t) - 1));
  1281. } else {
  1282. /*
  1283. * We need to search the list for the inode being freed.
  1284. */
  1285. next_agino = be32_to_cpu(agi->agi_unlinked[bucket_index]);
  1286. last_ibp = NULL;
  1287. while (next_agino != agino) {
  1288. struct xfs_imap imap;
  1289. if (last_ibp)
  1290. xfs_trans_brelse(tp, last_ibp);
  1291. imap.im_blkno = 0;
  1292. next_ino = XFS_AGINO_TO_INO(mp, agno, next_agino);
  1293. error = xfs_imap(mp, tp, next_ino, &imap, 0);
  1294. if (error) {
  1295. xfs_warn(mp,
  1296. "%s: xfs_imap returned error %d.",
  1297. __func__, error);
  1298. return error;
  1299. }
  1300. error = xfs_imap_to_bp(mp, tp, &imap, &last_dip,
  1301. &last_ibp, 0, 0);
  1302. if (error) {
  1303. xfs_warn(mp,
  1304. "%s: xfs_imap_to_bp returned error %d.",
  1305. __func__, error);
  1306. return error;
  1307. }
  1308. last_offset = imap.im_boffset;
  1309. next_agino = be32_to_cpu(last_dip->di_next_unlinked);
  1310. ASSERT(next_agino != NULLAGINO);
  1311. ASSERT(next_agino != 0);
  1312. }
  1313. /*
  1314. * Now last_ibp points to the buffer previous to us on the
  1315. * unlinked list. Pull us from the list.
  1316. */
  1317. error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp,
  1318. 0, 0);
  1319. if (error) {
  1320. xfs_warn(mp, "%s: xfs_imap_to_bp(2) returned error %d.",
  1321. __func__, error);
  1322. return error;
  1323. }
  1324. next_agino = be32_to_cpu(dip->di_next_unlinked);
  1325. ASSERT(next_agino != 0);
  1326. ASSERT(next_agino != agino);
  1327. if (next_agino != NULLAGINO) {
  1328. dip->di_next_unlinked = cpu_to_be32(NULLAGINO);
  1329. offset = ip->i_imap.im_boffset +
  1330. offsetof(xfs_dinode_t, di_next_unlinked);
  1331. xfs_trans_inode_buf(tp, ibp);
  1332. xfs_trans_log_buf(tp, ibp, offset,
  1333. (offset + sizeof(xfs_agino_t) - 1));
  1334. xfs_inobp_check(mp, ibp);
  1335. } else {
  1336. xfs_trans_brelse(tp, ibp);
  1337. }
  1338. /*
  1339. * Point the previous inode on the list to the next inode.
  1340. */
  1341. last_dip->di_next_unlinked = cpu_to_be32(next_agino);
  1342. ASSERT(next_agino != 0);
  1343. offset = last_offset + offsetof(xfs_dinode_t, di_next_unlinked);
  1344. xfs_trans_inode_buf(tp, last_ibp);
  1345. xfs_trans_log_buf(tp, last_ibp, offset,
  1346. (offset + sizeof(xfs_agino_t) - 1));
  1347. xfs_inobp_check(mp, last_ibp);
  1348. }
  1349. return 0;
  1350. }
  1351. /*
  1352. * A big issue when freeing the inode cluster is is that we _cannot_ skip any
  1353. * inodes that are in memory - they all must be marked stale and attached to
  1354. * the cluster buffer.
  1355. */
  1356. STATIC int
  1357. xfs_ifree_cluster(
  1358. xfs_inode_t *free_ip,
  1359. xfs_trans_t *tp,
  1360. xfs_ino_t inum)
  1361. {
  1362. xfs_mount_t *mp = free_ip->i_mount;
  1363. int blks_per_cluster;
  1364. int nbufs;
  1365. int ninodes;
  1366. int i, j;
  1367. xfs_daddr_t blkno;
  1368. xfs_buf_t *bp;
  1369. xfs_inode_t *ip;
  1370. xfs_inode_log_item_t *iip;
  1371. xfs_log_item_t *lip;
  1372. struct xfs_perag *pag;
  1373. pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, inum));
  1374. if (mp->m_sb.sb_blocksize >= XFS_INODE_CLUSTER_SIZE(mp)) {
  1375. blks_per_cluster = 1;
  1376. ninodes = mp->m_sb.sb_inopblock;
  1377. nbufs = XFS_IALLOC_BLOCKS(mp);
  1378. } else {
  1379. blks_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) /
  1380. mp->m_sb.sb_blocksize;
  1381. ninodes = blks_per_cluster * mp->m_sb.sb_inopblock;
  1382. nbufs = XFS_IALLOC_BLOCKS(mp) / blks_per_cluster;
  1383. }
  1384. for (j = 0; j < nbufs; j++, inum += ninodes) {
  1385. blkno = XFS_AGB_TO_DADDR(mp, XFS_INO_TO_AGNO(mp, inum),
  1386. XFS_INO_TO_AGBNO(mp, inum));
  1387. /*
  1388. * We obtain and lock the backing buffer first in the process
  1389. * here, as we have to ensure that any dirty inode that we
  1390. * can't get the flush lock on is attached to the buffer.
  1391. * If we scan the in-memory inodes first, then buffer IO can
  1392. * complete before we get a lock on it, and hence we may fail
  1393. * to mark all the active inodes on the buffer stale.
  1394. */
  1395. bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, blkno,
  1396. mp->m_bsize * blks_per_cluster,
  1397. XBF_UNMAPPED);
  1398. if (!bp)
  1399. return ENOMEM;
  1400. /*
  1401. * Walk the inodes already attached to the buffer and mark them
  1402. * stale. These will all have the flush locks held, so an
  1403. * in-memory inode walk can't lock them. By marking them all
  1404. * stale first, we will not attempt to lock them in the loop
  1405. * below as the XFS_ISTALE flag will be set.
  1406. */
  1407. lip = bp->b_fspriv;
  1408. while (lip) {
  1409. if (lip->li_type == XFS_LI_INODE) {
  1410. iip = (xfs_inode_log_item_t *)lip;
  1411. ASSERT(iip->ili_logged == 1);
  1412. lip->li_cb = xfs_istale_done;
  1413. xfs_trans_ail_copy_lsn(mp->m_ail,
  1414. &iip->ili_flush_lsn,
  1415. &iip->ili_item.li_lsn);
  1416. xfs_iflags_set(iip->ili_inode, XFS_ISTALE);
  1417. }
  1418. lip = lip->li_bio_list;
  1419. }
  1420. /*
  1421. * For each inode in memory attempt to add it to the inode
  1422. * buffer and set it up for being staled on buffer IO
  1423. * completion. This is safe as we've locked out tail pushing
  1424. * and flushing by locking the buffer.
  1425. *
  1426. * We have already marked every inode that was part of a
  1427. * transaction stale above, which means there is no point in
  1428. * even trying to lock them.
  1429. */
  1430. for (i = 0; i < ninodes; i++) {
  1431. retry:
  1432. rcu_read_lock();
  1433. ip = radix_tree_lookup(&pag->pag_ici_root,
  1434. XFS_INO_TO_AGINO(mp, (inum + i)));
  1435. /* Inode not in memory, nothing to do */
  1436. if (!ip) {
  1437. rcu_read_unlock();
  1438. continue;
  1439. }
  1440. /*
  1441. * because this is an RCU protected lookup, we could
  1442. * find a recently freed or even reallocated inode
  1443. * during the lookup. We need to check under the
  1444. * i_flags_lock for a valid inode here. Skip it if it
  1445. * is not valid, the wrong inode or stale.
  1446. */
  1447. spin_lock(&ip->i_flags_lock);
  1448. if (ip->i_ino != inum + i ||
  1449. __xfs_iflags_test(ip, XFS_ISTALE)) {
  1450. spin_unlock(&ip->i_flags_lock);
  1451. rcu_read_unlock();
  1452. continue;
  1453. }
  1454. spin_unlock(&ip->i_flags_lock);
  1455. /*
  1456. * Don't try to lock/unlock the current inode, but we
  1457. * _cannot_ skip the other inodes that we did not find
  1458. * in the list attached to the buffer and are not
  1459. * already marked stale. If we can't lock it, back off
  1460. * and retry.
  1461. */
  1462. if (ip != free_ip &&
  1463. !xfs_ilock_nowait(ip, XFS_ILOCK_EXCL)) {
  1464. rcu_read_unlock();
  1465. delay(1);
  1466. goto retry;
  1467. }
  1468. rcu_read_unlock();
  1469. xfs_iflock(ip);
  1470. xfs_iflags_set(ip, XFS_ISTALE);
  1471. /*
  1472. * we don't need to attach clean inodes or those only
  1473. * with unlogged changes (which we throw away, anyway).
  1474. */
  1475. iip = ip->i_itemp;
  1476. if (!iip || xfs_inode_clean(ip)) {
  1477. ASSERT(ip != free_ip);
  1478. xfs_ifunlock(ip);
  1479. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1480. continue;
  1481. }
  1482. iip->ili_last_fields = iip->ili_fields;
  1483. iip->ili_fields = 0;
  1484. iip->ili_logged = 1;
  1485. xfs_trans_ail_copy_lsn(mp->m_ail, &iip->ili_flush_lsn,
  1486. &iip->ili_item.li_lsn);
  1487. xfs_buf_attach_iodone(bp, xfs_istale_done,
  1488. &iip->ili_item);
  1489. if (ip != free_ip)
  1490. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1491. }
  1492. xfs_trans_stale_inode_buf(tp, bp);
  1493. xfs_trans_binval(tp, bp);
  1494. }
  1495. xfs_perag_put(pag);
  1496. return 0;
  1497. }
  1498. /*
  1499. * This is called to return an inode to the inode free list.
  1500. * The inode should already be truncated to 0 length and have
  1501. * no pages associated with it. This routine also assumes that
  1502. * the inode is already a part of the transaction.
  1503. *
  1504. * The on-disk copy of the inode will have been added to the list
  1505. * of unlinked inodes in the AGI. We need to remove the inode from
  1506. * that list atomically with respect to freeing it here.
  1507. */
  1508. int
  1509. xfs_ifree(
  1510. xfs_trans_t *tp,
  1511. xfs_inode_t *ip,
  1512. xfs_bmap_free_t *flist)
  1513. {
  1514. int error;
  1515. int delete;
  1516. xfs_ino_t first_ino;
  1517. xfs_dinode_t *dip;
  1518. xfs_buf_t *ibp;
  1519. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  1520. ASSERT(ip->i_d.di_nlink == 0);
  1521. ASSERT(ip->i_d.di_nextents == 0);
  1522. ASSERT(ip->i_d.di_anextents == 0);
  1523. ASSERT(ip->i_d.di_size == 0 || !S_ISREG(ip->i_d.di_mode));
  1524. ASSERT(ip->i_d.di_nblocks == 0);
  1525. /*
  1526. * Pull the on-disk inode from the AGI unlinked list.
  1527. */
  1528. error = xfs_iunlink_remove(tp, ip);
  1529. if (error != 0) {
  1530. return error;
  1531. }
  1532. error = xfs_difree(tp, ip->i_ino, flist, &delete, &first_ino);
  1533. if (error != 0) {
  1534. return error;
  1535. }
  1536. ip->i_d.di_mode = 0; /* mark incore inode as free */
  1537. ip->i_d.di_flags = 0;
  1538. ip->i_d.di_dmevmask = 0;
  1539. ip->i_d.di_forkoff = 0; /* mark the attr fork not in use */
  1540. ip->i_d.di_format = XFS_DINODE_FMT_EXTENTS;
  1541. ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
  1542. /*
  1543. * Bump the generation count so no one will be confused
  1544. * by reincarnations of this inode.
  1545. */
  1546. ip->i_d.di_gen++;
  1547. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  1548. error = xfs_imap_to_bp(ip->i_mount, tp, &ip->i_imap, &dip, &ibp,
  1549. 0, 0);
  1550. if (error)
  1551. return error;
  1552. /*
  1553. * Clear the on-disk di_mode. This is to prevent xfs_bulkstat
  1554. * from picking up this inode when it is reclaimed (its incore state
  1555. * initialzed but not flushed to disk yet). The in-core di_mode is
  1556. * already cleared and a corresponding transaction logged.
  1557. * The hack here just synchronizes the in-core to on-disk
  1558. * di_mode value in advance before the actual inode sync to disk.
  1559. * This is OK because the inode is already unlinked and would never
  1560. * change its di_mode again for this inode generation.
  1561. * This is a temporary hack that would require a proper fix
  1562. * in the future.
  1563. */
  1564. dip->di_mode = 0;
  1565. if (delete) {
  1566. error = xfs_ifree_cluster(ip, tp, first_ino);
  1567. }
  1568. return error;
  1569. }
  1570. /*
  1571. * Reallocate the space for if_broot based on the number of records
  1572. * being added or deleted as indicated in rec_diff. Move the records
  1573. * and pointers in if_broot to fit the new size. When shrinking this
  1574. * will eliminate holes between the records and pointers created by
  1575. * the caller. When growing this will create holes to be filled in
  1576. * by the caller.
  1577. *
  1578. * The caller must not request to add more records than would fit in
  1579. * the on-disk inode root. If the if_broot is currently NULL, then
  1580. * if we adding records one will be allocated. The caller must also
  1581. * not request that the number of records go below zero, although
  1582. * it can go to zero.
  1583. *
  1584. * ip -- the inode whose if_broot area is changing
  1585. * ext_diff -- the change in the number of records, positive or negative,
  1586. * requested for the if_broot array.
  1587. */
  1588. void
  1589. xfs_iroot_realloc(
  1590. xfs_inode_t *ip,
  1591. int rec_diff,
  1592. int whichfork)
  1593. {
  1594. struct xfs_mount *mp = ip->i_mount;
  1595. int cur_max;
  1596. xfs_ifork_t *ifp;
  1597. struct xfs_btree_block *new_broot;
  1598. int new_max;
  1599. size_t new_size;
  1600. char *np;
  1601. char *op;
  1602. /*
  1603. * Handle the degenerate case quietly.
  1604. */
  1605. if (rec_diff == 0) {
  1606. return;
  1607. }
  1608. ifp = XFS_IFORK_PTR(ip, whichfork);
  1609. if (rec_diff > 0) {
  1610. /*
  1611. * If there wasn't any memory allocated before, just
  1612. * allocate it now and get out.
  1613. */
  1614. if (ifp->if_broot_bytes == 0) {
  1615. new_size = (size_t)XFS_BMAP_BROOT_SPACE_CALC(rec_diff);
  1616. ifp->if_broot = kmem_alloc(new_size, KM_SLEEP | KM_NOFS);
  1617. ifp->if_broot_bytes = (int)new_size;
  1618. return;
  1619. }
  1620. /*
  1621. * If there is already an existing if_broot, then we need
  1622. * to realloc() it and shift the pointers to their new
  1623. * location. The records don't change location because
  1624. * they are kept butted up against the btree block header.
  1625. */
  1626. cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0);
  1627. new_max = cur_max + rec_diff;
  1628. new_size = (size_t)XFS_BMAP_BROOT_SPACE_CALC(new_max);
  1629. ifp->if_broot = kmem_realloc(ifp->if_broot, new_size,
  1630. (size_t)XFS_BMAP_BROOT_SPACE_CALC(cur_max), /* old size */
  1631. KM_SLEEP | KM_NOFS);
  1632. op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
  1633. ifp->if_broot_bytes);
  1634. np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
  1635. (int)new_size);
  1636. ifp->if_broot_bytes = (int)new_size;
  1637. ASSERT(ifp->if_broot_bytes <=
  1638. XFS_IFORK_SIZE(ip, whichfork) + XFS_BROOT_SIZE_ADJ);
  1639. memmove(np, op, cur_max * (uint)sizeof(xfs_dfsbno_t));
  1640. return;
  1641. }
  1642. /*
  1643. * rec_diff is less than 0. In this case, we are shrinking the
  1644. * if_broot buffer. It must already exist. If we go to zero
  1645. * records, just get rid of the root and clear the status bit.
  1646. */
  1647. ASSERT((ifp->if_broot != NULL) && (ifp->if_broot_bytes > 0));
  1648. cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0);
  1649. new_max = cur_max + rec_diff;
  1650. ASSERT(new_max >= 0);
  1651. if (new_max > 0)
  1652. new_size = (size_t)XFS_BMAP_BROOT_SPACE_CALC(new_max);
  1653. else
  1654. new_size = 0;
  1655. if (new_size > 0) {
  1656. new_broot = kmem_alloc(new_size, KM_SLEEP | KM_NOFS);
  1657. /*
  1658. * First copy over the btree block header.
  1659. */
  1660. memcpy(new_broot, ifp->if_broot, XFS_BTREE_LBLOCK_LEN);
  1661. } else {
  1662. new_broot = NULL;
  1663. ifp->if_flags &= ~XFS_IFBROOT;
  1664. }
  1665. /*
  1666. * Only copy the records and pointers if there are any.
  1667. */
  1668. if (new_max > 0) {
  1669. /*
  1670. * First copy the records.
  1671. */
  1672. op = (char *)XFS_BMBT_REC_ADDR(mp, ifp->if_broot, 1);
  1673. np = (char *)XFS_BMBT_REC_ADDR(mp, new_broot, 1);
  1674. memcpy(np, op, new_max * (uint)sizeof(xfs_bmbt_rec_t));
  1675. /*
  1676. * Then copy the pointers.
  1677. */
  1678. op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
  1679. ifp->if_broot_bytes);
  1680. np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, new_broot, 1,
  1681. (int)new_size);
  1682. memcpy(np, op, new_max * (uint)sizeof(xfs_dfsbno_t));
  1683. }
  1684. kmem_free(ifp->if_broot);
  1685. ifp->if_broot = new_broot;
  1686. ifp->if_broot_bytes = (int)new_size;
  1687. ASSERT(ifp->if_broot_bytes <=
  1688. XFS_IFORK_SIZE(ip, whichfork) + XFS_BROOT_SIZE_ADJ);
  1689. return;
  1690. }
  1691. /*
  1692. * This is called when the amount of space needed for if_data
  1693. * is increased or decreased. The change in size is indicated by
  1694. * the number of bytes that need to be added or deleted in the
  1695. * byte_diff parameter.
  1696. *
  1697. * If the amount of space needed has decreased below the size of the
  1698. * inline buffer, then switch to using the inline buffer. Otherwise,
  1699. * use kmem_realloc() or kmem_alloc() to adjust the size of the buffer
  1700. * to what is needed.
  1701. *
  1702. * ip -- the inode whose if_data area is changing
  1703. * byte_diff -- the change in the number of bytes, positive or negative,
  1704. * requested for the if_data array.
  1705. */
  1706. void
  1707. xfs_idata_realloc(
  1708. xfs_inode_t *ip,
  1709. int byte_diff,
  1710. int whichfork)
  1711. {
  1712. xfs_ifork_t *ifp;
  1713. int new_size;
  1714. int real_size;
  1715. if (byte_diff == 0) {
  1716. return;
  1717. }
  1718. ifp = XFS_IFORK_PTR(ip, whichfork);
  1719. new_size = (int)ifp->if_bytes + byte_diff;
  1720. ASSERT(new_size >= 0);
  1721. if (new_size == 0) {
  1722. if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) {
  1723. kmem_free(ifp->if_u1.if_data);
  1724. }
  1725. ifp->if_u1.if_data = NULL;
  1726. real_size = 0;
  1727. } else if (new_size <= sizeof(ifp->if_u2.if_inline_data)) {
  1728. /*
  1729. * If the valid extents/data can fit in if_inline_ext/data,
  1730. * copy them from the malloc'd vector and free it.
  1731. */
  1732. if (ifp->if_u1.if_data == NULL) {
  1733. ifp->if_u1.if_data = ifp->if_u2.if_inline_data;
  1734. } else if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) {
  1735. ASSERT(ifp->if_real_bytes != 0);
  1736. memcpy(ifp->if_u2.if_inline_data, ifp->if_u1.if_data,
  1737. new_size);
  1738. kmem_free(ifp->if_u1.if_data);
  1739. ifp->if_u1.if_data = ifp->if_u2.if_inline_data;
  1740. }
  1741. real_size = 0;
  1742. } else {
  1743. /*
  1744. * Stuck with malloc/realloc.
  1745. * For inline data, the underlying buffer must be
  1746. * a multiple of 4 bytes in size so that it can be
  1747. * logged and stay on word boundaries. We enforce
  1748. * that here.
  1749. */
  1750. real_size = roundup(new_size, 4);
  1751. if (ifp->if_u1.if_data == NULL) {
  1752. ASSERT(ifp->if_real_bytes == 0);
  1753. ifp->if_u1.if_data = kmem_alloc(real_size,
  1754. KM_SLEEP | KM_NOFS);
  1755. } else if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) {
  1756. /*
  1757. * Only do the realloc if the underlying size
  1758. * is really changing.
  1759. */
  1760. if (ifp->if_real_bytes != real_size) {
  1761. ifp->if_u1.if_data =
  1762. kmem_realloc(ifp->if_u1.if_data,
  1763. real_size,
  1764. ifp->if_real_bytes,
  1765. KM_SLEEP | KM_NOFS);
  1766. }
  1767. } else {
  1768. ASSERT(ifp->if_real_bytes == 0);
  1769. ifp->if_u1.if_data = kmem_alloc(real_size,
  1770. KM_SLEEP | KM_NOFS);
  1771. memcpy(ifp->if_u1.if_data, ifp->if_u2.if_inline_data,
  1772. ifp->if_bytes);
  1773. }
  1774. }
  1775. ifp->if_real_bytes = real_size;
  1776. ifp->if_bytes = new_size;
  1777. ASSERT(ifp->if_bytes <= XFS_IFORK_SIZE(ip, whichfork));
  1778. }
  1779. void
  1780. xfs_idestroy_fork(
  1781. xfs_inode_t *ip,
  1782. int whichfork)
  1783. {
  1784. xfs_ifork_t *ifp;
  1785. ifp = XFS_IFORK_PTR(ip, whichfork);
  1786. if (ifp->if_broot != NULL) {
  1787. kmem_free(ifp->if_broot);
  1788. ifp->if_broot = NULL;
  1789. }
  1790. /*
  1791. * If the format is local, then we can't have an extents
  1792. * array so just look for an inline data array. If we're
  1793. * not local then we may or may not have an extents list,
  1794. * so check and free it up if we do.
  1795. */
  1796. if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
  1797. if ((ifp->if_u1.if_data != ifp->if_u2.if_inline_data) &&
  1798. (ifp->if_u1.if_data != NULL)) {
  1799. ASSERT(ifp->if_real_bytes != 0);
  1800. kmem_free(ifp->if_u1.if_data);
  1801. ifp->if_u1.if_data = NULL;
  1802. ifp->if_real_bytes = 0;
  1803. }
  1804. } else if ((ifp->if_flags & XFS_IFEXTENTS) &&
  1805. ((ifp->if_flags & XFS_IFEXTIREC) ||
  1806. ((ifp->if_u1.if_extents != NULL) &&
  1807. (ifp->if_u1.if_extents != ifp->if_u2.if_inline_ext)))) {
  1808. ASSERT(ifp->if_real_bytes != 0);
  1809. xfs_iext_destroy(ifp);
  1810. }
  1811. ASSERT(ifp->if_u1.if_extents == NULL ||
  1812. ifp->if_u1.if_extents == ifp->if_u2.if_inline_ext);
  1813. ASSERT(ifp->if_real_bytes == 0);
  1814. if (whichfork == XFS_ATTR_FORK) {
  1815. kmem_zone_free(xfs_ifork_zone, ip->i_afp);
  1816. ip->i_afp = NULL;
  1817. }
  1818. }
  1819. /*
  1820. * This is called to unpin an inode. The caller must have the inode locked
  1821. * in at least shared mode so that the buffer cannot be subsequently pinned
  1822. * once someone is waiting for it to be unpinned.
  1823. */
  1824. static void
  1825. xfs_iunpin(
  1826. struct xfs_inode *ip)
  1827. {
  1828. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
  1829. trace_xfs_inode_unpin_nowait(ip, _RET_IP_);
  1830. /* Give the log a push to start the unpinning I/O */
  1831. xfs_log_force_lsn(ip->i_mount, ip->i_itemp->ili_last_lsn, 0);
  1832. }
  1833. static void
  1834. __xfs_iunpin_wait(
  1835. struct xfs_inode *ip)
  1836. {
  1837. wait_queue_head_t *wq = bit_waitqueue(&ip->i_flags, __XFS_IPINNED_BIT);
  1838. DEFINE_WAIT_BIT(wait, &ip->i_flags, __XFS_IPINNED_BIT);
  1839. xfs_iunpin(ip);
  1840. do {
  1841. prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
  1842. if (xfs_ipincount(ip))
  1843. io_schedule();
  1844. } while (xfs_ipincount(ip));
  1845. finish_wait(wq, &wait.wait);
  1846. }
  1847. void
  1848. xfs_iunpin_wait(
  1849. struct xfs_inode *ip)
  1850. {
  1851. if (xfs_ipincount(ip))
  1852. __xfs_iunpin_wait(ip);
  1853. }
  1854. /*
  1855. * xfs_iextents_copy()
  1856. *
  1857. * This is called to copy the REAL extents (as opposed to the delayed
  1858. * allocation extents) from the inode into the given buffer. It
  1859. * returns the number of bytes copied into the buffer.
  1860. *
  1861. * If there are no delayed allocation extents, then we can just
  1862. * memcpy() the extents into the buffer. Otherwise, we need to
  1863. * examine each extent in turn and skip those which are delayed.
  1864. */
  1865. int
  1866. xfs_iextents_copy(
  1867. xfs_inode_t *ip,
  1868. xfs_bmbt_rec_t *dp,
  1869. int whichfork)
  1870. {
  1871. int copied;
  1872. int i;
  1873. xfs_ifork_t *ifp;
  1874. int nrecs;
  1875. xfs_fsblock_t start_block;
  1876. ifp = XFS_IFORK_PTR(ip, whichfork);
  1877. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
  1878. ASSERT(ifp->if_bytes > 0);
  1879. nrecs = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  1880. XFS_BMAP_TRACE_EXLIST(ip, nrecs, whichfork);
  1881. ASSERT(nrecs > 0);
  1882. /*
  1883. * There are some delayed allocation extents in the
  1884. * inode, so copy the extents one at a time and skip
  1885. * the delayed ones. There must be at least one
  1886. * non-delayed extent.
  1887. */
  1888. copied = 0;
  1889. for (i = 0; i < nrecs; i++) {
  1890. xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, i);
  1891. start_block = xfs_bmbt_get_startblock(ep);
  1892. if (isnullstartblock(start_block)) {
  1893. /*
  1894. * It's a delayed allocation extent, so skip it.
  1895. */
  1896. continue;
  1897. }
  1898. /* Translate to on disk format */
  1899. put_unaligned(cpu_to_be64(ep->l0), &dp->l0);
  1900. put_unaligned(cpu_to_be64(ep->l1), &dp->l1);
  1901. dp++;
  1902. copied++;
  1903. }
  1904. ASSERT(copied != 0);
  1905. xfs_validate_extents(ifp, copied, XFS_EXTFMT_INODE(ip));
  1906. return (copied * (uint)sizeof(xfs_bmbt_rec_t));
  1907. }
  1908. /*
  1909. * Each of the following cases stores data into the same region
  1910. * of the on-disk inode, so only one of them can be valid at
  1911. * any given time. While it is possible to have conflicting formats
  1912. * and log flags, e.g. having XFS_ILOG_?DATA set when the fork is
  1913. * in EXTENTS format, this can only happen when the fork has
  1914. * changed formats after being modified but before being flushed.
  1915. * In these cases, the format always takes precedence, because the
  1916. * format indicates the current state of the fork.
  1917. */
  1918. /*ARGSUSED*/
  1919. STATIC void
  1920. xfs_iflush_fork(
  1921. xfs_inode_t *ip,
  1922. xfs_dinode_t *dip,
  1923. xfs_inode_log_item_t *iip,
  1924. int whichfork,
  1925. xfs_buf_t *bp)
  1926. {
  1927. char *cp;
  1928. xfs_ifork_t *ifp;
  1929. xfs_mount_t *mp;
  1930. #ifdef XFS_TRANS_DEBUG
  1931. int first;
  1932. #endif
  1933. static const short brootflag[2] =
  1934. { XFS_ILOG_DBROOT, XFS_ILOG_ABROOT };
  1935. static const short dataflag[2] =
  1936. { XFS_ILOG_DDATA, XFS_ILOG_ADATA };
  1937. static const short extflag[2] =
  1938. { XFS_ILOG_DEXT, XFS_ILOG_AEXT };
  1939. if (!iip)
  1940. return;
  1941. ifp = XFS_IFORK_PTR(ip, whichfork);
  1942. /*
  1943. * This can happen if we gave up in iformat in an error path,
  1944. * for the attribute fork.
  1945. */
  1946. if (!ifp) {
  1947. ASSERT(whichfork == XFS_ATTR_FORK);
  1948. return;
  1949. }
  1950. cp = XFS_DFORK_PTR(dip, whichfork);
  1951. mp = ip->i_mount;
  1952. switch (XFS_IFORK_FORMAT(ip, whichfork)) {
  1953. case XFS_DINODE_FMT_LOCAL:
  1954. if ((iip->ili_fields & dataflag[whichfork]) &&
  1955. (ifp->if_bytes > 0)) {
  1956. ASSERT(ifp->if_u1.if_data != NULL);
  1957. ASSERT(ifp->if_bytes <= XFS_IFORK_SIZE(ip, whichfork));
  1958. memcpy(cp, ifp->if_u1.if_data, ifp->if_bytes);
  1959. }
  1960. break;
  1961. case XFS_DINODE_FMT_EXTENTS:
  1962. ASSERT((ifp->if_flags & XFS_IFEXTENTS) ||
  1963. !(iip->ili_fields & extflag[whichfork]));
  1964. if ((iip->ili_fields & extflag[whichfork]) &&
  1965. (ifp->if_bytes > 0)) {
  1966. ASSERT(xfs_iext_get_ext(ifp, 0));
  1967. ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) > 0);
  1968. (void)xfs_iextents_copy(ip, (xfs_bmbt_rec_t *)cp,
  1969. whichfork);
  1970. }
  1971. break;
  1972. case XFS_DINODE_FMT_BTREE:
  1973. if ((iip->ili_fields & brootflag[whichfork]) &&
  1974. (ifp->if_broot_bytes > 0)) {
  1975. ASSERT(ifp->if_broot != NULL);
  1976. ASSERT(ifp->if_broot_bytes <=
  1977. (XFS_IFORK_SIZE(ip, whichfork) +
  1978. XFS_BROOT_SIZE_ADJ));
  1979. xfs_bmbt_to_bmdr(mp, ifp->if_broot, ifp->if_broot_bytes,
  1980. (xfs_bmdr_block_t *)cp,
  1981. XFS_DFORK_SIZE(dip, mp, whichfork));
  1982. }
  1983. break;
  1984. case XFS_DINODE_FMT_DEV:
  1985. if (iip->ili_fields & XFS_ILOG_DEV) {
  1986. ASSERT(whichfork == XFS_DATA_FORK);
  1987. xfs_dinode_put_rdev(dip, ip->i_df.if_u2.if_rdev);
  1988. }
  1989. break;
  1990. case XFS_DINODE_FMT_UUID:
  1991. if (iip->ili_fields & XFS_ILOG_UUID) {
  1992. ASSERT(whichfork == XFS_DATA_FORK);
  1993. memcpy(XFS_DFORK_DPTR(dip),
  1994. &ip->i_df.if_u2.if_uuid,
  1995. sizeof(uuid_t));
  1996. }
  1997. break;
  1998. default:
  1999. ASSERT(0);
  2000. break;
  2001. }
  2002. }
  2003. STATIC int
  2004. xfs_iflush_cluster(
  2005. xfs_inode_t *ip,
  2006. xfs_buf_t *bp)
  2007. {
  2008. xfs_mount_t *mp = ip->i_mount;
  2009. struct xfs_perag *pag;
  2010. unsigned long first_index, mask;
  2011. unsigned long inodes_per_cluster;
  2012. int ilist_size;
  2013. xfs_inode_t **ilist;
  2014. xfs_inode_t *iq;
  2015. int nr_found;
  2016. int clcount = 0;
  2017. int bufwasdelwri;
  2018. int i;
  2019. pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
  2020. inodes_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_inodelog;
  2021. ilist_size = inodes_per_cluster * sizeof(xfs_inode_t *);
  2022. ilist = kmem_alloc(ilist_size, KM_MAYFAIL|KM_NOFS);
  2023. if (!ilist)
  2024. goto out_put;
  2025. mask = ~(((XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_inodelog)) - 1);
  2026. first_index = XFS_INO_TO_AGINO(mp, ip->i_ino) & mask;
  2027. rcu_read_lock();
  2028. /* really need a gang lookup range call here */
  2029. nr_found = radix_tree_gang_lookup(&pag->pag_ici_root, (void**)ilist,
  2030. first_index, inodes_per_cluster);
  2031. if (nr_found == 0)
  2032. goto out_free;
  2033. for (i = 0; i < nr_found; i++) {
  2034. iq = ilist[i];
  2035. if (iq == ip)
  2036. continue;
  2037. /*
  2038. * because this is an RCU protected lookup, we could find a
  2039. * recently freed or even reallocated inode during the lookup.
  2040. * We need to check under the i_flags_lock for a valid inode
  2041. * here. Skip it if it is not valid or the wrong inode.
  2042. */
  2043. spin_lock(&ip->i_flags_lock);
  2044. if (!ip->i_ino ||
  2045. (XFS_INO_TO_AGINO(mp, iq->i_ino) & mask) != first_index) {
  2046. spin_unlock(&ip->i_flags_lock);
  2047. continue;
  2048. }
  2049. spin_unlock(&ip->i_flags_lock);
  2050. /*
  2051. * Do an un-protected check to see if the inode is dirty and
  2052. * is a candidate for flushing. These checks will be repeated
  2053. * later after the appropriate locks are acquired.
  2054. */
  2055. if (xfs_inode_clean(iq) && xfs_ipincount(iq) == 0)
  2056. continue;
  2057. /*
  2058. * Try to get locks. If any are unavailable or it is pinned,
  2059. * then this inode cannot be flushed and is skipped.
  2060. */
  2061. if (!xfs_ilock_nowait(iq, XFS_ILOCK_SHARED))
  2062. continue;
  2063. if (!xfs_iflock_nowait(iq)) {
  2064. xfs_iunlock(iq, XFS_ILOCK_SHARED);
  2065. continue;
  2066. }
  2067. if (xfs_ipincount(iq)) {
  2068. xfs_ifunlock(iq);
  2069. xfs_iunlock(iq, XFS_ILOCK_SHARED);
  2070. continue;
  2071. }
  2072. /*
  2073. * arriving here means that this inode can be flushed. First
  2074. * re-check that it's dirty before flushing.
  2075. */
  2076. if (!xfs_inode_clean(iq)) {
  2077. int error;
  2078. error = xfs_iflush_int(iq, bp);
  2079. if (error) {
  2080. xfs_iunlock(iq, XFS_ILOCK_SHARED);
  2081. goto cluster_corrupt_out;
  2082. }
  2083. clcount++;
  2084. } else {
  2085. xfs_ifunlock(iq);
  2086. }
  2087. xfs_iunlock(iq, XFS_ILOCK_SHARED);
  2088. }
  2089. if (clcount) {
  2090. XFS_STATS_INC(xs_icluster_flushcnt);
  2091. XFS_STATS_ADD(xs_icluster_flushinode, clcount);
  2092. }
  2093. out_free:
  2094. rcu_read_unlock();
  2095. kmem_free(ilist);
  2096. out_put:
  2097. xfs_perag_put(pag);
  2098. return 0;
  2099. cluster_corrupt_out:
  2100. /*
  2101. * Corruption detected in the clustering loop. Invalidate the
  2102. * inode buffer and shut down the filesystem.
  2103. */
  2104. rcu_read_unlock();
  2105. /*
  2106. * Clean up the buffer. If it was delwri, just release it --
  2107. * brelse can handle it with no problems. If not, shut down the
  2108. * filesystem before releasing the buffer.
  2109. */
  2110. bufwasdelwri = (bp->b_flags & _XBF_DELWRI_Q);
  2111. if (bufwasdelwri)
  2112. xfs_buf_relse(bp);
  2113. xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
  2114. if (!bufwasdelwri) {
  2115. /*
  2116. * Just like incore_relse: if we have b_iodone functions,
  2117. * mark the buffer as an error and call them. Otherwise
  2118. * mark it as stale and brelse.
  2119. */
  2120. if (bp->b_iodone) {
  2121. XFS_BUF_UNDONE(bp);
  2122. xfs_buf_stale(bp);
  2123. xfs_buf_ioerror(bp, EIO);
  2124. xfs_buf_ioend(bp, 0);
  2125. } else {
  2126. xfs_buf_stale(bp);
  2127. xfs_buf_relse(bp);
  2128. }
  2129. }
  2130. /*
  2131. * Unlocks the flush lock
  2132. */
  2133. xfs_iflush_abort(iq, false);
  2134. kmem_free(ilist);
  2135. xfs_perag_put(pag);
  2136. return XFS_ERROR(EFSCORRUPTED);
  2137. }
  2138. /*
  2139. * Flush dirty inode metadata into the backing buffer.
  2140. *
  2141. * The caller must have the inode lock and the inode flush lock held. The
  2142. * inode lock will still be held upon return to the caller, and the inode
  2143. * flush lock will be released after the inode has reached the disk.
  2144. *
  2145. * The caller must write out the buffer returned in *bpp and release it.
  2146. */
  2147. int
  2148. xfs_iflush(
  2149. struct xfs_inode *ip,
  2150. struct xfs_buf **bpp)
  2151. {
  2152. struct xfs_mount *mp = ip->i_mount;
  2153. struct xfs_buf *bp;
  2154. struct xfs_dinode *dip;
  2155. int error;
  2156. XFS_STATS_INC(xs_iflush_count);
  2157. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
  2158. ASSERT(xfs_isiflocked(ip));
  2159. ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE ||
  2160. ip->i_d.di_nextents > XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK));
  2161. *bpp = NULL;
  2162. xfs_iunpin_wait(ip);
  2163. /*
  2164. * For stale inodes we cannot rely on the backing buffer remaining
  2165. * stale in cache for the remaining life of the stale inode and so
  2166. * xfs_imap_to_bp() below may give us a buffer that no longer contains
  2167. * inodes below. We have to check this after ensuring the inode is
  2168. * unpinned so that it is safe to reclaim the stale inode after the
  2169. * flush call.
  2170. */
  2171. if (xfs_iflags_test(ip, XFS_ISTALE)) {
  2172. xfs_ifunlock(ip);
  2173. return 0;
  2174. }
  2175. /*
  2176. * This may have been unpinned because the filesystem is shutting
  2177. * down forcibly. If that's the case we must not write this inode
  2178. * to disk, because the log record didn't make it to disk.
  2179. *
  2180. * We also have to remove the log item from the AIL in this case,
  2181. * as we wait for an empty AIL as part of the unmount process.
  2182. */
  2183. if (XFS_FORCED_SHUTDOWN(mp)) {
  2184. error = XFS_ERROR(EIO);
  2185. goto abort_out;
  2186. }
  2187. /*
  2188. * Get the buffer containing the on-disk inode.
  2189. */
  2190. error = xfs_imap_to_bp(mp, NULL, &ip->i_imap, &dip, &bp, XBF_TRYLOCK,
  2191. 0);
  2192. if (error || !bp) {
  2193. xfs_ifunlock(ip);
  2194. return error;
  2195. }
  2196. /*
  2197. * First flush out the inode that xfs_iflush was called with.
  2198. */
  2199. error = xfs_iflush_int(ip, bp);
  2200. if (error)
  2201. goto corrupt_out;
  2202. /*
  2203. * If the buffer is pinned then push on the log now so we won't
  2204. * get stuck waiting in the write for too long.
  2205. */
  2206. if (xfs_buf_ispinned(bp))
  2207. xfs_log_force(mp, 0);
  2208. /*
  2209. * inode clustering:
  2210. * see if other inodes can be gathered into this write
  2211. */
  2212. error = xfs_iflush_cluster(ip, bp);
  2213. if (error)
  2214. goto cluster_corrupt_out;
  2215. *bpp = bp;
  2216. return 0;
  2217. corrupt_out:
  2218. xfs_buf_relse(bp);
  2219. xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
  2220. cluster_corrupt_out:
  2221. error = XFS_ERROR(EFSCORRUPTED);
  2222. abort_out:
  2223. /*
  2224. * Unlocks the flush lock
  2225. */
  2226. xfs_iflush_abort(ip, false);
  2227. return error;
  2228. }
  2229. STATIC int
  2230. xfs_iflush_int(
  2231. xfs_inode_t *ip,
  2232. xfs_buf_t *bp)
  2233. {
  2234. xfs_inode_log_item_t *iip;
  2235. xfs_dinode_t *dip;
  2236. xfs_mount_t *mp;
  2237. #ifdef XFS_TRANS_DEBUG
  2238. int first;
  2239. #endif
  2240. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
  2241. ASSERT(xfs_isiflocked(ip));
  2242. ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE ||
  2243. ip->i_d.di_nextents > XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK));
  2244. iip = ip->i_itemp;
  2245. mp = ip->i_mount;
  2246. /* set *dip = inode's place in the buffer */
  2247. dip = (xfs_dinode_t *)xfs_buf_offset(bp, ip->i_imap.im_boffset);
  2248. if (XFS_TEST_ERROR(dip->di_magic != cpu_to_be16(XFS_DINODE_MAGIC),
  2249. mp, XFS_ERRTAG_IFLUSH_1, XFS_RANDOM_IFLUSH_1)) {
  2250. xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
  2251. "%s: Bad inode %Lu magic number 0x%x, ptr 0x%p",
  2252. __func__, ip->i_ino, be16_to_cpu(dip->di_magic), dip);
  2253. goto corrupt_out;
  2254. }
  2255. if (XFS_TEST_ERROR(ip->i_d.di_magic != XFS_DINODE_MAGIC,
  2256. mp, XFS_ERRTAG_IFLUSH_2, XFS_RANDOM_IFLUSH_2)) {
  2257. xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
  2258. "%s: Bad inode %Lu, ptr 0x%p, magic number 0x%x",
  2259. __func__, ip->i_ino, ip, ip->i_d.di_magic);
  2260. goto corrupt_out;
  2261. }
  2262. if (S_ISREG(ip->i_d.di_mode)) {
  2263. if (XFS_TEST_ERROR(
  2264. (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS) &&
  2265. (ip->i_d.di_format != XFS_DINODE_FMT_BTREE),
  2266. mp, XFS_ERRTAG_IFLUSH_3, XFS_RANDOM_IFLUSH_3)) {
  2267. xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
  2268. "%s: Bad regular inode %Lu, ptr 0x%p",
  2269. __func__, ip->i_ino, ip);
  2270. goto corrupt_out;
  2271. }
  2272. } else if (S_ISDIR(ip->i_d.di_mode)) {
  2273. if (XFS_TEST_ERROR(
  2274. (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS) &&
  2275. (ip->i_d.di_format != XFS_DINODE_FMT_BTREE) &&
  2276. (ip->i_d.di_format != XFS_DINODE_FMT_LOCAL),
  2277. mp, XFS_ERRTAG_IFLUSH_4, XFS_RANDOM_IFLUSH_4)) {
  2278. xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
  2279. "%s: Bad directory inode %Lu, ptr 0x%p",
  2280. __func__, ip->i_ino, ip);
  2281. goto corrupt_out;
  2282. }
  2283. }
  2284. if (XFS_TEST_ERROR(ip->i_d.di_nextents + ip->i_d.di_anextents >
  2285. ip->i_d.di_nblocks, mp, XFS_ERRTAG_IFLUSH_5,
  2286. XFS_RANDOM_IFLUSH_5)) {
  2287. xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
  2288. "%s: detected corrupt incore inode %Lu, "
  2289. "total extents = %d, nblocks = %Ld, ptr 0x%p",
  2290. __func__, ip->i_ino,
  2291. ip->i_d.di_nextents + ip->i_d.di_anextents,
  2292. ip->i_d.di_nblocks, ip);
  2293. goto corrupt_out;
  2294. }
  2295. if (XFS_TEST_ERROR(ip->i_d.di_forkoff > mp->m_sb.sb_inodesize,
  2296. mp, XFS_ERRTAG_IFLUSH_6, XFS_RANDOM_IFLUSH_6)) {
  2297. xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
  2298. "%s: bad inode %Lu, forkoff 0x%x, ptr 0x%p",
  2299. __func__, ip->i_ino, ip->i_d.di_forkoff, ip);
  2300. goto corrupt_out;
  2301. }
  2302. /*
  2303. * bump the flush iteration count, used to detect flushes which
  2304. * postdate a log record during recovery.
  2305. */
  2306. ip->i_d.di_flushiter++;
  2307. /*
  2308. * Copy the dirty parts of the inode into the on-disk
  2309. * inode. We always copy out the core of the inode,
  2310. * because if the inode is dirty at all the core must
  2311. * be.
  2312. */
  2313. xfs_dinode_to_disk(dip, &ip->i_d);
  2314. /* Wrap, we never let the log put out DI_MAX_FLUSH */
  2315. if (ip->i_d.di_flushiter == DI_MAX_FLUSH)
  2316. ip->i_d.di_flushiter = 0;
  2317. /*
  2318. * If this is really an old format inode and the superblock version
  2319. * has not been updated to support only new format inodes, then
  2320. * convert back to the old inode format. If the superblock version
  2321. * has been updated, then make the conversion permanent.
  2322. */
  2323. ASSERT(ip->i_d.di_version == 1 || xfs_sb_version_hasnlink(&mp->m_sb));
  2324. if (ip->i_d.di_version == 1) {
  2325. if (!xfs_sb_version_hasnlink(&mp->m_sb)) {
  2326. /*
  2327. * Convert it back.
  2328. */
  2329. ASSERT(ip->i_d.di_nlink <= XFS_MAXLINK_1);
  2330. dip->di_onlink = cpu_to_be16(ip->i_d.di_nlink);
  2331. } else {
  2332. /*
  2333. * The superblock version has already been bumped,
  2334. * so just make the conversion to the new inode
  2335. * format permanent.
  2336. */
  2337. ip->i_d.di_version = 2;
  2338. dip->di_version = 2;
  2339. ip->i_d.di_onlink = 0;
  2340. dip->di_onlink = 0;
  2341. memset(&(ip->i_d.di_pad[0]), 0, sizeof(ip->i_d.di_pad));
  2342. memset(&(dip->di_pad[0]), 0,
  2343. sizeof(dip->di_pad));
  2344. ASSERT(xfs_get_projid(ip) == 0);
  2345. }
  2346. }
  2347. xfs_iflush_fork(ip, dip, iip, XFS_DATA_FORK, bp);
  2348. if (XFS_IFORK_Q(ip))
  2349. xfs_iflush_fork(ip, dip, iip, XFS_ATTR_FORK, bp);
  2350. xfs_inobp_check(mp, bp);
  2351. /*
  2352. * We've recorded everything logged in the inode, so we'd like to clear
  2353. * the ili_fields bits so we don't log and flush things unnecessarily.
  2354. * However, we can't stop logging all this information until the data
  2355. * we've copied into the disk buffer is written to disk. If we did we
  2356. * might overwrite the copy of the inode in the log with all the data
  2357. * after re-logging only part of it, and in the face of a crash we
  2358. * wouldn't have all the data we need to recover.
  2359. *
  2360. * What we do is move the bits to the ili_last_fields field. When
  2361. * logging the inode, these bits are moved back to the ili_fields field.
  2362. * In the xfs_iflush_done() routine we clear ili_last_fields, since we
  2363. * know that the information those bits represent is permanently on
  2364. * disk. As long as the flush completes before the inode is logged
  2365. * again, then both ili_fields and ili_last_fields will be cleared.
  2366. *
  2367. * We can play with the ili_fields bits here, because the inode lock
  2368. * must be held exclusively in order to set bits there and the flush
  2369. * lock protects the ili_last_fields bits. Set ili_logged so the flush
  2370. * done routine can tell whether or not to look in the AIL. Also, store
  2371. * the current LSN of the inode so that we can tell whether the item has
  2372. * moved in the AIL from xfs_iflush_done(). In order to read the lsn we
  2373. * need the AIL lock, because it is a 64 bit value that cannot be read
  2374. * atomically.
  2375. */
  2376. if (iip != NULL && iip->ili_fields != 0) {
  2377. iip->ili_last_fields = iip->ili_fields;
  2378. iip->ili_fields = 0;
  2379. iip->ili_logged = 1;
  2380. xfs_trans_ail_copy_lsn(mp->m_ail, &iip->ili_flush_lsn,
  2381. &iip->ili_item.li_lsn);
  2382. /*
  2383. * Attach the function xfs_iflush_done to the inode's
  2384. * buffer. This will remove the inode from the AIL
  2385. * and unlock the inode's flush lock when the inode is
  2386. * completely written to disk.
  2387. */
  2388. xfs_buf_attach_iodone(bp, xfs_iflush_done, &iip->ili_item);
  2389. ASSERT(bp->b_fspriv != NULL);
  2390. ASSERT(bp->b_iodone != NULL);
  2391. } else {
  2392. /*
  2393. * We're flushing an inode which is not in the AIL and has
  2394. * not been logged. For this case we can immediately drop
  2395. * the inode flush lock because we can avoid the whole
  2396. * AIL state thing. It's OK to drop the flush lock now,
  2397. * because we've already locked the buffer and to do anything
  2398. * you really need both.
  2399. */
  2400. if (iip != NULL) {
  2401. ASSERT(iip->ili_logged == 0);
  2402. ASSERT(iip->ili_last_fields == 0);
  2403. ASSERT((iip->ili_item.li_flags & XFS_LI_IN_AIL) == 0);
  2404. }
  2405. xfs_ifunlock(ip);
  2406. }
  2407. return 0;
  2408. corrupt_out:
  2409. return XFS_ERROR(EFSCORRUPTED);
  2410. }
  2411. /*
  2412. * Return a pointer to the extent record at file index idx.
  2413. */
  2414. xfs_bmbt_rec_host_t *
  2415. xfs_iext_get_ext(
  2416. xfs_ifork_t *ifp, /* inode fork pointer */
  2417. xfs_extnum_t idx) /* index of target extent */
  2418. {
  2419. ASSERT(idx >= 0);
  2420. ASSERT(idx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t));
  2421. if ((ifp->if_flags & XFS_IFEXTIREC) && (idx == 0)) {
  2422. return ifp->if_u1.if_ext_irec->er_extbuf;
  2423. } else if (ifp->if_flags & XFS_IFEXTIREC) {
  2424. xfs_ext_irec_t *erp; /* irec pointer */
  2425. int erp_idx = 0; /* irec index */
  2426. xfs_extnum_t page_idx = idx; /* ext index in target list */
  2427. erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 0);
  2428. return &erp->er_extbuf[page_idx];
  2429. } else if (ifp->if_bytes) {
  2430. return &ifp->if_u1.if_extents[idx];
  2431. } else {
  2432. return NULL;
  2433. }
  2434. }
  2435. /*
  2436. * Insert new item(s) into the extent records for incore inode
  2437. * fork 'ifp'. 'count' new items are inserted at index 'idx'.
  2438. */
  2439. void
  2440. xfs_iext_insert(
  2441. xfs_inode_t *ip, /* incore inode pointer */
  2442. xfs_extnum_t idx, /* starting index of new items */
  2443. xfs_extnum_t count, /* number of inserted items */
  2444. xfs_bmbt_irec_t *new, /* items to insert */
  2445. int state) /* type of extent conversion */
  2446. {
  2447. xfs_ifork_t *ifp = (state & BMAP_ATTRFORK) ? ip->i_afp : &ip->i_df;
  2448. xfs_extnum_t i; /* extent record index */
  2449. trace_xfs_iext_insert(ip, idx, new, state, _RET_IP_);
  2450. ASSERT(ifp->if_flags & XFS_IFEXTENTS);
  2451. xfs_iext_add(ifp, idx, count);
  2452. for (i = idx; i < idx + count; i++, new++)
  2453. xfs_bmbt_set_all(xfs_iext_get_ext(ifp, i), new);
  2454. }
  2455. /*
  2456. * This is called when the amount of space required for incore file
  2457. * extents needs to be increased. The ext_diff parameter stores the
  2458. * number of new extents being added and the idx parameter contains
  2459. * the extent index where the new extents will be added. If the new
  2460. * extents are being appended, then we just need to (re)allocate and
  2461. * initialize the space. Otherwise, if the new extents are being
  2462. * inserted into the middle of the existing entries, a bit more work
  2463. * is required to make room for the new extents to be inserted. The
  2464. * caller is responsible for filling in the new extent entries upon
  2465. * return.
  2466. */
  2467. void
  2468. xfs_iext_add(
  2469. xfs_ifork_t *ifp, /* inode fork pointer */
  2470. xfs_extnum_t idx, /* index to begin adding exts */
  2471. int ext_diff) /* number of extents to add */
  2472. {
  2473. int byte_diff; /* new bytes being added */
  2474. int new_size; /* size of extents after adding */
  2475. xfs_extnum_t nextents; /* number of extents in file */
  2476. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  2477. ASSERT((idx >= 0) && (idx <= nextents));
  2478. byte_diff = ext_diff * sizeof(xfs_bmbt_rec_t);
  2479. new_size = ifp->if_bytes + byte_diff;
  2480. /*
  2481. * If the new number of extents (nextents + ext_diff)
  2482. * fits inside the inode, then continue to use the inline
  2483. * extent buffer.
  2484. */
  2485. if (nextents + ext_diff <= XFS_INLINE_EXTS) {
  2486. if (idx < nextents) {
  2487. memmove(&ifp->if_u2.if_inline_ext[idx + ext_diff],
  2488. &ifp->if_u2.if_inline_ext[idx],
  2489. (nextents - idx) * sizeof(xfs_bmbt_rec_t));
  2490. memset(&ifp->if_u2.if_inline_ext[idx], 0, byte_diff);
  2491. }
  2492. ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext;
  2493. ifp->if_real_bytes = 0;
  2494. }
  2495. /*
  2496. * Otherwise use a linear (direct) extent list.
  2497. * If the extents are currently inside the inode,
  2498. * xfs_iext_realloc_direct will switch us from
  2499. * inline to direct extent allocation mode.
  2500. */
  2501. else if (nextents + ext_diff <= XFS_LINEAR_EXTS) {
  2502. xfs_iext_realloc_direct(ifp, new_size);
  2503. if (idx < nextents) {
  2504. memmove(&ifp->if_u1.if_extents[idx + ext_diff],
  2505. &ifp->if_u1.if_extents[idx],
  2506. (nextents - idx) * sizeof(xfs_bmbt_rec_t));
  2507. memset(&ifp->if_u1.if_extents[idx], 0, byte_diff);
  2508. }
  2509. }
  2510. /* Indirection array */
  2511. else {
  2512. xfs_ext_irec_t *erp;
  2513. int erp_idx = 0;
  2514. int page_idx = idx;
  2515. ASSERT(nextents + ext_diff > XFS_LINEAR_EXTS);
  2516. if (ifp->if_flags & XFS_IFEXTIREC) {
  2517. erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 1);
  2518. } else {
  2519. xfs_iext_irec_init(ifp);
  2520. ASSERT(ifp->if_flags & XFS_IFEXTIREC);
  2521. erp = ifp->if_u1.if_ext_irec;
  2522. }
  2523. /* Extents fit in target extent page */
  2524. if (erp && erp->er_extcount + ext_diff <= XFS_LINEAR_EXTS) {
  2525. if (page_idx < erp->er_extcount) {
  2526. memmove(&erp->er_extbuf[page_idx + ext_diff],
  2527. &erp->er_extbuf[page_idx],
  2528. (erp->er_extcount - page_idx) *
  2529. sizeof(xfs_bmbt_rec_t));
  2530. memset(&erp->er_extbuf[page_idx], 0, byte_diff);
  2531. }
  2532. erp->er_extcount += ext_diff;
  2533. xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff);
  2534. }
  2535. /* Insert a new extent page */
  2536. else if (erp) {
  2537. xfs_iext_add_indirect_multi(ifp,
  2538. erp_idx, page_idx, ext_diff);
  2539. }
  2540. /*
  2541. * If extent(s) are being appended to the last page in
  2542. * the indirection array and the new extent(s) don't fit
  2543. * in the page, then erp is NULL and erp_idx is set to
  2544. * the next index needed in the indirection array.
  2545. */
  2546. else {
  2547. int count = ext_diff;
  2548. while (count) {
  2549. erp = xfs_iext_irec_new(ifp, erp_idx);
  2550. erp->er_extcount = count;
  2551. count -= MIN(count, (int)XFS_LINEAR_EXTS);
  2552. if (count) {
  2553. erp_idx++;
  2554. }
  2555. }
  2556. }
  2557. }
  2558. ifp->if_bytes = new_size;
  2559. }
  2560. /*
  2561. * This is called when incore extents are being added to the indirection
  2562. * array and the new extents do not fit in the target extent list. The
  2563. * erp_idx parameter contains the irec index for the target extent list
  2564. * in the indirection array, and the idx parameter contains the extent
  2565. * index within the list. The number of extents being added is stored
  2566. * in the count parameter.
  2567. *
  2568. * |-------| |-------|
  2569. * | | | | idx - number of extents before idx
  2570. * | idx | | count |
  2571. * | | | | count - number of extents being inserted at idx
  2572. * |-------| |-------|
  2573. * | count | | nex2 | nex2 - number of extents after idx + count
  2574. * |-------| |-------|
  2575. */
  2576. void
  2577. xfs_iext_add_indirect_multi(
  2578. xfs_ifork_t *ifp, /* inode fork pointer */
  2579. int erp_idx, /* target extent irec index */
  2580. xfs_extnum_t idx, /* index within target list */
  2581. int count) /* new extents being added */
  2582. {
  2583. int byte_diff; /* new bytes being added */
  2584. xfs_ext_irec_t *erp; /* pointer to irec entry */
  2585. xfs_extnum_t ext_diff; /* number of extents to add */
  2586. xfs_extnum_t ext_cnt; /* new extents still needed */
  2587. xfs_extnum_t nex2; /* extents after idx + count */
  2588. xfs_bmbt_rec_t *nex2_ep = NULL; /* temp list for nex2 extents */
  2589. int nlists; /* number of irec's (lists) */
  2590. ASSERT(ifp->if_flags & XFS_IFEXTIREC);
  2591. erp = &ifp->if_u1.if_ext_irec[erp_idx];
  2592. nex2 = erp->er_extcount - idx;
  2593. nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
  2594. /*
  2595. * Save second part of target extent list
  2596. * (all extents past */
  2597. if (nex2) {
  2598. byte_diff = nex2 * sizeof(xfs_bmbt_rec_t);
  2599. nex2_ep = (xfs_bmbt_rec_t *) kmem_alloc(byte_diff, KM_NOFS);
  2600. memmove(nex2_ep, &erp->er_extbuf[idx], byte_diff);
  2601. erp->er_extcount -= nex2;
  2602. xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, -nex2);
  2603. memset(&erp->er_extbuf[idx], 0, byte_diff);
  2604. }
  2605. /*
  2606. * Add the new extents to the end of the target
  2607. * list, then allocate new irec record(s) and
  2608. * extent buffer(s) as needed to store the rest
  2609. * of the new extents.
  2610. */
  2611. ext_cnt = count;
  2612. ext_diff = MIN(ext_cnt, (int)XFS_LINEAR_EXTS - erp->er_extcount);
  2613. if (ext_diff) {
  2614. erp->er_extcount += ext_diff;
  2615. xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff);
  2616. ext_cnt -= ext_diff;
  2617. }
  2618. while (ext_cnt) {
  2619. erp_idx++;
  2620. erp = xfs_iext_irec_new(ifp, erp_idx);
  2621. ext_diff = MIN(ext_cnt, (int)XFS_LINEAR_EXTS);
  2622. erp->er_extcount = ext_diff;
  2623. xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff);
  2624. ext_cnt -= ext_diff;
  2625. }
  2626. /* Add nex2 extents back to indirection array */
  2627. if (nex2) {
  2628. xfs_extnum_t ext_avail;
  2629. int i;
  2630. byte_diff = nex2 * sizeof(xfs_bmbt_rec_t);
  2631. ext_avail = XFS_LINEAR_EXTS - erp->er_extcount;
  2632. i = 0;
  2633. /*
  2634. * If nex2 extents fit in the current page, append
  2635. * nex2_ep after the new extents.
  2636. */
  2637. if (nex2 <= ext_avail) {
  2638. i = erp->er_extcount;
  2639. }
  2640. /*
  2641. * Otherwise, check if space is available in the
  2642. * next page.
  2643. */
  2644. else if ((erp_idx < nlists - 1) &&
  2645. (nex2 <= (ext_avail = XFS_LINEAR_EXTS -
  2646. ifp->if_u1.if_ext_irec[erp_idx+1].er_extcount))) {
  2647. erp_idx++;
  2648. erp++;
  2649. /* Create a hole for nex2 extents */
  2650. memmove(&erp->er_extbuf[nex2], erp->er_extbuf,
  2651. erp->er_extcount * sizeof(xfs_bmbt_rec_t));
  2652. }
  2653. /*
  2654. * Final choice, create a new extent page for
  2655. * nex2 extents.
  2656. */
  2657. else {
  2658. erp_idx++;
  2659. erp = xfs_iext_irec_new(ifp, erp_idx);
  2660. }
  2661. memmove(&erp->er_extbuf[i], nex2_ep, byte_diff);
  2662. kmem_free(nex2_ep);
  2663. erp->er_extcount += nex2;
  2664. xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, nex2);
  2665. }
  2666. }
  2667. /*
  2668. * This is called when the amount of space required for incore file
  2669. * extents needs to be decreased. The ext_diff parameter stores the
  2670. * number of extents to be removed and the idx parameter contains
  2671. * the extent index where the extents will be removed from.
  2672. *
  2673. * If the amount of space needed has decreased below the linear
  2674. * limit, XFS_IEXT_BUFSZ, then switch to using the contiguous
  2675. * extent array. Otherwise, use kmem_realloc() to adjust the
  2676. * size to what is needed.
  2677. */
  2678. void
  2679. xfs_iext_remove(
  2680. xfs_inode_t *ip, /* incore inode pointer */
  2681. xfs_extnum_t idx, /* index to begin removing exts */
  2682. int ext_diff, /* number of extents to remove */
  2683. int state) /* type of extent conversion */
  2684. {
  2685. xfs_ifork_t *ifp = (state & BMAP_ATTRFORK) ? ip->i_afp : &ip->i_df;
  2686. xfs_extnum_t nextents; /* number of extents in file */
  2687. int new_size; /* size of extents after removal */
  2688. trace_xfs_iext_remove(ip, idx, state, _RET_IP_);
  2689. ASSERT(ext_diff > 0);
  2690. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  2691. new_size = (nextents - ext_diff) * sizeof(xfs_bmbt_rec_t);
  2692. if (new_size == 0) {
  2693. xfs_iext_destroy(ifp);
  2694. } else if (ifp->if_flags & XFS_IFEXTIREC) {
  2695. xfs_iext_remove_indirect(ifp, idx, ext_diff);
  2696. } else if (ifp->if_real_bytes) {
  2697. xfs_iext_remove_direct(ifp, idx, ext_diff);
  2698. } else {
  2699. xfs_iext_remove_inline(ifp, idx, ext_diff);
  2700. }
  2701. ifp->if_bytes = new_size;
  2702. }
  2703. /*
  2704. * This removes ext_diff extents from the inline buffer, beginning
  2705. * at extent index idx.
  2706. */
  2707. void
  2708. xfs_iext_remove_inline(
  2709. xfs_ifork_t *ifp, /* inode fork pointer */
  2710. xfs_extnum_t idx, /* index to begin removing exts */
  2711. int ext_diff) /* number of extents to remove */
  2712. {
  2713. int nextents; /* number of extents in file */
  2714. ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
  2715. ASSERT(idx < XFS_INLINE_EXTS);
  2716. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  2717. ASSERT(((nextents - ext_diff) > 0) &&
  2718. (nextents - ext_diff) < XFS_INLINE_EXTS);
  2719. if (idx + ext_diff < nextents) {
  2720. memmove(&ifp->if_u2.if_inline_ext[idx],
  2721. &ifp->if_u2.if_inline_ext[idx + ext_diff],
  2722. (nextents - (idx + ext_diff)) *
  2723. sizeof(xfs_bmbt_rec_t));
  2724. memset(&ifp->if_u2.if_inline_ext[nextents - ext_diff],
  2725. 0, ext_diff * sizeof(xfs_bmbt_rec_t));
  2726. } else {
  2727. memset(&ifp->if_u2.if_inline_ext[idx], 0,
  2728. ext_diff * sizeof(xfs_bmbt_rec_t));
  2729. }
  2730. }
  2731. /*
  2732. * This removes ext_diff extents from a linear (direct) extent list,
  2733. * beginning at extent index idx. If the extents are being removed
  2734. * from the end of the list (ie. truncate) then we just need to re-
  2735. * allocate the list to remove the extra space. Otherwise, if the
  2736. * extents are being removed from the middle of the existing extent
  2737. * entries, then we first need to move the extent records beginning
  2738. * at idx + ext_diff up in the list to overwrite the records being
  2739. * removed, then remove the extra space via kmem_realloc.
  2740. */
  2741. void
  2742. xfs_iext_remove_direct(
  2743. xfs_ifork_t *ifp, /* inode fork pointer */
  2744. xfs_extnum_t idx, /* index to begin removing exts */
  2745. int ext_diff) /* number of extents to remove */
  2746. {
  2747. xfs_extnum_t nextents; /* number of extents in file */
  2748. int new_size; /* size of extents after removal */
  2749. ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
  2750. new_size = ifp->if_bytes -
  2751. (ext_diff * sizeof(xfs_bmbt_rec_t));
  2752. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  2753. if (new_size == 0) {
  2754. xfs_iext_destroy(ifp);
  2755. return;
  2756. }
  2757. /* Move extents up in the list (if needed) */
  2758. if (idx + ext_diff < nextents) {
  2759. memmove(&ifp->if_u1.if_extents[idx],
  2760. &ifp->if_u1.if_extents[idx + ext_diff],
  2761. (nextents - (idx + ext_diff)) *
  2762. sizeof(xfs_bmbt_rec_t));
  2763. }
  2764. memset(&ifp->if_u1.if_extents[nextents - ext_diff],
  2765. 0, ext_diff * sizeof(xfs_bmbt_rec_t));
  2766. /*
  2767. * Reallocate the direct extent list. If the extents
  2768. * will fit inside the inode then xfs_iext_realloc_direct
  2769. * will switch from direct to inline extent allocation
  2770. * mode for us.
  2771. */
  2772. xfs_iext_realloc_direct(ifp, new_size);
  2773. ifp->if_bytes = new_size;
  2774. }
  2775. /*
  2776. * This is called when incore extents are being removed from the
  2777. * indirection array and the extents being removed span multiple extent
  2778. * buffers. The idx parameter contains the file extent index where we
  2779. * want to begin removing extents, and the count parameter contains
  2780. * how many extents need to be removed.
  2781. *
  2782. * |-------| |-------|
  2783. * | nex1 | | | nex1 - number of extents before idx
  2784. * |-------| | count |
  2785. * | | | | count - number of extents being removed at idx
  2786. * | count | |-------|
  2787. * | | | nex2 | nex2 - number of extents after idx + count
  2788. * |-------| |-------|
  2789. */
  2790. void
  2791. xfs_iext_remove_indirect(
  2792. xfs_ifork_t *ifp, /* inode fork pointer */
  2793. xfs_extnum_t idx, /* index to begin removing extents */
  2794. int count) /* number of extents to remove */
  2795. {
  2796. xfs_ext_irec_t *erp; /* indirection array pointer */
  2797. int erp_idx = 0; /* indirection array index */
  2798. xfs_extnum_t ext_cnt; /* extents left to remove */
  2799. xfs_extnum_t ext_diff; /* extents to remove in current list */
  2800. xfs_extnum_t nex1; /* number of extents before idx */
  2801. xfs_extnum_t nex2; /* extents after idx + count */
  2802. int page_idx = idx; /* index in target extent list */
  2803. ASSERT(ifp->if_flags & XFS_IFEXTIREC);
  2804. erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 0);
  2805. ASSERT(erp != NULL);
  2806. nex1 = page_idx;
  2807. ext_cnt = count;
  2808. while (ext_cnt) {
  2809. nex2 = MAX((erp->er_extcount - (nex1 + ext_cnt)), 0);
  2810. ext_diff = MIN(ext_cnt, (erp->er_extcount - nex1));
  2811. /*
  2812. * Check for deletion of entire list;
  2813. * xfs_iext_irec_remove() updates extent offsets.
  2814. */
  2815. if (ext_diff == erp->er_extcount) {
  2816. xfs_iext_irec_remove(ifp, erp_idx);
  2817. ext_cnt -= ext_diff;
  2818. nex1 = 0;
  2819. if (ext_cnt) {
  2820. ASSERT(erp_idx < ifp->if_real_bytes /
  2821. XFS_IEXT_BUFSZ);
  2822. erp = &ifp->if_u1.if_ext_irec[erp_idx];
  2823. nex1 = 0;
  2824. continue;
  2825. } else {
  2826. break;
  2827. }
  2828. }
  2829. /* Move extents up (if needed) */
  2830. if (nex2) {
  2831. memmove(&erp->er_extbuf[nex1],
  2832. &erp->er_extbuf[nex1 + ext_diff],
  2833. nex2 * sizeof(xfs_bmbt_rec_t));
  2834. }
  2835. /* Zero out rest of page */
  2836. memset(&erp->er_extbuf[nex1 + nex2], 0, (XFS_IEXT_BUFSZ -
  2837. ((nex1 + nex2) * sizeof(xfs_bmbt_rec_t))));
  2838. /* Update remaining counters */
  2839. erp->er_extcount -= ext_diff;
  2840. xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, -ext_diff);
  2841. ext_cnt -= ext_diff;
  2842. nex1 = 0;
  2843. erp_idx++;
  2844. erp++;
  2845. }
  2846. ifp->if_bytes -= count * sizeof(xfs_bmbt_rec_t);
  2847. xfs_iext_irec_compact(ifp);
  2848. }
  2849. /*
  2850. * Create, destroy, or resize a linear (direct) block of extents.
  2851. */
  2852. void
  2853. xfs_iext_realloc_direct(
  2854. xfs_ifork_t *ifp, /* inode fork pointer */
  2855. int new_size) /* new size of extents */
  2856. {
  2857. int rnew_size; /* real new size of extents */
  2858. rnew_size = new_size;
  2859. ASSERT(!(ifp->if_flags & XFS_IFEXTIREC) ||
  2860. ((new_size >= 0) && (new_size <= XFS_IEXT_BUFSZ) &&
  2861. (new_size != ifp->if_real_bytes)));
  2862. /* Free extent records */
  2863. if (new_size == 0) {
  2864. xfs_iext_destroy(ifp);
  2865. }
  2866. /* Resize direct extent list and zero any new bytes */
  2867. else if (ifp->if_real_bytes) {
  2868. /* Check if extents will fit inside the inode */
  2869. if (new_size <= XFS_INLINE_EXTS * sizeof(xfs_bmbt_rec_t)) {
  2870. xfs_iext_direct_to_inline(ifp, new_size /
  2871. (uint)sizeof(xfs_bmbt_rec_t));
  2872. ifp->if_bytes = new_size;
  2873. return;
  2874. }
  2875. if (!is_power_of_2(new_size)){
  2876. rnew_size = roundup_pow_of_two(new_size);
  2877. }
  2878. if (rnew_size != ifp->if_real_bytes) {
  2879. ifp->if_u1.if_extents =
  2880. kmem_realloc(ifp->if_u1.if_extents,
  2881. rnew_size,
  2882. ifp->if_real_bytes, KM_NOFS);
  2883. }
  2884. if (rnew_size > ifp->if_real_bytes) {
  2885. memset(&ifp->if_u1.if_extents[ifp->if_bytes /
  2886. (uint)sizeof(xfs_bmbt_rec_t)], 0,
  2887. rnew_size - ifp->if_real_bytes);
  2888. }
  2889. }
  2890. /*
  2891. * Switch from the inline extent buffer to a direct
  2892. * extent list. Be sure to include the inline extent
  2893. * bytes in new_size.
  2894. */
  2895. else {
  2896. new_size += ifp->if_bytes;
  2897. if (!is_power_of_2(new_size)) {
  2898. rnew_size = roundup_pow_of_two(new_size);
  2899. }
  2900. xfs_iext_inline_to_direct(ifp, rnew_size);
  2901. }
  2902. ifp->if_real_bytes = rnew_size;
  2903. ifp->if_bytes = new_size;
  2904. }
  2905. /*
  2906. * Switch from linear (direct) extent records to inline buffer.
  2907. */
  2908. void
  2909. xfs_iext_direct_to_inline(
  2910. xfs_ifork_t *ifp, /* inode fork pointer */
  2911. xfs_extnum_t nextents) /* number of extents in file */
  2912. {
  2913. ASSERT(ifp->if_flags & XFS_IFEXTENTS);
  2914. ASSERT(nextents <= XFS_INLINE_EXTS);
  2915. /*
  2916. * The inline buffer was zeroed when we switched
  2917. * from inline to direct extent allocation mode,
  2918. * so we don't need to clear it here.
  2919. */
  2920. memcpy(ifp->if_u2.if_inline_ext, ifp->if_u1.if_extents,
  2921. nextents * sizeof(xfs_bmbt_rec_t));
  2922. kmem_free(ifp->if_u1.if_extents);
  2923. ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext;
  2924. ifp->if_real_bytes = 0;
  2925. }
  2926. /*
  2927. * Switch from inline buffer to linear (direct) extent records.
  2928. * new_size should already be rounded up to the next power of 2
  2929. * by the caller (when appropriate), so use new_size as it is.
  2930. * However, since new_size may be rounded up, we can't update
  2931. * if_bytes here. It is the caller's responsibility to update
  2932. * if_bytes upon return.
  2933. */
  2934. void
  2935. xfs_iext_inline_to_direct(
  2936. xfs_ifork_t *ifp, /* inode fork pointer */
  2937. int new_size) /* number of extents in file */
  2938. {
  2939. ifp->if_u1.if_extents = kmem_alloc(new_size, KM_NOFS);
  2940. memset(ifp->if_u1.if_extents, 0, new_size);
  2941. if (ifp->if_bytes) {
  2942. memcpy(ifp->if_u1.if_extents, ifp->if_u2.if_inline_ext,
  2943. ifp->if_bytes);
  2944. memset(ifp->if_u2.if_inline_ext, 0, XFS_INLINE_EXTS *
  2945. sizeof(xfs_bmbt_rec_t));
  2946. }
  2947. ifp->if_real_bytes = new_size;
  2948. }
  2949. /*
  2950. * Resize an extent indirection array to new_size bytes.
  2951. */
  2952. STATIC void
  2953. xfs_iext_realloc_indirect(
  2954. xfs_ifork_t *ifp, /* inode fork pointer */
  2955. int new_size) /* new indirection array size */
  2956. {
  2957. int nlists; /* number of irec's (ex lists) */
  2958. int size; /* current indirection array size */
  2959. ASSERT(ifp->if_flags & XFS_IFEXTIREC);
  2960. nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
  2961. size = nlists * sizeof(xfs_ext_irec_t);
  2962. ASSERT(ifp->if_real_bytes);
  2963. ASSERT((new_size >= 0) && (new_size != size));
  2964. if (new_size == 0) {
  2965. xfs_iext_destroy(ifp);
  2966. } else {
  2967. ifp->if_u1.if_ext_irec = (xfs_ext_irec_t *)
  2968. kmem_realloc(ifp->if_u1.if_ext_irec,
  2969. new_size, size, KM_NOFS);
  2970. }
  2971. }
  2972. /*
  2973. * Switch from indirection array to linear (direct) extent allocations.
  2974. */
  2975. STATIC void
  2976. xfs_iext_indirect_to_direct(
  2977. xfs_ifork_t *ifp) /* inode fork pointer */
  2978. {
  2979. xfs_bmbt_rec_host_t *ep; /* extent record pointer */
  2980. xfs_extnum_t nextents; /* number of extents in file */
  2981. int size; /* size of file extents */
  2982. ASSERT(ifp->if_flags & XFS_IFEXTIREC);
  2983. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  2984. ASSERT(nextents <= XFS_LINEAR_EXTS);
  2985. size = nextents * sizeof(xfs_bmbt_rec_t);
  2986. xfs_iext_irec_compact_pages(ifp);
  2987. ASSERT(ifp->if_real_bytes == XFS_IEXT_BUFSZ);
  2988. ep = ifp->if_u1.if_ext_irec->er_extbuf;
  2989. kmem_free(ifp->if_u1.if_ext_irec);
  2990. ifp->if_flags &= ~XFS_IFEXTIREC;
  2991. ifp->if_u1.if_extents = ep;
  2992. ifp->if_bytes = size;
  2993. if (nextents < XFS_LINEAR_EXTS) {
  2994. xfs_iext_realloc_direct(ifp, size);
  2995. }
  2996. }
  2997. /*
  2998. * Free incore file extents.
  2999. */
  3000. void
  3001. xfs_iext_destroy(
  3002. xfs_ifork_t *ifp) /* inode fork pointer */
  3003. {
  3004. if (ifp->if_flags & XFS_IFEXTIREC) {
  3005. int erp_idx;
  3006. int nlists;
  3007. nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
  3008. for (erp_idx = nlists - 1; erp_idx >= 0 ; erp_idx--) {
  3009. xfs_iext_irec_remove(ifp, erp_idx);
  3010. }
  3011. ifp->if_flags &= ~XFS_IFEXTIREC;
  3012. } else if (ifp->if_real_bytes) {
  3013. kmem_free(ifp->if_u1.if_extents);
  3014. } else if (ifp->if_bytes) {
  3015. memset(ifp->if_u2.if_inline_ext, 0, XFS_INLINE_EXTS *
  3016. sizeof(xfs_bmbt_rec_t));
  3017. }
  3018. ifp->if_u1.if_extents = NULL;
  3019. ifp->if_real_bytes = 0;
  3020. ifp->if_bytes = 0;
  3021. }
  3022. /*
  3023. * Return a pointer to the extent record for file system block bno.
  3024. */
  3025. xfs_bmbt_rec_host_t * /* pointer to found extent record */
  3026. xfs_iext_bno_to_ext(
  3027. xfs_ifork_t *ifp, /* inode fork pointer */
  3028. xfs_fileoff_t bno, /* block number to search for */
  3029. xfs_extnum_t *idxp) /* index of target extent */
  3030. {
  3031. xfs_bmbt_rec_host_t *base; /* pointer to first extent */
  3032. xfs_filblks_t blockcount = 0; /* number of blocks in extent */
  3033. xfs_bmbt_rec_host_t *ep = NULL; /* pointer to target extent */
  3034. xfs_ext_irec_t *erp = NULL; /* indirection array pointer */
  3035. int high; /* upper boundary in search */
  3036. xfs_extnum_t idx = 0; /* index of target extent */
  3037. int low; /* lower boundary in search */
  3038. xfs_extnum_t nextents; /* number of file extents */
  3039. xfs_fileoff_t startoff = 0; /* start offset of extent */
  3040. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  3041. if (nextents == 0) {
  3042. *idxp = 0;
  3043. return NULL;
  3044. }
  3045. low = 0;
  3046. if (ifp->if_flags & XFS_IFEXTIREC) {
  3047. /* Find target extent list */
  3048. int erp_idx = 0;
  3049. erp = xfs_iext_bno_to_irec(ifp, bno, &erp_idx);
  3050. base = erp->er_extbuf;
  3051. high = erp->er_extcount - 1;
  3052. } else {
  3053. base = ifp->if_u1.if_extents;
  3054. high = nextents - 1;
  3055. }
  3056. /* Binary search extent records */
  3057. while (low <= high) {
  3058. idx = (low + high) >> 1;
  3059. ep = base + idx;
  3060. startoff = xfs_bmbt_get_startoff(ep);
  3061. blockcount = xfs_bmbt_get_blockcount(ep);
  3062. if (bno < startoff) {
  3063. high = idx - 1;
  3064. } else if (bno >= startoff + blockcount) {
  3065. low = idx + 1;
  3066. } else {
  3067. /* Convert back to file-based extent index */
  3068. if (ifp->if_flags & XFS_IFEXTIREC) {
  3069. idx += erp->er_extoff;
  3070. }
  3071. *idxp = idx;
  3072. return ep;
  3073. }
  3074. }
  3075. /* Convert back to file-based extent index */
  3076. if (ifp->if_flags & XFS_IFEXTIREC) {
  3077. idx += erp->er_extoff;
  3078. }
  3079. if (bno >= startoff + blockcount) {
  3080. if (++idx == nextents) {
  3081. ep = NULL;
  3082. } else {
  3083. ep = xfs_iext_get_ext(ifp, idx);
  3084. }
  3085. }
  3086. *idxp = idx;
  3087. return ep;
  3088. }
  3089. /*
  3090. * Return a pointer to the indirection array entry containing the
  3091. * extent record for filesystem block bno. Store the index of the
  3092. * target irec in *erp_idxp.
  3093. */
  3094. xfs_ext_irec_t * /* pointer to found extent record */
  3095. xfs_iext_bno_to_irec(
  3096. xfs_ifork_t *ifp, /* inode fork pointer */
  3097. xfs_fileoff_t bno, /* block number to search for */
  3098. int *erp_idxp) /* irec index of target ext list */
  3099. {
  3100. xfs_ext_irec_t *erp = NULL; /* indirection array pointer */
  3101. xfs_ext_irec_t *erp_next; /* next indirection array entry */
  3102. int erp_idx; /* indirection array index */
  3103. int nlists; /* number of extent irec's (lists) */
  3104. int high; /* binary search upper limit */
  3105. int low; /* binary search lower limit */
  3106. ASSERT(ifp->if_flags & XFS_IFEXTIREC);
  3107. nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
  3108. erp_idx = 0;
  3109. low = 0;
  3110. high = nlists - 1;
  3111. while (low <= high) {
  3112. erp_idx = (low + high) >> 1;
  3113. erp = &ifp->if_u1.if_ext_irec[erp_idx];
  3114. erp_next = erp_idx < nlists - 1 ? erp + 1 : NULL;
  3115. if (bno < xfs_bmbt_get_startoff(erp->er_extbuf)) {
  3116. high = erp_idx - 1;
  3117. } else if (erp_next && bno >=
  3118. xfs_bmbt_get_startoff(erp_next->er_extbuf)) {
  3119. low = erp_idx + 1;
  3120. } else {
  3121. break;
  3122. }
  3123. }
  3124. *erp_idxp = erp_idx;
  3125. return erp;
  3126. }
  3127. /*
  3128. * Return a pointer to the indirection array entry containing the
  3129. * extent record at file extent index *idxp. Store the index of the
  3130. * target irec in *erp_idxp and store the page index of the target
  3131. * extent record in *idxp.
  3132. */
  3133. xfs_ext_irec_t *
  3134. xfs_iext_idx_to_irec(
  3135. xfs_ifork_t *ifp, /* inode fork pointer */
  3136. xfs_extnum_t *idxp, /* extent index (file -> page) */
  3137. int *erp_idxp, /* pointer to target irec */
  3138. int realloc) /* new bytes were just added */
  3139. {
  3140. xfs_ext_irec_t *prev; /* pointer to previous irec */
  3141. xfs_ext_irec_t *erp = NULL; /* pointer to current irec */
  3142. int erp_idx; /* indirection array index */
  3143. int nlists; /* number of irec's (ex lists) */
  3144. int high; /* binary search upper limit */
  3145. int low; /* binary search lower limit */
  3146. xfs_extnum_t page_idx = *idxp; /* extent index in target list */
  3147. ASSERT(ifp->if_flags & XFS_IFEXTIREC);
  3148. ASSERT(page_idx >= 0);
  3149. ASSERT(page_idx <= ifp->if_bytes / sizeof(xfs_bmbt_rec_t));
  3150. ASSERT(page_idx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t) || realloc);
  3151. nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
  3152. erp_idx = 0;
  3153. low = 0;
  3154. high = nlists - 1;
  3155. /* Binary search extent irec's */
  3156. while (low <= high) {
  3157. erp_idx = (low + high) >> 1;
  3158. erp = &ifp->if_u1.if_ext_irec[erp_idx];
  3159. prev = erp_idx > 0 ? erp - 1 : NULL;
  3160. if (page_idx < erp->er_extoff || (page_idx == erp->er_extoff &&
  3161. realloc && prev && prev->er_extcount < XFS_LINEAR_EXTS)) {
  3162. high = erp_idx - 1;
  3163. } else if (page_idx > erp->er_extoff + erp->er_extcount ||
  3164. (page_idx == erp->er_extoff + erp->er_extcount &&
  3165. !realloc)) {
  3166. low = erp_idx + 1;
  3167. } else if (page_idx == erp->er_extoff + erp->er_extcount &&
  3168. erp->er_extcount == XFS_LINEAR_EXTS) {
  3169. ASSERT(realloc);
  3170. page_idx = 0;
  3171. erp_idx++;
  3172. erp = erp_idx < nlists ? erp + 1 : NULL;
  3173. break;
  3174. } else {
  3175. page_idx -= erp->er_extoff;
  3176. break;
  3177. }
  3178. }
  3179. *idxp = page_idx;
  3180. *erp_idxp = erp_idx;
  3181. return(erp);
  3182. }
  3183. /*
  3184. * Allocate and initialize an indirection array once the space needed
  3185. * for incore extents increases above XFS_IEXT_BUFSZ.
  3186. */
  3187. void
  3188. xfs_iext_irec_init(
  3189. xfs_ifork_t *ifp) /* inode fork pointer */
  3190. {
  3191. xfs_ext_irec_t *erp; /* indirection array pointer */
  3192. xfs_extnum_t nextents; /* number of extents in file */
  3193. ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
  3194. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  3195. ASSERT(nextents <= XFS_LINEAR_EXTS);
  3196. erp = kmem_alloc(sizeof(xfs_ext_irec_t), KM_NOFS);
  3197. if (nextents == 0) {
  3198. ifp->if_u1.if_extents = kmem_alloc(XFS_IEXT_BUFSZ, KM_NOFS);
  3199. } else if (!ifp->if_real_bytes) {
  3200. xfs_iext_inline_to_direct(ifp, XFS_IEXT_BUFSZ);
  3201. } else if (ifp->if_real_bytes < XFS_IEXT_BUFSZ) {
  3202. xfs_iext_realloc_direct(ifp, XFS_IEXT_BUFSZ);
  3203. }
  3204. erp->er_extbuf = ifp->if_u1.if_extents;
  3205. erp->er_extcount = nextents;
  3206. erp->er_extoff = 0;
  3207. ifp->if_flags |= XFS_IFEXTIREC;
  3208. ifp->if_real_bytes = XFS_IEXT_BUFSZ;
  3209. ifp->if_bytes = nextents * sizeof(xfs_bmbt_rec_t);
  3210. ifp->if_u1.if_ext_irec = erp;
  3211. return;
  3212. }
  3213. /*
  3214. * Allocate and initialize a new entry in the indirection array.
  3215. */
  3216. xfs_ext_irec_t *
  3217. xfs_iext_irec_new(
  3218. xfs_ifork_t *ifp, /* inode fork pointer */
  3219. int erp_idx) /* index for new irec */
  3220. {
  3221. xfs_ext_irec_t *erp; /* indirection array pointer */
  3222. int i; /* loop counter */
  3223. int nlists; /* number of irec's (ex lists) */
  3224. ASSERT(ifp->if_flags & XFS_IFEXTIREC);
  3225. nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
  3226. /* Resize indirection array */
  3227. xfs_iext_realloc_indirect(ifp, ++nlists *
  3228. sizeof(xfs_ext_irec_t));
  3229. /*
  3230. * Move records down in the array so the
  3231. * new page can use erp_idx.
  3232. */
  3233. erp = ifp->if_u1.if_ext_irec;
  3234. for (i = nlists - 1; i > erp_idx; i--) {
  3235. memmove(&erp[i], &erp[i-1], sizeof(xfs_ext_irec_t));
  3236. }
  3237. ASSERT(i == erp_idx);
  3238. /* Initialize new extent record */
  3239. erp = ifp->if_u1.if_ext_irec;
  3240. erp[erp_idx].er_extbuf = kmem_alloc(XFS_IEXT_BUFSZ, KM_NOFS);
  3241. ifp->if_real_bytes = nlists * XFS_IEXT_BUFSZ;
  3242. memset(erp[erp_idx].er_extbuf, 0, XFS_IEXT_BUFSZ);
  3243. erp[erp_idx].er_extcount = 0;
  3244. erp[erp_idx].er_extoff = erp_idx > 0 ?
  3245. erp[erp_idx-1].er_extoff + erp[erp_idx-1].er_extcount : 0;
  3246. return (&erp[erp_idx]);
  3247. }
  3248. /*
  3249. * Remove a record from the indirection array.
  3250. */
  3251. void
  3252. xfs_iext_irec_remove(
  3253. xfs_ifork_t *ifp, /* inode fork pointer */
  3254. int erp_idx) /* irec index to remove */
  3255. {
  3256. xfs_ext_irec_t *erp; /* indirection array pointer */
  3257. int i; /* loop counter */
  3258. int nlists; /* number of irec's (ex lists) */
  3259. ASSERT(ifp->if_flags & XFS_IFEXTIREC);
  3260. nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
  3261. erp = &ifp->if_u1.if_ext_irec[erp_idx];
  3262. if (erp->er_extbuf) {
  3263. xfs_iext_irec_update_extoffs(ifp, erp_idx + 1,
  3264. -erp->er_extcount);
  3265. kmem_free(erp->er_extbuf);
  3266. }
  3267. /* Compact extent records */
  3268. erp = ifp->if_u1.if_ext_irec;
  3269. for (i = erp_idx; i < nlists - 1; i++) {
  3270. memmove(&erp[i], &erp[i+1], sizeof(xfs_ext_irec_t));
  3271. }
  3272. /*
  3273. * Manually free the last extent record from the indirection
  3274. * array. A call to xfs_iext_realloc_indirect() with a size
  3275. * of zero would result in a call to xfs_iext_destroy() which
  3276. * would in turn call this function again, creating a nasty
  3277. * infinite loop.
  3278. */
  3279. if (--nlists) {
  3280. xfs_iext_realloc_indirect(ifp,
  3281. nlists * sizeof(xfs_ext_irec_t));
  3282. } else {
  3283. kmem_free(ifp->if_u1.if_ext_irec);
  3284. }
  3285. ifp->if_real_bytes = nlists * XFS_IEXT_BUFSZ;
  3286. }
  3287. /*
  3288. * This is called to clean up large amounts of unused memory allocated
  3289. * by the indirection array. Before compacting anything though, verify
  3290. * that the indirection array is still needed and switch back to the
  3291. * linear extent list (or even the inline buffer) if possible. The
  3292. * compaction policy is as follows:
  3293. *
  3294. * Full Compaction: Extents fit into a single page (or inline buffer)
  3295. * Partial Compaction: Extents occupy less than 50% of allocated space
  3296. * No Compaction: Extents occupy at least 50% of allocated space
  3297. */
  3298. void
  3299. xfs_iext_irec_compact(
  3300. xfs_ifork_t *ifp) /* inode fork pointer */
  3301. {
  3302. xfs_extnum_t nextents; /* number of extents in file */
  3303. int nlists; /* number of irec's (ex lists) */
  3304. ASSERT(ifp->if_flags & XFS_IFEXTIREC);
  3305. nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
  3306. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  3307. if (nextents == 0) {
  3308. xfs_iext_destroy(ifp);
  3309. } else if (nextents <= XFS_INLINE_EXTS) {
  3310. xfs_iext_indirect_to_direct(ifp);
  3311. xfs_iext_direct_to_inline(ifp, nextents);
  3312. } else if (nextents <= XFS_LINEAR_EXTS) {
  3313. xfs_iext_indirect_to_direct(ifp);
  3314. } else if (nextents < (nlists * XFS_LINEAR_EXTS) >> 1) {
  3315. xfs_iext_irec_compact_pages(ifp);
  3316. }
  3317. }
  3318. /*
  3319. * Combine extents from neighboring extent pages.
  3320. */
  3321. void
  3322. xfs_iext_irec_compact_pages(
  3323. xfs_ifork_t *ifp) /* inode fork pointer */
  3324. {
  3325. xfs_ext_irec_t *erp, *erp_next;/* pointers to irec entries */
  3326. int erp_idx = 0; /* indirection array index */
  3327. int nlists; /* number of irec's (ex lists) */
  3328. ASSERT(ifp->if_flags & XFS_IFEXTIREC);
  3329. nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
  3330. while (erp_idx < nlists - 1) {
  3331. erp = &ifp->if_u1.if_ext_irec[erp_idx];
  3332. erp_next = erp + 1;
  3333. if (erp_next->er_extcount <=
  3334. (XFS_LINEAR_EXTS - erp->er_extcount)) {
  3335. memcpy(&erp->er_extbuf[erp->er_extcount],
  3336. erp_next->er_extbuf, erp_next->er_extcount *
  3337. sizeof(xfs_bmbt_rec_t));
  3338. erp->er_extcount += erp_next->er_extcount;
  3339. /*
  3340. * Free page before removing extent record
  3341. * so er_extoffs don't get modified in
  3342. * xfs_iext_irec_remove.
  3343. */
  3344. kmem_free(erp_next->er_extbuf);
  3345. erp_next->er_extbuf = NULL;
  3346. xfs_iext_irec_remove(ifp, erp_idx + 1);
  3347. nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
  3348. } else {
  3349. erp_idx++;
  3350. }
  3351. }
  3352. }
  3353. /*
  3354. * This is called to update the er_extoff field in the indirection
  3355. * array when extents have been added or removed from one of the
  3356. * extent lists. erp_idx contains the irec index to begin updating
  3357. * at and ext_diff contains the number of extents that were added
  3358. * or removed.
  3359. */
  3360. void
  3361. xfs_iext_irec_update_extoffs(
  3362. xfs_ifork_t *ifp, /* inode fork pointer */
  3363. int erp_idx, /* irec index to update */
  3364. int ext_diff) /* number of new extents */
  3365. {
  3366. int i; /* loop counter */
  3367. int nlists; /* number of irec's (ex lists */
  3368. ASSERT(ifp->if_flags & XFS_IFEXTIREC);
  3369. nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
  3370. for (i = erp_idx; i < nlists; i++) {
  3371. ifp->if_u1.if_ext_irec[i].er_extoff += ext_diff;
  3372. }
  3373. }