xfs_inode.c 91 KB

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