xfs_inode.c 114 KB

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