xfs_inode.c 89 KB

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