xfs_inode.c 121 KB

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