xfs_inode.c 100 KB

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