xfs_inode.c 98 KB

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