xfs_inode.c 110 KB

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