xfs_inode.c 95 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535
  1. /*
  2. * Copyright (c) 2000-2006 Silicon Graphics, Inc.
  3. * All Rights Reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it would be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write the Free Software Foundation,
  16. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include <linux/log2.h>
  19. #include "xfs.h"
  20. #include "xfs_fs.h"
  21. #include "xfs_shared.h"
  22. #include "xfs_format.h"
  23. #include "xfs_log_format.h"
  24. #include "xfs_trans_resv.h"
  25. #include "xfs_sb.h"
  26. #include "xfs_mount.h"
  27. #include "xfs_inode.h"
  28. #include "xfs_da_format.h"
  29. #include "xfs_da_btree.h"
  30. #include "xfs_dir2.h"
  31. #include "xfs_attr_sf.h"
  32. #include "xfs_attr.h"
  33. #include "xfs_trans_space.h"
  34. #include "xfs_trans.h"
  35. #include "xfs_buf_item.h"
  36. #include "xfs_inode_item.h"
  37. #include "xfs_ialloc.h"
  38. #include "xfs_bmap.h"
  39. #include "xfs_bmap_util.h"
  40. #include "xfs_error.h"
  41. #include "xfs_quota.h"
  42. #include "xfs_filestream.h"
  43. #include "xfs_cksum.h"
  44. #include "xfs_trace.h"
  45. #include "xfs_icache.h"
  46. #include "xfs_symlink.h"
  47. #include "xfs_trans_priv.h"
  48. #include "xfs_log.h"
  49. #include "xfs_bmap_btree.h"
  50. kmem_zone_t *xfs_inode_zone;
  51. /*
  52. * Used in xfs_itruncate_extents(). This is the maximum number of extents
  53. * freed from a file in a single transaction.
  54. */
  55. #define XFS_ITRUNC_MAX_EXTENTS 2
  56. STATIC int xfs_iflush_int(xfs_inode_t *, xfs_buf_t *);
  57. STATIC int xfs_iunlink_remove(xfs_trans_t *, xfs_inode_t *);
  58. /*
  59. * helper function to extract extent size hint from inode
  60. */
  61. xfs_extlen_t
  62. xfs_get_extsz_hint(
  63. struct xfs_inode *ip)
  64. {
  65. if ((ip->i_d.di_flags & XFS_DIFLAG_EXTSIZE) && ip->i_d.di_extsize)
  66. return ip->i_d.di_extsize;
  67. if (XFS_IS_REALTIME_INODE(ip))
  68. return ip->i_mount->m_sb.sb_rextsize;
  69. return 0;
  70. }
  71. /*
  72. * These two are wrapper routines around the xfs_ilock() routine used to
  73. * centralize some grungy code. They are used in places that wish to lock the
  74. * inode solely for reading the extents. The reason these places can't just
  75. * call xfs_ilock(ip, XFS_ILOCK_SHARED) is that the inode lock also guards to
  76. * bringing in of the extents from disk for a file in b-tree format. If the
  77. * inode is in b-tree format, then we need to lock the inode exclusively until
  78. * the extents are read in. Locking it exclusively all the time would limit
  79. * our parallelism unnecessarily, though. What we do instead is check to see
  80. * if the extents have been read in yet, and only lock the inode exclusively
  81. * if they have not.
  82. *
  83. * The functions return a value which should be given to the corresponding
  84. * xfs_iunlock() call.
  85. */
  86. uint
  87. xfs_ilock_data_map_shared(
  88. struct xfs_inode *ip)
  89. {
  90. uint lock_mode = XFS_ILOCK_SHARED;
  91. if (ip->i_d.di_format == XFS_DINODE_FMT_BTREE &&
  92. (ip->i_df.if_flags & XFS_IFEXTENTS) == 0)
  93. lock_mode = XFS_ILOCK_EXCL;
  94. xfs_ilock(ip, lock_mode);
  95. return lock_mode;
  96. }
  97. uint
  98. xfs_ilock_attr_map_shared(
  99. struct xfs_inode *ip)
  100. {
  101. uint lock_mode = XFS_ILOCK_SHARED;
  102. if (ip->i_d.di_aformat == XFS_DINODE_FMT_BTREE &&
  103. (ip->i_afp->if_flags & XFS_IFEXTENTS) == 0)
  104. lock_mode = XFS_ILOCK_EXCL;
  105. xfs_ilock(ip, lock_mode);
  106. return lock_mode;
  107. }
  108. /*
  109. * The xfs inode contains 3 multi-reader locks: the i_iolock the i_mmap_lock and
  110. * the i_lock. This routine allows various combinations of the locks to be
  111. * obtained.
  112. *
  113. * The 3 locks should always be ordered so that the IO lock is obtained first,
  114. * the mmap lock second and the ilock last in order to prevent deadlock.
  115. *
  116. * Basic locking order:
  117. *
  118. * i_iolock -> i_mmap_lock -> page_lock -> i_ilock
  119. *
  120. * mmap_sem locking order:
  121. *
  122. * i_iolock -> page lock -> mmap_sem
  123. * mmap_sem -> i_mmap_lock -> page_lock
  124. *
  125. * The difference in mmap_sem locking order mean that we cannot hold the
  126. * i_mmap_lock over syscall based read(2)/write(2) based IO. These IO paths can
  127. * fault in pages during copy in/out (for buffered IO) or require the mmap_sem
  128. * in get_user_pages() to map the user pages into the kernel address space for
  129. * direct IO. Similarly the i_iolock cannot be taken inside a page fault because
  130. * page faults already hold the mmap_sem.
  131. *
  132. * Hence to serialise fully against both syscall and mmap based IO, we need to
  133. * take both the i_iolock and the i_mmap_lock. These locks should *only* be both
  134. * taken in places where we need to invalidate the page cache in a race
  135. * free manner (e.g. truncate, hole punch and other extent manipulation
  136. * functions).
  137. */
  138. void
  139. xfs_ilock(
  140. xfs_inode_t *ip,
  141. uint lock_flags)
  142. {
  143. trace_xfs_ilock(ip, lock_flags, _RET_IP_);
  144. /*
  145. * You can't set both SHARED and EXCL for the same lock,
  146. * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
  147. * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
  148. */
  149. ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
  150. (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
  151. ASSERT((lock_flags & (XFS_MMAPLOCK_SHARED | XFS_MMAPLOCK_EXCL)) !=
  152. (XFS_MMAPLOCK_SHARED | XFS_MMAPLOCK_EXCL));
  153. ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
  154. (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
  155. ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0);
  156. if (lock_flags & XFS_IOLOCK_EXCL)
  157. mrupdate_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags));
  158. else if (lock_flags & XFS_IOLOCK_SHARED)
  159. mraccess_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags));
  160. if (lock_flags & XFS_MMAPLOCK_EXCL)
  161. mrupdate_nested(&ip->i_mmaplock, XFS_MMAPLOCK_DEP(lock_flags));
  162. else if (lock_flags & XFS_MMAPLOCK_SHARED)
  163. mraccess_nested(&ip->i_mmaplock, XFS_MMAPLOCK_DEP(lock_flags));
  164. if (lock_flags & XFS_ILOCK_EXCL)
  165. mrupdate_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags));
  166. else if (lock_flags & XFS_ILOCK_SHARED)
  167. mraccess_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags));
  168. }
  169. /*
  170. * This is just like xfs_ilock(), except that the caller
  171. * is guaranteed not to sleep. It returns 1 if it gets
  172. * the requested locks and 0 otherwise. If the IO lock is
  173. * obtained but the inode lock cannot be, then the IO lock
  174. * is dropped before returning.
  175. *
  176. * ip -- the inode being locked
  177. * lock_flags -- this parameter indicates the inode's locks to be
  178. * to be locked. See the comment for xfs_ilock() for a list
  179. * of valid values.
  180. */
  181. int
  182. xfs_ilock_nowait(
  183. xfs_inode_t *ip,
  184. uint lock_flags)
  185. {
  186. trace_xfs_ilock_nowait(ip, lock_flags, _RET_IP_);
  187. /*
  188. * You can't set both SHARED and EXCL for the same lock,
  189. * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
  190. * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
  191. */
  192. ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
  193. (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
  194. ASSERT((lock_flags & (XFS_MMAPLOCK_SHARED | XFS_MMAPLOCK_EXCL)) !=
  195. (XFS_MMAPLOCK_SHARED | XFS_MMAPLOCK_EXCL));
  196. ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
  197. (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
  198. ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0);
  199. if (lock_flags & XFS_IOLOCK_EXCL) {
  200. if (!mrtryupdate(&ip->i_iolock))
  201. goto out;
  202. } else if (lock_flags & XFS_IOLOCK_SHARED) {
  203. if (!mrtryaccess(&ip->i_iolock))
  204. goto out;
  205. }
  206. if (lock_flags & XFS_MMAPLOCK_EXCL) {
  207. if (!mrtryupdate(&ip->i_mmaplock))
  208. goto out_undo_iolock;
  209. } else if (lock_flags & XFS_MMAPLOCK_SHARED) {
  210. if (!mrtryaccess(&ip->i_mmaplock))
  211. goto out_undo_iolock;
  212. }
  213. if (lock_flags & XFS_ILOCK_EXCL) {
  214. if (!mrtryupdate(&ip->i_lock))
  215. goto out_undo_mmaplock;
  216. } else if (lock_flags & XFS_ILOCK_SHARED) {
  217. if (!mrtryaccess(&ip->i_lock))
  218. goto out_undo_mmaplock;
  219. }
  220. return 1;
  221. out_undo_mmaplock:
  222. if (lock_flags & XFS_MMAPLOCK_EXCL)
  223. mrunlock_excl(&ip->i_mmaplock);
  224. else if (lock_flags & XFS_MMAPLOCK_SHARED)
  225. mrunlock_shared(&ip->i_mmaplock);
  226. out_undo_iolock:
  227. if (lock_flags & XFS_IOLOCK_EXCL)
  228. mrunlock_excl(&ip->i_iolock);
  229. else if (lock_flags & XFS_IOLOCK_SHARED)
  230. mrunlock_shared(&ip->i_iolock);
  231. out:
  232. return 0;
  233. }
  234. /*
  235. * xfs_iunlock() is used to drop the inode locks acquired with
  236. * xfs_ilock() and xfs_ilock_nowait(). The caller must pass
  237. * in the flags given to xfs_ilock() or xfs_ilock_nowait() so
  238. * that we know which locks to drop.
  239. *
  240. * ip -- the inode being unlocked
  241. * lock_flags -- this parameter indicates the inode's locks to be
  242. * to be unlocked. See the comment for xfs_ilock() for a list
  243. * of valid values for this parameter.
  244. *
  245. */
  246. void
  247. xfs_iunlock(
  248. xfs_inode_t *ip,
  249. uint lock_flags)
  250. {
  251. /*
  252. * You can't set both SHARED and EXCL for the same lock,
  253. * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
  254. * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
  255. */
  256. ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
  257. (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
  258. ASSERT((lock_flags & (XFS_MMAPLOCK_SHARED | XFS_MMAPLOCK_EXCL)) !=
  259. (XFS_MMAPLOCK_SHARED | XFS_MMAPLOCK_EXCL));
  260. ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
  261. (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
  262. ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0);
  263. ASSERT(lock_flags != 0);
  264. if (lock_flags & XFS_IOLOCK_EXCL)
  265. mrunlock_excl(&ip->i_iolock);
  266. else if (lock_flags & XFS_IOLOCK_SHARED)
  267. mrunlock_shared(&ip->i_iolock);
  268. if (lock_flags & XFS_MMAPLOCK_EXCL)
  269. mrunlock_excl(&ip->i_mmaplock);
  270. else if (lock_flags & XFS_MMAPLOCK_SHARED)
  271. mrunlock_shared(&ip->i_mmaplock);
  272. if (lock_flags & XFS_ILOCK_EXCL)
  273. mrunlock_excl(&ip->i_lock);
  274. else if (lock_flags & XFS_ILOCK_SHARED)
  275. mrunlock_shared(&ip->i_lock);
  276. trace_xfs_iunlock(ip, lock_flags, _RET_IP_);
  277. }
  278. /*
  279. * give up write locks. the i/o lock cannot be held nested
  280. * if it is being demoted.
  281. */
  282. void
  283. xfs_ilock_demote(
  284. xfs_inode_t *ip,
  285. uint lock_flags)
  286. {
  287. ASSERT(lock_flags & (XFS_IOLOCK_EXCL|XFS_MMAPLOCK_EXCL|XFS_ILOCK_EXCL));
  288. ASSERT((lock_flags &
  289. ~(XFS_IOLOCK_EXCL|XFS_MMAPLOCK_EXCL|XFS_ILOCK_EXCL)) == 0);
  290. if (lock_flags & XFS_ILOCK_EXCL)
  291. mrdemote(&ip->i_lock);
  292. if (lock_flags & XFS_MMAPLOCK_EXCL)
  293. mrdemote(&ip->i_mmaplock);
  294. if (lock_flags & XFS_IOLOCK_EXCL)
  295. mrdemote(&ip->i_iolock);
  296. trace_xfs_ilock_demote(ip, lock_flags, _RET_IP_);
  297. }
  298. #if defined(DEBUG) || defined(XFS_WARN)
  299. int
  300. xfs_isilocked(
  301. xfs_inode_t *ip,
  302. uint lock_flags)
  303. {
  304. if (lock_flags & (XFS_ILOCK_EXCL|XFS_ILOCK_SHARED)) {
  305. if (!(lock_flags & XFS_ILOCK_SHARED))
  306. return !!ip->i_lock.mr_writer;
  307. return rwsem_is_locked(&ip->i_lock.mr_lock);
  308. }
  309. if (lock_flags & (XFS_MMAPLOCK_EXCL|XFS_MMAPLOCK_SHARED)) {
  310. if (!(lock_flags & XFS_MMAPLOCK_SHARED))
  311. return !!ip->i_mmaplock.mr_writer;
  312. return rwsem_is_locked(&ip->i_mmaplock.mr_lock);
  313. }
  314. if (lock_flags & (XFS_IOLOCK_EXCL|XFS_IOLOCK_SHARED)) {
  315. if (!(lock_flags & XFS_IOLOCK_SHARED))
  316. return !!ip->i_iolock.mr_writer;
  317. return rwsem_is_locked(&ip->i_iolock.mr_lock);
  318. }
  319. ASSERT(0);
  320. return 0;
  321. }
  322. #endif
  323. #ifdef DEBUG
  324. int xfs_locked_n;
  325. int xfs_small_retries;
  326. int xfs_middle_retries;
  327. int xfs_lots_retries;
  328. int xfs_lock_delays;
  329. #endif
  330. /*
  331. * Bump the subclass so xfs_lock_inodes() acquires each lock with a different
  332. * value. This shouldn't be called for page fault locking, but we also need to
  333. * ensure we don't overrun the number of lockdep subclasses for the iolock or
  334. * mmaplock as that is limited to 12 by the mmap lock lockdep annotations.
  335. */
  336. static inline int
  337. xfs_lock_inumorder(int lock_mode, int subclass)
  338. {
  339. if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL)) {
  340. ASSERT(subclass + XFS_LOCK_INUMORDER <
  341. (1 << (XFS_MMAPLOCK_SHIFT - XFS_IOLOCK_SHIFT)));
  342. lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_IOLOCK_SHIFT;
  343. }
  344. if (lock_mode & (XFS_MMAPLOCK_SHARED|XFS_MMAPLOCK_EXCL)) {
  345. ASSERT(subclass + XFS_LOCK_INUMORDER <
  346. (1 << (XFS_ILOCK_SHIFT - XFS_MMAPLOCK_SHIFT)));
  347. lock_mode |= (subclass + XFS_LOCK_INUMORDER) <<
  348. XFS_MMAPLOCK_SHIFT;
  349. }
  350. if (lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL))
  351. lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_ILOCK_SHIFT;
  352. return lock_mode;
  353. }
  354. /*
  355. * The following routine will lock n inodes in exclusive mode. We assume the
  356. * caller calls us with the inodes in i_ino order.
  357. *
  358. * We need to detect deadlock where an inode that we lock is in the AIL and we
  359. * start waiting for another inode that is locked by a thread in a long running
  360. * transaction (such as truncate). This can result in deadlock since the long
  361. * running trans might need to wait for the inode we just locked in order to
  362. * push the tail and free space in the log.
  363. */
  364. void
  365. xfs_lock_inodes(
  366. xfs_inode_t **ips,
  367. int inodes,
  368. uint lock_mode)
  369. {
  370. int attempts = 0, i, j, try_lock;
  371. xfs_log_item_t *lp;
  372. /* currently supports between 2 and 5 inodes */
  373. ASSERT(ips && inodes >= 2 && inodes <= 5);
  374. try_lock = 0;
  375. i = 0;
  376. again:
  377. for (; i < inodes; i++) {
  378. ASSERT(ips[i]);
  379. if (i && (ips[i] == ips[i - 1])) /* Already locked */
  380. continue;
  381. /*
  382. * If try_lock is not set yet, make sure all locked inodes are
  383. * not in the AIL. If any are, set try_lock to be used later.
  384. */
  385. if (!try_lock) {
  386. for (j = (i - 1); j >= 0 && !try_lock; j--) {
  387. lp = (xfs_log_item_t *)ips[j]->i_itemp;
  388. if (lp && (lp->li_flags & XFS_LI_IN_AIL))
  389. try_lock++;
  390. }
  391. }
  392. /*
  393. * If any of the previous locks we have locked is in the AIL,
  394. * we must TRY to get the second and subsequent locks. If
  395. * we can't get any, we must release all we have
  396. * and try again.
  397. */
  398. if (!try_lock) {
  399. xfs_ilock(ips[i], xfs_lock_inumorder(lock_mode, i));
  400. continue;
  401. }
  402. /* try_lock means we have an inode locked that is in the AIL. */
  403. ASSERT(i != 0);
  404. if (xfs_ilock_nowait(ips[i], xfs_lock_inumorder(lock_mode, i)))
  405. continue;
  406. /*
  407. * Unlock all previous guys and try again. xfs_iunlock will try
  408. * to push the tail if the inode is in the AIL.
  409. */
  410. attempts++;
  411. for (j = i - 1; j >= 0; j--) {
  412. /*
  413. * Check to see if we've already unlocked this one. Not
  414. * the first one going back, and the inode ptr is the
  415. * same.
  416. */
  417. if (j != (i - 1) && ips[j] == ips[j + 1])
  418. continue;
  419. xfs_iunlock(ips[j], lock_mode);
  420. }
  421. if ((attempts % 5) == 0) {
  422. delay(1); /* Don't just spin the CPU */
  423. #ifdef DEBUG
  424. xfs_lock_delays++;
  425. #endif
  426. }
  427. i = 0;
  428. try_lock = 0;
  429. goto again;
  430. }
  431. #ifdef DEBUG
  432. if (attempts) {
  433. if (attempts < 5) xfs_small_retries++;
  434. else if (attempts < 100) xfs_middle_retries++;
  435. else xfs_lots_retries++;
  436. } else {
  437. xfs_locked_n++;
  438. }
  439. #endif
  440. }
  441. /*
  442. * xfs_lock_two_inodes() can only be used to lock one type of lock at a time -
  443. * the iolock, the mmaplock or the ilock, but not more than one at a time. If we
  444. * lock more than one at a time, lockdep will report false positives saying we
  445. * have violated locking orders.
  446. */
  447. void
  448. xfs_lock_two_inodes(
  449. xfs_inode_t *ip0,
  450. xfs_inode_t *ip1,
  451. uint lock_mode)
  452. {
  453. xfs_inode_t *temp;
  454. int attempts = 0;
  455. xfs_log_item_t *lp;
  456. if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL)) {
  457. ASSERT(!(lock_mode & (XFS_MMAPLOCK_SHARED|XFS_MMAPLOCK_EXCL)));
  458. ASSERT(!(lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)));
  459. } else if (lock_mode & (XFS_MMAPLOCK_SHARED|XFS_MMAPLOCK_EXCL))
  460. ASSERT(!(lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)));
  461. ASSERT(ip0->i_ino != ip1->i_ino);
  462. if (ip0->i_ino > ip1->i_ino) {
  463. temp = ip0;
  464. ip0 = ip1;
  465. ip1 = temp;
  466. }
  467. again:
  468. xfs_ilock(ip0, xfs_lock_inumorder(lock_mode, 0));
  469. /*
  470. * If the first lock we have locked is in the AIL, we must TRY to get
  471. * the second lock. If we can't get it, we must release the first one
  472. * and try again.
  473. */
  474. lp = (xfs_log_item_t *)ip0->i_itemp;
  475. if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
  476. if (!xfs_ilock_nowait(ip1, xfs_lock_inumorder(lock_mode, 1))) {
  477. xfs_iunlock(ip0, lock_mode);
  478. if ((++attempts % 5) == 0)
  479. delay(1); /* Don't just spin the CPU */
  480. goto again;
  481. }
  482. } else {
  483. xfs_ilock(ip1, xfs_lock_inumorder(lock_mode, 1));
  484. }
  485. }
  486. void
  487. __xfs_iflock(
  488. struct xfs_inode *ip)
  489. {
  490. wait_queue_head_t *wq = bit_waitqueue(&ip->i_flags, __XFS_IFLOCK_BIT);
  491. DEFINE_WAIT_BIT(wait, &ip->i_flags, __XFS_IFLOCK_BIT);
  492. do {
  493. prepare_to_wait_exclusive(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
  494. if (xfs_isiflocked(ip))
  495. io_schedule();
  496. } while (!xfs_iflock_nowait(ip));
  497. finish_wait(wq, &wait.wait);
  498. }
  499. STATIC uint
  500. _xfs_dic2xflags(
  501. __uint16_t di_flags)
  502. {
  503. uint flags = 0;
  504. if (di_flags & XFS_DIFLAG_ANY) {
  505. if (di_flags & XFS_DIFLAG_REALTIME)
  506. flags |= XFS_XFLAG_REALTIME;
  507. if (di_flags & XFS_DIFLAG_PREALLOC)
  508. flags |= XFS_XFLAG_PREALLOC;
  509. if (di_flags & XFS_DIFLAG_IMMUTABLE)
  510. flags |= XFS_XFLAG_IMMUTABLE;
  511. if (di_flags & XFS_DIFLAG_APPEND)
  512. flags |= XFS_XFLAG_APPEND;
  513. if (di_flags & XFS_DIFLAG_SYNC)
  514. flags |= XFS_XFLAG_SYNC;
  515. if (di_flags & XFS_DIFLAG_NOATIME)
  516. flags |= XFS_XFLAG_NOATIME;
  517. if (di_flags & XFS_DIFLAG_NODUMP)
  518. flags |= XFS_XFLAG_NODUMP;
  519. if (di_flags & XFS_DIFLAG_RTINHERIT)
  520. flags |= XFS_XFLAG_RTINHERIT;
  521. if (di_flags & XFS_DIFLAG_PROJINHERIT)
  522. flags |= XFS_XFLAG_PROJINHERIT;
  523. if (di_flags & XFS_DIFLAG_NOSYMLINKS)
  524. flags |= XFS_XFLAG_NOSYMLINKS;
  525. if (di_flags & XFS_DIFLAG_EXTSIZE)
  526. flags |= XFS_XFLAG_EXTSIZE;
  527. if (di_flags & XFS_DIFLAG_EXTSZINHERIT)
  528. flags |= XFS_XFLAG_EXTSZINHERIT;
  529. if (di_flags & XFS_DIFLAG_NODEFRAG)
  530. flags |= XFS_XFLAG_NODEFRAG;
  531. if (di_flags & XFS_DIFLAG_FILESTREAM)
  532. flags |= XFS_XFLAG_FILESTREAM;
  533. }
  534. return flags;
  535. }
  536. uint
  537. xfs_ip2xflags(
  538. xfs_inode_t *ip)
  539. {
  540. xfs_icdinode_t *dic = &ip->i_d;
  541. return _xfs_dic2xflags(dic->di_flags) |
  542. (XFS_IFORK_Q(ip) ? XFS_XFLAG_HASATTR : 0);
  543. }
  544. uint
  545. xfs_dic2xflags(
  546. xfs_dinode_t *dip)
  547. {
  548. return _xfs_dic2xflags(be16_to_cpu(dip->di_flags)) |
  549. (XFS_DFORK_Q(dip) ? XFS_XFLAG_HASATTR : 0);
  550. }
  551. /*
  552. * Lookups up an inode from "name". If ci_name is not NULL, then a CI match
  553. * is allowed, otherwise it has to be an exact match. If a CI match is found,
  554. * ci_name->name will point to a the actual name (caller must free) or
  555. * will be set to NULL if an exact match is found.
  556. */
  557. int
  558. xfs_lookup(
  559. xfs_inode_t *dp,
  560. struct xfs_name *name,
  561. xfs_inode_t **ipp,
  562. struct xfs_name *ci_name)
  563. {
  564. xfs_ino_t inum;
  565. int error;
  566. uint lock_mode;
  567. trace_xfs_lookup(dp, name);
  568. if (XFS_FORCED_SHUTDOWN(dp->i_mount))
  569. return -EIO;
  570. lock_mode = xfs_ilock_data_map_shared(dp);
  571. error = xfs_dir_lookup(NULL, dp, name, &inum, ci_name);
  572. xfs_iunlock(dp, lock_mode);
  573. if (error)
  574. goto out;
  575. error = xfs_iget(dp->i_mount, NULL, inum, 0, 0, ipp);
  576. if (error)
  577. goto out_free_name;
  578. return 0;
  579. out_free_name:
  580. if (ci_name)
  581. kmem_free(ci_name->name);
  582. out:
  583. *ipp = NULL;
  584. return error;
  585. }
  586. /*
  587. * Allocate an inode on disk and return a copy of its in-core version.
  588. * The in-core inode is locked exclusively. Set mode, nlink, and rdev
  589. * appropriately within the inode. The uid and gid for the inode are
  590. * set according to the contents of the given cred structure.
  591. *
  592. * Use xfs_dialloc() to allocate the on-disk inode. If xfs_dialloc()
  593. * has a free inode available, call xfs_iget() to obtain the in-core
  594. * version of the allocated inode. Finally, fill in the inode and
  595. * log its initial contents. In this case, ialloc_context would be
  596. * set to NULL.
  597. *
  598. * If xfs_dialloc() does not have an available inode, it will replenish
  599. * its supply by doing an allocation. Since we can only do one
  600. * allocation within a transaction without deadlocks, we must commit
  601. * the current transaction before returning the inode itself.
  602. * In this case, therefore, we will set ialloc_context and return.
  603. * The caller should then commit the current transaction, start a new
  604. * transaction, and call xfs_ialloc() again to actually get the inode.
  605. *
  606. * To ensure that some other process does not grab the inode that
  607. * was allocated during the first call to xfs_ialloc(), this routine
  608. * also returns the [locked] bp pointing to the head of the freelist
  609. * as ialloc_context. The caller should hold this buffer across
  610. * the commit and pass it back into this routine on the second call.
  611. *
  612. * If we are allocating quota inodes, we do not have a parent inode
  613. * to attach to or associate with (i.e. pip == NULL) because they
  614. * are not linked into the directory structure - they are attached
  615. * directly to the superblock - and so have no parent.
  616. */
  617. int
  618. xfs_ialloc(
  619. xfs_trans_t *tp,
  620. xfs_inode_t *pip,
  621. umode_t mode,
  622. xfs_nlink_t nlink,
  623. xfs_dev_t rdev,
  624. prid_t prid,
  625. int okalloc,
  626. xfs_buf_t **ialloc_context,
  627. xfs_inode_t **ipp)
  628. {
  629. struct xfs_mount *mp = tp->t_mountp;
  630. xfs_ino_t ino;
  631. xfs_inode_t *ip;
  632. uint flags;
  633. int error;
  634. struct timespec tv;
  635. /*
  636. * Call the space management code to pick
  637. * the on-disk inode to be allocated.
  638. */
  639. error = xfs_dialloc(tp, pip ? pip->i_ino : 0, mode, okalloc,
  640. ialloc_context, &ino);
  641. if (error)
  642. return error;
  643. if (*ialloc_context || ino == NULLFSINO) {
  644. *ipp = NULL;
  645. return 0;
  646. }
  647. ASSERT(*ialloc_context == NULL);
  648. /*
  649. * Get the in-core inode with the lock held exclusively.
  650. * This is because we're setting fields here we need
  651. * to prevent others from looking at until we're done.
  652. */
  653. error = xfs_iget(mp, tp, ino, XFS_IGET_CREATE,
  654. XFS_ILOCK_EXCL, &ip);
  655. if (error)
  656. return error;
  657. ASSERT(ip != NULL);
  658. /*
  659. * We always convert v1 inodes to v2 now - we only support filesystems
  660. * with >= v2 inode capability, so there is no reason for ever leaving
  661. * an inode in v1 format.
  662. */
  663. if (ip->i_d.di_version == 1)
  664. ip->i_d.di_version = 2;
  665. ip->i_d.di_mode = mode;
  666. ip->i_d.di_onlink = 0;
  667. ip->i_d.di_nlink = nlink;
  668. ASSERT(ip->i_d.di_nlink == nlink);
  669. ip->i_d.di_uid = xfs_kuid_to_uid(current_fsuid());
  670. ip->i_d.di_gid = xfs_kgid_to_gid(current_fsgid());
  671. xfs_set_projid(ip, prid);
  672. memset(&(ip->i_d.di_pad[0]), 0, sizeof(ip->i_d.di_pad));
  673. if (pip && XFS_INHERIT_GID(pip)) {
  674. ip->i_d.di_gid = pip->i_d.di_gid;
  675. if ((pip->i_d.di_mode & S_ISGID) && S_ISDIR(mode)) {
  676. ip->i_d.di_mode |= S_ISGID;
  677. }
  678. }
  679. /*
  680. * If the group ID of the new file does not match the effective group
  681. * ID or one of the supplementary group IDs, the S_ISGID bit is cleared
  682. * (and only if the irix_sgid_inherit compatibility variable is set).
  683. */
  684. if ((irix_sgid_inherit) &&
  685. (ip->i_d.di_mode & S_ISGID) &&
  686. (!in_group_p(xfs_gid_to_kgid(ip->i_d.di_gid)))) {
  687. ip->i_d.di_mode &= ~S_ISGID;
  688. }
  689. ip->i_d.di_size = 0;
  690. ip->i_d.di_nextents = 0;
  691. ASSERT(ip->i_d.di_nblocks == 0);
  692. tv = current_fs_time(mp->m_super);
  693. ip->i_d.di_mtime.t_sec = (__int32_t)tv.tv_sec;
  694. ip->i_d.di_mtime.t_nsec = (__int32_t)tv.tv_nsec;
  695. ip->i_d.di_atime = ip->i_d.di_mtime;
  696. ip->i_d.di_ctime = ip->i_d.di_mtime;
  697. /*
  698. * di_gen will have been taken care of in xfs_iread.
  699. */
  700. ip->i_d.di_extsize = 0;
  701. ip->i_d.di_dmevmask = 0;
  702. ip->i_d.di_dmstate = 0;
  703. ip->i_d.di_flags = 0;
  704. if (ip->i_d.di_version == 3) {
  705. ASSERT(ip->i_d.di_ino == ino);
  706. ASSERT(uuid_equal(&ip->i_d.di_uuid, &mp->m_sb.sb_uuid));
  707. ip->i_d.di_crc = 0;
  708. ip->i_d.di_changecount = 1;
  709. ip->i_d.di_lsn = 0;
  710. ip->i_d.di_flags2 = 0;
  711. memset(&(ip->i_d.di_pad2[0]), 0, sizeof(ip->i_d.di_pad2));
  712. ip->i_d.di_crtime = ip->i_d.di_mtime;
  713. }
  714. flags = XFS_ILOG_CORE;
  715. switch (mode & S_IFMT) {
  716. case S_IFIFO:
  717. case S_IFCHR:
  718. case S_IFBLK:
  719. case S_IFSOCK:
  720. ip->i_d.di_format = XFS_DINODE_FMT_DEV;
  721. ip->i_df.if_u2.if_rdev = rdev;
  722. ip->i_df.if_flags = 0;
  723. flags |= XFS_ILOG_DEV;
  724. break;
  725. case S_IFREG:
  726. case S_IFDIR:
  727. if (pip && (pip->i_d.di_flags & XFS_DIFLAG_ANY)) {
  728. uint di_flags = 0;
  729. if (S_ISDIR(mode)) {
  730. if (pip->i_d.di_flags & XFS_DIFLAG_RTINHERIT)
  731. di_flags |= XFS_DIFLAG_RTINHERIT;
  732. if (pip->i_d.di_flags & XFS_DIFLAG_EXTSZINHERIT) {
  733. di_flags |= XFS_DIFLAG_EXTSZINHERIT;
  734. ip->i_d.di_extsize = pip->i_d.di_extsize;
  735. }
  736. if (pip->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
  737. di_flags |= XFS_DIFLAG_PROJINHERIT;
  738. } else if (S_ISREG(mode)) {
  739. if (pip->i_d.di_flags & XFS_DIFLAG_RTINHERIT)
  740. di_flags |= XFS_DIFLAG_REALTIME;
  741. if (pip->i_d.di_flags & XFS_DIFLAG_EXTSZINHERIT) {
  742. di_flags |= XFS_DIFLAG_EXTSIZE;
  743. ip->i_d.di_extsize = pip->i_d.di_extsize;
  744. }
  745. }
  746. if ((pip->i_d.di_flags & XFS_DIFLAG_NOATIME) &&
  747. xfs_inherit_noatime)
  748. di_flags |= XFS_DIFLAG_NOATIME;
  749. if ((pip->i_d.di_flags & XFS_DIFLAG_NODUMP) &&
  750. xfs_inherit_nodump)
  751. di_flags |= XFS_DIFLAG_NODUMP;
  752. if ((pip->i_d.di_flags & XFS_DIFLAG_SYNC) &&
  753. xfs_inherit_sync)
  754. di_flags |= XFS_DIFLAG_SYNC;
  755. if ((pip->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) &&
  756. xfs_inherit_nosymlinks)
  757. di_flags |= XFS_DIFLAG_NOSYMLINKS;
  758. if ((pip->i_d.di_flags & XFS_DIFLAG_NODEFRAG) &&
  759. xfs_inherit_nodefrag)
  760. di_flags |= XFS_DIFLAG_NODEFRAG;
  761. if (pip->i_d.di_flags & XFS_DIFLAG_FILESTREAM)
  762. di_flags |= XFS_DIFLAG_FILESTREAM;
  763. ip->i_d.di_flags |= di_flags;
  764. }
  765. /* FALLTHROUGH */
  766. case S_IFLNK:
  767. ip->i_d.di_format = XFS_DINODE_FMT_EXTENTS;
  768. ip->i_df.if_flags = XFS_IFEXTENTS;
  769. ip->i_df.if_bytes = ip->i_df.if_real_bytes = 0;
  770. ip->i_df.if_u1.if_extents = NULL;
  771. break;
  772. default:
  773. ASSERT(0);
  774. }
  775. /*
  776. * Attribute fork settings for new inode.
  777. */
  778. ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
  779. ip->i_d.di_anextents = 0;
  780. /*
  781. * Log the new values stuffed into the inode.
  782. */
  783. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  784. xfs_trans_log_inode(tp, ip, flags);
  785. /* now that we have an i_mode we can setup the inode structure */
  786. xfs_setup_inode(ip);
  787. *ipp = ip;
  788. return 0;
  789. }
  790. /*
  791. * Allocates a new inode from disk and return a pointer to the
  792. * incore copy. This routine will internally commit the current
  793. * transaction and allocate a new one if the Space Manager needed
  794. * to do an allocation to replenish the inode free-list.
  795. *
  796. * This routine is designed to be called from xfs_create and
  797. * xfs_create_dir.
  798. *
  799. */
  800. int
  801. xfs_dir_ialloc(
  802. xfs_trans_t **tpp, /* input: current transaction;
  803. output: may be a new transaction. */
  804. xfs_inode_t *dp, /* directory within whose allocate
  805. the inode. */
  806. umode_t mode,
  807. xfs_nlink_t nlink,
  808. xfs_dev_t rdev,
  809. prid_t prid, /* project id */
  810. int okalloc, /* ok to allocate new space */
  811. xfs_inode_t **ipp, /* pointer to inode; it will be
  812. locked. */
  813. int *committed)
  814. {
  815. xfs_trans_t *tp;
  816. xfs_inode_t *ip;
  817. xfs_buf_t *ialloc_context = NULL;
  818. int code;
  819. void *dqinfo;
  820. uint tflags;
  821. tp = *tpp;
  822. ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
  823. /*
  824. * xfs_ialloc will return a pointer to an incore inode if
  825. * the Space Manager has an available inode on the free
  826. * list. Otherwise, it will do an allocation and replenish
  827. * the freelist. Since we can only do one allocation per
  828. * transaction without deadlocks, we will need to commit the
  829. * current transaction and start a new one. We will then
  830. * need to call xfs_ialloc again to get the inode.
  831. *
  832. * If xfs_ialloc did an allocation to replenish the freelist,
  833. * it returns the bp containing the head of the freelist as
  834. * ialloc_context. We will hold a lock on it across the
  835. * transaction commit so that no other process can steal
  836. * the inode(s) that we've just allocated.
  837. */
  838. code = xfs_ialloc(tp, dp, mode, nlink, rdev, prid, okalloc,
  839. &ialloc_context, &ip);
  840. /*
  841. * Return an error if we were unable to allocate a new inode.
  842. * This should only happen if we run out of space on disk or
  843. * encounter a disk error.
  844. */
  845. if (code) {
  846. *ipp = NULL;
  847. return code;
  848. }
  849. if (!ialloc_context && !ip) {
  850. *ipp = NULL;
  851. return -ENOSPC;
  852. }
  853. /*
  854. * If the AGI buffer is non-NULL, then we were unable to get an
  855. * inode in one operation. We need to commit the current
  856. * transaction and call xfs_ialloc() again. It is guaranteed
  857. * to succeed the second time.
  858. */
  859. if (ialloc_context) {
  860. /*
  861. * Normally, xfs_trans_commit releases all the locks.
  862. * We call bhold to hang on to the ialloc_context across
  863. * the commit. Holding this buffer prevents any other
  864. * processes from doing any allocations in this
  865. * allocation group.
  866. */
  867. xfs_trans_bhold(tp, ialloc_context);
  868. /*
  869. * We want the quota changes to be associated with the next
  870. * transaction, NOT this one. So, detach the dqinfo from this
  871. * and attach it to the next transaction.
  872. */
  873. dqinfo = NULL;
  874. tflags = 0;
  875. if (tp->t_dqinfo) {
  876. dqinfo = (void *)tp->t_dqinfo;
  877. tp->t_dqinfo = NULL;
  878. tflags = tp->t_flags & XFS_TRANS_DQ_DIRTY;
  879. tp->t_flags &= ~(XFS_TRANS_DQ_DIRTY);
  880. }
  881. code = xfs_trans_roll(&tp, 0);
  882. if (committed != NULL)
  883. *committed = 1;
  884. /*
  885. * Re-attach the quota info that we detached from prev trx.
  886. */
  887. if (dqinfo) {
  888. tp->t_dqinfo = dqinfo;
  889. tp->t_flags |= tflags;
  890. }
  891. if (code) {
  892. xfs_buf_relse(ialloc_context);
  893. *tpp = tp;
  894. *ipp = NULL;
  895. return code;
  896. }
  897. xfs_trans_bjoin(tp, ialloc_context);
  898. /*
  899. * Call ialloc again. Since we've locked out all
  900. * other allocations in this allocation group,
  901. * this call should always succeed.
  902. */
  903. code = xfs_ialloc(tp, dp, mode, nlink, rdev, prid,
  904. okalloc, &ialloc_context, &ip);
  905. /*
  906. * If we get an error at this point, return to the caller
  907. * so that the current transaction can be aborted.
  908. */
  909. if (code) {
  910. *tpp = tp;
  911. *ipp = NULL;
  912. return code;
  913. }
  914. ASSERT(!ialloc_context && ip);
  915. } else {
  916. if (committed != NULL)
  917. *committed = 0;
  918. }
  919. *ipp = ip;
  920. *tpp = tp;
  921. return 0;
  922. }
  923. /*
  924. * Decrement the link count on an inode & log the change.
  925. * If this causes the link count to go to zero, initiate the
  926. * logging activity required to truncate a file.
  927. */
  928. int /* error */
  929. xfs_droplink(
  930. xfs_trans_t *tp,
  931. xfs_inode_t *ip)
  932. {
  933. int error;
  934. xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_CHG);
  935. ASSERT (ip->i_d.di_nlink > 0);
  936. ip->i_d.di_nlink--;
  937. drop_nlink(VFS_I(ip));
  938. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  939. error = 0;
  940. if (ip->i_d.di_nlink == 0) {
  941. /*
  942. * We're dropping the last link to this file.
  943. * Move the on-disk inode to the AGI unlinked list.
  944. * From xfs_inactive() we will pull the inode from
  945. * the list and free it.
  946. */
  947. error = xfs_iunlink(tp, ip);
  948. }
  949. return error;
  950. }
  951. /*
  952. * Increment the link count on an inode & log the change.
  953. */
  954. int
  955. xfs_bumplink(
  956. xfs_trans_t *tp,
  957. xfs_inode_t *ip)
  958. {
  959. xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_CHG);
  960. ASSERT(ip->i_d.di_version > 1);
  961. ASSERT(ip->i_d.di_nlink > 0 || (VFS_I(ip)->i_state & I_LINKABLE));
  962. ip->i_d.di_nlink++;
  963. inc_nlink(VFS_I(ip));
  964. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  965. return 0;
  966. }
  967. int
  968. xfs_create(
  969. xfs_inode_t *dp,
  970. struct xfs_name *name,
  971. umode_t mode,
  972. xfs_dev_t rdev,
  973. xfs_inode_t **ipp)
  974. {
  975. int is_dir = S_ISDIR(mode);
  976. struct xfs_mount *mp = dp->i_mount;
  977. struct xfs_inode *ip = NULL;
  978. struct xfs_trans *tp = NULL;
  979. int error;
  980. xfs_bmap_free_t free_list;
  981. xfs_fsblock_t first_block;
  982. bool unlock_dp_on_error = false;
  983. int committed;
  984. prid_t prid;
  985. struct xfs_dquot *udqp = NULL;
  986. struct xfs_dquot *gdqp = NULL;
  987. struct xfs_dquot *pdqp = NULL;
  988. struct xfs_trans_res *tres;
  989. uint resblks;
  990. trace_xfs_create(dp, name);
  991. if (XFS_FORCED_SHUTDOWN(mp))
  992. return -EIO;
  993. prid = xfs_get_initial_prid(dp);
  994. /*
  995. * Make sure that we have allocated dquot(s) on disk.
  996. */
  997. error = xfs_qm_vop_dqalloc(dp, xfs_kuid_to_uid(current_fsuid()),
  998. xfs_kgid_to_gid(current_fsgid()), prid,
  999. XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT,
  1000. &udqp, &gdqp, &pdqp);
  1001. if (error)
  1002. return error;
  1003. if (is_dir) {
  1004. rdev = 0;
  1005. resblks = XFS_MKDIR_SPACE_RES(mp, name->len);
  1006. tres = &M_RES(mp)->tr_mkdir;
  1007. tp = xfs_trans_alloc(mp, XFS_TRANS_MKDIR);
  1008. } else {
  1009. resblks = XFS_CREATE_SPACE_RES(mp, name->len);
  1010. tres = &M_RES(mp)->tr_create;
  1011. tp = xfs_trans_alloc(mp, XFS_TRANS_CREATE);
  1012. }
  1013. /*
  1014. * Initially assume that the file does not exist and
  1015. * reserve the resources for that case. If that is not
  1016. * the case we'll drop the one we have and get a more
  1017. * appropriate transaction later.
  1018. */
  1019. error = xfs_trans_reserve(tp, tres, resblks, 0);
  1020. if (error == -ENOSPC) {
  1021. /* flush outstanding delalloc blocks and retry */
  1022. xfs_flush_inodes(mp);
  1023. error = xfs_trans_reserve(tp, tres, resblks, 0);
  1024. }
  1025. if (error == -ENOSPC) {
  1026. /* No space at all so try a "no-allocation" reservation */
  1027. resblks = 0;
  1028. error = xfs_trans_reserve(tp, tres, 0, 0);
  1029. }
  1030. if (error)
  1031. goto out_trans_cancel;
  1032. xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
  1033. unlock_dp_on_error = true;
  1034. xfs_bmap_init(&free_list, &first_block);
  1035. /*
  1036. * Reserve disk quota and the inode.
  1037. */
  1038. error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp,
  1039. pdqp, resblks, 1, 0);
  1040. if (error)
  1041. goto out_trans_cancel;
  1042. if (!resblks) {
  1043. error = xfs_dir_canenter(tp, dp, name);
  1044. if (error)
  1045. goto out_trans_cancel;
  1046. }
  1047. /*
  1048. * A newly created regular or special file just has one directory
  1049. * entry pointing to them, but a directory also the "." entry
  1050. * pointing to itself.
  1051. */
  1052. error = xfs_dir_ialloc(&tp, dp, mode, is_dir ? 2 : 1, rdev,
  1053. prid, resblks > 0, &ip, &committed);
  1054. if (error) {
  1055. if (error == -ENOSPC)
  1056. goto out_trans_cancel;
  1057. goto out_trans_cancel;
  1058. }
  1059. /*
  1060. * Now we join the directory inode to the transaction. We do not do it
  1061. * earlier because xfs_dir_ialloc might commit the previous transaction
  1062. * (and release all the locks). An error from here on will result in
  1063. * the transaction cancel unlocking dp so don't do it explicitly in the
  1064. * error path.
  1065. */
  1066. xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
  1067. unlock_dp_on_error = false;
  1068. error = xfs_dir_createname(tp, dp, name, ip->i_ino,
  1069. &first_block, &free_list, resblks ?
  1070. resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
  1071. if (error) {
  1072. ASSERT(error != -ENOSPC);
  1073. goto out_trans_cancel;
  1074. }
  1075. xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  1076. xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
  1077. if (is_dir) {
  1078. error = xfs_dir_init(tp, ip, dp);
  1079. if (error)
  1080. goto out_bmap_cancel;
  1081. error = xfs_bumplink(tp, dp);
  1082. if (error)
  1083. goto out_bmap_cancel;
  1084. }
  1085. /*
  1086. * If this is a synchronous mount, make sure that the
  1087. * create transaction goes to disk before returning to
  1088. * the user.
  1089. */
  1090. if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
  1091. xfs_trans_set_sync(tp);
  1092. /*
  1093. * Attach the dquot(s) to the inodes and modify them incore.
  1094. * These ids of the inode couldn't have changed since the new
  1095. * inode has been locked ever since it was created.
  1096. */
  1097. xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp, pdqp);
  1098. error = xfs_bmap_finish(&tp, &free_list, &committed);
  1099. if (error)
  1100. goto out_bmap_cancel;
  1101. error = xfs_trans_commit(tp);
  1102. if (error)
  1103. goto out_release_inode;
  1104. xfs_qm_dqrele(udqp);
  1105. xfs_qm_dqrele(gdqp);
  1106. xfs_qm_dqrele(pdqp);
  1107. *ipp = ip;
  1108. return 0;
  1109. out_bmap_cancel:
  1110. xfs_bmap_cancel(&free_list);
  1111. out_trans_cancel:
  1112. xfs_trans_cancel(tp);
  1113. out_release_inode:
  1114. /*
  1115. * Wait until after the current transaction is aborted to finish the
  1116. * setup of the inode and release the inode. This prevents recursive
  1117. * transactions and deadlocks from xfs_inactive.
  1118. */
  1119. if (ip) {
  1120. xfs_finish_inode_setup(ip);
  1121. IRELE(ip);
  1122. }
  1123. xfs_qm_dqrele(udqp);
  1124. xfs_qm_dqrele(gdqp);
  1125. xfs_qm_dqrele(pdqp);
  1126. if (unlock_dp_on_error)
  1127. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  1128. return error;
  1129. }
  1130. int
  1131. xfs_create_tmpfile(
  1132. struct xfs_inode *dp,
  1133. struct dentry *dentry,
  1134. umode_t mode,
  1135. struct xfs_inode **ipp)
  1136. {
  1137. struct xfs_mount *mp = dp->i_mount;
  1138. struct xfs_inode *ip = NULL;
  1139. struct xfs_trans *tp = NULL;
  1140. int error;
  1141. prid_t prid;
  1142. struct xfs_dquot *udqp = NULL;
  1143. struct xfs_dquot *gdqp = NULL;
  1144. struct xfs_dquot *pdqp = NULL;
  1145. struct xfs_trans_res *tres;
  1146. uint resblks;
  1147. if (XFS_FORCED_SHUTDOWN(mp))
  1148. return -EIO;
  1149. prid = xfs_get_initial_prid(dp);
  1150. /*
  1151. * Make sure that we have allocated dquot(s) on disk.
  1152. */
  1153. error = xfs_qm_vop_dqalloc(dp, xfs_kuid_to_uid(current_fsuid()),
  1154. xfs_kgid_to_gid(current_fsgid()), prid,
  1155. XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT,
  1156. &udqp, &gdqp, &pdqp);
  1157. if (error)
  1158. return error;
  1159. resblks = XFS_IALLOC_SPACE_RES(mp);
  1160. tp = xfs_trans_alloc(mp, XFS_TRANS_CREATE_TMPFILE);
  1161. tres = &M_RES(mp)->tr_create_tmpfile;
  1162. error = xfs_trans_reserve(tp, tres, resblks, 0);
  1163. if (error == -ENOSPC) {
  1164. /* No space at all so try a "no-allocation" reservation */
  1165. resblks = 0;
  1166. error = xfs_trans_reserve(tp, tres, 0, 0);
  1167. }
  1168. if (error)
  1169. goto out_trans_cancel;
  1170. error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp,
  1171. pdqp, resblks, 1, 0);
  1172. if (error)
  1173. goto out_trans_cancel;
  1174. error = xfs_dir_ialloc(&tp, dp, mode, 1, 0,
  1175. prid, resblks > 0, &ip, NULL);
  1176. if (error) {
  1177. if (error == -ENOSPC)
  1178. goto out_trans_cancel;
  1179. goto out_trans_cancel;
  1180. }
  1181. if (mp->m_flags & XFS_MOUNT_WSYNC)
  1182. xfs_trans_set_sync(tp);
  1183. /*
  1184. * Attach the dquot(s) to the inodes and modify them incore.
  1185. * These ids of the inode couldn't have changed since the new
  1186. * inode has been locked ever since it was created.
  1187. */
  1188. xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp, pdqp);
  1189. ip->i_d.di_nlink--;
  1190. error = xfs_iunlink(tp, ip);
  1191. if (error)
  1192. goto out_trans_cancel;
  1193. error = xfs_trans_commit(tp);
  1194. if (error)
  1195. goto out_release_inode;
  1196. xfs_qm_dqrele(udqp);
  1197. xfs_qm_dqrele(gdqp);
  1198. xfs_qm_dqrele(pdqp);
  1199. *ipp = ip;
  1200. return 0;
  1201. out_trans_cancel:
  1202. xfs_trans_cancel(tp);
  1203. out_release_inode:
  1204. /*
  1205. * Wait until after the current transaction is aborted to finish the
  1206. * setup of the inode and release the inode. This prevents recursive
  1207. * transactions and deadlocks from xfs_inactive.
  1208. */
  1209. if (ip) {
  1210. xfs_finish_inode_setup(ip);
  1211. IRELE(ip);
  1212. }
  1213. xfs_qm_dqrele(udqp);
  1214. xfs_qm_dqrele(gdqp);
  1215. xfs_qm_dqrele(pdqp);
  1216. return error;
  1217. }
  1218. int
  1219. xfs_link(
  1220. xfs_inode_t *tdp,
  1221. xfs_inode_t *sip,
  1222. struct xfs_name *target_name)
  1223. {
  1224. xfs_mount_t *mp = tdp->i_mount;
  1225. xfs_trans_t *tp;
  1226. int error;
  1227. xfs_bmap_free_t free_list;
  1228. xfs_fsblock_t first_block;
  1229. int committed;
  1230. int resblks;
  1231. trace_xfs_link(tdp, target_name);
  1232. ASSERT(!S_ISDIR(sip->i_d.di_mode));
  1233. if (XFS_FORCED_SHUTDOWN(mp))
  1234. return -EIO;
  1235. error = xfs_qm_dqattach(sip, 0);
  1236. if (error)
  1237. goto std_return;
  1238. error = xfs_qm_dqattach(tdp, 0);
  1239. if (error)
  1240. goto std_return;
  1241. tp = xfs_trans_alloc(mp, XFS_TRANS_LINK);
  1242. resblks = XFS_LINK_SPACE_RES(mp, target_name->len);
  1243. error = xfs_trans_reserve(tp, &M_RES(mp)->tr_link, resblks, 0);
  1244. if (error == -ENOSPC) {
  1245. resblks = 0;
  1246. error = xfs_trans_reserve(tp, &M_RES(mp)->tr_link, 0, 0);
  1247. }
  1248. if (error)
  1249. goto error_return;
  1250. xfs_lock_two_inodes(sip, tdp, XFS_ILOCK_EXCL);
  1251. xfs_trans_ijoin(tp, sip, XFS_ILOCK_EXCL);
  1252. xfs_trans_ijoin(tp, tdp, XFS_ILOCK_EXCL);
  1253. /*
  1254. * If we are using project inheritance, we only allow hard link
  1255. * creation in our tree when the project IDs are the same; else
  1256. * the tree quota mechanism could be circumvented.
  1257. */
  1258. if (unlikely((tdp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
  1259. (xfs_get_projid(tdp) != xfs_get_projid(sip)))) {
  1260. error = -EXDEV;
  1261. goto error_return;
  1262. }
  1263. if (!resblks) {
  1264. error = xfs_dir_canenter(tp, tdp, target_name);
  1265. if (error)
  1266. goto error_return;
  1267. }
  1268. xfs_bmap_init(&free_list, &first_block);
  1269. if (sip->i_d.di_nlink == 0) {
  1270. error = xfs_iunlink_remove(tp, sip);
  1271. if (error)
  1272. goto error_return;
  1273. }
  1274. error = xfs_dir_createname(tp, tdp, target_name, sip->i_ino,
  1275. &first_block, &free_list, resblks);
  1276. if (error)
  1277. goto error_return;
  1278. xfs_trans_ichgtime(tp, tdp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  1279. xfs_trans_log_inode(tp, tdp, XFS_ILOG_CORE);
  1280. error = xfs_bumplink(tp, sip);
  1281. if (error)
  1282. goto error_return;
  1283. /*
  1284. * If this is a synchronous mount, make sure that the
  1285. * link transaction goes to disk before returning to
  1286. * the user.
  1287. */
  1288. if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
  1289. xfs_trans_set_sync(tp);
  1290. }
  1291. error = xfs_bmap_finish (&tp, &free_list, &committed);
  1292. if (error) {
  1293. xfs_bmap_cancel(&free_list);
  1294. goto error_return;
  1295. }
  1296. return xfs_trans_commit(tp);
  1297. error_return:
  1298. xfs_trans_cancel(tp);
  1299. std_return:
  1300. return error;
  1301. }
  1302. /*
  1303. * Free up the underlying blocks past new_size. The new size must be smaller
  1304. * than the current size. This routine can be used both for the attribute and
  1305. * data fork, and does not modify the inode size, which is left to the caller.
  1306. *
  1307. * The transaction passed to this routine must have made a permanent log
  1308. * reservation of at least XFS_ITRUNCATE_LOG_RES. This routine may commit the
  1309. * given transaction and start new ones, so make sure everything involved in
  1310. * the transaction is tidy before calling here. Some transaction will be
  1311. * returned to the caller to be committed. The incoming transaction must
  1312. * already include the inode, and both inode locks must be held exclusively.
  1313. * The inode must also be "held" within the transaction. On return the inode
  1314. * will be "held" within the returned transaction. This routine does NOT
  1315. * require any disk space to be reserved for it within the transaction.
  1316. *
  1317. * If we get an error, we must return with the inode locked and linked into the
  1318. * current transaction. This keeps things simple for the higher level code,
  1319. * because it always knows that the inode is locked and held in the transaction
  1320. * that returns to it whether errors occur or not. We don't mark the inode
  1321. * dirty on error so that transactions can be easily aborted if possible.
  1322. */
  1323. int
  1324. xfs_itruncate_extents(
  1325. struct xfs_trans **tpp,
  1326. struct xfs_inode *ip,
  1327. int whichfork,
  1328. xfs_fsize_t new_size)
  1329. {
  1330. struct xfs_mount *mp = ip->i_mount;
  1331. struct xfs_trans *tp = *tpp;
  1332. xfs_bmap_free_t free_list;
  1333. xfs_fsblock_t first_block;
  1334. xfs_fileoff_t first_unmap_block;
  1335. xfs_fileoff_t last_block;
  1336. xfs_filblks_t unmap_len;
  1337. int committed;
  1338. int error = 0;
  1339. int done = 0;
  1340. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  1341. ASSERT(!atomic_read(&VFS_I(ip)->i_count) ||
  1342. xfs_isilocked(ip, XFS_IOLOCK_EXCL));
  1343. ASSERT(new_size <= XFS_ISIZE(ip));
  1344. ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
  1345. ASSERT(ip->i_itemp != NULL);
  1346. ASSERT(ip->i_itemp->ili_lock_flags == 0);
  1347. ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
  1348. trace_xfs_itruncate_extents_start(ip, new_size);
  1349. /*
  1350. * Since it is possible for space to become allocated beyond
  1351. * the end of the file (in a crash where the space is allocated
  1352. * but the inode size is not yet updated), simply remove any
  1353. * blocks which show up between the new EOF and the maximum
  1354. * possible file size. If the first block to be removed is
  1355. * beyond the maximum file size (ie it is the same as last_block),
  1356. * then there is nothing to do.
  1357. */
  1358. first_unmap_block = XFS_B_TO_FSB(mp, (xfs_ufsize_t)new_size);
  1359. last_block = XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes);
  1360. if (first_unmap_block == last_block)
  1361. return 0;
  1362. ASSERT(first_unmap_block < last_block);
  1363. unmap_len = last_block - first_unmap_block + 1;
  1364. while (!done) {
  1365. xfs_bmap_init(&free_list, &first_block);
  1366. error = xfs_bunmapi(tp, ip,
  1367. first_unmap_block, unmap_len,
  1368. xfs_bmapi_aflag(whichfork),
  1369. XFS_ITRUNC_MAX_EXTENTS,
  1370. &first_block, &free_list,
  1371. &done);
  1372. if (error)
  1373. goto out_bmap_cancel;
  1374. /*
  1375. * Duplicate the transaction that has the permanent
  1376. * reservation and commit the old transaction.
  1377. */
  1378. error = xfs_bmap_finish(&tp, &free_list, &committed);
  1379. if (committed)
  1380. xfs_trans_ijoin(tp, ip, 0);
  1381. if (error)
  1382. goto out_bmap_cancel;
  1383. error = xfs_trans_roll(&tp, ip);
  1384. if (error)
  1385. goto out;
  1386. }
  1387. /*
  1388. * Always re-log the inode so that our permanent transaction can keep
  1389. * on rolling it forward in the log.
  1390. */
  1391. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  1392. trace_xfs_itruncate_extents_end(ip, new_size);
  1393. out:
  1394. *tpp = tp;
  1395. return error;
  1396. out_bmap_cancel:
  1397. /*
  1398. * If the bunmapi call encounters an error, return to the caller where
  1399. * the transaction can be properly aborted. We just need to make sure
  1400. * we're not holding any resources that we were not when we came in.
  1401. */
  1402. xfs_bmap_cancel(&free_list);
  1403. goto out;
  1404. }
  1405. int
  1406. xfs_release(
  1407. xfs_inode_t *ip)
  1408. {
  1409. xfs_mount_t *mp = ip->i_mount;
  1410. int error;
  1411. if (!S_ISREG(ip->i_d.di_mode) || (ip->i_d.di_mode == 0))
  1412. return 0;
  1413. /* If this is a read-only mount, don't do this (would generate I/O) */
  1414. if (mp->m_flags & XFS_MOUNT_RDONLY)
  1415. return 0;
  1416. if (!XFS_FORCED_SHUTDOWN(mp)) {
  1417. int truncated;
  1418. /*
  1419. * If we previously truncated this file and removed old data
  1420. * in the process, we want to initiate "early" writeout on
  1421. * the last close. This is an attempt to combat the notorious
  1422. * NULL files problem which is particularly noticeable from a
  1423. * truncate down, buffered (re-)write (delalloc), followed by
  1424. * a crash. What we are effectively doing here is
  1425. * significantly reducing the time window where we'd otherwise
  1426. * be exposed to that problem.
  1427. */
  1428. truncated = xfs_iflags_test_and_clear(ip, XFS_ITRUNCATED);
  1429. if (truncated) {
  1430. xfs_iflags_clear(ip, XFS_IDIRTY_RELEASE);
  1431. if (ip->i_delayed_blks > 0) {
  1432. error = filemap_flush(VFS_I(ip)->i_mapping);
  1433. if (error)
  1434. return error;
  1435. }
  1436. }
  1437. }
  1438. if (ip->i_d.di_nlink == 0)
  1439. return 0;
  1440. if (xfs_can_free_eofblocks(ip, false)) {
  1441. /*
  1442. * If we can't get the iolock just skip truncating the blocks
  1443. * past EOF because we could deadlock with the mmap_sem
  1444. * otherwise. We'll get another chance to drop them once the
  1445. * last reference to the inode is dropped, so we'll never leak
  1446. * blocks permanently.
  1447. *
  1448. * Further, check if the inode is being opened, written and
  1449. * closed frequently and we have delayed allocation blocks
  1450. * outstanding (e.g. streaming writes from the NFS server),
  1451. * truncating the blocks past EOF will cause fragmentation to
  1452. * occur.
  1453. *
  1454. * In this case don't do the truncation, either, but we have to
  1455. * be careful how we detect this case. Blocks beyond EOF show
  1456. * up as i_delayed_blks even when the inode is clean, so we
  1457. * need to truncate them away first before checking for a dirty
  1458. * release. Hence on the first dirty close we will still remove
  1459. * the speculative allocation, but after that we will leave it
  1460. * in place.
  1461. */
  1462. if (xfs_iflags_test(ip, XFS_IDIRTY_RELEASE))
  1463. return 0;
  1464. error = xfs_free_eofblocks(mp, ip, true);
  1465. if (error && error != -EAGAIN)
  1466. return error;
  1467. /* delalloc blocks after truncation means it really is dirty */
  1468. if (ip->i_delayed_blks)
  1469. xfs_iflags_set(ip, XFS_IDIRTY_RELEASE);
  1470. }
  1471. return 0;
  1472. }
  1473. /*
  1474. * xfs_inactive_truncate
  1475. *
  1476. * Called to perform a truncate when an inode becomes unlinked.
  1477. */
  1478. STATIC int
  1479. xfs_inactive_truncate(
  1480. struct xfs_inode *ip)
  1481. {
  1482. struct xfs_mount *mp = ip->i_mount;
  1483. struct xfs_trans *tp;
  1484. int error;
  1485. tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
  1486. error = xfs_trans_reserve(tp, &M_RES(mp)->tr_itruncate, 0, 0);
  1487. if (error) {
  1488. ASSERT(XFS_FORCED_SHUTDOWN(mp));
  1489. xfs_trans_cancel(tp);
  1490. return error;
  1491. }
  1492. xfs_ilock(ip, XFS_ILOCK_EXCL);
  1493. xfs_trans_ijoin(tp, ip, 0);
  1494. /*
  1495. * Log the inode size first to prevent stale data exposure in the event
  1496. * of a system crash before the truncate completes. See the related
  1497. * comment in xfs_setattr_size() for details.
  1498. */
  1499. ip->i_d.di_size = 0;
  1500. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  1501. error = xfs_itruncate_extents(&tp, ip, XFS_DATA_FORK, 0);
  1502. if (error)
  1503. goto error_trans_cancel;
  1504. ASSERT(ip->i_d.di_nextents == 0);
  1505. error = xfs_trans_commit(tp);
  1506. if (error)
  1507. goto error_unlock;
  1508. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1509. return 0;
  1510. error_trans_cancel:
  1511. xfs_trans_cancel(tp);
  1512. error_unlock:
  1513. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1514. return error;
  1515. }
  1516. /*
  1517. * xfs_inactive_ifree()
  1518. *
  1519. * Perform the inode free when an inode is unlinked.
  1520. */
  1521. STATIC int
  1522. xfs_inactive_ifree(
  1523. struct xfs_inode *ip)
  1524. {
  1525. xfs_bmap_free_t free_list;
  1526. xfs_fsblock_t first_block;
  1527. int committed;
  1528. struct xfs_mount *mp = ip->i_mount;
  1529. struct xfs_trans *tp;
  1530. int error;
  1531. tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
  1532. /*
  1533. * The ifree transaction might need to allocate blocks for record
  1534. * insertion to the finobt. We don't want to fail here at ENOSPC, so
  1535. * allow ifree to dip into the reserved block pool if necessary.
  1536. *
  1537. * Freeing large sets of inodes generally means freeing inode chunks,
  1538. * directory and file data blocks, so this should be relatively safe.
  1539. * Only under severe circumstances should it be possible to free enough
  1540. * inodes to exhaust the reserve block pool via finobt expansion while
  1541. * at the same time not creating free space in the filesystem.
  1542. *
  1543. * Send a warning if the reservation does happen to fail, as the inode
  1544. * now remains allocated and sits on the unlinked list until the fs is
  1545. * repaired.
  1546. */
  1547. tp->t_flags |= XFS_TRANS_RESERVE;
  1548. error = xfs_trans_reserve(tp, &M_RES(mp)->tr_ifree,
  1549. XFS_IFREE_SPACE_RES(mp), 0);
  1550. if (error) {
  1551. if (error == -ENOSPC) {
  1552. xfs_warn_ratelimited(mp,
  1553. "Failed to remove inode(s) from unlinked list. "
  1554. "Please free space, unmount and run xfs_repair.");
  1555. } else {
  1556. ASSERT(XFS_FORCED_SHUTDOWN(mp));
  1557. }
  1558. xfs_trans_cancel(tp);
  1559. return error;
  1560. }
  1561. xfs_ilock(ip, XFS_ILOCK_EXCL);
  1562. xfs_trans_ijoin(tp, ip, 0);
  1563. xfs_bmap_init(&free_list, &first_block);
  1564. error = xfs_ifree(tp, ip, &free_list);
  1565. if (error) {
  1566. /*
  1567. * If we fail to free the inode, shut down. The cancel
  1568. * might do that, we need to make sure. Otherwise the
  1569. * inode might be lost for a long time or forever.
  1570. */
  1571. if (!XFS_FORCED_SHUTDOWN(mp)) {
  1572. xfs_notice(mp, "%s: xfs_ifree returned error %d",
  1573. __func__, error);
  1574. xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
  1575. }
  1576. xfs_trans_cancel(tp);
  1577. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1578. return error;
  1579. }
  1580. /*
  1581. * Credit the quota account(s). The inode is gone.
  1582. */
  1583. xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_ICOUNT, -1);
  1584. /*
  1585. * Just ignore errors at this point. There is nothing we can
  1586. * do except to try to keep going. Make sure it's not a silent
  1587. * error.
  1588. */
  1589. error = xfs_bmap_finish(&tp, &free_list, &committed);
  1590. if (error)
  1591. xfs_notice(mp, "%s: xfs_bmap_finish returned error %d",
  1592. __func__, error);
  1593. error = xfs_trans_commit(tp);
  1594. if (error)
  1595. xfs_notice(mp, "%s: xfs_trans_commit returned error %d",
  1596. __func__, error);
  1597. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1598. return 0;
  1599. }
  1600. /*
  1601. * xfs_inactive
  1602. *
  1603. * This is called when the vnode reference count for the vnode
  1604. * goes to zero. If the file has been unlinked, then it must
  1605. * now be truncated. Also, we clear all of the read-ahead state
  1606. * kept for the inode here since the file is now closed.
  1607. */
  1608. void
  1609. xfs_inactive(
  1610. xfs_inode_t *ip)
  1611. {
  1612. struct xfs_mount *mp;
  1613. int error;
  1614. int truncate = 0;
  1615. /*
  1616. * If the inode is already free, then there can be nothing
  1617. * to clean up here.
  1618. */
  1619. if (ip->i_d.di_mode == 0) {
  1620. ASSERT(ip->i_df.if_real_bytes == 0);
  1621. ASSERT(ip->i_df.if_broot_bytes == 0);
  1622. return;
  1623. }
  1624. mp = ip->i_mount;
  1625. /* If this is a read-only mount, don't do this (would generate I/O) */
  1626. if (mp->m_flags & XFS_MOUNT_RDONLY)
  1627. return;
  1628. if (ip->i_d.di_nlink != 0) {
  1629. /*
  1630. * force is true because we are evicting an inode from the
  1631. * cache. Post-eof blocks must be freed, lest we end up with
  1632. * broken free space accounting.
  1633. */
  1634. if (xfs_can_free_eofblocks(ip, true))
  1635. xfs_free_eofblocks(mp, ip, false);
  1636. return;
  1637. }
  1638. if (S_ISREG(ip->i_d.di_mode) &&
  1639. (ip->i_d.di_size != 0 || XFS_ISIZE(ip) != 0 ||
  1640. ip->i_d.di_nextents > 0 || ip->i_delayed_blks > 0))
  1641. truncate = 1;
  1642. error = xfs_qm_dqattach(ip, 0);
  1643. if (error)
  1644. return;
  1645. if (S_ISLNK(ip->i_d.di_mode))
  1646. error = xfs_inactive_symlink(ip);
  1647. else if (truncate)
  1648. error = xfs_inactive_truncate(ip);
  1649. if (error)
  1650. return;
  1651. /*
  1652. * If there are attributes associated with the file then blow them away
  1653. * now. The code calls a routine that recursively deconstructs the
  1654. * attribute fork. If also blows away the in-core attribute fork.
  1655. */
  1656. if (XFS_IFORK_Q(ip)) {
  1657. error = xfs_attr_inactive(ip);
  1658. if (error)
  1659. return;
  1660. }
  1661. ASSERT(!ip->i_afp);
  1662. ASSERT(ip->i_d.di_anextents == 0);
  1663. ASSERT(ip->i_d.di_forkoff == 0);
  1664. /*
  1665. * Free the inode.
  1666. */
  1667. error = xfs_inactive_ifree(ip);
  1668. if (error)
  1669. return;
  1670. /*
  1671. * Release the dquots held by inode, if any.
  1672. */
  1673. xfs_qm_dqdetach(ip);
  1674. }
  1675. /*
  1676. * This is called when the inode's link count goes to 0.
  1677. * We place the on-disk inode on a list in the AGI. It
  1678. * will be pulled from this list when the inode is freed.
  1679. */
  1680. int
  1681. xfs_iunlink(
  1682. xfs_trans_t *tp,
  1683. xfs_inode_t *ip)
  1684. {
  1685. xfs_mount_t *mp;
  1686. xfs_agi_t *agi;
  1687. xfs_dinode_t *dip;
  1688. xfs_buf_t *agibp;
  1689. xfs_buf_t *ibp;
  1690. xfs_agino_t agino;
  1691. short bucket_index;
  1692. int offset;
  1693. int error;
  1694. ASSERT(ip->i_d.di_nlink == 0);
  1695. ASSERT(ip->i_d.di_mode != 0);
  1696. mp = tp->t_mountp;
  1697. /*
  1698. * Get the agi buffer first. It ensures lock ordering
  1699. * on the list.
  1700. */
  1701. error = xfs_read_agi(mp, tp, XFS_INO_TO_AGNO(mp, ip->i_ino), &agibp);
  1702. if (error)
  1703. return error;
  1704. agi = XFS_BUF_TO_AGI(agibp);
  1705. /*
  1706. * Get the index into the agi hash table for the
  1707. * list this inode will go on.
  1708. */
  1709. agino = XFS_INO_TO_AGINO(mp, ip->i_ino);
  1710. ASSERT(agino != 0);
  1711. bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS;
  1712. ASSERT(agi->agi_unlinked[bucket_index]);
  1713. ASSERT(be32_to_cpu(agi->agi_unlinked[bucket_index]) != agino);
  1714. if (agi->agi_unlinked[bucket_index] != cpu_to_be32(NULLAGINO)) {
  1715. /*
  1716. * There is already another inode in the bucket we need
  1717. * to add ourselves to. Add us at the front of the list.
  1718. * Here we put the head pointer into our next pointer,
  1719. * and then we fall through to point the head at us.
  1720. */
  1721. error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp,
  1722. 0, 0);
  1723. if (error)
  1724. return error;
  1725. ASSERT(dip->di_next_unlinked == cpu_to_be32(NULLAGINO));
  1726. dip->di_next_unlinked = agi->agi_unlinked[bucket_index];
  1727. offset = ip->i_imap.im_boffset +
  1728. offsetof(xfs_dinode_t, di_next_unlinked);
  1729. /* need to recalc the inode CRC if appropriate */
  1730. xfs_dinode_calc_crc(mp, dip);
  1731. xfs_trans_inode_buf(tp, ibp);
  1732. xfs_trans_log_buf(tp, ibp, offset,
  1733. (offset + sizeof(xfs_agino_t) - 1));
  1734. xfs_inobp_check(mp, ibp);
  1735. }
  1736. /*
  1737. * Point the bucket head pointer at the inode being inserted.
  1738. */
  1739. ASSERT(agino != 0);
  1740. agi->agi_unlinked[bucket_index] = cpu_to_be32(agino);
  1741. offset = offsetof(xfs_agi_t, agi_unlinked) +
  1742. (sizeof(xfs_agino_t) * bucket_index);
  1743. xfs_trans_buf_set_type(tp, agibp, XFS_BLFT_AGI_BUF);
  1744. xfs_trans_log_buf(tp, agibp, offset,
  1745. (offset + sizeof(xfs_agino_t) - 1));
  1746. return 0;
  1747. }
  1748. /*
  1749. * Pull the on-disk inode from the AGI unlinked list.
  1750. */
  1751. STATIC int
  1752. xfs_iunlink_remove(
  1753. xfs_trans_t *tp,
  1754. xfs_inode_t *ip)
  1755. {
  1756. xfs_ino_t next_ino;
  1757. xfs_mount_t *mp;
  1758. xfs_agi_t *agi;
  1759. xfs_dinode_t *dip;
  1760. xfs_buf_t *agibp;
  1761. xfs_buf_t *ibp;
  1762. xfs_agnumber_t agno;
  1763. xfs_agino_t agino;
  1764. xfs_agino_t next_agino;
  1765. xfs_buf_t *last_ibp;
  1766. xfs_dinode_t *last_dip = NULL;
  1767. short bucket_index;
  1768. int offset, last_offset = 0;
  1769. int error;
  1770. mp = tp->t_mountp;
  1771. agno = XFS_INO_TO_AGNO(mp, ip->i_ino);
  1772. /*
  1773. * Get the agi buffer first. It ensures lock ordering
  1774. * on the list.
  1775. */
  1776. error = xfs_read_agi(mp, tp, agno, &agibp);
  1777. if (error)
  1778. return error;
  1779. agi = XFS_BUF_TO_AGI(agibp);
  1780. /*
  1781. * Get the index into the agi hash table for the
  1782. * list this inode will go on.
  1783. */
  1784. agino = XFS_INO_TO_AGINO(mp, ip->i_ino);
  1785. ASSERT(agino != 0);
  1786. bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS;
  1787. ASSERT(agi->agi_unlinked[bucket_index] != cpu_to_be32(NULLAGINO));
  1788. ASSERT(agi->agi_unlinked[bucket_index]);
  1789. if (be32_to_cpu(agi->agi_unlinked[bucket_index]) == agino) {
  1790. /*
  1791. * We're at the head of the list. Get the inode's on-disk
  1792. * buffer to see if there is anyone after us on the list.
  1793. * Only modify our next pointer if it is not already NULLAGINO.
  1794. * This saves us the overhead of dealing with the buffer when
  1795. * there is no need to change it.
  1796. */
  1797. error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp,
  1798. 0, 0);
  1799. if (error) {
  1800. xfs_warn(mp, "%s: xfs_imap_to_bp returned error %d.",
  1801. __func__, error);
  1802. return error;
  1803. }
  1804. next_agino = be32_to_cpu(dip->di_next_unlinked);
  1805. ASSERT(next_agino != 0);
  1806. if (next_agino != NULLAGINO) {
  1807. dip->di_next_unlinked = cpu_to_be32(NULLAGINO);
  1808. offset = ip->i_imap.im_boffset +
  1809. offsetof(xfs_dinode_t, di_next_unlinked);
  1810. /* need to recalc the inode CRC if appropriate */
  1811. xfs_dinode_calc_crc(mp, dip);
  1812. xfs_trans_inode_buf(tp, ibp);
  1813. xfs_trans_log_buf(tp, ibp, offset,
  1814. (offset + sizeof(xfs_agino_t) - 1));
  1815. xfs_inobp_check(mp, ibp);
  1816. } else {
  1817. xfs_trans_brelse(tp, ibp);
  1818. }
  1819. /*
  1820. * Point the bucket head pointer at the next inode.
  1821. */
  1822. ASSERT(next_agino != 0);
  1823. ASSERT(next_agino != agino);
  1824. agi->agi_unlinked[bucket_index] = cpu_to_be32(next_agino);
  1825. offset = offsetof(xfs_agi_t, agi_unlinked) +
  1826. (sizeof(xfs_agino_t) * bucket_index);
  1827. xfs_trans_buf_set_type(tp, agibp, XFS_BLFT_AGI_BUF);
  1828. xfs_trans_log_buf(tp, agibp, offset,
  1829. (offset + sizeof(xfs_agino_t) - 1));
  1830. } else {
  1831. /*
  1832. * We need to search the list for the inode being freed.
  1833. */
  1834. next_agino = be32_to_cpu(agi->agi_unlinked[bucket_index]);
  1835. last_ibp = NULL;
  1836. while (next_agino != agino) {
  1837. struct xfs_imap imap;
  1838. if (last_ibp)
  1839. xfs_trans_brelse(tp, last_ibp);
  1840. imap.im_blkno = 0;
  1841. next_ino = XFS_AGINO_TO_INO(mp, agno, next_agino);
  1842. error = xfs_imap(mp, tp, next_ino, &imap, 0);
  1843. if (error) {
  1844. xfs_warn(mp,
  1845. "%s: xfs_imap returned error %d.",
  1846. __func__, error);
  1847. return error;
  1848. }
  1849. error = xfs_imap_to_bp(mp, tp, &imap, &last_dip,
  1850. &last_ibp, 0, 0);
  1851. if (error) {
  1852. xfs_warn(mp,
  1853. "%s: xfs_imap_to_bp returned error %d.",
  1854. __func__, error);
  1855. return error;
  1856. }
  1857. last_offset = imap.im_boffset;
  1858. next_agino = be32_to_cpu(last_dip->di_next_unlinked);
  1859. ASSERT(next_agino != NULLAGINO);
  1860. ASSERT(next_agino != 0);
  1861. }
  1862. /*
  1863. * Now last_ibp points to the buffer previous to us on the
  1864. * unlinked list. Pull us from the list.
  1865. */
  1866. error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp,
  1867. 0, 0);
  1868. if (error) {
  1869. xfs_warn(mp, "%s: xfs_imap_to_bp(2) returned error %d.",
  1870. __func__, error);
  1871. return error;
  1872. }
  1873. next_agino = be32_to_cpu(dip->di_next_unlinked);
  1874. ASSERT(next_agino != 0);
  1875. ASSERT(next_agino != agino);
  1876. if (next_agino != NULLAGINO) {
  1877. dip->di_next_unlinked = cpu_to_be32(NULLAGINO);
  1878. offset = ip->i_imap.im_boffset +
  1879. offsetof(xfs_dinode_t, di_next_unlinked);
  1880. /* need to recalc the inode CRC if appropriate */
  1881. xfs_dinode_calc_crc(mp, dip);
  1882. xfs_trans_inode_buf(tp, ibp);
  1883. xfs_trans_log_buf(tp, ibp, offset,
  1884. (offset + sizeof(xfs_agino_t) - 1));
  1885. xfs_inobp_check(mp, ibp);
  1886. } else {
  1887. xfs_trans_brelse(tp, ibp);
  1888. }
  1889. /*
  1890. * Point the previous inode on the list to the next inode.
  1891. */
  1892. last_dip->di_next_unlinked = cpu_to_be32(next_agino);
  1893. ASSERT(next_agino != 0);
  1894. offset = last_offset + offsetof(xfs_dinode_t, di_next_unlinked);
  1895. /* need to recalc the inode CRC if appropriate */
  1896. xfs_dinode_calc_crc(mp, last_dip);
  1897. xfs_trans_inode_buf(tp, last_ibp);
  1898. xfs_trans_log_buf(tp, last_ibp, offset,
  1899. (offset + sizeof(xfs_agino_t) - 1));
  1900. xfs_inobp_check(mp, last_ibp);
  1901. }
  1902. return 0;
  1903. }
  1904. /*
  1905. * A big issue when freeing the inode cluster is that we _cannot_ skip any
  1906. * inodes that are in memory - they all must be marked stale and attached to
  1907. * the cluster buffer.
  1908. */
  1909. STATIC int
  1910. xfs_ifree_cluster(
  1911. xfs_inode_t *free_ip,
  1912. xfs_trans_t *tp,
  1913. struct xfs_icluster *xic)
  1914. {
  1915. xfs_mount_t *mp = free_ip->i_mount;
  1916. int blks_per_cluster;
  1917. int inodes_per_cluster;
  1918. int nbufs;
  1919. int i, j;
  1920. int ioffset;
  1921. xfs_daddr_t blkno;
  1922. xfs_buf_t *bp;
  1923. xfs_inode_t *ip;
  1924. xfs_inode_log_item_t *iip;
  1925. xfs_log_item_t *lip;
  1926. struct xfs_perag *pag;
  1927. xfs_ino_t inum;
  1928. inum = xic->first_ino;
  1929. pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, inum));
  1930. blks_per_cluster = xfs_icluster_size_fsb(mp);
  1931. inodes_per_cluster = blks_per_cluster << mp->m_sb.sb_inopblog;
  1932. nbufs = mp->m_ialloc_blks / blks_per_cluster;
  1933. for (j = 0; j < nbufs; j++, inum += inodes_per_cluster) {
  1934. /*
  1935. * The allocation bitmap tells us which inodes of the chunk were
  1936. * physically allocated. Skip the cluster if an inode falls into
  1937. * a sparse region.
  1938. */
  1939. ioffset = inum - xic->first_ino;
  1940. if ((xic->alloc & XFS_INOBT_MASK(ioffset)) == 0) {
  1941. ASSERT(do_mod(ioffset, inodes_per_cluster) == 0);
  1942. continue;
  1943. }
  1944. blkno = XFS_AGB_TO_DADDR(mp, XFS_INO_TO_AGNO(mp, inum),
  1945. XFS_INO_TO_AGBNO(mp, inum));
  1946. /*
  1947. * We obtain and lock the backing buffer first in the process
  1948. * here, as we have to ensure that any dirty inode that we
  1949. * can't get the flush lock on is attached to the buffer.
  1950. * If we scan the in-memory inodes first, then buffer IO can
  1951. * complete before we get a lock on it, and hence we may fail
  1952. * to mark all the active inodes on the buffer stale.
  1953. */
  1954. bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, blkno,
  1955. mp->m_bsize * blks_per_cluster,
  1956. XBF_UNMAPPED);
  1957. if (!bp)
  1958. return -ENOMEM;
  1959. /*
  1960. * This buffer may not have been correctly initialised as we
  1961. * didn't read it from disk. That's not important because we are
  1962. * only using to mark the buffer as stale in the log, and to
  1963. * attach stale cached inodes on it. That means it will never be
  1964. * dispatched for IO. If it is, we want to know about it, and we
  1965. * want it to fail. We can acheive this by adding a write
  1966. * verifier to the buffer.
  1967. */
  1968. bp->b_ops = &xfs_inode_buf_ops;
  1969. /*
  1970. * Walk the inodes already attached to the buffer and mark them
  1971. * stale. These will all have the flush locks held, so an
  1972. * in-memory inode walk can't lock them. By marking them all
  1973. * stale first, we will not attempt to lock them in the loop
  1974. * below as the XFS_ISTALE flag will be set.
  1975. */
  1976. lip = bp->b_fspriv;
  1977. while (lip) {
  1978. if (lip->li_type == XFS_LI_INODE) {
  1979. iip = (xfs_inode_log_item_t *)lip;
  1980. ASSERT(iip->ili_logged == 1);
  1981. lip->li_cb = xfs_istale_done;
  1982. xfs_trans_ail_copy_lsn(mp->m_ail,
  1983. &iip->ili_flush_lsn,
  1984. &iip->ili_item.li_lsn);
  1985. xfs_iflags_set(iip->ili_inode, XFS_ISTALE);
  1986. }
  1987. lip = lip->li_bio_list;
  1988. }
  1989. /*
  1990. * For each inode in memory attempt to add it to the inode
  1991. * buffer and set it up for being staled on buffer IO
  1992. * completion. This is safe as we've locked out tail pushing
  1993. * and flushing by locking the buffer.
  1994. *
  1995. * We have already marked every inode that was part of a
  1996. * transaction stale above, which means there is no point in
  1997. * even trying to lock them.
  1998. */
  1999. for (i = 0; i < inodes_per_cluster; i++) {
  2000. retry:
  2001. rcu_read_lock();
  2002. ip = radix_tree_lookup(&pag->pag_ici_root,
  2003. XFS_INO_TO_AGINO(mp, (inum + i)));
  2004. /* Inode not in memory, nothing to do */
  2005. if (!ip) {
  2006. rcu_read_unlock();
  2007. continue;
  2008. }
  2009. /*
  2010. * because this is an RCU protected lookup, we could
  2011. * find a recently freed or even reallocated inode
  2012. * during the lookup. We need to check under the
  2013. * i_flags_lock for a valid inode here. Skip it if it
  2014. * is not valid, the wrong inode or stale.
  2015. */
  2016. spin_lock(&ip->i_flags_lock);
  2017. if (ip->i_ino != inum + i ||
  2018. __xfs_iflags_test(ip, XFS_ISTALE)) {
  2019. spin_unlock(&ip->i_flags_lock);
  2020. rcu_read_unlock();
  2021. continue;
  2022. }
  2023. spin_unlock(&ip->i_flags_lock);
  2024. /*
  2025. * Don't try to lock/unlock the current inode, but we
  2026. * _cannot_ skip the other inodes that we did not find
  2027. * in the list attached to the buffer and are not
  2028. * already marked stale. If we can't lock it, back off
  2029. * and retry.
  2030. */
  2031. if (ip != free_ip &&
  2032. !xfs_ilock_nowait(ip, XFS_ILOCK_EXCL)) {
  2033. rcu_read_unlock();
  2034. delay(1);
  2035. goto retry;
  2036. }
  2037. rcu_read_unlock();
  2038. xfs_iflock(ip);
  2039. xfs_iflags_set(ip, XFS_ISTALE);
  2040. /*
  2041. * we don't need to attach clean inodes or those only
  2042. * with unlogged changes (which we throw away, anyway).
  2043. */
  2044. iip = ip->i_itemp;
  2045. if (!iip || xfs_inode_clean(ip)) {
  2046. ASSERT(ip != free_ip);
  2047. xfs_ifunlock(ip);
  2048. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  2049. continue;
  2050. }
  2051. iip->ili_last_fields = iip->ili_fields;
  2052. iip->ili_fields = 0;
  2053. iip->ili_logged = 1;
  2054. xfs_trans_ail_copy_lsn(mp->m_ail, &iip->ili_flush_lsn,
  2055. &iip->ili_item.li_lsn);
  2056. xfs_buf_attach_iodone(bp, xfs_istale_done,
  2057. &iip->ili_item);
  2058. if (ip != free_ip)
  2059. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  2060. }
  2061. xfs_trans_stale_inode_buf(tp, bp);
  2062. xfs_trans_binval(tp, bp);
  2063. }
  2064. xfs_perag_put(pag);
  2065. return 0;
  2066. }
  2067. /*
  2068. * This is called to return an inode to the inode free list.
  2069. * The inode should already be truncated to 0 length and have
  2070. * no pages associated with it. This routine also assumes that
  2071. * the inode is already a part of the transaction.
  2072. *
  2073. * The on-disk copy of the inode will have been added to the list
  2074. * of unlinked inodes in the AGI. We need to remove the inode from
  2075. * that list atomically with respect to freeing it here.
  2076. */
  2077. int
  2078. xfs_ifree(
  2079. xfs_trans_t *tp,
  2080. xfs_inode_t *ip,
  2081. xfs_bmap_free_t *flist)
  2082. {
  2083. int error;
  2084. struct xfs_icluster xic = { 0 };
  2085. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  2086. ASSERT(ip->i_d.di_nlink == 0);
  2087. ASSERT(ip->i_d.di_nextents == 0);
  2088. ASSERT(ip->i_d.di_anextents == 0);
  2089. ASSERT(ip->i_d.di_size == 0 || !S_ISREG(ip->i_d.di_mode));
  2090. ASSERT(ip->i_d.di_nblocks == 0);
  2091. /*
  2092. * Pull the on-disk inode from the AGI unlinked list.
  2093. */
  2094. error = xfs_iunlink_remove(tp, ip);
  2095. if (error)
  2096. return error;
  2097. error = xfs_difree(tp, ip->i_ino, flist, &xic);
  2098. if (error)
  2099. return error;
  2100. ip->i_d.di_mode = 0; /* mark incore inode as free */
  2101. ip->i_d.di_flags = 0;
  2102. ip->i_d.di_dmevmask = 0;
  2103. ip->i_d.di_forkoff = 0; /* mark the attr fork not in use */
  2104. ip->i_d.di_format = XFS_DINODE_FMT_EXTENTS;
  2105. ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
  2106. /*
  2107. * Bump the generation count so no one will be confused
  2108. * by reincarnations of this inode.
  2109. */
  2110. ip->i_d.di_gen++;
  2111. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  2112. if (xic.deleted)
  2113. error = xfs_ifree_cluster(ip, tp, &xic);
  2114. return error;
  2115. }
  2116. /*
  2117. * This is called to unpin an inode. The caller must have the inode locked
  2118. * in at least shared mode so that the buffer cannot be subsequently pinned
  2119. * once someone is waiting for it to be unpinned.
  2120. */
  2121. static void
  2122. xfs_iunpin(
  2123. struct xfs_inode *ip)
  2124. {
  2125. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
  2126. trace_xfs_inode_unpin_nowait(ip, _RET_IP_);
  2127. /* Give the log a push to start the unpinning I/O */
  2128. xfs_log_force_lsn(ip->i_mount, ip->i_itemp->ili_last_lsn, 0);
  2129. }
  2130. static void
  2131. __xfs_iunpin_wait(
  2132. struct xfs_inode *ip)
  2133. {
  2134. wait_queue_head_t *wq = bit_waitqueue(&ip->i_flags, __XFS_IPINNED_BIT);
  2135. DEFINE_WAIT_BIT(wait, &ip->i_flags, __XFS_IPINNED_BIT);
  2136. xfs_iunpin(ip);
  2137. do {
  2138. prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
  2139. if (xfs_ipincount(ip))
  2140. io_schedule();
  2141. } while (xfs_ipincount(ip));
  2142. finish_wait(wq, &wait.wait);
  2143. }
  2144. void
  2145. xfs_iunpin_wait(
  2146. struct xfs_inode *ip)
  2147. {
  2148. if (xfs_ipincount(ip))
  2149. __xfs_iunpin_wait(ip);
  2150. }
  2151. /*
  2152. * Removing an inode from the namespace involves removing the directory entry
  2153. * and dropping the link count on the inode. Removing the directory entry can
  2154. * result in locking an AGF (directory blocks were freed) and removing a link
  2155. * count can result in placing the inode on an unlinked list which results in
  2156. * locking an AGI.
  2157. *
  2158. * The big problem here is that we have an ordering constraint on AGF and AGI
  2159. * locking - inode allocation locks the AGI, then can allocate a new extent for
  2160. * new inodes, locking the AGF after the AGI. Similarly, freeing the inode
  2161. * removes the inode from the unlinked list, requiring that we lock the AGI
  2162. * first, and then freeing the inode can result in an inode chunk being freed
  2163. * and hence freeing disk space requiring that we lock an AGF.
  2164. *
  2165. * Hence the ordering that is imposed by other parts of the code is AGI before
  2166. * AGF. This means we cannot remove the directory entry before we drop the inode
  2167. * reference count and put it on the unlinked list as this results in a lock
  2168. * order of AGF then AGI, and this can deadlock against inode allocation and
  2169. * freeing. Therefore we must drop the link counts before we remove the
  2170. * directory entry.
  2171. *
  2172. * This is still safe from a transactional point of view - it is not until we
  2173. * get to xfs_bmap_finish() that we have the possibility of multiple
  2174. * transactions in this operation. Hence as long as we remove the directory
  2175. * entry and drop the link count in the first transaction of the remove
  2176. * operation, there are no transactional constraints on the ordering here.
  2177. */
  2178. int
  2179. xfs_remove(
  2180. xfs_inode_t *dp,
  2181. struct xfs_name *name,
  2182. xfs_inode_t *ip)
  2183. {
  2184. xfs_mount_t *mp = dp->i_mount;
  2185. xfs_trans_t *tp = NULL;
  2186. int is_dir = S_ISDIR(ip->i_d.di_mode);
  2187. int error = 0;
  2188. xfs_bmap_free_t free_list;
  2189. xfs_fsblock_t first_block;
  2190. int committed;
  2191. uint resblks;
  2192. trace_xfs_remove(dp, name);
  2193. if (XFS_FORCED_SHUTDOWN(mp))
  2194. return -EIO;
  2195. error = xfs_qm_dqattach(dp, 0);
  2196. if (error)
  2197. goto std_return;
  2198. error = xfs_qm_dqattach(ip, 0);
  2199. if (error)
  2200. goto std_return;
  2201. if (is_dir)
  2202. tp = xfs_trans_alloc(mp, XFS_TRANS_RMDIR);
  2203. else
  2204. tp = xfs_trans_alloc(mp, XFS_TRANS_REMOVE);
  2205. /*
  2206. * We try to get the real space reservation first,
  2207. * allowing for directory btree deletion(s) implying
  2208. * possible bmap insert(s). If we can't get the space
  2209. * reservation then we use 0 instead, and avoid the bmap
  2210. * btree insert(s) in the directory code by, if the bmap
  2211. * insert tries to happen, instead trimming the LAST
  2212. * block from the directory.
  2213. */
  2214. resblks = XFS_REMOVE_SPACE_RES(mp);
  2215. error = xfs_trans_reserve(tp, &M_RES(mp)->tr_remove, resblks, 0);
  2216. if (error == -ENOSPC) {
  2217. resblks = 0;
  2218. error = xfs_trans_reserve(tp, &M_RES(mp)->tr_remove, 0, 0);
  2219. }
  2220. if (error) {
  2221. ASSERT(error != -ENOSPC);
  2222. goto out_trans_cancel;
  2223. }
  2224. xfs_lock_two_inodes(dp, ip, XFS_ILOCK_EXCL);
  2225. xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
  2226. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  2227. /*
  2228. * If we're removing a directory perform some additional validation.
  2229. */
  2230. if (is_dir) {
  2231. ASSERT(ip->i_d.di_nlink >= 2);
  2232. if (ip->i_d.di_nlink != 2) {
  2233. error = -ENOTEMPTY;
  2234. goto out_trans_cancel;
  2235. }
  2236. if (!xfs_dir_isempty(ip)) {
  2237. error = -ENOTEMPTY;
  2238. goto out_trans_cancel;
  2239. }
  2240. /* Drop the link from ip's "..". */
  2241. error = xfs_droplink(tp, dp);
  2242. if (error)
  2243. goto out_trans_cancel;
  2244. /* Drop the "." link from ip to self. */
  2245. error = xfs_droplink(tp, ip);
  2246. if (error)
  2247. goto out_trans_cancel;
  2248. } else {
  2249. /*
  2250. * When removing a non-directory we need to log the parent
  2251. * inode here. For a directory this is done implicitly
  2252. * by the xfs_droplink call for the ".." entry.
  2253. */
  2254. xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
  2255. }
  2256. xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  2257. /* Drop the link from dp to ip. */
  2258. error = xfs_droplink(tp, ip);
  2259. if (error)
  2260. goto out_trans_cancel;
  2261. xfs_bmap_init(&free_list, &first_block);
  2262. error = xfs_dir_removename(tp, dp, name, ip->i_ino,
  2263. &first_block, &free_list, resblks);
  2264. if (error) {
  2265. ASSERT(error != -ENOENT);
  2266. goto out_bmap_cancel;
  2267. }
  2268. /*
  2269. * If this is a synchronous mount, make sure that the
  2270. * remove transaction goes to disk before returning to
  2271. * the user.
  2272. */
  2273. if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
  2274. xfs_trans_set_sync(tp);
  2275. error = xfs_bmap_finish(&tp, &free_list, &committed);
  2276. if (error)
  2277. goto out_bmap_cancel;
  2278. error = xfs_trans_commit(tp);
  2279. if (error)
  2280. goto std_return;
  2281. if (is_dir && xfs_inode_is_filestream(ip))
  2282. xfs_filestream_deassociate(ip);
  2283. return 0;
  2284. out_bmap_cancel:
  2285. xfs_bmap_cancel(&free_list);
  2286. out_trans_cancel:
  2287. xfs_trans_cancel(tp);
  2288. std_return:
  2289. return error;
  2290. }
  2291. /*
  2292. * Enter all inodes for a rename transaction into a sorted array.
  2293. */
  2294. #define __XFS_SORT_INODES 5
  2295. STATIC void
  2296. xfs_sort_for_rename(
  2297. struct xfs_inode *dp1, /* in: old (source) directory inode */
  2298. struct xfs_inode *dp2, /* in: new (target) directory inode */
  2299. struct xfs_inode *ip1, /* in: inode of old entry */
  2300. struct xfs_inode *ip2, /* in: inode of new entry */
  2301. struct xfs_inode *wip, /* in: whiteout inode */
  2302. struct xfs_inode **i_tab,/* out: sorted array of inodes */
  2303. int *num_inodes) /* in/out: inodes in array */
  2304. {
  2305. int i, j;
  2306. ASSERT(*num_inodes == __XFS_SORT_INODES);
  2307. memset(i_tab, 0, *num_inodes * sizeof(struct xfs_inode *));
  2308. /*
  2309. * i_tab contains a list of pointers to inodes. We initialize
  2310. * the table here & we'll sort it. We will then use it to
  2311. * order the acquisition of the inode locks.
  2312. *
  2313. * Note that the table may contain duplicates. e.g., dp1 == dp2.
  2314. */
  2315. i = 0;
  2316. i_tab[i++] = dp1;
  2317. i_tab[i++] = dp2;
  2318. i_tab[i++] = ip1;
  2319. if (ip2)
  2320. i_tab[i++] = ip2;
  2321. if (wip)
  2322. i_tab[i++] = wip;
  2323. *num_inodes = i;
  2324. /*
  2325. * Sort the elements via bubble sort. (Remember, there are at
  2326. * most 5 elements to sort, so this is adequate.)
  2327. */
  2328. for (i = 0; i < *num_inodes; i++) {
  2329. for (j = 1; j < *num_inodes; j++) {
  2330. if (i_tab[j]->i_ino < i_tab[j-1]->i_ino) {
  2331. struct xfs_inode *temp = i_tab[j];
  2332. i_tab[j] = i_tab[j-1];
  2333. i_tab[j-1] = temp;
  2334. }
  2335. }
  2336. }
  2337. }
  2338. static int
  2339. xfs_finish_rename(
  2340. struct xfs_trans *tp,
  2341. struct xfs_bmap_free *free_list)
  2342. {
  2343. int committed = 0;
  2344. int error;
  2345. /*
  2346. * If this is a synchronous mount, make sure that the rename transaction
  2347. * goes to disk before returning to the user.
  2348. */
  2349. if (tp->t_mountp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
  2350. xfs_trans_set_sync(tp);
  2351. error = xfs_bmap_finish(&tp, free_list, &committed);
  2352. if (error) {
  2353. xfs_bmap_cancel(free_list);
  2354. xfs_trans_cancel(tp);
  2355. return error;
  2356. }
  2357. return xfs_trans_commit(tp);
  2358. }
  2359. /*
  2360. * xfs_cross_rename()
  2361. *
  2362. * responsible for handling RENAME_EXCHANGE flag in renameat2() sytemcall
  2363. */
  2364. STATIC int
  2365. xfs_cross_rename(
  2366. struct xfs_trans *tp,
  2367. struct xfs_inode *dp1,
  2368. struct xfs_name *name1,
  2369. struct xfs_inode *ip1,
  2370. struct xfs_inode *dp2,
  2371. struct xfs_name *name2,
  2372. struct xfs_inode *ip2,
  2373. struct xfs_bmap_free *free_list,
  2374. xfs_fsblock_t *first_block,
  2375. int spaceres)
  2376. {
  2377. int error = 0;
  2378. int ip1_flags = 0;
  2379. int ip2_flags = 0;
  2380. int dp2_flags = 0;
  2381. /* Swap inode number for dirent in first parent */
  2382. error = xfs_dir_replace(tp, dp1, name1,
  2383. ip2->i_ino,
  2384. first_block, free_list, spaceres);
  2385. if (error)
  2386. goto out_trans_abort;
  2387. /* Swap inode number for dirent in second parent */
  2388. error = xfs_dir_replace(tp, dp2, name2,
  2389. ip1->i_ino,
  2390. first_block, free_list, spaceres);
  2391. if (error)
  2392. goto out_trans_abort;
  2393. /*
  2394. * If we're renaming one or more directories across different parents,
  2395. * update the respective ".." entries (and link counts) to match the new
  2396. * parents.
  2397. */
  2398. if (dp1 != dp2) {
  2399. dp2_flags = XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG;
  2400. if (S_ISDIR(ip2->i_d.di_mode)) {
  2401. error = xfs_dir_replace(tp, ip2, &xfs_name_dotdot,
  2402. dp1->i_ino, first_block,
  2403. free_list, spaceres);
  2404. if (error)
  2405. goto out_trans_abort;
  2406. /* transfer ip2 ".." reference to dp1 */
  2407. if (!S_ISDIR(ip1->i_d.di_mode)) {
  2408. error = xfs_droplink(tp, dp2);
  2409. if (error)
  2410. goto out_trans_abort;
  2411. error = xfs_bumplink(tp, dp1);
  2412. if (error)
  2413. goto out_trans_abort;
  2414. }
  2415. /*
  2416. * Although ip1 isn't changed here, userspace needs
  2417. * to be warned about the change, so that applications
  2418. * relying on it (like backup ones), will properly
  2419. * notify the change
  2420. */
  2421. ip1_flags |= XFS_ICHGTIME_CHG;
  2422. ip2_flags |= XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG;
  2423. }
  2424. if (S_ISDIR(ip1->i_d.di_mode)) {
  2425. error = xfs_dir_replace(tp, ip1, &xfs_name_dotdot,
  2426. dp2->i_ino, first_block,
  2427. free_list, spaceres);
  2428. if (error)
  2429. goto out_trans_abort;
  2430. /* transfer ip1 ".." reference to dp2 */
  2431. if (!S_ISDIR(ip2->i_d.di_mode)) {
  2432. error = xfs_droplink(tp, dp1);
  2433. if (error)
  2434. goto out_trans_abort;
  2435. error = xfs_bumplink(tp, dp2);
  2436. if (error)
  2437. goto out_trans_abort;
  2438. }
  2439. /*
  2440. * Although ip2 isn't changed here, userspace needs
  2441. * to be warned about the change, so that applications
  2442. * relying on it (like backup ones), will properly
  2443. * notify the change
  2444. */
  2445. ip1_flags |= XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG;
  2446. ip2_flags |= XFS_ICHGTIME_CHG;
  2447. }
  2448. }
  2449. if (ip1_flags) {
  2450. xfs_trans_ichgtime(tp, ip1, ip1_flags);
  2451. xfs_trans_log_inode(tp, ip1, XFS_ILOG_CORE);
  2452. }
  2453. if (ip2_flags) {
  2454. xfs_trans_ichgtime(tp, ip2, ip2_flags);
  2455. xfs_trans_log_inode(tp, ip2, XFS_ILOG_CORE);
  2456. }
  2457. if (dp2_flags) {
  2458. xfs_trans_ichgtime(tp, dp2, dp2_flags);
  2459. xfs_trans_log_inode(tp, dp2, XFS_ILOG_CORE);
  2460. }
  2461. xfs_trans_ichgtime(tp, dp1, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  2462. xfs_trans_log_inode(tp, dp1, XFS_ILOG_CORE);
  2463. return xfs_finish_rename(tp, free_list);
  2464. out_trans_abort:
  2465. xfs_bmap_cancel(free_list);
  2466. xfs_trans_cancel(tp);
  2467. return error;
  2468. }
  2469. /*
  2470. * xfs_rename_alloc_whiteout()
  2471. *
  2472. * Return a referenced, unlinked, unlocked inode that that can be used as a
  2473. * whiteout in a rename transaction. We use a tmpfile inode here so that if we
  2474. * crash between allocating the inode and linking it into the rename transaction
  2475. * recovery will free the inode and we won't leak it.
  2476. */
  2477. static int
  2478. xfs_rename_alloc_whiteout(
  2479. struct xfs_inode *dp,
  2480. struct xfs_inode **wip)
  2481. {
  2482. struct xfs_inode *tmpfile;
  2483. int error;
  2484. error = xfs_create_tmpfile(dp, NULL, S_IFCHR | WHITEOUT_MODE, &tmpfile);
  2485. if (error)
  2486. return error;
  2487. /*
  2488. * Prepare the tmpfile inode as if it were created through the VFS.
  2489. * Otherwise, the link increment paths will complain about nlink 0->1.
  2490. * Drop the link count as done by d_tmpfile(), complete the inode setup
  2491. * and flag it as linkable.
  2492. */
  2493. drop_nlink(VFS_I(tmpfile));
  2494. xfs_finish_inode_setup(tmpfile);
  2495. VFS_I(tmpfile)->i_state |= I_LINKABLE;
  2496. *wip = tmpfile;
  2497. return 0;
  2498. }
  2499. /*
  2500. * xfs_rename
  2501. */
  2502. int
  2503. xfs_rename(
  2504. struct xfs_inode *src_dp,
  2505. struct xfs_name *src_name,
  2506. struct xfs_inode *src_ip,
  2507. struct xfs_inode *target_dp,
  2508. struct xfs_name *target_name,
  2509. struct xfs_inode *target_ip,
  2510. unsigned int flags)
  2511. {
  2512. struct xfs_mount *mp = src_dp->i_mount;
  2513. struct xfs_trans *tp;
  2514. struct xfs_bmap_free free_list;
  2515. xfs_fsblock_t first_block;
  2516. struct xfs_inode *wip = NULL; /* whiteout inode */
  2517. struct xfs_inode *inodes[__XFS_SORT_INODES];
  2518. int num_inodes = __XFS_SORT_INODES;
  2519. bool new_parent = (src_dp != target_dp);
  2520. bool src_is_directory = S_ISDIR(src_ip->i_d.di_mode);
  2521. int spaceres;
  2522. int error;
  2523. trace_xfs_rename(src_dp, target_dp, src_name, target_name);
  2524. if ((flags & RENAME_EXCHANGE) && !target_ip)
  2525. return -EINVAL;
  2526. /*
  2527. * If we are doing a whiteout operation, allocate the whiteout inode
  2528. * we will be placing at the target and ensure the type is set
  2529. * appropriately.
  2530. */
  2531. if (flags & RENAME_WHITEOUT) {
  2532. ASSERT(!(flags & (RENAME_NOREPLACE | RENAME_EXCHANGE)));
  2533. error = xfs_rename_alloc_whiteout(target_dp, &wip);
  2534. if (error)
  2535. return error;
  2536. /* setup target dirent info as whiteout */
  2537. src_name->type = XFS_DIR3_FT_CHRDEV;
  2538. }
  2539. xfs_sort_for_rename(src_dp, target_dp, src_ip, target_ip, wip,
  2540. inodes, &num_inodes);
  2541. tp = xfs_trans_alloc(mp, XFS_TRANS_RENAME);
  2542. spaceres = XFS_RENAME_SPACE_RES(mp, target_name->len);
  2543. error = xfs_trans_reserve(tp, &M_RES(mp)->tr_rename, spaceres, 0);
  2544. if (error == -ENOSPC) {
  2545. spaceres = 0;
  2546. error = xfs_trans_reserve(tp, &M_RES(mp)->tr_rename, 0, 0);
  2547. }
  2548. if (error)
  2549. goto out_trans_cancel;
  2550. /*
  2551. * Attach the dquots to the inodes
  2552. */
  2553. error = xfs_qm_vop_rename_dqattach(inodes);
  2554. if (error)
  2555. goto out_trans_cancel;
  2556. /*
  2557. * Lock all the participating inodes. Depending upon whether
  2558. * the target_name exists in the target directory, and
  2559. * whether the target directory is the same as the source
  2560. * directory, we can lock from 2 to 4 inodes.
  2561. */
  2562. xfs_lock_inodes(inodes, num_inodes, XFS_ILOCK_EXCL);
  2563. /*
  2564. * Join all the inodes to the transaction. From this point on,
  2565. * we can rely on either trans_commit or trans_cancel to unlock
  2566. * them.
  2567. */
  2568. xfs_trans_ijoin(tp, src_dp, XFS_ILOCK_EXCL);
  2569. if (new_parent)
  2570. xfs_trans_ijoin(tp, target_dp, XFS_ILOCK_EXCL);
  2571. xfs_trans_ijoin(tp, src_ip, XFS_ILOCK_EXCL);
  2572. if (target_ip)
  2573. xfs_trans_ijoin(tp, target_ip, XFS_ILOCK_EXCL);
  2574. if (wip)
  2575. xfs_trans_ijoin(tp, wip, XFS_ILOCK_EXCL);
  2576. /*
  2577. * If we are using project inheritance, we only allow renames
  2578. * into our tree when the project IDs are the same; else the
  2579. * tree quota mechanism would be circumvented.
  2580. */
  2581. if (unlikely((target_dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
  2582. (xfs_get_projid(target_dp) != xfs_get_projid(src_ip)))) {
  2583. error = -EXDEV;
  2584. goto out_trans_cancel;
  2585. }
  2586. xfs_bmap_init(&free_list, &first_block);
  2587. /* RENAME_EXCHANGE is unique from here on. */
  2588. if (flags & RENAME_EXCHANGE)
  2589. return xfs_cross_rename(tp, src_dp, src_name, src_ip,
  2590. target_dp, target_name, target_ip,
  2591. &free_list, &first_block, spaceres);
  2592. /*
  2593. * Set up the target.
  2594. */
  2595. if (target_ip == NULL) {
  2596. /*
  2597. * If there's no space reservation, check the entry will
  2598. * fit before actually inserting it.
  2599. */
  2600. if (!spaceres) {
  2601. error = xfs_dir_canenter(tp, target_dp, target_name);
  2602. if (error)
  2603. goto out_trans_cancel;
  2604. }
  2605. /*
  2606. * If target does not exist and the rename crosses
  2607. * directories, adjust the target directory link count
  2608. * to account for the ".." reference from the new entry.
  2609. */
  2610. error = xfs_dir_createname(tp, target_dp, target_name,
  2611. src_ip->i_ino, &first_block,
  2612. &free_list, spaceres);
  2613. if (error)
  2614. goto out_bmap_cancel;
  2615. xfs_trans_ichgtime(tp, target_dp,
  2616. XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  2617. if (new_parent && src_is_directory) {
  2618. error = xfs_bumplink(tp, target_dp);
  2619. if (error)
  2620. goto out_bmap_cancel;
  2621. }
  2622. } else { /* target_ip != NULL */
  2623. /*
  2624. * If target exists and it's a directory, check that both
  2625. * target and source are directories and that target can be
  2626. * destroyed, or that neither is a directory.
  2627. */
  2628. if (S_ISDIR(target_ip->i_d.di_mode)) {
  2629. /*
  2630. * Make sure target dir is empty.
  2631. */
  2632. if (!(xfs_dir_isempty(target_ip)) ||
  2633. (target_ip->i_d.di_nlink > 2)) {
  2634. error = -EEXIST;
  2635. goto out_trans_cancel;
  2636. }
  2637. }
  2638. /*
  2639. * Link the source inode under the target name.
  2640. * If the source inode is a directory and we are moving
  2641. * it across directories, its ".." entry will be
  2642. * inconsistent until we replace that down below.
  2643. *
  2644. * In case there is already an entry with the same
  2645. * name at the destination directory, remove it first.
  2646. */
  2647. error = xfs_dir_replace(tp, target_dp, target_name,
  2648. src_ip->i_ino,
  2649. &first_block, &free_list, spaceres);
  2650. if (error)
  2651. goto out_bmap_cancel;
  2652. xfs_trans_ichgtime(tp, target_dp,
  2653. XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  2654. /*
  2655. * Decrement the link count on the target since the target
  2656. * dir no longer points to it.
  2657. */
  2658. error = xfs_droplink(tp, target_ip);
  2659. if (error)
  2660. goto out_bmap_cancel;
  2661. if (src_is_directory) {
  2662. /*
  2663. * Drop the link from the old "." entry.
  2664. */
  2665. error = xfs_droplink(tp, target_ip);
  2666. if (error)
  2667. goto out_bmap_cancel;
  2668. }
  2669. } /* target_ip != NULL */
  2670. /*
  2671. * Remove the source.
  2672. */
  2673. if (new_parent && src_is_directory) {
  2674. /*
  2675. * Rewrite the ".." entry to point to the new
  2676. * directory.
  2677. */
  2678. error = xfs_dir_replace(tp, src_ip, &xfs_name_dotdot,
  2679. target_dp->i_ino,
  2680. &first_block, &free_list, spaceres);
  2681. ASSERT(error != -EEXIST);
  2682. if (error)
  2683. goto out_bmap_cancel;
  2684. }
  2685. /*
  2686. * We always want to hit the ctime on the source inode.
  2687. *
  2688. * This isn't strictly required by the standards since the source
  2689. * inode isn't really being changed, but old unix file systems did
  2690. * it and some incremental backup programs won't work without it.
  2691. */
  2692. xfs_trans_ichgtime(tp, src_ip, XFS_ICHGTIME_CHG);
  2693. xfs_trans_log_inode(tp, src_ip, XFS_ILOG_CORE);
  2694. /*
  2695. * Adjust the link count on src_dp. This is necessary when
  2696. * renaming a directory, either within one parent when
  2697. * the target existed, or across two parent directories.
  2698. */
  2699. if (src_is_directory && (new_parent || target_ip != NULL)) {
  2700. /*
  2701. * Decrement link count on src_directory since the
  2702. * entry that's moved no longer points to it.
  2703. */
  2704. error = xfs_droplink(tp, src_dp);
  2705. if (error)
  2706. goto out_bmap_cancel;
  2707. }
  2708. /*
  2709. * For whiteouts, we only need to update the source dirent with the
  2710. * inode number of the whiteout inode rather than removing it
  2711. * altogether.
  2712. */
  2713. if (wip) {
  2714. error = xfs_dir_replace(tp, src_dp, src_name, wip->i_ino,
  2715. &first_block, &free_list, spaceres);
  2716. } else
  2717. error = xfs_dir_removename(tp, src_dp, src_name, src_ip->i_ino,
  2718. &first_block, &free_list, spaceres);
  2719. if (error)
  2720. goto out_bmap_cancel;
  2721. /*
  2722. * For whiteouts, we need to bump the link count on the whiteout inode.
  2723. * This means that failures all the way up to this point leave the inode
  2724. * on the unlinked list and so cleanup is a simple matter of dropping
  2725. * the remaining reference to it. If we fail here after bumping the link
  2726. * count, we're shutting down the filesystem so we'll never see the
  2727. * intermediate state on disk.
  2728. */
  2729. if (wip) {
  2730. ASSERT(VFS_I(wip)->i_nlink == 0 && wip->i_d.di_nlink == 0);
  2731. error = xfs_bumplink(tp, wip);
  2732. if (error)
  2733. goto out_bmap_cancel;
  2734. error = xfs_iunlink_remove(tp, wip);
  2735. if (error)
  2736. goto out_bmap_cancel;
  2737. xfs_trans_log_inode(tp, wip, XFS_ILOG_CORE);
  2738. /*
  2739. * Now we have a real link, clear the "I'm a tmpfile" state
  2740. * flag from the inode so it doesn't accidentally get misused in
  2741. * future.
  2742. */
  2743. VFS_I(wip)->i_state &= ~I_LINKABLE;
  2744. }
  2745. xfs_trans_ichgtime(tp, src_dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  2746. xfs_trans_log_inode(tp, src_dp, XFS_ILOG_CORE);
  2747. if (new_parent)
  2748. xfs_trans_log_inode(tp, target_dp, XFS_ILOG_CORE);
  2749. error = xfs_finish_rename(tp, &free_list);
  2750. if (wip)
  2751. IRELE(wip);
  2752. return error;
  2753. out_bmap_cancel:
  2754. xfs_bmap_cancel(&free_list);
  2755. out_trans_cancel:
  2756. xfs_trans_cancel(tp);
  2757. if (wip)
  2758. IRELE(wip);
  2759. return error;
  2760. }
  2761. STATIC int
  2762. xfs_iflush_cluster(
  2763. xfs_inode_t *ip,
  2764. xfs_buf_t *bp)
  2765. {
  2766. xfs_mount_t *mp = ip->i_mount;
  2767. struct xfs_perag *pag;
  2768. unsigned long first_index, mask;
  2769. unsigned long inodes_per_cluster;
  2770. int ilist_size;
  2771. xfs_inode_t **ilist;
  2772. xfs_inode_t *iq;
  2773. int nr_found;
  2774. int clcount = 0;
  2775. int bufwasdelwri;
  2776. int i;
  2777. pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
  2778. inodes_per_cluster = mp->m_inode_cluster_size >> mp->m_sb.sb_inodelog;
  2779. ilist_size = inodes_per_cluster * sizeof(xfs_inode_t *);
  2780. ilist = kmem_alloc(ilist_size, KM_MAYFAIL|KM_NOFS);
  2781. if (!ilist)
  2782. goto out_put;
  2783. mask = ~(((mp->m_inode_cluster_size >> mp->m_sb.sb_inodelog)) - 1);
  2784. first_index = XFS_INO_TO_AGINO(mp, ip->i_ino) & mask;
  2785. rcu_read_lock();
  2786. /* really need a gang lookup range call here */
  2787. nr_found = radix_tree_gang_lookup(&pag->pag_ici_root, (void**)ilist,
  2788. first_index, inodes_per_cluster);
  2789. if (nr_found == 0)
  2790. goto out_free;
  2791. for (i = 0; i < nr_found; i++) {
  2792. iq = ilist[i];
  2793. if (iq == ip)
  2794. continue;
  2795. /*
  2796. * because this is an RCU protected lookup, we could find a
  2797. * recently freed or even reallocated inode during the lookup.
  2798. * We need to check under the i_flags_lock for a valid inode
  2799. * here. Skip it if it is not valid or the wrong inode.
  2800. */
  2801. spin_lock(&ip->i_flags_lock);
  2802. if (!ip->i_ino ||
  2803. (XFS_INO_TO_AGINO(mp, iq->i_ino) & mask) != first_index) {
  2804. spin_unlock(&ip->i_flags_lock);
  2805. continue;
  2806. }
  2807. spin_unlock(&ip->i_flags_lock);
  2808. /*
  2809. * Do an un-protected check to see if the inode is dirty and
  2810. * is a candidate for flushing. These checks will be repeated
  2811. * later after the appropriate locks are acquired.
  2812. */
  2813. if (xfs_inode_clean(iq) && xfs_ipincount(iq) == 0)
  2814. continue;
  2815. /*
  2816. * Try to get locks. If any are unavailable or it is pinned,
  2817. * then this inode cannot be flushed and is skipped.
  2818. */
  2819. if (!xfs_ilock_nowait(iq, XFS_ILOCK_SHARED))
  2820. continue;
  2821. if (!xfs_iflock_nowait(iq)) {
  2822. xfs_iunlock(iq, XFS_ILOCK_SHARED);
  2823. continue;
  2824. }
  2825. if (xfs_ipincount(iq)) {
  2826. xfs_ifunlock(iq);
  2827. xfs_iunlock(iq, XFS_ILOCK_SHARED);
  2828. continue;
  2829. }
  2830. /*
  2831. * arriving here means that this inode can be flushed. First
  2832. * re-check that it's dirty before flushing.
  2833. */
  2834. if (!xfs_inode_clean(iq)) {
  2835. int error;
  2836. error = xfs_iflush_int(iq, bp);
  2837. if (error) {
  2838. xfs_iunlock(iq, XFS_ILOCK_SHARED);
  2839. goto cluster_corrupt_out;
  2840. }
  2841. clcount++;
  2842. } else {
  2843. xfs_ifunlock(iq);
  2844. }
  2845. xfs_iunlock(iq, XFS_ILOCK_SHARED);
  2846. }
  2847. if (clcount) {
  2848. XFS_STATS_INC(xs_icluster_flushcnt);
  2849. XFS_STATS_ADD(xs_icluster_flushinode, clcount);
  2850. }
  2851. out_free:
  2852. rcu_read_unlock();
  2853. kmem_free(ilist);
  2854. out_put:
  2855. xfs_perag_put(pag);
  2856. return 0;
  2857. cluster_corrupt_out:
  2858. /*
  2859. * Corruption detected in the clustering loop. Invalidate the
  2860. * inode buffer and shut down the filesystem.
  2861. */
  2862. rcu_read_unlock();
  2863. /*
  2864. * Clean up the buffer. If it was delwri, just release it --
  2865. * brelse can handle it with no problems. If not, shut down the
  2866. * filesystem before releasing the buffer.
  2867. */
  2868. bufwasdelwri = (bp->b_flags & _XBF_DELWRI_Q);
  2869. if (bufwasdelwri)
  2870. xfs_buf_relse(bp);
  2871. xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
  2872. if (!bufwasdelwri) {
  2873. /*
  2874. * Just like incore_relse: if we have b_iodone functions,
  2875. * mark the buffer as an error and call them. Otherwise
  2876. * mark it as stale and brelse.
  2877. */
  2878. if (bp->b_iodone) {
  2879. XFS_BUF_UNDONE(bp);
  2880. xfs_buf_stale(bp);
  2881. xfs_buf_ioerror(bp, -EIO);
  2882. xfs_buf_ioend(bp);
  2883. } else {
  2884. xfs_buf_stale(bp);
  2885. xfs_buf_relse(bp);
  2886. }
  2887. }
  2888. /*
  2889. * Unlocks the flush lock
  2890. */
  2891. xfs_iflush_abort(iq, false);
  2892. kmem_free(ilist);
  2893. xfs_perag_put(pag);
  2894. return -EFSCORRUPTED;
  2895. }
  2896. /*
  2897. * Flush dirty inode metadata into the backing buffer.
  2898. *
  2899. * The caller must have the inode lock and the inode flush lock held. The
  2900. * inode lock will still be held upon return to the caller, and the inode
  2901. * flush lock will be released after the inode has reached the disk.
  2902. *
  2903. * The caller must write out the buffer returned in *bpp and release it.
  2904. */
  2905. int
  2906. xfs_iflush(
  2907. struct xfs_inode *ip,
  2908. struct xfs_buf **bpp)
  2909. {
  2910. struct xfs_mount *mp = ip->i_mount;
  2911. struct xfs_buf *bp;
  2912. struct xfs_dinode *dip;
  2913. int error;
  2914. XFS_STATS_INC(xs_iflush_count);
  2915. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
  2916. ASSERT(xfs_isiflocked(ip));
  2917. ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE ||
  2918. ip->i_d.di_nextents > XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK));
  2919. *bpp = NULL;
  2920. xfs_iunpin_wait(ip);
  2921. /*
  2922. * For stale inodes we cannot rely on the backing buffer remaining
  2923. * stale in cache for the remaining life of the stale inode and so
  2924. * xfs_imap_to_bp() below may give us a buffer that no longer contains
  2925. * inodes below. We have to check this after ensuring the inode is
  2926. * unpinned so that it is safe to reclaim the stale inode after the
  2927. * flush call.
  2928. */
  2929. if (xfs_iflags_test(ip, XFS_ISTALE)) {
  2930. xfs_ifunlock(ip);
  2931. return 0;
  2932. }
  2933. /*
  2934. * This may have been unpinned because the filesystem is shutting
  2935. * down forcibly. If that's the case we must not write this inode
  2936. * to disk, because the log record didn't make it to disk.
  2937. *
  2938. * We also have to remove the log item from the AIL in this case,
  2939. * as we wait for an empty AIL as part of the unmount process.
  2940. */
  2941. if (XFS_FORCED_SHUTDOWN(mp)) {
  2942. error = -EIO;
  2943. goto abort_out;
  2944. }
  2945. /*
  2946. * Get the buffer containing the on-disk inode.
  2947. */
  2948. error = xfs_imap_to_bp(mp, NULL, &ip->i_imap, &dip, &bp, XBF_TRYLOCK,
  2949. 0);
  2950. if (error || !bp) {
  2951. xfs_ifunlock(ip);
  2952. return error;
  2953. }
  2954. /*
  2955. * First flush out the inode that xfs_iflush was called with.
  2956. */
  2957. error = xfs_iflush_int(ip, bp);
  2958. if (error)
  2959. goto corrupt_out;
  2960. /*
  2961. * If the buffer is pinned then push on the log now so we won't
  2962. * get stuck waiting in the write for too long.
  2963. */
  2964. if (xfs_buf_ispinned(bp))
  2965. xfs_log_force(mp, 0);
  2966. /*
  2967. * inode clustering:
  2968. * see if other inodes can be gathered into this write
  2969. */
  2970. error = xfs_iflush_cluster(ip, bp);
  2971. if (error)
  2972. goto cluster_corrupt_out;
  2973. *bpp = bp;
  2974. return 0;
  2975. corrupt_out:
  2976. xfs_buf_relse(bp);
  2977. xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
  2978. cluster_corrupt_out:
  2979. error = -EFSCORRUPTED;
  2980. abort_out:
  2981. /*
  2982. * Unlocks the flush lock
  2983. */
  2984. xfs_iflush_abort(ip, false);
  2985. return error;
  2986. }
  2987. STATIC int
  2988. xfs_iflush_int(
  2989. struct xfs_inode *ip,
  2990. struct xfs_buf *bp)
  2991. {
  2992. struct xfs_inode_log_item *iip = ip->i_itemp;
  2993. struct xfs_dinode *dip;
  2994. struct xfs_mount *mp = ip->i_mount;
  2995. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
  2996. ASSERT(xfs_isiflocked(ip));
  2997. ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE ||
  2998. ip->i_d.di_nextents > XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK));
  2999. ASSERT(iip != NULL && iip->ili_fields != 0);
  3000. ASSERT(ip->i_d.di_version > 1);
  3001. /* set *dip = inode's place in the buffer */
  3002. dip = xfs_buf_offset(bp, ip->i_imap.im_boffset);
  3003. if (XFS_TEST_ERROR(dip->di_magic != cpu_to_be16(XFS_DINODE_MAGIC),
  3004. mp, XFS_ERRTAG_IFLUSH_1, XFS_RANDOM_IFLUSH_1)) {
  3005. xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
  3006. "%s: Bad inode %Lu magic number 0x%x, ptr 0x%p",
  3007. __func__, ip->i_ino, be16_to_cpu(dip->di_magic), dip);
  3008. goto corrupt_out;
  3009. }
  3010. if (XFS_TEST_ERROR(ip->i_d.di_magic != XFS_DINODE_MAGIC,
  3011. mp, XFS_ERRTAG_IFLUSH_2, XFS_RANDOM_IFLUSH_2)) {
  3012. xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
  3013. "%s: Bad inode %Lu, ptr 0x%p, magic number 0x%x",
  3014. __func__, ip->i_ino, ip, ip->i_d.di_magic);
  3015. goto corrupt_out;
  3016. }
  3017. if (S_ISREG(ip->i_d.di_mode)) {
  3018. if (XFS_TEST_ERROR(
  3019. (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS) &&
  3020. (ip->i_d.di_format != XFS_DINODE_FMT_BTREE),
  3021. mp, XFS_ERRTAG_IFLUSH_3, XFS_RANDOM_IFLUSH_3)) {
  3022. xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
  3023. "%s: Bad regular inode %Lu, ptr 0x%p",
  3024. __func__, ip->i_ino, ip);
  3025. goto corrupt_out;
  3026. }
  3027. } else if (S_ISDIR(ip->i_d.di_mode)) {
  3028. if (XFS_TEST_ERROR(
  3029. (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS) &&
  3030. (ip->i_d.di_format != XFS_DINODE_FMT_BTREE) &&
  3031. (ip->i_d.di_format != XFS_DINODE_FMT_LOCAL),
  3032. mp, XFS_ERRTAG_IFLUSH_4, XFS_RANDOM_IFLUSH_4)) {
  3033. xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
  3034. "%s: Bad directory inode %Lu, ptr 0x%p",
  3035. __func__, ip->i_ino, ip);
  3036. goto corrupt_out;
  3037. }
  3038. }
  3039. if (XFS_TEST_ERROR(ip->i_d.di_nextents + ip->i_d.di_anextents >
  3040. ip->i_d.di_nblocks, mp, XFS_ERRTAG_IFLUSH_5,
  3041. XFS_RANDOM_IFLUSH_5)) {
  3042. xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
  3043. "%s: detected corrupt incore inode %Lu, "
  3044. "total extents = %d, nblocks = %Ld, ptr 0x%p",
  3045. __func__, ip->i_ino,
  3046. ip->i_d.di_nextents + ip->i_d.di_anextents,
  3047. ip->i_d.di_nblocks, ip);
  3048. goto corrupt_out;
  3049. }
  3050. if (XFS_TEST_ERROR(ip->i_d.di_forkoff > mp->m_sb.sb_inodesize,
  3051. mp, XFS_ERRTAG_IFLUSH_6, XFS_RANDOM_IFLUSH_6)) {
  3052. xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
  3053. "%s: bad inode %Lu, forkoff 0x%x, ptr 0x%p",
  3054. __func__, ip->i_ino, ip->i_d.di_forkoff, ip);
  3055. goto corrupt_out;
  3056. }
  3057. /*
  3058. * Inode item log recovery for v2 inodes are dependent on the
  3059. * di_flushiter count for correct sequencing. We bump the flush
  3060. * iteration count so we can detect flushes which postdate a log record
  3061. * during recovery. This is redundant as we now log every change and
  3062. * hence this can't happen but we need to still do it to ensure
  3063. * backwards compatibility with old kernels that predate logging all
  3064. * inode changes.
  3065. */
  3066. if (ip->i_d.di_version < 3)
  3067. ip->i_d.di_flushiter++;
  3068. /*
  3069. * Copy the dirty parts of the inode into the on-disk
  3070. * inode. We always copy out the core of the inode,
  3071. * because if the inode is dirty at all the core must
  3072. * be.
  3073. */
  3074. xfs_dinode_to_disk(dip, &ip->i_d);
  3075. /* Wrap, we never let the log put out DI_MAX_FLUSH */
  3076. if (ip->i_d.di_flushiter == DI_MAX_FLUSH)
  3077. ip->i_d.di_flushiter = 0;
  3078. xfs_iflush_fork(ip, dip, iip, XFS_DATA_FORK);
  3079. if (XFS_IFORK_Q(ip))
  3080. xfs_iflush_fork(ip, dip, iip, XFS_ATTR_FORK);
  3081. xfs_inobp_check(mp, bp);
  3082. /*
  3083. * We've recorded everything logged in the inode, so we'd like to clear
  3084. * the ili_fields bits so we don't log and flush things unnecessarily.
  3085. * However, we can't stop logging all this information until the data
  3086. * we've copied into the disk buffer is written to disk. If we did we
  3087. * might overwrite the copy of the inode in the log with all the data
  3088. * after re-logging only part of it, and in the face of a crash we
  3089. * wouldn't have all the data we need to recover.
  3090. *
  3091. * What we do is move the bits to the ili_last_fields field. When
  3092. * logging the inode, these bits are moved back to the ili_fields field.
  3093. * In the xfs_iflush_done() routine we clear ili_last_fields, since we
  3094. * know that the information those bits represent is permanently on
  3095. * disk. As long as the flush completes before the inode is logged
  3096. * again, then both ili_fields and ili_last_fields will be cleared.
  3097. *
  3098. * We can play with the ili_fields bits here, because the inode lock
  3099. * must be held exclusively in order to set bits there and the flush
  3100. * lock protects the ili_last_fields bits. Set ili_logged so the flush
  3101. * done routine can tell whether or not to look in the AIL. Also, store
  3102. * the current LSN of the inode so that we can tell whether the item has
  3103. * moved in the AIL from xfs_iflush_done(). In order to read the lsn we
  3104. * need the AIL lock, because it is a 64 bit value that cannot be read
  3105. * atomically.
  3106. */
  3107. iip->ili_last_fields = iip->ili_fields;
  3108. iip->ili_fields = 0;
  3109. iip->ili_logged = 1;
  3110. xfs_trans_ail_copy_lsn(mp->m_ail, &iip->ili_flush_lsn,
  3111. &iip->ili_item.li_lsn);
  3112. /*
  3113. * Attach the function xfs_iflush_done to the inode's
  3114. * buffer. This will remove the inode from the AIL
  3115. * and unlock the inode's flush lock when the inode is
  3116. * completely written to disk.
  3117. */
  3118. xfs_buf_attach_iodone(bp, xfs_iflush_done, &iip->ili_item);
  3119. /* update the lsn in the on disk inode if required */
  3120. if (ip->i_d.di_version == 3)
  3121. dip->di_lsn = cpu_to_be64(iip->ili_item.li_lsn);
  3122. /* generate the checksum. */
  3123. xfs_dinode_calc_crc(mp, dip);
  3124. ASSERT(bp->b_fspriv != NULL);
  3125. ASSERT(bp->b_iodone != NULL);
  3126. return 0;
  3127. corrupt_out:
  3128. return -EFSCORRUPTED;
  3129. }