xfs_inode.c 112 KB

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