xfs_inode.c 109 KB

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