xfs_inode.c 98 KB

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