xfs_inode.c 104 KB

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