xfs_inode.c 97 KB

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