xfs_inode.c 97 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604
  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_trans_t *ntp;
  817. xfs_inode_t *ip;
  818. xfs_buf_t *ialloc_context = NULL;
  819. int code;
  820. void *dqinfo;
  821. uint tflags;
  822. tp = *tpp;
  823. ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
  824. /*
  825. * xfs_ialloc will return a pointer to an incore inode if
  826. * the Space Manager has an available inode on the free
  827. * list. Otherwise, it will do an allocation and replenish
  828. * the freelist. Since we can only do one allocation per
  829. * transaction without deadlocks, we will need to commit the
  830. * current transaction and start a new one. We will then
  831. * need to call xfs_ialloc again to get the inode.
  832. *
  833. * If xfs_ialloc did an allocation to replenish the freelist,
  834. * it returns the bp containing the head of the freelist as
  835. * ialloc_context. We will hold a lock on it across the
  836. * transaction commit so that no other process can steal
  837. * the inode(s) that we've just allocated.
  838. */
  839. code = xfs_ialloc(tp, dp, mode, nlink, rdev, prid, okalloc,
  840. &ialloc_context, &ip);
  841. /*
  842. * Return an error if we were unable to allocate a new inode.
  843. * This should only happen if we run out of space on disk or
  844. * encounter a disk error.
  845. */
  846. if (code) {
  847. *ipp = NULL;
  848. return code;
  849. }
  850. if (!ialloc_context && !ip) {
  851. *ipp = NULL;
  852. return -ENOSPC;
  853. }
  854. /*
  855. * If the AGI buffer is non-NULL, then we were unable to get an
  856. * inode in one operation. We need to commit the current
  857. * transaction and call xfs_ialloc() again. It is guaranteed
  858. * to succeed the second time.
  859. */
  860. if (ialloc_context) {
  861. struct xfs_trans_res tres;
  862. /*
  863. * Normally, xfs_trans_commit releases all the locks.
  864. * We call bhold to hang on to the ialloc_context across
  865. * the commit. Holding this buffer prevents any other
  866. * processes from doing any allocations in this
  867. * allocation group.
  868. */
  869. xfs_trans_bhold(tp, ialloc_context);
  870. /*
  871. * Save the log reservation so we can use
  872. * them in the next transaction.
  873. */
  874. tres.tr_logres = xfs_trans_get_log_res(tp);
  875. tres.tr_logcount = xfs_trans_get_log_count(tp);
  876. /*
  877. * We want the quota changes to be associated with the next
  878. * transaction, NOT this one. So, detach the dqinfo from this
  879. * and attach it to the next transaction.
  880. */
  881. dqinfo = NULL;
  882. tflags = 0;
  883. if (tp->t_dqinfo) {
  884. dqinfo = (void *)tp->t_dqinfo;
  885. tp->t_dqinfo = NULL;
  886. tflags = tp->t_flags & XFS_TRANS_DQ_DIRTY;
  887. tp->t_flags &= ~(XFS_TRANS_DQ_DIRTY);
  888. }
  889. ntp = xfs_trans_dup(tp);
  890. code = xfs_trans_commit(tp, 0);
  891. tp = ntp;
  892. if (committed != NULL) {
  893. *committed = 1;
  894. }
  895. /*
  896. * If we get an error during the commit processing,
  897. * release the buffer that is still held and return
  898. * to the caller.
  899. */
  900. if (code) {
  901. xfs_buf_relse(ialloc_context);
  902. if (dqinfo) {
  903. tp->t_dqinfo = dqinfo;
  904. xfs_trans_free_dqinfo(tp);
  905. }
  906. *tpp = ntp;
  907. *ipp = NULL;
  908. return code;
  909. }
  910. /*
  911. * transaction commit worked ok so we can drop the extra ticket
  912. * reference that we gained in xfs_trans_dup()
  913. */
  914. xfs_log_ticket_put(tp->t_ticket);
  915. tres.tr_logflags = XFS_TRANS_PERM_LOG_RES;
  916. code = xfs_trans_reserve(tp, &tres, 0, 0);
  917. /*
  918. * Re-attach the quota info that we detached from prev trx.
  919. */
  920. if (dqinfo) {
  921. tp->t_dqinfo = dqinfo;
  922. tp->t_flags |= tflags;
  923. }
  924. if (code) {
  925. xfs_buf_relse(ialloc_context);
  926. *tpp = ntp;
  927. *ipp = NULL;
  928. return code;
  929. }
  930. xfs_trans_bjoin(tp, ialloc_context);
  931. /*
  932. * Call ialloc again. Since we've locked out all
  933. * other allocations in this allocation group,
  934. * this call should always succeed.
  935. */
  936. code = xfs_ialloc(tp, dp, mode, nlink, rdev, prid,
  937. okalloc, &ialloc_context, &ip);
  938. /*
  939. * If we get an error at this point, return to the caller
  940. * so that the current transaction can be aborted.
  941. */
  942. if (code) {
  943. *tpp = tp;
  944. *ipp = NULL;
  945. return code;
  946. }
  947. ASSERT(!ialloc_context && ip);
  948. } else {
  949. if (committed != NULL)
  950. *committed = 0;
  951. }
  952. *ipp = ip;
  953. *tpp = tp;
  954. return 0;
  955. }
  956. /*
  957. * Decrement the link count on an inode & log the change.
  958. * If this causes the link count to go to zero, initiate the
  959. * logging activity required to truncate a file.
  960. */
  961. int /* error */
  962. xfs_droplink(
  963. xfs_trans_t *tp,
  964. xfs_inode_t *ip)
  965. {
  966. int error;
  967. xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_CHG);
  968. ASSERT (ip->i_d.di_nlink > 0);
  969. ip->i_d.di_nlink--;
  970. drop_nlink(VFS_I(ip));
  971. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  972. error = 0;
  973. if (ip->i_d.di_nlink == 0) {
  974. /*
  975. * We're dropping the last link to this file.
  976. * Move the on-disk inode to the AGI unlinked list.
  977. * From xfs_inactive() we will pull the inode from
  978. * the list and free it.
  979. */
  980. error = xfs_iunlink(tp, ip);
  981. }
  982. return error;
  983. }
  984. /*
  985. * Increment the link count on an inode & log the change.
  986. */
  987. int
  988. xfs_bumplink(
  989. xfs_trans_t *tp,
  990. xfs_inode_t *ip)
  991. {
  992. xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_CHG);
  993. ASSERT(ip->i_d.di_version > 1);
  994. ASSERT(ip->i_d.di_nlink > 0 || (VFS_I(ip)->i_state & I_LINKABLE));
  995. ip->i_d.di_nlink++;
  996. inc_nlink(VFS_I(ip));
  997. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  998. return 0;
  999. }
  1000. int
  1001. xfs_create(
  1002. xfs_inode_t *dp,
  1003. struct xfs_name *name,
  1004. umode_t mode,
  1005. xfs_dev_t rdev,
  1006. xfs_inode_t **ipp)
  1007. {
  1008. int is_dir = S_ISDIR(mode);
  1009. struct xfs_mount *mp = dp->i_mount;
  1010. struct xfs_inode *ip = NULL;
  1011. struct xfs_trans *tp = NULL;
  1012. int error;
  1013. xfs_bmap_free_t free_list;
  1014. xfs_fsblock_t first_block;
  1015. bool unlock_dp_on_error = false;
  1016. uint cancel_flags;
  1017. int committed;
  1018. prid_t prid;
  1019. struct xfs_dquot *udqp = NULL;
  1020. struct xfs_dquot *gdqp = NULL;
  1021. struct xfs_dquot *pdqp = NULL;
  1022. struct xfs_trans_res *tres;
  1023. uint resblks;
  1024. trace_xfs_create(dp, name);
  1025. if (XFS_FORCED_SHUTDOWN(mp))
  1026. return -EIO;
  1027. prid = xfs_get_initial_prid(dp);
  1028. /*
  1029. * Make sure that we have allocated dquot(s) on disk.
  1030. */
  1031. error = xfs_qm_vop_dqalloc(dp, xfs_kuid_to_uid(current_fsuid()),
  1032. xfs_kgid_to_gid(current_fsgid()), prid,
  1033. XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT,
  1034. &udqp, &gdqp, &pdqp);
  1035. if (error)
  1036. return error;
  1037. if (is_dir) {
  1038. rdev = 0;
  1039. resblks = XFS_MKDIR_SPACE_RES(mp, name->len);
  1040. tres = &M_RES(mp)->tr_mkdir;
  1041. tp = xfs_trans_alloc(mp, XFS_TRANS_MKDIR);
  1042. } else {
  1043. resblks = XFS_CREATE_SPACE_RES(mp, name->len);
  1044. tres = &M_RES(mp)->tr_create;
  1045. tp = xfs_trans_alloc(mp, XFS_TRANS_CREATE);
  1046. }
  1047. cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
  1048. /*
  1049. * Initially assume that the file does not exist and
  1050. * reserve the resources for that case. If that is not
  1051. * the case we'll drop the one we have and get a more
  1052. * appropriate transaction later.
  1053. */
  1054. error = xfs_trans_reserve(tp, tres, resblks, 0);
  1055. if (error == -ENOSPC) {
  1056. /* flush outstanding delalloc blocks and retry */
  1057. xfs_flush_inodes(mp);
  1058. error = xfs_trans_reserve(tp, tres, resblks, 0);
  1059. }
  1060. if (error == -ENOSPC) {
  1061. /* No space at all so try a "no-allocation" reservation */
  1062. resblks = 0;
  1063. error = xfs_trans_reserve(tp, tres, 0, 0);
  1064. }
  1065. if (error) {
  1066. cancel_flags = 0;
  1067. goto out_trans_cancel;
  1068. }
  1069. xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
  1070. unlock_dp_on_error = true;
  1071. xfs_bmap_init(&free_list, &first_block);
  1072. /*
  1073. * Reserve disk quota and the inode.
  1074. */
  1075. error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp,
  1076. pdqp, resblks, 1, 0);
  1077. if (error)
  1078. goto out_trans_cancel;
  1079. if (!resblks) {
  1080. error = xfs_dir_canenter(tp, dp, name);
  1081. if (error)
  1082. goto out_trans_cancel;
  1083. }
  1084. /*
  1085. * A newly created regular or special file just has one directory
  1086. * entry pointing to them, but a directory also the "." entry
  1087. * pointing to itself.
  1088. */
  1089. error = xfs_dir_ialloc(&tp, dp, mode, is_dir ? 2 : 1, rdev,
  1090. prid, resblks > 0, &ip, &committed);
  1091. if (error) {
  1092. if (error == -ENOSPC)
  1093. goto out_trans_cancel;
  1094. goto out_trans_abort;
  1095. }
  1096. /*
  1097. * Now we join the directory inode to the transaction. We do not do it
  1098. * earlier because xfs_dir_ialloc might commit the previous transaction
  1099. * (and release all the locks). An error from here on will result in
  1100. * the transaction cancel unlocking dp so don't do it explicitly in the
  1101. * error path.
  1102. */
  1103. xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
  1104. unlock_dp_on_error = false;
  1105. error = xfs_dir_createname(tp, dp, name, ip->i_ino,
  1106. &first_block, &free_list, resblks ?
  1107. resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
  1108. if (error) {
  1109. ASSERT(error != -ENOSPC);
  1110. goto out_trans_abort;
  1111. }
  1112. xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  1113. xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
  1114. if (is_dir) {
  1115. error = xfs_dir_init(tp, ip, dp);
  1116. if (error)
  1117. goto out_bmap_cancel;
  1118. error = xfs_bumplink(tp, dp);
  1119. if (error)
  1120. goto out_bmap_cancel;
  1121. }
  1122. /*
  1123. * If this is a synchronous mount, make sure that the
  1124. * create transaction goes to disk before returning to
  1125. * the user.
  1126. */
  1127. if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
  1128. xfs_trans_set_sync(tp);
  1129. /*
  1130. * Attach the dquot(s) to the inodes and modify them incore.
  1131. * These ids of the inode couldn't have changed since the new
  1132. * inode has been locked ever since it was created.
  1133. */
  1134. xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp, pdqp);
  1135. error = xfs_bmap_finish(&tp, &free_list, &committed);
  1136. if (error)
  1137. goto out_bmap_cancel;
  1138. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  1139. if (error)
  1140. goto out_release_inode;
  1141. xfs_qm_dqrele(udqp);
  1142. xfs_qm_dqrele(gdqp);
  1143. xfs_qm_dqrele(pdqp);
  1144. *ipp = ip;
  1145. return 0;
  1146. out_bmap_cancel:
  1147. xfs_bmap_cancel(&free_list);
  1148. out_trans_abort:
  1149. cancel_flags |= XFS_TRANS_ABORT;
  1150. out_trans_cancel:
  1151. xfs_trans_cancel(tp, cancel_flags);
  1152. out_release_inode:
  1153. /*
  1154. * Wait until after the current transaction is aborted to finish the
  1155. * setup of the inode and release the inode. This prevents recursive
  1156. * transactions and deadlocks from xfs_inactive.
  1157. */
  1158. if (ip) {
  1159. xfs_finish_inode_setup(ip);
  1160. IRELE(ip);
  1161. }
  1162. xfs_qm_dqrele(udqp);
  1163. xfs_qm_dqrele(gdqp);
  1164. xfs_qm_dqrele(pdqp);
  1165. if (unlock_dp_on_error)
  1166. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  1167. return error;
  1168. }
  1169. int
  1170. xfs_create_tmpfile(
  1171. struct xfs_inode *dp,
  1172. struct dentry *dentry,
  1173. umode_t mode,
  1174. struct xfs_inode **ipp)
  1175. {
  1176. struct xfs_mount *mp = dp->i_mount;
  1177. struct xfs_inode *ip = NULL;
  1178. struct xfs_trans *tp = NULL;
  1179. int error;
  1180. uint cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
  1181. prid_t prid;
  1182. struct xfs_dquot *udqp = NULL;
  1183. struct xfs_dquot *gdqp = NULL;
  1184. struct xfs_dquot *pdqp = NULL;
  1185. struct xfs_trans_res *tres;
  1186. uint resblks;
  1187. if (XFS_FORCED_SHUTDOWN(mp))
  1188. return -EIO;
  1189. prid = xfs_get_initial_prid(dp);
  1190. /*
  1191. * Make sure that we have allocated dquot(s) on disk.
  1192. */
  1193. error = xfs_qm_vop_dqalloc(dp, xfs_kuid_to_uid(current_fsuid()),
  1194. xfs_kgid_to_gid(current_fsgid()), prid,
  1195. XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT,
  1196. &udqp, &gdqp, &pdqp);
  1197. if (error)
  1198. return error;
  1199. resblks = XFS_IALLOC_SPACE_RES(mp);
  1200. tp = xfs_trans_alloc(mp, XFS_TRANS_CREATE_TMPFILE);
  1201. tres = &M_RES(mp)->tr_create_tmpfile;
  1202. error = xfs_trans_reserve(tp, tres, resblks, 0);
  1203. if (error == -ENOSPC) {
  1204. /* No space at all so try a "no-allocation" reservation */
  1205. resblks = 0;
  1206. error = xfs_trans_reserve(tp, tres, 0, 0);
  1207. }
  1208. if (error) {
  1209. cancel_flags = 0;
  1210. goto out_trans_cancel;
  1211. }
  1212. error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp,
  1213. pdqp, resblks, 1, 0);
  1214. if (error)
  1215. goto out_trans_cancel;
  1216. error = xfs_dir_ialloc(&tp, dp, mode, 1, 0,
  1217. prid, resblks > 0, &ip, NULL);
  1218. if (error) {
  1219. if (error == -ENOSPC)
  1220. goto out_trans_cancel;
  1221. goto out_trans_abort;
  1222. }
  1223. if (mp->m_flags & XFS_MOUNT_WSYNC)
  1224. xfs_trans_set_sync(tp);
  1225. /*
  1226. * Attach the dquot(s) to the inodes and modify them incore.
  1227. * These ids of the inode couldn't have changed since the new
  1228. * inode has been locked ever since it was created.
  1229. */
  1230. xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp, pdqp);
  1231. ip->i_d.di_nlink--;
  1232. error = xfs_iunlink(tp, ip);
  1233. if (error)
  1234. goto out_trans_abort;
  1235. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  1236. if (error)
  1237. goto out_release_inode;
  1238. xfs_qm_dqrele(udqp);
  1239. xfs_qm_dqrele(gdqp);
  1240. xfs_qm_dqrele(pdqp);
  1241. *ipp = ip;
  1242. return 0;
  1243. out_trans_abort:
  1244. cancel_flags |= XFS_TRANS_ABORT;
  1245. out_trans_cancel:
  1246. xfs_trans_cancel(tp, cancel_flags);
  1247. out_release_inode:
  1248. /*
  1249. * Wait until after the current transaction is aborted to finish the
  1250. * setup of the inode and release the inode. This prevents recursive
  1251. * transactions and deadlocks from xfs_inactive.
  1252. */
  1253. if (ip) {
  1254. xfs_finish_inode_setup(ip);
  1255. IRELE(ip);
  1256. }
  1257. xfs_qm_dqrele(udqp);
  1258. xfs_qm_dqrele(gdqp);
  1259. xfs_qm_dqrele(pdqp);
  1260. return error;
  1261. }
  1262. int
  1263. xfs_link(
  1264. xfs_inode_t *tdp,
  1265. xfs_inode_t *sip,
  1266. struct xfs_name *target_name)
  1267. {
  1268. xfs_mount_t *mp = tdp->i_mount;
  1269. xfs_trans_t *tp;
  1270. int error;
  1271. xfs_bmap_free_t free_list;
  1272. xfs_fsblock_t first_block;
  1273. int cancel_flags;
  1274. int committed;
  1275. int resblks;
  1276. trace_xfs_link(tdp, target_name);
  1277. ASSERT(!S_ISDIR(sip->i_d.di_mode));
  1278. if (XFS_FORCED_SHUTDOWN(mp))
  1279. return -EIO;
  1280. error = xfs_qm_dqattach(sip, 0);
  1281. if (error)
  1282. goto std_return;
  1283. error = xfs_qm_dqattach(tdp, 0);
  1284. if (error)
  1285. goto std_return;
  1286. tp = xfs_trans_alloc(mp, XFS_TRANS_LINK);
  1287. cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
  1288. resblks = XFS_LINK_SPACE_RES(mp, target_name->len);
  1289. error = xfs_trans_reserve(tp, &M_RES(mp)->tr_link, resblks, 0);
  1290. if (error == -ENOSPC) {
  1291. resblks = 0;
  1292. error = xfs_trans_reserve(tp, &M_RES(mp)->tr_link, 0, 0);
  1293. }
  1294. if (error) {
  1295. cancel_flags = 0;
  1296. goto error_return;
  1297. }
  1298. xfs_lock_two_inodes(sip, tdp, XFS_ILOCK_EXCL);
  1299. xfs_trans_ijoin(tp, sip, XFS_ILOCK_EXCL);
  1300. xfs_trans_ijoin(tp, tdp, XFS_ILOCK_EXCL);
  1301. /*
  1302. * If we are using project inheritance, we only allow hard link
  1303. * creation in our tree when the project IDs are the same; else
  1304. * the tree quota mechanism could be circumvented.
  1305. */
  1306. if (unlikely((tdp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
  1307. (xfs_get_projid(tdp) != xfs_get_projid(sip)))) {
  1308. error = -EXDEV;
  1309. goto error_return;
  1310. }
  1311. if (!resblks) {
  1312. error = xfs_dir_canenter(tp, tdp, target_name);
  1313. if (error)
  1314. goto error_return;
  1315. }
  1316. xfs_bmap_init(&free_list, &first_block);
  1317. if (sip->i_d.di_nlink == 0) {
  1318. error = xfs_iunlink_remove(tp, sip);
  1319. if (error)
  1320. goto abort_return;
  1321. }
  1322. error = xfs_dir_createname(tp, tdp, target_name, sip->i_ino,
  1323. &first_block, &free_list, resblks);
  1324. if (error)
  1325. goto abort_return;
  1326. xfs_trans_ichgtime(tp, tdp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  1327. xfs_trans_log_inode(tp, tdp, XFS_ILOG_CORE);
  1328. error = xfs_bumplink(tp, sip);
  1329. if (error)
  1330. goto abort_return;
  1331. /*
  1332. * If this is a synchronous mount, make sure that the
  1333. * link transaction goes to disk before returning to
  1334. * the user.
  1335. */
  1336. if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
  1337. xfs_trans_set_sync(tp);
  1338. }
  1339. error = xfs_bmap_finish (&tp, &free_list, &committed);
  1340. if (error) {
  1341. xfs_bmap_cancel(&free_list);
  1342. goto abort_return;
  1343. }
  1344. return xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  1345. abort_return:
  1346. cancel_flags |= XFS_TRANS_ABORT;
  1347. error_return:
  1348. xfs_trans_cancel(tp, cancel_flags);
  1349. std_return:
  1350. return error;
  1351. }
  1352. /*
  1353. * Free up the underlying blocks past new_size. The new size must be smaller
  1354. * than the current size. This routine can be used both for the attribute and
  1355. * data fork, and does not modify the inode size, which is left to the caller.
  1356. *
  1357. * The transaction passed to this routine must have made a permanent log
  1358. * reservation of at least XFS_ITRUNCATE_LOG_RES. This routine may commit the
  1359. * given transaction and start new ones, so make sure everything involved in
  1360. * the transaction is tidy before calling here. Some transaction will be
  1361. * returned to the caller to be committed. The incoming transaction must
  1362. * already include the inode, and both inode locks must be held exclusively.
  1363. * The inode must also be "held" within the transaction. On return the inode
  1364. * will be "held" within the returned transaction. This routine does NOT
  1365. * require any disk space to be reserved for it within the transaction.
  1366. *
  1367. * If we get an error, we must return with the inode locked and linked into the
  1368. * current transaction. This keeps things simple for the higher level code,
  1369. * because it always knows that the inode is locked and held in the transaction
  1370. * that returns to it whether errors occur or not. We don't mark the inode
  1371. * dirty on error so that transactions can be easily aborted if possible.
  1372. */
  1373. int
  1374. xfs_itruncate_extents(
  1375. struct xfs_trans **tpp,
  1376. struct xfs_inode *ip,
  1377. int whichfork,
  1378. xfs_fsize_t new_size)
  1379. {
  1380. struct xfs_mount *mp = ip->i_mount;
  1381. struct xfs_trans *tp = *tpp;
  1382. struct xfs_trans *ntp;
  1383. xfs_bmap_free_t free_list;
  1384. xfs_fsblock_t first_block;
  1385. xfs_fileoff_t first_unmap_block;
  1386. xfs_fileoff_t last_block;
  1387. xfs_filblks_t unmap_len;
  1388. int committed;
  1389. int error = 0;
  1390. int done = 0;
  1391. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  1392. ASSERT(!atomic_read(&VFS_I(ip)->i_count) ||
  1393. xfs_isilocked(ip, XFS_IOLOCK_EXCL));
  1394. ASSERT(new_size <= XFS_ISIZE(ip));
  1395. ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
  1396. ASSERT(ip->i_itemp != NULL);
  1397. ASSERT(ip->i_itemp->ili_lock_flags == 0);
  1398. ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
  1399. trace_xfs_itruncate_extents_start(ip, new_size);
  1400. /*
  1401. * Since it is possible for space to become allocated beyond
  1402. * the end of the file (in a crash where the space is allocated
  1403. * but the inode size is not yet updated), simply remove any
  1404. * blocks which show up between the new EOF and the maximum
  1405. * possible file size. If the first block to be removed is
  1406. * beyond the maximum file size (ie it is the same as last_block),
  1407. * then there is nothing to do.
  1408. */
  1409. first_unmap_block = XFS_B_TO_FSB(mp, (xfs_ufsize_t)new_size);
  1410. last_block = XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes);
  1411. if (first_unmap_block == last_block)
  1412. return 0;
  1413. ASSERT(first_unmap_block < last_block);
  1414. unmap_len = last_block - first_unmap_block + 1;
  1415. while (!done) {
  1416. xfs_bmap_init(&free_list, &first_block);
  1417. error = xfs_bunmapi(tp, ip,
  1418. first_unmap_block, unmap_len,
  1419. xfs_bmapi_aflag(whichfork),
  1420. XFS_ITRUNC_MAX_EXTENTS,
  1421. &first_block, &free_list,
  1422. &done);
  1423. if (error)
  1424. goto out_bmap_cancel;
  1425. /*
  1426. * Duplicate the transaction that has the permanent
  1427. * reservation and commit the old transaction.
  1428. */
  1429. error = xfs_bmap_finish(&tp, &free_list, &committed);
  1430. if (committed)
  1431. xfs_trans_ijoin(tp, ip, 0);
  1432. if (error)
  1433. goto out_bmap_cancel;
  1434. if (committed) {
  1435. /*
  1436. * Mark the inode dirty so it will be logged and
  1437. * moved forward in the log as part of every commit.
  1438. */
  1439. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  1440. }
  1441. ntp = xfs_trans_dup(tp);
  1442. error = xfs_trans_commit(tp, 0);
  1443. tp = ntp;
  1444. xfs_trans_ijoin(tp, ip, 0);
  1445. if (error)
  1446. goto out;
  1447. /*
  1448. * Transaction commit worked ok so we can drop the extra ticket
  1449. * reference that we gained in xfs_trans_dup()
  1450. */
  1451. xfs_log_ticket_put(tp->t_ticket);
  1452. error = xfs_trans_reserve(tp, &M_RES(mp)->tr_itruncate, 0, 0);
  1453. if (error)
  1454. goto out;
  1455. }
  1456. /*
  1457. * Always re-log the inode so that our permanent transaction can keep
  1458. * on rolling it forward in the log.
  1459. */
  1460. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  1461. trace_xfs_itruncate_extents_end(ip, new_size);
  1462. out:
  1463. *tpp = tp;
  1464. return error;
  1465. out_bmap_cancel:
  1466. /*
  1467. * If the bunmapi call encounters an error, return to the caller where
  1468. * the transaction can be properly aborted. We just need to make sure
  1469. * we're not holding any resources that we were not when we came in.
  1470. */
  1471. xfs_bmap_cancel(&free_list);
  1472. goto out;
  1473. }
  1474. int
  1475. xfs_release(
  1476. xfs_inode_t *ip)
  1477. {
  1478. xfs_mount_t *mp = ip->i_mount;
  1479. int error;
  1480. if (!S_ISREG(ip->i_d.di_mode) || (ip->i_d.di_mode == 0))
  1481. return 0;
  1482. /* If this is a read-only mount, don't do this (would generate I/O) */
  1483. if (mp->m_flags & XFS_MOUNT_RDONLY)
  1484. return 0;
  1485. if (!XFS_FORCED_SHUTDOWN(mp)) {
  1486. int truncated;
  1487. /*
  1488. * If we previously truncated this file and removed old data
  1489. * in the process, we want to initiate "early" writeout on
  1490. * the last close. This is an attempt to combat the notorious
  1491. * NULL files problem which is particularly noticeable from a
  1492. * truncate down, buffered (re-)write (delalloc), followed by
  1493. * a crash. What we are effectively doing here is
  1494. * significantly reducing the time window where we'd otherwise
  1495. * be exposed to that problem.
  1496. */
  1497. truncated = xfs_iflags_test_and_clear(ip, XFS_ITRUNCATED);
  1498. if (truncated) {
  1499. xfs_iflags_clear(ip, XFS_IDIRTY_RELEASE);
  1500. if (ip->i_delayed_blks > 0) {
  1501. error = filemap_flush(VFS_I(ip)->i_mapping);
  1502. if (error)
  1503. return error;
  1504. }
  1505. }
  1506. }
  1507. if (ip->i_d.di_nlink == 0)
  1508. return 0;
  1509. if (xfs_can_free_eofblocks(ip, false)) {
  1510. /*
  1511. * If we can't get the iolock just skip truncating the blocks
  1512. * past EOF because we could deadlock with the mmap_sem
  1513. * otherwise. We'll get another chance to drop them once the
  1514. * last reference to the inode is dropped, so we'll never leak
  1515. * blocks permanently.
  1516. *
  1517. * Further, check if the inode is being opened, written and
  1518. * closed frequently and we have delayed allocation blocks
  1519. * outstanding (e.g. streaming writes from the NFS server),
  1520. * truncating the blocks past EOF will cause fragmentation to
  1521. * occur.
  1522. *
  1523. * In this case don't do the truncation, either, but we have to
  1524. * be careful how we detect this case. Blocks beyond EOF show
  1525. * up as i_delayed_blks even when the inode is clean, so we
  1526. * need to truncate them away first before checking for a dirty
  1527. * release. Hence on the first dirty close we will still remove
  1528. * the speculative allocation, but after that we will leave it
  1529. * in place.
  1530. */
  1531. if (xfs_iflags_test(ip, XFS_IDIRTY_RELEASE))
  1532. return 0;
  1533. error = xfs_free_eofblocks(mp, ip, true);
  1534. if (error && error != -EAGAIN)
  1535. return error;
  1536. /* delalloc blocks after truncation means it really is dirty */
  1537. if (ip->i_delayed_blks)
  1538. xfs_iflags_set(ip, XFS_IDIRTY_RELEASE);
  1539. }
  1540. return 0;
  1541. }
  1542. /*
  1543. * xfs_inactive_truncate
  1544. *
  1545. * Called to perform a truncate when an inode becomes unlinked.
  1546. */
  1547. STATIC int
  1548. xfs_inactive_truncate(
  1549. struct xfs_inode *ip)
  1550. {
  1551. struct xfs_mount *mp = ip->i_mount;
  1552. struct xfs_trans *tp;
  1553. int error;
  1554. tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
  1555. error = xfs_trans_reserve(tp, &M_RES(mp)->tr_itruncate, 0, 0);
  1556. if (error) {
  1557. ASSERT(XFS_FORCED_SHUTDOWN(mp));
  1558. xfs_trans_cancel(tp, 0);
  1559. return error;
  1560. }
  1561. xfs_ilock(ip, XFS_ILOCK_EXCL);
  1562. xfs_trans_ijoin(tp, ip, 0);
  1563. /*
  1564. * Log the inode size first to prevent stale data exposure in the event
  1565. * of a system crash before the truncate completes. See the related
  1566. * comment in xfs_setattr_size() for details.
  1567. */
  1568. ip->i_d.di_size = 0;
  1569. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  1570. error = xfs_itruncate_extents(&tp, ip, XFS_DATA_FORK, 0);
  1571. if (error)
  1572. goto error_trans_cancel;
  1573. ASSERT(ip->i_d.di_nextents == 0);
  1574. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  1575. if (error)
  1576. goto error_unlock;
  1577. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1578. return 0;
  1579. error_trans_cancel:
  1580. xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
  1581. error_unlock:
  1582. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1583. return error;
  1584. }
  1585. /*
  1586. * xfs_inactive_ifree()
  1587. *
  1588. * Perform the inode free when an inode is unlinked.
  1589. */
  1590. STATIC int
  1591. xfs_inactive_ifree(
  1592. struct xfs_inode *ip)
  1593. {
  1594. xfs_bmap_free_t free_list;
  1595. xfs_fsblock_t first_block;
  1596. int committed;
  1597. struct xfs_mount *mp = ip->i_mount;
  1598. struct xfs_trans *tp;
  1599. int error;
  1600. tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
  1601. /*
  1602. * The ifree transaction might need to allocate blocks for record
  1603. * insertion to the finobt. We don't want to fail here at ENOSPC, so
  1604. * allow ifree to dip into the reserved block pool if necessary.
  1605. *
  1606. * Freeing large sets of inodes generally means freeing inode chunks,
  1607. * directory and file data blocks, so this should be relatively safe.
  1608. * Only under severe circumstances should it be possible to free enough
  1609. * inodes to exhaust the reserve block pool via finobt expansion while
  1610. * at the same time not creating free space in the filesystem.
  1611. *
  1612. * Send a warning if the reservation does happen to fail, as the inode
  1613. * now remains allocated and sits on the unlinked list until the fs is
  1614. * repaired.
  1615. */
  1616. tp->t_flags |= XFS_TRANS_RESERVE;
  1617. error = xfs_trans_reserve(tp, &M_RES(mp)->tr_ifree,
  1618. XFS_IFREE_SPACE_RES(mp), 0);
  1619. if (error) {
  1620. if (error == -ENOSPC) {
  1621. xfs_warn_ratelimited(mp,
  1622. "Failed to remove inode(s) from unlinked list. "
  1623. "Please free space, unmount and run xfs_repair.");
  1624. } else {
  1625. ASSERT(XFS_FORCED_SHUTDOWN(mp));
  1626. }
  1627. xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES);
  1628. return error;
  1629. }
  1630. xfs_ilock(ip, XFS_ILOCK_EXCL);
  1631. xfs_trans_ijoin(tp, ip, 0);
  1632. xfs_bmap_init(&free_list, &first_block);
  1633. error = xfs_ifree(tp, ip, &free_list);
  1634. if (error) {
  1635. /*
  1636. * If we fail to free the inode, shut down. The cancel
  1637. * might do that, we need to make sure. Otherwise the
  1638. * inode might be lost for a long time or forever.
  1639. */
  1640. if (!XFS_FORCED_SHUTDOWN(mp)) {
  1641. xfs_notice(mp, "%s: xfs_ifree returned error %d",
  1642. __func__, error);
  1643. xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
  1644. }
  1645. xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
  1646. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1647. return error;
  1648. }
  1649. /*
  1650. * Credit the quota account(s). The inode is gone.
  1651. */
  1652. xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_ICOUNT, -1);
  1653. /*
  1654. * Just ignore errors at this point. There is nothing we can
  1655. * do except to try to keep going. Make sure it's not a silent
  1656. * error.
  1657. */
  1658. error = xfs_bmap_finish(&tp, &free_list, &committed);
  1659. if (error)
  1660. xfs_notice(mp, "%s: xfs_bmap_finish returned error %d",
  1661. __func__, error);
  1662. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  1663. if (error)
  1664. xfs_notice(mp, "%s: xfs_trans_commit returned error %d",
  1665. __func__, error);
  1666. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1667. return 0;
  1668. }
  1669. /*
  1670. * xfs_inactive
  1671. *
  1672. * This is called when the vnode reference count for the vnode
  1673. * goes to zero. If the file has been unlinked, then it must
  1674. * now be truncated. Also, we clear all of the read-ahead state
  1675. * kept for the inode here since the file is now closed.
  1676. */
  1677. void
  1678. xfs_inactive(
  1679. xfs_inode_t *ip)
  1680. {
  1681. struct xfs_mount *mp;
  1682. int error;
  1683. int truncate = 0;
  1684. /*
  1685. * If the inode is already free, then there can be nothing
  1686. * to clean up here.
  1687. */
  1688. if (ip->i_d.di_mode == 0) {
  1689. ASSERT(ip->i_df.if_real_bytes == 0);
  1690. ASSERT(ip->i_df.if_broot_bytes == 0);
  1691. return;
  1692. }
  1693. mp = ip->i_mount;
  1694. /* If this is a read-only mount, don't do this (would generate I/O) */
  1695. if (mp->m_flags & XFS_MOUNT_RDONLY)
  1696. return;
  1697. if (ip->i_d.di_nlink != 0) {
  1698. /*
  1699. * force is true because we are evicting an inode from the
  1700. * cache. Post-eof blocks must be freed, lest we end up with
  1701. * broken free space accounting.
  1702. */
  1703. if (xfs_can_free_eofblocks(ip, true))
  1704. xfs_free_eofblocks(mp, ip, false);
  1705. return;
  1706. }
  1707. if (S_ISREG(ip->i_d.di_mode) &&
  1708. (ip->i_d.di_size != 0 || XFS_ISIZE(ip) != 0 ||
  1709. ip->i_d.di_nextents > 0 || ip->i_delayed_blks > 0))
  1710. truncate = 1;
  1711. error = xfs_qm_dqattach(ip, 0);
  1712. if (error)
  1713. return;
  1714. if (S_ISLNK(ip->i_d.di_mode))
  1715. error = xfs_inactive_symlink(ip);
  1716. else if (truncate)
  1717. error = xfs_inactive_truncate(ip);
  1718. if (error)
  1719. return;
  1720. /*
  1721. * If there are attributes associated with the file then blow them away
  1722. * now. The code calls a routine that recursively deconstructs the
  1723. * attribute fork. We need to just commit the current transaction
  1724. * because we can't use it for xfs_attr_inactive().
  1725. */
  1726. if (ip->i_d.di_anextents > 0) {
  1727. ASSERT(ip->i_d.di_forkoff != 0);
  1728. error = xfs_attr_inactive(ip);
  1729. if (error)
  1730. return;
  1731. }
  1732. if (ip->i_afp)
  1733. xfs_idestroy_fork(ip, XFS_ATTR_FORK);
  1734. ASSERT(ip->i_d.di_anextents == 0);
  1735. /*
  1736. * Free the inode.
  1737. */
  1738. error = xfs_inactive_ifree(ip);
  1739. if (error)
  1740. return;
  1741. /*
  1742. * Release the dquots held by inode, if any.
  1743. */
  1744. xfs_qm_dqdetach(ip);
  1745. }
  1746. /*
  1747. * This is called when the inode's link count goes to 0.
  1748. * We place the on-disk inode on a list in the AGI. It
  1749. * will be pulled from this list when the inode is freed.
  1750. */
  1751. int
  1752. xfs_iunlink(
  1753. xfs_trans_t *tp,
  1754. xfs_inode_t *ip)
  1755. {
  1756. xfs_mount_t *mp;
  1757. xfs_agi_t *agi;
  1758. xfs_dinode_t *dip;
  1759. xfs_buf_t *agibp;
  1760. xfs_buf_t *ibp;
  1761. xfs_agino_t agino;
  1762. short bucket_index;
  1763. int offset;
  1764. int error;
  1765. ASSERT(ip->i_d.di_nlink == 0);
  1766. ASSERT(ip->i_d.di_mode != 0);
  1767. mp = tp->t_mountp;
  1768. /*
  1769. * Get the agi buffer first. It ensures lock ordering
  1770. * on the list.
  1771. */
  1772. error = xfs_read_agi(mp, tp, XFS_INO_TO_AGNO(mp, ip->i_ino), &agibp);
  1773. if (error)
  1774. return error;
  1775. agi = XFS_BUF_TO_AGI(agibp);
  1776. /*
  1777. * Get the index into the agi hash table for the
  1778. * list this inode will go on.
  1779. */
  1780. agino = XFS_INO_TO_AGINO(mp, ip->i_ino);
  1781. ASSERT(agino != 0);
  1782. bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS;
  1783. ASSERT(agi->agi_unlinked[bucket_index]);
  1784. ASSERT(be32_to_cpu(agi->agi_unlinked[bucket_index]) != agino);
  1785. if (agi->agi_unlinked[bucket_index] != cpu_to_be32(NULLAGINO)) {
  1786. /*
  1787. * There is already another inode in the bucket we need
  1788. * to add ourselves to. Add us at the front of the list.
  1789. * Here we put the head pointer into our next pointer,
  1790. * and then we fall through to point the head at us.
  1791. */
  1792. error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp,
  1793. 0, 0);
  1794. if (error)
  1795. return error;
  1796. ASSERT(dip->di_next_unlinked == cpu_to_be32(NULLAGINO));
  1797. dip->di_next_unlinked = agi->agi_unlinked[bucket_index];
  1798. offset = ip->i_imap.im_boffset +
  1799. offsetof(xfs_dinode_t, di_next_unlinked);
  1800. /* need to recalc the inode CRC if appropriate */
  1801. xfs_dinode_calc_crc(mp, dip);
  1802. xfs_trans_inode_buf(tp, ibp);
  1803. xfs_trans_log_buf(tp, ibp, offset,
  1804. (offset + sizeof(xfs_agino_t) - 1));
  1805. xfs_inobp_check(mp, ibp);
  1806. }
  1807. /*
  1808. * Point the bucket head pointer at the inode being inserted.
  1809. */
  1810. ASSERT(agino != 0);
  1811. agi->agi_unlinked[bucket_index] = cpu_to_be32(agino);
  1812. offset = offsetof(xfs_agi_t, agi_unlinked) +
  1813. (sizeof(xfs_agino_t) * bucket_index);
  1814. xfs_trans_buf_set_type(tp, agibp, XFS_BLFT_AGI_BUF);
  1815. xfs_trans_log_buf(tp, agibp, offset,
  1816. (offset + sizeof(xfs_agino_t) - 1));
  1817. return 0;
  1818. }
  1819. /*
  1820. * Pull the on-disk inode from the AGI unlinked list.
  1821. */
  1822. STATIC int
  1823. xfs_iunlink_remove(
  1824. xfs_trans_t *tp,
  1825. xfs_inode_t *ip)
  1826. {
  1827. xfs_ino_t next_ino;
  1828. xfs_mount_t *mp;
  1829. xfs_agi_t *agi;
  1830. xfs_dinode_t *dip;
  1831. xfs_buf_t *agibp;
  1832. xfs_buf_t *ibp;
  1833. xfs_agnumber_t agno;
  1834. xfs_agino_t agino;
  1835. xfs_agino_t next_agino;
  1836. xfs_buf_t *last_ibp;
  1837. xfs_dinode_t *last_dip = NULL;
  1838. short bucket_index;
  1839. int offset, last_offset = 0;
  1840. int error;
  1841. mp = tp->t_mountp;
  1842. agno = XFS_INO_TO_AGNO(mp, ip->i_ino);
  1843. /*
  1844. * Get the agi buffer first. It ensures lock ordering
  1845. * on the list.
  1846. */
  1847. error = xfs_read_agi(mp, tp, agno, &agibp);
  1848. if (error)
  1849. return error;
  1850. agi = XFS_BUF_TO_AGI(agibp);
  1851. /*
  1852. * Get the index into the agi hash table for the
  1853. * list this inode will go on.
  1854. */
  1855. agino = XFS_INO_TO_AGINO(mp, ip->i_ino);
  1856. ASSERT(agino != 0);
  1857. bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS;
  1858. ASSERT(agi->agi_unlinked[bucket_index] != cpu_to_be32(NULLAGINO));
  1859. ASSERT(agi->agi_unlinked[bucket_index]);
  1860. if (be32_to_cpu(agi->agi_unlinked[bucket_index]) == agino) {
  1861. /*
  1862. * We're at the head of the list. Get the inode's on-disk
  1863. * buffer to see if there is anyone after us on the list.
  1864. * Only modify our next pointer if it is not already NULLAGINO.
  1865. * This saves us the overhead of dealing with the buffer when
  1866. * there is no need to change it.
  1867. */
  1868. error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp,
  1869. 0, 0);
  1870. if (error) {
  1871. xfs_warn(mp, "%s: xfs_imap_to_bp returned error %d.",
  1872. __func__, error);
  1873. return error;
  1874. }
  1875. next_agino = be32_to_cpu(dip->di_next_unlinked);
  1876. ASSERT(next_agino != 0);
  1877. if (next_agino != NULLAGINO) {
  1878. dip->di_next_unlinked = cpu_to_be32(NULLAGINO);
  1879. offset = ip->i_imap.im_boffset +
  1880. offsetof(xfs_dinode_t, di_next_unlinked);
  1881. /* need to recalc the inode CRC if appropriate */
  1882. xfs_dinode_calc_crc(mp, dip);
  1883. xfs_trans_inode_buf(tp, ibp);
  1884. xfs_trans_log_buf(tp, ibp, offset,
  1885. (offset + sizeof(xfs_agino_t) - 1));
  1886. xfs_inobp_check(mp, ibp);
  1887. } else {
  1888. xfs_trans_brelse(tp, ibp);
  1889. }
  1890. /*
  1891. * Point the bucket head pointer at the next inode.
  1892. */
  1893. ASSERT(next_agino != 0);
  1894. ASSERT(next_agino != agino);
  1895. agi->agi_unlinked[bucket_index] = cpu_to_be32(next_agino);
  1896. offset = offsetof(xfs_agi_t, agi_unlinked) +
  1897. (sizeof(xfs_agino_t) * bucket_index);
  1898. xfs_trans_buf_set_type(tp, agibp, XFS_BLFT_AGI_BUF);
  1899. xfs_trans_log_buf(tp, agibp, offset,
  1900. (offset + sizeof(xfs_agino_t) - 1));
  1901. } else {
  1902. /*
  1903. * We need to search the list for the inode being freed.
  1904. */
  1905. next_agino = be32_to_cpu(agi->agi_unlinked[bucket_index]);
  1906. last_ibp = NULL;
  1907. while (next_agino != agino) {
  1908. struct xfs_imap imap;
  1909. if (last_ibp)
  1910. xfs_trans_brelse(tp, last_ibp);
  1911. imap.im_blkno = 0;
  1912. next_ino = XFS_AGINO_TO_INO(mp, agno, next_agino);
  1913. error = xfs_imap(mp, tp, next_ino, &imap, 0);
  1914. if (error) {
  1915. xfs_warn(mp,
  1916. "%s: xfs_imap returned error %d.",
  1917. __func__, error);
  1918. return error;
  1919. }
  1920. error = xfs_imap_to_bp(mp, tp, &imap, &last_dip,
  1921. &last_ibp, 0, 0);
  1922. if (error) {
  1923. xfs_warn(mp,
  1924. "%s: xfs_imap_to_bp returned error %d.",
  1925. __func__, error);
  1926. return error;
  1927. }
  1928. last_offset = imap.im_boffset;
  1929. next_agino = be32_to_cpu(last_dip->di_next_unlinked);
  1930. ASSERT(next_agino != NULLAGINO);
  1931. ASSERT(next_agino != 0);
  1932. }
  1933. /*
  1934. * Now last_ibp points to the buffer previous to us on the
  1935. * unlinked list. Pull us from the list.
  1936. */
  1937. error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp,
  1938. 0, 0);
  1939. if (error) {
  1940. xfs_warn(mp, "%s: xfs_imap_to_bp(2) returned error %d.",
  1941. __func__, error);
  1942. return error;
  1943. }
  1944. next_agino = be32_to_cpu(dip->di_next_unlinked);
  1945. ASSERT(next_agino != 0);
  1946. ASSERT(next_agino != agino);
  1947. if (next_agino != NULLAGINO) {
  1948. dip->di_next_unlinked = cpu_to_be32(NULLAGINO);
  1949. offset = ip->i_imap.im_boffset +
  1950. offsetof(xfs_dinode_t, di_next_unlinked);
  1951. /* need to recalc the inode CRC if appropriate */
  1952. xfs_dinode_calc_crc(mp, dip);
  1953. xfs_trans_inode_buf(tp, ibp);
  1954. xfs_trans_log_buf(tp, ibp, offset,
  1955. (offset + sizeof(xfs_agino_t) - 1));
  1956. xfs_inobp_check(mp, ibp);
  1957. } else {
  1958. xfs_trans_brelse(tp, ibp);
  1959. }
  1960. /*
  1961. * Point the previous inode on the list to the next inode.
  1962. */
  1963. last_dip->di_next_unlinked = cpu_to_be32(next_agino);
  1964. ASSERT(next_agino != 0);
  1965. offset = last_offset + offsetof(xfs_dinode_t, di_next_unlinked);
  1966. /* need to recalc the inode CRC if appropriate */
  1967. xfs_dinode_calc_crc(mp, last_dip);
  1968. xfs_trans_inode_buf(tp, last_ibp);
  1969. xfs_trans_log_buf(tp, last_ibp, offset,
  1970. (offset + sizeof(xfs_agino_t) - 1));
  1971. xfs_inobp_check(mp, last_ibp);
  1972. }
  1973. return 0;
  1974. }
  1975. /*
  1976. * A big issue when freeing the inode cluster is that we _cannot_ skip any
  1977. * inodes that are in memory - they all must be marked stale and attached to
  1978. * the cluster buffer.
  1979. */
  1980. STATIC int
  1981. xfs_ifree_cluster(
  1982. xfs_inode_t *free_ip,
  1983. xfs_trans_t *tp,
  1984. xfs_ino_t inum)
  1985. {
  1986. xfs_mount_t *mp = free_ip->i_mount;
  1987. int blks_per_cluster;
  1988. int inodes_per_cluster;
  1989. int nbufs;
  1990. int i, j;
  1991. xfs_daddr_t blkno;
  1992. xfs_buf_t *bp;
  1993. xfs_inode_t *ip;
  1994. xfs_inode_log_item_t *iip;
  1995. xfs_log_item_t *lip;
  1996. struct xfs_perag *pag;
  1997. pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, inum));
  1998. blks_per_cluster = xfs_icluster_size_fsb(mp);
  1999. inodes_per_cluster = blks_per_cluster << mp->m_sb.sb_inopblog;
  2000. nbufs = mp->m_ialloc_blks / blks_per_cluster;
  2001. for (j = 0; j < nbufs; j++, inum += inodes_per_cluster) {
  2002. blkno = XFS_AGB_TO_DADDR(mp, XFS_INO_TO_AGNO(mp, inum),
  2003. XFS_INO_TO_AGBNO(mp, inum));
  2004. /*
  2005. * We obtain and lock the backing buffer first in the process
  2006. * here, as we have to ensure that any dirty inode that we
  2007. * can't get the flush lock on is attached to the buffer.
  2008. * If we scan the in-memory inodes first, then buffer IO can
  2009. * complete before we get a lock on it, and hence we may fail
  2010. * to mark all the active inodes on the buffer stale.
  2011. */
  2012. bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, blkno,
  2013. mp->m_bsize * blks_per_cluster,
  2014. XBF_UNMAPPED);
  2015. if (!bp)
  2016. return -ENOMEM;
  2017. /*
  2018. * This buffer may not have been correctly initialised as we
  2019. * didn't read it from disk. That's not important because we are
  2020. * only using to mark the buffer as stale in the log, and to
  2021. * attach stale cached inodes on it. That means it will never be
  2022. * dispatched for IO. If it is, we want to know about it, and we
  2023. * want it to fail. We can acheive this by adding a write
  2024. * verifier to the buffer.
  2025. */
  2026. bp->b_ops = &xfs_inode_buf_ops;
  2027. /*
  2028. * Walk the inodes already attached to the buffer and mark them
  2029. * stale. These will all have the flush locks held, so an
  2030. * in-memory inode walk can't lock them. By marking them all
  2031. * stale first, we will not attempt to lock them in the loop
  2032. * below as the XFS_ISTALE flag will be set.
  2033. */
  2034. lip = bp->b_fspriv;
  2035. while (lip) {
  2036. if (lip->li_type == XFS_LI_INODE) {
  2037. iip = (xfs_inode_log_item_t *)lip;
  2038. ASSERT(iip->ili_logged == 1);
  2039. lip->li_cb = xfs_istale_done;
  2040. xfs_trans_ail_copy_lsn(mp->m_ail,
  2041. &iip->ili_flush_lsn,
  2042. &iip->ili_item.li_lsn);
  2043. xfs_iflags_set(iip->ili_inode, XFS_ISTALE);
  2044. }
  2045. lip = lip->li_bio_list;
  2046. }
  2047. /*
  2048. * For each inode in memory attempt to add it to the inode
  2049. * buffer and set it up for being staled on buffer IO
  2050. * completion. This is safe as we've locked out tail pushing
  2051. * and flushing by locking the buffer.
  2052. *
  2053. * We have already marked every inode that was part of a
  2054. * transaction stale above, which means there is no point in
  2055. * even trying to lock them.
  2056. */
  2057. for (i = 0; i < inodes_per_cluster; i++) {
  2058. retry:
  2059. rcu_read_lock();
  2060. ip = radix_tree_lookup(&pag->pag_ici_root,
  2061. XFS_INO_TO_AGINO(mp, (inum + i)));
  2062. /* Inode not in memory, nothing to do */
  2063. if (!ip) {
  2064. rcu_read_unlock();
  2065. continue;
  2066. }
  2067. /*
  2068. * because this is an RCU protected lookup, we could
  2069. * find a recently freed or even reallocated inode
  2070. * during the lookup. We need to check under the
  2071. * i_flags_lock for a valid inode here. Skip it if it
  2072. * is not valid, the wrong inode or stale.
  2073. */
  2074. spin_lock(&ip->i_flags_lock);
  2075. if (ip->i_ino != inum + i ||
  2076. __xfs_iflags_test(ip, XFS_ISTALE)) {
  2077. spin_unlock(&ip->i_flags_lock);
  2078. rcu_read_unlock();
  2079. continue;
  2080. }
  2081. spin_unlock(&ip->i_flags_lock);
  2082. /*
  2083. * Don't try to lock/unlock the current inode, but we
  2084. * _cannot_ skip the other inodes that we did not find
  2085. * in the list attached to the buffer and are not
  2086. * already marked stale. If we can't lock it, back off
  2087. * and retry.
  2088. */
  2089. if (ip != free_ip &&
  2090. !xfs_ilock_nowait(ip, XFS_ILOCK_EXCL)) {
  2091. rcu_read_unlock();
  2092. delay(1);
  2093. goto retry;
  2094. }
  2095. rcu_read_unlock();
  2096. xfs_iflock(ip);
  2097. xfs_iflags_set(ip, XFS_ISTALE);
  2098. /*
  2099. * we don't need to attach clean inodes or those only
  2100. * with unlogged changes (which we throw away, anyway).
  2101. */
  2102. iip = ip->i_itemp;
  2103. if (!iip || xfs_inode_clean(ip)) {
  2104. ASSERT(ip != free_ip);
  2105. xfs_ifunlock(ip);
  2106. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  2107. continue;
  2108. }
  2109. iip->ili_last_fields = iip->ili_fields;
  2110. iip->ili_fields = 0;
  2111. iip->ili_logged = 1;
  2112. xfs_trans_ail_copy_lsn(mp->m_ail, &iip->ili_flush_lsn,
  2113. &iip->ili_item.li_lsn);
  2114. xfs_buf_attach_iodone(bp, xfs_istale_done,
  2115. &iip->ili_item);
  2116. if (ip != free_ip)
  2117. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  2118. }
  2119. xfs_trans_stale_inode_buf(tp, bp);
  2120. xfs_trans_binval(tp, bp);
  2121. }
  2122. xfs_perag_put(pag);
  2123. return 0;
  2124. }
  2125. /*
  2126. * This is called to return an inode to the inode free list.
  2127. * The inode should already be truncated to 0 length and have
  2128. * no pages associated with it. This routine also assumes that
  2129. * the inode is already a part of the transaction.
  2130. *
  2131. * The on-disk copy of the inode will have been added to the list
  2132. * of unlinked inodes in the AGI. We need to remove the inode from
  2133. * that list atomically with respect to freeing it here.
  2134. */
  2135. int
  2136. xfs_ifree(
  2137. xfs_trans_t *tp,
  2138. xfs_inode_t *ip,
  2139. xfs_bmap_free_t *flist)
  2140. {
  2141. int error;
  2142. int delete;
  2143. xfs_ino_t first_ino;
  2144. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  2145. ASSERT(ip->i_d.di_nlink == 0);
  2146. ASSERT(ip->i_d.di_nextents == 0);
  2147. ASSERT(ip->i_d.di_anextents == 0);
  2148. ASSERT(ip->i_d.di_size == 0 || !S_ISREG(ip->i_d.di_mode));
  2149. ASSERT(ip->i_d.di_nblocks == 0);
  2150. /*
  2151. * Pull the on-disk inode from the AGI unlinked list.
  2152. */
  2153. error = xfs_iunlink_remove(tp, ip);
  2154. if (error)
  2155. return error;
  2156. error = xfs_difree(tp, ip->i_ino, flist, &delete, &first_ino);
  2157. if (error)
  2158. return error;
  2159. ip->i_d.di_mode = 0; /* mark incore inode as free */
  2160. ip->i_d.di_flags = 0;
  2161. ip->i_d.di_dmevmask = 0;
  2162. ip->i_d.di_forkoff = 0; /* mark the attr fork not in use */
  2163. ip->i_d.di_format = XFS_DINODE_FMT_EXTENTS;
  2164. ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
  2165. /*
  2166. * Bump the generation count so no one will be confused
  2167. * by reincarnations of this inode.
  2168. */
  2169. ip->i_d.di_gen++;
  2170. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  2171. if (delete)
  2172. error = xfs_ifree_cluster(ip, tp, first_ino);
  2173. return error;
  2174. }
  2175. /*
  2176. * This is called to unpin an inode. The caller must have the inode locked
  2177. * in at least shared mode so that the buffer cannot be subsequently pinned
  2178. * once someone is waiting for it to be unpinned.
  2179. */
  2180. static void
  2181. xfs_iunpin(
  2182. struct xfs_inode *ip)
  2183. {
  2184. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
  2185. trace_xfs_inode_unpin_nowait(ip, _RET_IP_);
  2186. /* Give the log a push to start the unpinning I/O */
  2187. xfs_log_force_lsn(ip->i_mount, ip->i_itemp->ili_last_lsn, 0);
  2188. }
  2189. static void
  2190. __xfs_iunpin_wait(
  2191. struct xfs_inode *ip)
  2192. {
  2193. wait_queue_head_t *wq = bit_waitqueue(&ip->i_flags, __XFS_IPINNED_BIT);
  2194. DEFINE_WAIT_BIT(wait, &ip->i_flags, __XFS_IPINNED_BIT);
  2195. xfs_iunpin(ip);
  2196. do {
  2197. prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
  2198. if (xfs_ipincount(ip))
  2199. io_schedule();
  2200. } while (xfs_ipincount(ip));
  2201. finish_wait(wq, &wait.wait);
  2202. }
  2203. void
  2204. xfs_iunpin_wait(
  2205. struct xfs_inode *ip)
  2206. {
  2207. if (xfs_ipincount(ip))
  2208. __xfs_iunpin_wait(ip);
  2209. }
  2210. /*
  2211. * Removing an inode from the namespace involves removing the directory entry
  2212. * and dropping the link count on the inode. Removing the directory entry can
  2213. * result in locking an AGF (directory blocks were freed) and removing a link
  2214. * count can result in placing the inode on an unlinked list which results in
  2215. * locking an AGI.
  2216. *
  2217. * The big problem here is that we have an ordering constraint on AGF and AGI
  2218. * locking - inode allocation locks the AGI, then can allocate a new extent for
  2219. * new inodes, locking the AGF after the AGI. Similarly, freeing the inode
  2220. * removes the inode from the unlinked list, requiring that we lock the AGI
  2221. * first, and then freeing the inode can result in an inode chunk being freed
  2222. * and hence freeing disk space requiring that we lock an AGF.
  2223. *
  2224. * Hence the ordering that is imposed by other parts of the code is AGI before
  2225. * AGF. This means we cannot remove the directory entry before we drop the inode
  2226. * reference count and put it on the unlinked list as this results in a lock
  2227. * order of AGF then AGI, and this can deadlock against inode allocation and
  2228. * freeing. Therefore we must drop the link counts before we remove the
  2229. * directory entry.
  2230. *
  2231. * This is still safe from a transactional point of view - it is not until we
  2232. * get to xfs_bmap_finish() that we have the possibility of multiple
  2233. * transactions in this operation. Hence as long as we remove the directory
  2234. * entry and drop the link count in the first transaction of the remove
  2235. * operation, there are no transactional constraints on the ordering here.
  2236. */
  2237. int
  2238. xfs_remove(
  2239. xfs_inode_t *dp,
  2240. struct xfs_name *name,
  2241. xfs_inode_t *ip)
  2242. {
  2243. xfs_mount_t *mp = dp->i_mount;
  2244. xfs_trans_t *tp = NULL;
  2245. int is_dir = S_ISDIR(ip->i_d.di_mode);
  2246. int error = 0;
  2247. xfs_bmap_free_t free_list;
  2248. xfs_fsblock_t first_block;
  2249. int cancel_flags;
  2250. int committed;
  2251. uint resblks;
  2252. trace_xfs_remove(dp, name);
  2253. if (XFS_FORCED_SHUTDOWN(mp))
  2254. return -EIO;
  2255. error = xfs_qm_dqattach(dp, 0);
  2256. if (error)
  2257. goto std_return;
  2258. error = xfs_qm_dqattach(ip, 0);
  2259. if (error)
  2260. goto std_return;
  2261. if (is_dir)
  2262. tp = xfs_trans_alloc(mp, XFS_TRANS_RMDIR);
  2263. else
  2264. tp = xfs_trans_alloc(mp, XFS_TRANS_REMOVE);
  2265. cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
  2266. /*
  2267. * We try to get the real space reservation first,
  2268. * allowing for directory btree deletion(s) implying
  2269. * possible bmap insert(s). If we can't get the space
  2270. * reservation then we use 0 instead, and avoid the bmap
  2271. * btree insert(s) in the directory code by, if the bmap
  2272. * insert tries to happen, instead trimming the LAST
  2273. * block from the directory.
  2274. */
  2275. resblks = XFS_REMOVE_SPACE_RES(mp);
  2276. error = xfs_trans_reserve(tp, &M_RES(mp)->tr_remove, resblks, 0);
  2277. if (error == -ENOSPC) {
  2278. resblks = 0;
  2279. error = xfs_trans_reserve(tp, &M_RES(mp)->tr_remove, 0, 0);
  2280. }
  2281. if (error) {
  2282. ASSERT(error != -ENOSPC);
  2283. cancel_flags = 0;
  2284. goto out_trans_cancel;
  2285. }
  2286. xfs_lock_two_inodes(dp, ip, XFS_ILOCK_EXCL);
  2287. xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
  2288. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  2289. /*
  2290. * If we're removing a directory perform some additional validation.
  2291. */
  2292. cancel_flags |= XFS_TRANS_ABORT;
  2293. if (is_dir) {
  2294. ASSERT(ip->i_d.di_nlink >= 2);
  2295. if (ip->i_d.di_nlink != 2) {
  2296. error = -ENOTEMPTY;
  2297. goto out_trans_cancel;
  2298. }
  2299. if (!xfs_dir_isempty(ip)) {
  2300. error = -ENOTEMPTY;
  2301. goto out_trans_cancel;
  2302. }
  2303. /* Drop the link from ip's "..". */
  2304. error = xfs_droplink(tp, dp);
  2305. if (error)
  2306. goto out_trans_cancel;
  2307. /* Drop the "." link from ip to self. */
  2308. error = xfs_droplink(tp, ip);
  2309. if (error)
  2310. goto out_trans_cancel;
  2311. } else {
  2312. /*
  2313. * When removing a non-directory we need to log the parent
  2314. * inode here. For a directory this is done implicitly
  2315. * by the xfs_droplink call for the ".." entry.
  2316. */
  2317. xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
  2318. }
  2319. xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  2320. /* Drop the link from dp to ip. */
  2321. error = xfs_droplink(tp, ip);
  2322. if (error)
  2323. goto out_trans_cancel;
  2324. xfs_bmap_init(&free_list, &first_block);
  2325. error = xfs_dir_removename(tp, dp, name, ip->i_ino,
  2326. &first_block, &free_list, resblks);
  2327. if (error) {
  2328. ASSERT(error != -ENOENT);
  2329. goto out_bmap_cancel;
  2330. }
  2331. /*
  2332. * If this is a synchronous mount, make sure that the
  2333. * remove transaction goes to disk before returning to
  2334. * the user.
  2335. */
  2336. if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
  2337. xfs_trans_set_sync(tp);
  2338. error = xfs_bmap_finish(&tp, &free_list, &committed);
  2339. if (error)
  2340. goto out_bmap_cancel;
  2341. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  2342. if (error)
  2343. goto std_return;
  2344. if (is_dir && xfs_inode_is_filestream(ip))
  2345. xfs_filestream_deassociate(ip);
  2346. return 0;
  2347. out_bmap_cancel:
  2348. xfs_bmap_cancel(&free_list);
  2349. out_trans_cancel:
  2350. xfs_trans_cancel(tp, cancel_flags);
  2351. std_return:
  2352. return error;
  2353. }
  2354. /*
  2355. * Enter all inodes for a rename transaction into a sorted array.
  2356. */
  2357. #define __XFS_SORT_INODES 5
  2358. STATIC void
  2359. xfs_sort_for_rename(
  2360. struct xfs_inode *dp1, /* in: old (source) directory inode */
  2361. struct xfs_inode *dp2, /* in: new (target) directory inode */
  2362. struct xfs_inode *ip1, /* in: inode of old entry */
  2363. struct xfs_inode *ip2, /* in: inode of new entry */
  2364. struct xfs_inode *wip, /* in: whiteout inode */
  2365. struct xfs_inode **i_tab,/* out: sorted array of inodes */
  2366. int *num_inodes) /* in/out: inodes in array */
  2367. {
  2368. int i, j;
  2369. ASSERT(*num_inodes == __XFS_SORT_INODES);
  2370. memset(i_tab, 0, *num_inodes * sizeof(struct xfs_inode *));
  2371. /*
  2372. * i_tab contains a list of pointers to inodes. We initialize
  2373. * the table here & we'll sort it. We will then use it to
  2374. * order the acquisition of the inode locks.
  2375. *
  2376. * Note that the table may contain duplicates. e.g., dp1 == dp2.
  2377. */
  2378. i = 0;
  2379. i_tab[i++] = dp1;
  2380. i_tab[i++] = dp2;
  2381. i_tab[i++] = ip1;
  2382. if (ip2)
  2383. i_tab[i++] = ip2;
  2384. if (wip)
  2385. i_tab[i++] = wip;
  2386. *num_inodes = i;
  2387. /*
  2388. * Sort the elements via bubble sort. (Remember, there are at
  2389. * most 5 elements to sort, so this is adequate.)
  2390. */
  2391. for (i = 0; i < *num_inodes; i++) {
  2392. for (j = 1; j < *num_inodes; j++) {
  2393. if (i_tab[j]->i_ino < i_tab[j-1]->i_ino) {
  2394. struct xfs_inode *temp = i_tab[j];
  2395. i_tab[j] = i_tab[j-1];
  2396. i_tab[j-1] = temp;
  2397. }
  2398. }
  2399. }
  2400. }
  2401. static int
  2402. xfs_finish_rename(
  2403. struct xfs_trans *tp,
  2404. struct xfs_bmap_free *free_list)
  2405. {
  2406. int committed = 0;
  2407. int error;
  2408. /*
  2409. * If this is a synchronous mount, make sure that the rename transaction
  2410. * goes to disk before returning to the user.
  2411. */
  2412. if (tp->t_mountp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
  2413. xfs_trans_set_sync(tp);
  2414. error = xfs_bmap_finish(&tp, free_list, &committed);
  2415. if (error) {
  2416. xfs_bmap_cancel(free_list);
  2417. xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
  2418. return error;
  2419. }
  2420. return xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  2421. }
  2422. /*
  2423. * xfs_cross_rename()
  2424. *
  2425. * responsible for handling RENAME_EXCHANGE flag in renameat2() sytemcall
  2426. */
  2427. STATIC int
  2428. xfs_cross_rename(
  2429. struct xfs_trans *tp,
  2430. struct xfs_inode *dp1,
  2431. struct xfs_name *name1,
  2432. struct xfs_inode *ip1,
  2433. struct xfs_inode *dp2,
  2434. struct xfs_name *name2,
  2435. struct xfs_inode *ip2,
  2436. struct xfs_bmap_free *free_list,
  2437. xfs_fsblock_t *first_block,
  2438. int spaceres)
  2439. {
  2440. int error = 0;
  2441. int ip1_flags = 0;
  2442. int ip2_flags = 0;
  2443. int dp2_flags = 0;
  2444. /* Swap inode number for dirent in first parent */
  2445. error = xfs_dir_replace(tp, dp1, name1,
  2446. ip2->i_ino,
  2447. first_block, free_list, spaceres);
  2448. if (error)
  2449. goto out_trans_abort;
  2450. /* Swap inode number for dirent in second parent */
  2451. error = xfs_dir_replace(tp, dp2, name2,
  2452. ip1->i_ino,
  2453. first_block, free_list, spaceres);
  2454. if (error)
  2455. goto out_trans_abort;
  2456. /*
  2457. * If we're renaming one or more directories across different parents,
  2458. * update the respective ".." entries (and link counts) to match the new
  2459. * parents.
  2460. */
  2461. if (dp1 != dp2) {
  2462. dp2_flags = XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG;
  2463. if (S_ISDIR(ip2->i_d.di_mode)) {
  2464. error = xfs_dir_replace(tp, ip2, &xfs_name_dotdot,
  2465. dp1->i_ino, first_block,
  2466. free_list, spaceres);
  2467. if (error)
  2468. goto out_trans_abort;
  2469. /* transfer ip2 ".." reference to dp1 */
  2470. if (!S_ISDIR(ip1->i_d.di_mode)) {
  2471. error = xfs_droplink(tp, dp2);
  2472. if (error)
  2473. goto out_trans_abort;
  2474. error = xfs_bumplink(tp, dp1);
  2475. if (error)
  2476. goto out_trans_abort;
  2477. }
  2478. /*
  2479. * Although ip1 isn't changed here, userspace needs
  2480. * to be warned about the change, so that applications
  2481. * relying on it (like backup ones), will properly
  2482. * notify the change
  2483. */
  2484. ip1_flags |= XFS_ICHGTIME_CHG;
  2485. ip2_flags |= XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG;
  2486. }
  2487. if (S_ISDIR(ip1->i_d.di_mode)) {
  2488. error = xfs_dir_replace(tp, ip1, &xfs_name_dotdot,
  2489. dp2->i_ino, first_block,
  2490. free_list, spaceres);
  2491. if (error)
  2492. goto out_trans_abort;
  2493. /* transfer ip1 ".." reference to dp2 */
  2494. if (!S_ISDIR(ip2->i_d.di_mode)) {
  2495. error = xfs_droplink(tp, dp1);
  2496. if (error)
  2497. goto out_trans_abort;
  2498. error = xfs_bumplink(tp, dp2);
  2499. if (error)
  2500. goto out_trans_abort;
  2501. }
  2502. /*
  2503. * Although ip2 isn't changed here, userspace needs
  2504. * to be warned about the change, so that applications
  2505. * relying on it (like backup ones), will properly
  2506. * notify the change
  2507. */
  2508. ip1_flags |= XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG;
  2509. ip2_flags |= XFS_ICHGTIME_CHG;
  2510. }
  2511. }
  2512. if (ip1_flags) {
  2513. xfs_trans_ichgtime(tp, ip1, ip1_flags);
  2514. xfs_trans_log_inode(tp, ip1, XFS_ILOG_CORE);
  2515. }
  2516. if (ip2_flags) {
  2517. xfs_trans_ichgtime(tp, ip2, ip2_flags);
  2518. xfs_trans_log_inode(tp, ip2, XFS_ILOG_CORE);
  2519. }
  2520. if (dp2_flags) {
  2521. xfs_trans_ichgtime(tp, dp2, dp2_flags);
  2522. xfs_trans_log_inode(tp, dp2, XFS_ILOG_CORE);
  2523. }
  2524. xfs_trans_ichgtime(tp, dp1, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  2525. xfs_trans_log_inode(tp, dp1, XFS_ILOG_CORE);
  2526. return xfs_finish_rename(tp, free_list);
  2527. out_trans_abort:
  2528. xfs_bmap_cancel(free_list);
  2529. xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
  2530. return error;
  2531. }
  2532. /*
  2533. * xfs_rename_alloc_whiteout()
  2534. *
  2535. * Return a referenced, unlinked, unlocked inode that that can be used as a
  2536. * whiteout in a rename transaction. We use a tmpfile inode here so that if we
  2537. * crash between allocating the inode and linking it into the rename transaction
  2538. * recovery will free the inode and we won't leak it.
  2539. */
  2540. static int
  2541. xfs_rename_alloc_whiteout(
  2542. struct xfs_inode *dp,
  2543. struct xfs_inode **wip)
  2544. {
  2545. struct xfs_inode *tmpfile;
  2546. int error;
  2547. error = xfs_create_tmpfile(dp, NULL, S_IFCHR | WHITEOUT_MODE, &tmpfile);
  2548. if (error)
  2549. return error;
  2550. /* Satisfy xfs_bumplink that this is a real tmpfile */
  2551. xfs_finish_inode_setup(tmpfile);
  2552. VFS_I(tmpfile)->i_state |= I_LINKABLE;
  2553. *wip = tmpfile;
  2554. return 0;
  2555. }
  2556. /*
  2557. * xfs_rename
  2558. */
  2559. int
  2560. xfs_rename(
  2561. struct xfs_inode *src_dp,
  2562. struct xfs_name *src_name,
  2563. struct xfs_inode *src_ip,
  2564. struct xfs_inode *target_dp,
  2565. struct xfs_name *target_name,
  2566. struct xfs_inode *target_ip,
  2567. unsigned int flags)
  2568. {
  2569. struct xfs_mount *mp = src_dp->i_mount;
  2570. struct xfs_trans *tp;
  2571. struct xfs_bmap_free free_list;
  2572. xfs_fsblock_t first_block;
  2573. struct xfs_inode *wip = NULL; /* whiteout inode */
  2574. struct xfs_inode *inodes[__XFS_SORT_INODES];
  2575. int num_inodes = __XFS_SORT_INODES;
  2576. bool new_parent = (src_dp != target_dp);
  2577. bool src_is_directory = S_ISDIR(src_ip->i_d.di_mode);
  2578. int cancel_flags = 0;
  2579. int spaceres;
  2580. int error;
  2581. trace_xfs_rename(src_dp, target_dp, src_name, target_name);
  2582. if ((flags & RENAME_EXCHANGE) && !target_ip)
  2583. return -EINVAL;
  2584. /*
  2585. * If we are doing a whiteout operation, allocate the whiteout inode
  2586. * we will be placing at the target and ensure the type is set
  2587. * appropriately.
  2588. */
  2589. if (flags & RENAME_WHITEOUT) {
  2590. ASSERT(!(flags & (RENAME_NOREPLACE | RENAME_EXCHANGE)));
  2591. error = xfs_rename_alloc_whiteout(target_dp, &wip);
  2592. if (error)
  2593. return error;
  2594. /* setup target dirent info as whiteout */
  2595. src_name->type = XFS_DIR3_FT_CHRDEV;
  2596. }
  2597. xfs_sort_for_rename(src_dp, target_dp, src_ip, target_ip, wip,
  2598. inodes, &num_inodes);
  2599. tp = xfs_trans_alloc(mp, XFS_TRANS_RENAME);
  2600. spaceres = XFS_RENAME_SPACE_RES(mp, target_name->len);
  2601. error = xfs_trans_reserve(tp, &M_RES(mp)->tr_rename, spaceres, 0);
  2602. if (error == -ENOSPC) {
  2603. spaceres = 0;
  2604. error = xfs_trans_reserve(tp, &M_RES(mp)->tr_rename, 0, 0);
  2605. }
  2606. if (error)
  2607. goto out_trans_cancel;
  2608. cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
  2609. /*
  2610. * Attach the dquots to the inodes
  2611. */
  2612. error = xfs_qm_vop_rename_dqattach(inodes);
  2613. if (error)
  2614. goto out_trans_cancel;
  2615. /*
  2616. * Lock all the participating inodes. Depending upon whether
  2617. * the target_name exists in the target directory, and
  2618. * whether the target directory is the same as the source
  2619. * directory, we can lock from 2 to 4 inodes.
  2620. */
  2621. xfs_lock_inodes(inodes, num_inodes, XFS_ILOCK_EXCL);
  2622. /*
  2623. * Join all the inodes to the transaction. From this point on,
  2624. * we can rely on either trans_commit or trans_cancel to unlock
  2625. * them.
  2626. */
  2627. xfs_trans_ijoin(tp, src_dp, XFS_ILOCK_EXCL);
  2628. if (new_parent)
  2629. xfs_trans_ijoin(tp, target_dp, XFS_ILOCK_EXCL);
  2630. xfs_trans_ijoin(tp, src_ip, XFS_ILOCK_EXCL);
  2631. if (target_ip)
  2632. xfs_trans_ijoin(tp, target_ip, XFS_ILOCK_EXCL);
  2633. if (wip)
  2634. xfs_trans_ijoin(tp, wip, XFS_ILOCK_EXCL);
  2635. /*
  2636. * If we are using project inheritance, we only allow renames
  2637. * into our tree when the project IDs are the same; else the
  2638. * tree quota mechanism would be circumvented.
  2639. */
  2640. if (unlikely((target_dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
  2641. (xfs_get_projid(target_dp) != xfs_get_projid(src_ip)))) {
  2642. error = -EXDEV;
  2643. goto out_trans_cancel;
  2644. }
  2645. xfs_bmap_init(&free_list, &first_block);
  2646. /* RENAME_EXCHANGE is unique from here on. */
  2647. if (flags & RENAME_EXCHANGE)
  2648. return xfs_cross_rename(tp, src_dp, src_name, src_ip,
  2649. target_dp, target_name, target_ip,
  2650. &free_list, &first_block, spaceres);
  2651. /*
  2652. * Set up the target.
  2653. */
  2654. if (target_ip == NULL) {
  2655. /*
  2656. * If there's no space reservation, check the entry will
  2657. * fit before actually inserting it.
  2658. */
  2659. if (!spaceres) {
  2660. error = xfs_dir_canenter(tp, target_dp, target_name);
  2661. if (error)
  2662. goto out_trans_cancel;
  2663. }
  2664. /*
  2665. * If target does not exist and the rename crosses
  2666. * directories, adjust the target directory link count
  2667. * to account for the ".." reference from the new entry.
  2668. */
  2669. error = xfs_dir_createname(tp, target_dp, target_name,
  2670. src_ip->i_ino, &first_block,
  2671. &free_list, spaceres);
  2672. if (error == -ENOSPC)
  2673. goto out_bmap_cancel;
  2674. if (error)
  2675. goto out_trans_abort;
  2676. xfs_trans_ichgtime(tp, target_dp,
  2677. XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  2678. if (new_parent && src_is_directory) {
  2679. error = xfs_bumplink(tp, target_dp);
  2680. if (error)
  2681. goto out_trans_abort;
  2682. }
  2683. } else { /* target_ip != NULL */
  2684. /*
  2685. * If target exists and it's a directory, check that both
  2686. * target and source are directories and that target can be
  2687. * destroyed, or that neither is a directory.
  2688. */
  2689. if (S_ISDIR(target_ip->i_d.di_mode)) {
  2690. /*
  2691. * Make sure target dir is empty.
  2692. */
  2693. if (!(xfs_dir_isempty(target_ip)) ||
  2694. (target_ip->i_d.di_nlink > 2)) {
  2695. error = -EEXIST;
  2696. goto out_trans_cancel;
  2697. }
  2698. }
  2699. /*
  2700. * Link the source inode under the target name.
  2701. * If the source inode is a directory and we are moving
  2702. * it across directories, its ".." entry will be
  2703. * inconsistent until we replace that down below.
  2704. *
  2705. * In case there is already an entry with the same
  2706. * name at the destination directory, remove it first.
  2707. */
  2708. error = xfs_dir_replace(tp, target_dp, target_name,
  2709. src_ip->i_ino,
  2710. &first_block, &free_list, spaceres);
  2711. if (error)
  2712. goto out_trans_abort;
  2713. xfs_trans_ichgtime(tp, target_dp,
  2714. XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  2715. /*
  2716. * Decrement the link count on the target since the target
  2717. * dir no longer points to it.
  2718. */
  2719. error = xfs_droplink(tp, target_ip);
  2720. if (error)
  2721. goto out_trans_abort;
  2722. if (src_is_directory) {
  2723. /*
  2724. * Drop the link from the old "." entry.
  2725. */
  2726. error = xfs_droplink(tp, target_ip);
  2727. if (error)
  2728. goto out_trans_abort;
  2729. }
  2730. } /* target_ip != NULL */
  2731. /*
  2732. * Remove the source.
  2733. */
  2734. if (new_parent && src_is_directory) {
  2735. /*
  2736. * Rewrite the ".." entry to point to the new
  2737. * directory.
  2738. */
  2739. error = xfs_dir_replace(tp, src_ip, &xfs_name_dotdot,
  2740. target_dp->i_ino,
  2741. &first_block, &free_list, spaceres);
  2742. ASSERT(error != -EEXIST);
  2743. if (error)
  2744. goto out_trans_abort;
  2745. }
  2746. /*
  2747. * We always want to hit the ctime on the source inode.
  2748. *
  2749. * This isn't strictly required by the standards since the source
  2750. * inode isn't really being changed, but old unix file systems did
  2751. * it and some incremental backup programs won't work without it.
  2752. */
  2753. xfs_trans_ichgtime(tp, src_ip, XFS_ICHGTIME_CHG);
  2754. xfs_trans_log_inode(tp, src_ip, XFS_ILOG_CORE);
  2755. /*
  2756. * Adjust the link count on src_dp. This is necessary when
  2757. * renaming a directory, either within one parent when
  2758. * the target existed, or across two parent directories.
  2759. */
  2760. if (src_is_directory && (new_parent || target_ip != NULL)) {
  2761. /*
  2762. * Decrement link count on src_directory since the
  2763. * entry that's moved no longer points to it.
  2764. */
  2765. error = xfs_droplink(tp, src_dp);
  2766. if (error)
  2767. goto out_trans_abort;
  2768. }
  2769. /*
  2770. * For whiteouts, we only need to update the source dirent with the
  2771. * inode number of the whiteout inode rather than removing it
  2772. * altogether.
  2773. */
  2774. if (wip) {
  2775. error = xfs_dir_replace(tp, src_dp, src_name, wip->i_ino,
  2776. &first_block, &free_list, spaceres);
  2777. } else
  2778. error = xfs_dir_removename(tp, src_dp, src_name, src_ip->i_ino,
  2779. &first_block, &free_list, spaceres);
  2780. if (error)
  2781. goto out_trans_abort;
  2782. /*
  2783. * For whiteouts, we need to bump the link count on the whiteout inode.
  2784. * This means that failures all the way up to this point leave the inode
  2785. * on the unlinked list and so cleanup is a simple matter of dropping
  2786. * the remaining reference to it. If we fail here after bumping the link
  2787. * count, we're shutting down the filesystem so we'll never see the
  2788. * intermediate state on disk.
  2789. */
  2790. if (wip) {
  2791. ASSERT(wip->i_d.di_nlink == 0);
  2792. error = xfs_bumplink(tp, wip);
  2793. if (error)
  2794. goto out_trans_abort;
  2795. error = xfs_iunlink_remove(tp, wip);
  2796. if (error)
  2797. goto out_trans_abort;
  2798. xfs_trans_log_inode(tp, wip, XFS_ILOG_CORE);
  2799. /*
  2800. * Now we have a real link, clear the "I'm a tmpfile" state
  2801. * flag from the inode so it doesn't accidentally get misused in
  2802. * future.
  2803. */
  2804. VFS_I(wip)->i_state &= ~I_LINKABLE;
  2805. }
  2806. xfs_trans_ichgtime(tp, src_dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  2807. xfs_trans_log_inode(tp, src_dp, XFS_ILOG_CORE);
  2808. if (new_parent)
  2809. xfs_trans_log_inode(tp, target_dp, XFS_ILOG_CORE);
  2810. error = xfs_finish_rename(tp, &free_list);
  2811. if (wip)
  2812. IRELE(wip);
  2813. return error;
  2814. out_trans_abort:
  2815. cancel_flags |= XFS_TRANS_ABORT;
  2816. out_bmap_cancel:
  2817. xfs_bmap_cancel(&free_list);
  2818. out_trans_cancel:
  2819. xfs_trans_cancel(tp, cancel_flags);
  2820. if (wip)
  2821. IRELE(wip);
  2822. return error;
  2823. }
  2824. STATIC int
  2825. xfs_iflush_cluster(
  2826. xfs_inode_t *ip,
  2827. xfs_buf_t *bp)
  2828. {
  2829. xfs_mount_t *mp = ip->i_mount;
  2830. struct xfs_perag *pag;
  2831. unsigned long first_index, mask;
  2832. unsigned long inodes_per_cluster;
  2833. int ilist_size;
  2834. xfs_inode_t **ilist;
  2835. xfs_inode_t *iq;
  2836. int nr_found;
  2837. int clcount = 0;
  2838. int bufwasdelwri;
  2839. int i;
  2840. pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
  2841. inodes_per_cluster = mp->m_inode_cluster_size >> mp->m_sb.sb_inodelog;
  2842. ilist_size = inodes_per_cluster * sizeof(xfs_inode_t *);
  2843. ilist = kmem_alloc(ilist_size, KM_MAYFAIL|KM_NOFS);
  2844. if (!ilist)
  2845. goto out_put;
  2846. mask = ~(((mp->m_inode_cluster_size >> mp->m_sb.sb_inodelog)) - 1);
  2847. first_index = XFS_INO_TO_AGINO(mp, ip->i_ino) & mask;
  2848. rcu_read_lock();
  2849. /* really need a gang lookup range call here */
  2850. nr_found = radix_tree_gang_lookup(&pag->pag_ici_root, (void**)ilist,
  2851. first_index, inodes_per_cluster);
  2852. if (nr_found == 0)
  2853. goto out_free;
  2854. for (i = 0; i < nr_found; i++) {
  2855. iq = ilist[i];
  2856. if (iq == ip)
  2857. continue;
  2858. /*
  2859. * because this is an RCU protected lookup, we could find a
  2860. * recently freed or even reallocated inode during the lookup.
  2861. * We need to check under the i_flags_lock for a valid inode
  2862. * here. Skip it if it is not valid or the wrong inode.
  2863. */
  2864. spin_lock(&ip->i_flags_lock);
  2865. if (!ip->i_ino ||
  2866. (XFS_INO_TO_AGINO(mp, iq->i_ino) & mask) != first_index) {
  2867. spin_unlock(&ip->i_flags_lock);
  2868. continue;
  2869. }
  2870. spin_unlock(&ip->i_flags_lock);
  2871. /*
  2872. * Do an un-protected check to see if the inode is dirty and
  2873. * is a candidate for flushing. These checks will be repeated
  2874. * later after the appropriate locks are acquired.
  2875. */
  2876. if (xfs_inode_clean(iq) && xfs_ipincount(iq) == 0)
  2877. continue;
  2878. /*
  2879. * Try to get locks. If any are unavailable or it is pinned,
  2880. * then this inode cannot be flushed and is skipped.
  2881. */
  2882. if (!xfs_ilock_nowait(iq, XFS_ILOCK_SHARED))
  2883. continue;
  2884. if (!xfs_iflock_nowait(iq)) {
  2885. xfs_iunlock(iq, XFS_ILOCK_SHARED);
  2886. continue;
  2887. }
  2888. if (xfs_ipincount(iq)) {
  2889. xfs_ifunlock(iq);
  2890. xfs_iunlock(iq, XFS_ILOCK_SHARED);
  2891. continue;
  2892. }
  2893. /*
  2894. * arriving here means that this inode can be flushed. First
  2895. * re-check that it's dirty before flushing.
  2896. */
  2897. if (!xfs_inode_clean(iq)) {
  2898. int error;
  2899. error = xfs_iflush_int(iq, bp);
  2900. if (error) {
  2901. xfs_iunlock(iq, XFS_ILOCK_SHARED);
  2902. goto cluster_corrupt_out;
  2903. }
  2904. clcount++;
  2905. } else {
  2906. xfs_ifunlock(iq);
  2907. }
  2908. xfs_iunlock(iq, XFS_ILOCK_SHARED);
  2909. }
  2910. if (clcount) {
  2911. XFS_STATS_INC(xs_icluster_flushcnt);
  2912. XFS_STATS_ADD(xs_icluster_flushinode, clcount);
  2913. }
  2914. out_free:
  2915. rcu_read_unlock();
  2916. kmem_free(ilist);
  2917. out_put:
  2918. xfs_perag_put(pag);
  2919. return 0;
  2920. cluster_corrupt_out:
  2921. /*
  2922. * Corruption detected in the clustering loop. Invalidate the
  2923. * inode buffer and shut down the filesystem.
  2924. */
  2925. rcu_read_unlock();
  2926. /*
  2927. * Clean up the buffer. If it was delwri, just release it --
  2928. * brelse can handle it with no problems. If not, shut down the
  2929. * filesystem before releasing the buffer.
  2930. */
  2931. bufwasdelwri = (bp->b_flags & _XBF_DELWRI_Q);
  2932. if (bufwasdelwri)
  2933. xfs_buf_relse(bp);
  2934. xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
  2935. if (!bufwasdelwri) {
  2936. /*
  2937. * Just like incore_relse: if we have b_iodone functions,
  2938. * mark the buffer as an error and call them. Otherwise
  2939. * mark it as stale and brelse.
  2940. */
  2941. if (bp->b_iodone) {
  2942. XFS_BUF_UNDONE(bp);
  2943. xfs_buf_stale(bp);
  2944. xfs_buf_ioerror(bp, -EIO);
  2945. xfs_buf_ioend(bp);
  2946. } else {
  2947. xfs_buf_stale(bp);
  2948. xfs_buf_relse(bp);
  2949. }
  2950. }
  2951. /*
  2952. * Unlocks the flush lock
  2953. */
  2954. xfs_iflush_abort(iq, false);
  2955. kmem_free(ilist);
  2956. xfs_perag_put(pag);
  2957. return -EFSCORRUPTED;
  2958. }
  2959. /*
  2960. * Flush dirty inode metadata into the backing buffer.
  2961. *
  2962. * The caller must have the inode lock and the inode flush lock held. The
  2963. * inode lock will still be held upon return to the caller, and the inode
  2964. * flush lock will be released after the inode has reached the disk.
  2965. *
  2966. * The caller must write out the buffer returned in *bpp and release it.
  2967. */
  2968. int
  2969. xfs_iflush(
  2970. struct xfs_inode *ip,
  2971. struct xfs_buf **bpp)
  2972. {
  2973. struct xfs_mount *mp = ip->i_mount;
  2974. struct xfs_buf *bp;
  2975. struct xfs_dinode *dip;
  2976. int error;
  2977. XFS_STATS_INC(xs_iflush_count);
  2978. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
  2979. ASSERT(xfs_isiflocked(ip));
  2980. ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE ||
  2981. ip->i_d.di_nextents > XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK));
  2982. *bpp = NULL;
  2983. xfs_iunpin_wait(ip);
  2984. /*
  2985. * For stale inodes we cannot rely on the backing buffer remaining
  2986. * stale in cache for the remaining life of the stale inode and so
  2987. * xfs_imap_to_bp() below may give us a buffer that no longer contains
  2988. * inodes below. We have to check this after ensuring the inode is
  2989. * unpinned so that it is safe to reclaim the stale inode after the
  2990. * flush call.
  2991. */
  2992. if (xfs_iflags_test(ip, XFS_ISTALE)) {
  2993. xfs_ifunlock(ip);
  2994. return 0;
  2995. }
  2996. /*
  2997. * This may have been unpinned because the filesystem is shutting
  2998. * down forcibly. If that's the case we must not write this inode
  2999. * to disk, because the log record didn't make it to disk.
  3000. *
  3001. * We also have to remove the log item from the AIL in this case,
  3002. * as we wait for an empty AIL as part of the unmount process.
  3003. */
  3004. if (XFS_FORCED_SHUTDOWN(mp)) {
  3005. error = -EIO;
  3006. goto abort_out;
  3007. }
  3008. /*
  3009. * Get the buffer containing the on-disk inode.
  3010. */
  3011. error = xfs_imap_to_bp(mp, NULL, &ip->i_imap, &dip, &bp, XBF_TRYLOCK,
  3012. 0);
  3013. if (error || !bp) {
  3014. xfs_ifunlock(ip);
  3015. return error;
  3016. }
  3017. /*
  3018. * First flush out the inode that xfs_iflush was called with.
  3019. */
  3020. error = xfs_iflush_int(ip, bp);
  3021. if (error)
  3022. goto corrupt_out;
  3023. /*
  3024. * If the buffer is pinned then push on the log now so we won't
  3025. * get stuck waiting in the write for too long.
  3026. */
  3027. if (xfs_buf_ispinned(bp))
  3028. xfs_log_force(mp, 0);
  3029. /*
  3030. * inode clustering:
  3031. * see if other inodes can be gathered into this write
  3032. */
  3033. error = xfs_iflush_cluster(ip, bp);
  3034. if (error)
  3035. goto cluster_corrupt_out;
  3036. *bpp = bp;
  3037. return 0;
  3038. corrupt_out:
  3039. xfs_buf_relse(bp);
  3040. xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
  3041. cluster_corrupt_out:
  3042. error = -EFSCORRUPTED;
  3043. abort_out:
  3044. /*
  3045. * Unlocks the flush lock
  3046. */
  3047. xfs_iflush_abort(ip, false);
  3048. return error;
  3049. }
  3050. STATIC int
  3051. xfs_iflush_int(
  3052. struct xfs_inode *ip,
  3053. struct xfs_buf *bp)
  3054. {
  3055. struct xfs_inode_log_item *iip = ip->i_itemp;
  3056. struct xfs_dinode *dip;
  3057. struct xfs_mount *mp = ip->i_mount;
  3058. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
  3059. ASSERT(xfs_isiflocked(ip));
  3060. ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE ||
  3061. ip->i_d.di_nextents > XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK));
  3062. ASSERT(iip != NULL && iip->ili_fields != 0);
  3063. ASSERT(ip->i_d.di_version > 1);
  3064. /* set *dip = inode's place in the buffer */
  3065. dip = (xfs_dinode_t *)xfs_buf_offset(bp, ip->i_imap.im_boffset);
  3066. if (XFS_TEST_ERROR(dip->di_magic != cpu_to_be16(XFS_DINODE_MAGIC),
  3067. mp, XFS_ERRTAG_IFLUSH_1, XFS_RANDOM_IFLUSH_1)) {
  3068. xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
  3069. "%s: Bad inode %Lu magic number 0x%x, ptr 0x%p",
  3070. __func__, ip->i_ino, be16_to_cpu(dip->di_magic), dip);
  3071. goto corrupt_out;
  3072. }
  3073. if (XFS_TEST_ERROR(ip->i_d.di_magic != XFS_DINODE_MAGIC,
  3074. mp, XFS_ERRTAG_IFLUSH_2, XFS_RANDOM_IFLUSH_2)) {
  3075. xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
  3076. "%s: Bad inode %Lu, ptr 0x%p, magic number 0x%x",
  3077. __func__, ip->i_ino, ip, ip->i_d.di_magic);
  3078. goto corrupt_out;
  3079. }
  3080. if (S_ISREG(ip->i_d.di_mode)) {
  3081. if (XFS_TEST_ERROR(
  3082. (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS) &&
  3083. (ip->i_d.di_format != XFS_DINODE_FMT_BTREE),
  3084. mp, XFS_ERRTAG_IFLUSH_3, XFS_RANDOM_IFLUSH_3)) {
  3085. xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
  3086. "%s: Bad regular inode %Lu, ptr 0x%p",
  3087. __func__, ip->i_ino, ip);
  3088. goto corrupt_out;
  3089. }
  3090. } else if (S_ISDIR(ip->i_d.di_mode)) {
  3091. if (XFS_TEST_ERROR(
  3092. (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS) &&
  3093. (ip->i_d.di_format != XFS_DINODE_FMT_BTREE) &&
  3094. (ip->i_d.di_format != XFS_DINODE_FMT_LOCAL),
  3095. mp, XFS_ERRTAG_IFLUSH_4, XFS_RANDOM_IFLUSH_4)) {
  3096. xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
  3097. "%s: Bad directory inode %Lu, ptr 0x%p",
  3098. __func__, ip->i_ino, ip);
  3099. goto corrupt_out;
  3100. }
  3101. }
  3102. if (XFS_TEST_ERROR(ip->i_d.di_nextents + ip->i_d.di_anextents >
  3103. ip->i_d.di_nblocks, mp, XFS_ERRTAG_IFLUSH_5,
  3104. XFS_RANDOM_IFLUSH_5)) {
  3105. xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
  3106. "%s: detected corrupt incore inode %Lu, "
  3107. "total extents = %d, nblocks = %Ld, ptr 0x%p",
  3108. __func__, ip->i_ino,
  3109. ip->i_d.di_nextents + ip->i_d.di_anextents,
  3110. ip->i_d.di_nblocks, ip);
  3111. goto corrupt_out;
  3112. }
  3113. if (XFS_TEST_ERROR(ip->i_d.di_forkoff > mp->m_sb.sb_inodesize,
  3114. mp, XFS_ERRTAG_IFLUSH_6, XFS_RANDOM_IFLUSH_6)) {
  3115. xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
  3116. "%s: bad inode %Lu, forkoff 0x%x, ptr 0x%p",
  3117. __func__, ip->i_ino, ip->i_d.di_forkoff, ip);
  3118. goto corrupt_out;
  3119. }
  3120. /*
  3121. * Inode item log recovery for v2 inodes are dependent on the
  3122. * di_flushiter count for correct sequencing. We bump the flush
  3123. * iteration count so we can detect flushes which postdate a log record
  3124. * during recovery. This is redundant as we now log every change and
  3125. * hence this can't happen but we need to still do it to ensure
  3126. * backwards compatibility with old kernels that predate logging all
  3127. * inode changes.
  3128. */
  3129. if (ip->i_d.di_version < 3)
  3130. ip->i_d.di_flushiter++;
  3131. /*
  3132. * Copy the dirty parts of the inode into the on-disk
  3133. * inode. We always copy out the core of the inode,
  3134. * because if the inode is dirty at all the core must
  3135. * be.
  3136. */
  3137. xfs_dinode_to_disk(dip, &ip->i_d);
  3138. /* Wrap, we never let the log put out DI_MAX_FLUSH */
  3139. if (ip->i_d.di_flushiter == DI_MAX_FLUSH)
  3140. ip->i_d.di_flushiter = 0;
  3141. xfs_iflush_fork(ip, dip, iip, XFS_DATA_FORK);
  3142. if (XFS_IFORK_Q(ip))
  3143. xfs_iflush_fork(ip, dip, iip, XFS_ATTR_FORK);
  3144. xfs_inobp_check(mp, bp);
  3145. /*
  3146. * We've recorded everything logged in the inode, so we'd like to clear
  3147. * the ili_fields bits so we don't log and flush things unnecessarily.
  3148. * However, we can't stop logging all this information until the data
  3149. * we've copied into the disk buffer is written to disk. If we did we
  3150. * might overwrite the copy of the inode in the log with all the data
  3151. * after re-logging only part of it, and in the face of a crash we
  3152. * wouldn't have all the data we need to recover.
  3153. *
  3154. * What we do is move the bits to the ili_last_fields field. When
  3155. * logging the inode, these bits are moved back to the ili_fields field.
  3156. * In the xfs_iflush_done() routine we clear ili_last_fields, since we
  3157. * know that the information those bits represent is permanently on
  3158. * disk. As long as the flush completes before the inode is logged
  3159. * again, then both ili_fields and ili_last_fields will be cleared.
  3160. *
  3161. * We can play with the ili_fields bits here, because the inode lock
  3162. * must be held exclusively in order to set bits there and the flush
  3163. * lock protects the ili_last_fields bits. Set ili_logged so the flush
  3164. * done routine can tell whether or not to look in the AIL. Also, store
  3165. * the current LSN of the inode so that we can tell whether the item has
  3166. * moved in the AIL from xfs_iflush_done(). In order to read the lsn we
  3167. * need the AIL lock, because it is a 64 bit value that cannot be read
  3168. * atomically.
  3169. */
  3170. iip->ili_last_fields = iip->ili_fields;
  3171. iip->ili_fields = 0;
  3172. iip->ili_logged = 1;
  3173. xfs_trans_ail_copy_lsn(mp->m_ail, &iip->ili_flush_lsn,
  3174. &iip->ili_item.li_lsn);
  3175. /*
  3176. * Attach the function xfs_iflush_done to the inode's
  3177. * buffer. This will remove the inode from the AIL
  3178. * and unlock the inode's flush lock when the inode is
  3179. * completely written to disk.
  3180. */
  3181. xfs_buf_attach_iodone(bp, xfs_iflush_done, &iip->ili_item);
  3182. /* update the lsn in the on disk inode if required */
  3183. if (ip->i_d.di_version == 3)
  3184. dip->di_lsn = cpu_to_be64(iip->ili_item.li_lsn);
  3185. /* generate the checksum. */
  3186. xfs_dinode_calc_crc(mp, dip);
  3187. ASSERT(bp->b_fspriv != NULL);
  3188. ASSERT(bp->b_iodone != NULL);
  3189. return 0;
  3190. corrupt_out:
  3191. return -EFSCORRUPTED;
  3192. }