xfs_inode.c 92 KB

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