xfs_inode.c 81 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2000-2006 Silicon Graphics, Inc.
  4. * All Rights Reserved.
  5. */
  6. #include <linux/iversion.h>
  7. #include "xfs.h"
  8. #include "xfs_fs.h"
  9. #include "xfs_shared.h"
  10. #include "xfs_format.h"
  11. #include "xfs_log_format.h"
  12. #include "xfs_trans_resv.h"
  13. #include "xfs_mount.h"
  14. #include "xfs_defer.h"
  15. #include "xfs_inode.h"
  16. #include "xfs_dir2.h"
  17. #include "xfs_attr.h"
  18. #include "xfs_bit.h"
  19. #include "xfs_trans_space.h"
  20. #include "xfs_trans.h"
  21. #include "xfs_buf_item.h"
  22. #include "xfs_inode_item.h"
  23. #include "xfs_iunlink_item.h"
  24. #include "xfs_ialloc.h"
  25. #include "xfs_bmap.h"
  26. #include "xfs_bmap_util.h"
  27. #include "xfs_errortag.h"
  28. #include "xfs_error.h"
  29. #include "xfs_quota.h"
  30. #include "xfs_filestream.h"
  31. #include "xfs_trace.h"
  32. #include "xfs_icache.h"
  33. #include "xfs_symlink.h"
  34. #include "xfs_trans_priv.h"
  35. #include "xfs_log.h"
  36. #include "xfs_bmap_btree.h"
  37. #include "xfs_reflink.h"
  38. #include "xfs_ag.h"
  39. #include "xfs_log_priv.h"
  40. #include "xfs_health.h"
  41. #include "xfs_pnfs.h"
  42. #include "xfs_parent.h"
  43. #include "xfs_xattr.h"
  44. #include "xfs_inode_util.h"
  45. struct kmem_cache *xfs_inode_cache;
  46. /*
  47. * These two are wrapper routines around the xfs_ilock() routine used to
  48. * centralize some grungy code. They are used in places that wish to lock the
  49. * inode solely for reading the extents. The reason these places can't just
  50. * call xfs_ilock(ip, XFS_ILOCK_SHARED) is that the inode lock also guards to
  51. * bringing in of the extents from disk for a file in b-tree format. If the
  52. * inode is in b-tree format, then we need to lock the inode exclusively until
  53. * the extents are read in. Locking it exclusively all the time would limit
  54. * our parallelism unnecessarily, though. What we do instead is check to see
  55. * if the extents have been read in yet, and only lock the inode exclusively
  56. * if they have not.
  57. *
  58. * The functions return a value which should be given to the corresponding
  59. * xfs_iunlock() call.
  60. */
  61. uint
  62. xfs_ilock_data_map_shared(
  63. struct xfs_inode *ip)
  64. {
  65. uint lock_mode = XFS_ILOCK_SHARED;
  66. if (xfs_need_iread_extents(&ip->i_df))
  67. lock_mode = XFS_ILOCK_EXCL;
  68. xfs_ilock(ip, lock_mode);
  69. return lock_mode;
  70. }
  71. uint
  72. xfs_ilock_attr_map_shared(
  73. struct xfs_inode *ip)
  74. {
  75. uint lock_mode = XFS_ILOCK_SHARED;
  76. if (xfs_inode_has_attr_fork(ip) && xfs_need_iread_extents(&ip->i_af))
  77. lock_mode = XFS_ILOCK_EXCL;
  78. xfs_ilock(ip, lock_mode);
  79. return lock_mode;
  80. }
  81. /*
  82. * You can't set both SHARED and EXCL for the same lock,
  83. * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_MMAPLOCK_SHARED,
  84. * XFS_MMAPLOCK_EXCL, XFS_ILOCK_SHARED, XFS_ILOCK_EXCL are valid values
  85. * to set in lock_flags.
  86. */
  87. static inline void
  88. xfs_lock_flags_assert(
  89. uint lock_flags)
  90. {
  91. ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
  92. (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
  93. ASSERT((lock_flags & (XFS_MMAPLOCK_SHARED | XFS_MMAPLOCK_EXCL)) !=
  94. (XFS_MMAPLOCK_SHARED | XFS_MMAPLOCK_EXCL));
  95. ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
  96. (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
  97. ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_SUBCLASS_MASK)) == 0);
  98. ASSERT(lock_flags != 0);
  99. }
  100. /*
  101. * In addition to i_rwsem in the VFS inode, the xfs inode contains 2
  102. * multi-reader locks: invalidate_lock and the i_lock. This routine allows
  103. * various combinations of the locks to be obtained.
  104. *
  105. * The 3 locks should always be ordered so that the IO lock is obtained first,
  106. * the mmap lock second and the ilock last in order to prevent deadlock.
  107. *
  108. * Basic locking order:
  109. *
  110. * i_rwsem -> invalidate_lock -> page_lock -> i_ilock
  111. *
  112. * mmap_lock locking order:
  113. *
  114. * i_rwsem -> page lock -> mmap_lock
  115. * mmap_lock -> invalidate_lock -> page_lock
  116. *
  117. * The difference in mmap_lock locking order mean that we cannot hold the
  118. * invalidate_lock over syscall based read(2)/write(2) based IO. These IO paths
  119. * can fault in pages during copy in/out (for buffered IO) or require the
  120. * mmap_lock in get_user_pages() to map the user pages into the kernel address
  121. * space for direct IO. Similarly the i_rwsem cannot be taken inside a page
  122. * fault because page faults already hold the mmap_lock.
  123. *
  124. * Hence to serialise fully against both syscall and mmap based IO, we need to
  125. * take both the i_rwsem and the invalidate_lock. These locks should *only* be
  126. * both taken in places where we need to invalidate the page cache in a race
  127. * free manner (e.g. truncate, hole punch and other extent manipulation
  128. * functions).
  129. */
  130. void
  131. xfs_ilock(
  132. xfs_inode_t *ip,
  133. uint lock_flags)
  134. {
  135. trace_xfs_ilock(ip, lock_flags, _RET_IP_);
  136. xfs_lock_flags_assert(lock_flags);
  137. if (lock_flags & XFS_IOLOCK_EXCL) {
  138. down_write_nested(&VFS_I(ip)->i_rwsem,
  139. XFS_IOLOCK_DEP(lock_flags));
  140. } else if (lock_flags & XFS_IOLOCK_SHARED) {
  141. down_read_nested(&VFS_I(ip)->i_rwsem,
  142. XFS_IOLOCK_DEP(lock_flags));
  143. }
  144. if (lock_flags & XFS_MMAPLOCK_EXCL) {
  145. down_write_nested(&VFS_I(ip)->i_mapping->invalidate_lock,
  146. XFS_MMAPLOCK_DEP(lock_flags));
  147. } else if (lock_flags & XFS_MMAPLOCK_SHARED) {
  148. down_read_nested(&VFS_I(ip)->i_mapping->invalidate_lock,
  149. XFS_MMAPLOCK_DEP(lock_flags));
  150. }
  151. if (lock_flags & XFS_ILOCK_EXCL)
  152. down_write_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags));
  153. else if (lock_flags & XFS_ILOCK_SHARED)
  154. down_read_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags));
  155. }
  156. /*
  157. * This is just like xfs_ilock(), except that the caller
  158. * is guaranteed not to sleep. It returns 1 if it gets
  159. * the requested locks and 0 otherwise. If the IO lock is
  160. * obtained but the inode lock cannot be, then the IO lock
  161. * is dropped before returning.
  162. *
  163. * ip -- the inode being locked
  164. * lock_flags -- this parameter indicates the inode's locks to be
  165. * to be locked. See the comment for xfs_ilock() for a list
  166. * of valid values.
  167. */
  168. int
  169. xfs_ilock_nowait(
  170. xfs_inode_t *ip,
  171. uint lock_flags)
  172. {
  173. trace_xfs_ilock_nowait(ip, lock_flags, _RET_IP_);
  174. xfs_lock_flags_assert(lock_flags);
  175. if (lock_flags & XFS_IOLOCK_EXCL) {
  176. if (!down_write_trylock(&VFS_I(ip)->i_rwsem))
  177. goto out;
  178. } else if (lock_flags & XFS_IOLOCK_SHARED) {
  179. if (!down_read_trylock(&VFS_I(ip)->i_rwsem))
  180. goto out;
  181. }
  182. if (lock_flags & XFS_MMAPLOCK_EXCL) {
  183. if (!down_write_trylock(&VFS_I(ip)->i_mapping->invalidate_lock))
  184. goto out_undo_iolock;
  185. } else if (lock_flags & XFS_MMAPLOCK_SHARED) {
  186. if (!down_read_trylock(&VFS_I(ip)->i_mapping->invalidate_lock))
  187. goto out_undo_iolock;
  188. }
  189. if (lock_flags & XFS_ILOCK_EXCL) {
  190. if (!down_write_trylock(&ip->i_lock))
  191. goto out_undo_mmaplock;
  192. } else if (lock_flags & XFS_ILOCK_SHARED) {
  193. if (!down_read_trylock(&ip->i_lock))
  194. goto out_undo_mmaplock;
  195. }
  196. return 1;
  197. out_undo_mmaplock:
  198. if (lock_flags & XFS_MMAPLOCK_EXCL)
  199. up_write(&VFS_I(ip)->i_mapping->invalidate_lock);
  200. else if (lock_flags & XFS_MMAPLOCK_SHARED)
  201. up_read(&VFS_I(ip)->i_mapping->invalidate_lock);
  202. out_undo_iolock:
  203. if (lock_flags & XFS_IOLOCK_EXCL)
  204. up_write(&VFS_I(ip)->i_rwsem);
  205. else if (lock_flags & XFS_IOLOCK_SHARED)
  206. up_read(&VFS_I(ip)->i_rwsem);
  207. out:
  208. return 0;
  209. }
  210. /*
  211. * xfs_iunlock() is used to drop the inode locks acquired with
  212. * xfs_ilock() and xfs_ilock_nowait(). The caller must pass
  213. * in the flags given to xfs_ilock() or xfs_ilock_nowait() so
  214. * that we know which locks to drop.
  215. *
  216. * ip -- the inode being unlocked
  217. * lock_flags -- this parameter indicates the inode's locks to be
  218. * to be unlocked. See the comment for xfs_ilock() for a list
  219. * of valid values for this parameter.
  220. *
  221. */
  222. void
  223. xfs_iunlock(
  224. xfs_inode_t *ip,
  225. uint lock_flags)
  226. {
  227. xfs_lock_flags_assert(lock_flags);
  228. if (lock_flags & XFS_IOLOCK_EXCL)
  229. up_write(&VFS_I(ip)->i_rwsem);
  230. else if (lock_flags & XFS_IOLOCK_SHARED)
  231. up_read(&VFS_I(ip)->i_rwsem);
  232. if (lock_flags & XFS_MMAPLOCK_EXCL)
  233. up_write(&VFS_I(ip)->i_mapping->invalidate_lock);
  234. else if (lock_flags & XFS_MMAPLOCK_SHARED)
  235. up_read(&VFS_I(ip)->i_mapping->invalidate_lock);
  236. if (lock_flags & XFS_ILOCK_EXCL)
  237. up_write(&ip->i_lock);
  238. else if (lock_flags & XFS_ILOCK_SHARED)
  239. up_read(&ip->i_lock);
  240. trace_xfs_iunlock(ip, lock_flags, _RET_IP_);
  241. }
  242. /*
  243. * give up write locks. the i/o lock cannot be held nested
  244. * if it is being demoted.
  245. */
  246. void
  247. xfs_ilock_demote(
  248. xfs_inode_t *ip,
  249. uint lock_flags)
  250. {
  251. ASSERT(lock_flags & (XFS_IOLOCK_EXCL|XFS_MMAPLOCK_EXCL|XFS_ILOCK_EXCL));
  252. ASSERT((lock_flags &
  253. ~(XFS_IOLOCK_EXCL|XFS_MMAPLOCK_EXCL|XFS_ILOCK_EXCL)) == 0);
  254. if (lock_flags & XFS_ILOCK_EXCL)
  255. downgrade_write(&ip->i_lock);
  256. if (lock_flags & XFS_MMAPLOCK_EXCL)
  257. downgrade_write(&VFS_I(ip)->i_mapping->invalidate_lock);
  258. if (lock_flags & XFS_IOLOCK_EXCL)
  259. downgrade_write(&VFS_I(ip)->i_rwsem);
  260. trace_xfs_ilock_demote(ip, lock_flags, _RET_IP_);
  261. }
  262. void
  263. xfs_assert_ilocked(
  264. struct xfs_inode *ip,
  265. uint lock_flags)
  266. {
  267. /*
  268. * Sometimes we assert the ILOCK is held exclusively, but we're in
  269. * a workqueue, so lockdep doesn't know we're the owner.
  270. */
  271. if (lock_flags & XFS_ILOCK_SHARED)
  272. rwsem_assert_held(&ip->i_lock);
  273. else if (lock_flags & XFS_ILOCK_EXCL)
  274. rwsem_assert_held_write_nolockdep(&ip->i_lock);
  275. if (lock_flags & XFS_MMAPLOCK_SHARED)
  276. rwsem_assert_held(&VFS_I(ip)->i_mapping->invalidate_lock);
  277. else if (lock_flags & XFS_MMAPLOCK_EXCL)
  278. rwsem_assert_held_write(&VFS_I(ip)->i_mapping->invalidate_lock);
  279. if (lock_flags & XFS_IOLOCK_SHARED)
  280. rwsem_assert_held(&VFS_I(ip)->i_rwsem);
  281. else if (lock_flags & XFS_IOLOCK_EXCL)
  282. rwsem_assert_held_write(&VFS_I(ip)->i_rwsem);
  283. }
  284. /*
  285. * xfs_lockdep_subclass_ok() is only used in an ASSERT, so is only called when
  286. * DEBUG or XFS_WARN is set. And MAX_LOCKDEP_SUBCLASSES is then only defined
  287. * when CONFIG_LOCKDEP is set. Hence the complex define below to avoid build
  288. * errors and warnings.
  289. */
  290. #if (defined(DEBUG) || defined(XFS_WARN)) && defined(CONFIG_LOCKDEP)
  291. static bool
  292. xfs_lockdep_subclass_ok(
  293. int subclass)
  294. {
  295. return subclass < MAX_LOCKDEP_SUBCLASSES;
  296. }
  297. #else
  298. #define xfs_lockdep_subclass_ok(subclass) (true)
  299. #endif
  300. /*
  301. * Bump the subclass so xfs_lock_inodes() acquires each lock with a different
  302. * value. This can be called for any type of inode lock combination, including
  303. * parent locking. Care must be taken to ensure we don't overrun the subclass
  304. * storage fields in the class mask we build.
  305. */
  306. static inline uint
  307. xfs_lock_inumorder(
  308. uint lock_mode,
  309. uint subclass)
  310. {
  311. uint class = 0;
  312. ASSERT(!(lock_mode & (XFS_ILOCK_PARENT | XFS_ILOCK_RTBITMAP |
  313. XFS_ILOCK_RTSUM)));
  314. ASSERT(xfs_lockdep_subclass_ok(subclass));
  315. if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL)) {
  316. ASSERT(subclass <= XFS_IOLOCK_MAX_SUBCLASS);
  317. class += subclass << XFS_IOLOCK_SHIFT;
  318. }
  319. if (lock_mode & (XFS_MMAPLOCK_SHARED|XFS_MMAPLOCK_EXCL)) {
  320. ASSERT(subclass <= XFS_MMAPLOCK_MAX_SUBCLASS);
  321. class += subclass << XFS_MMAPLOCK_SHIFT;
  322. }
  323. if (lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)) {
  324. ASSERT(subclass <= XFS_ILOCK_MAX_SUBCLASS);
  325. class += subclass << XFS_ILOCK_SHIFT;
  326. }
  327. return (lock_mode & ~XFS_LOCK_SUBCLASS_MASK) | class;
  328. }
  329. /*
  330. * The following routine will lock n inodes in exclusive mode. We assume the
  331. * caller calls us with the inodes in i_ino order.
  332. *
  333. * We need to detect deadlock where an inode that we lock is in the AIL and we
  334. * start waiting for another inode that is locked by a thread in a long running
  335. * transaction (such as truncate). This can result in deadlock since the long
  336. * running trans might need to wait for the inode we just locked in order to
  337. * push the tail and free space in the log.
  338. *
  339. * xfs_lock_inodes() can only be used to lock one type of lock at a time -
  340. * the iolock, the mmaplock or the ilock, but not more than one at a time. If we
  341. * lock more than one at a time, lockdep will report false positives saying we
  342. * have violated locking orders.
  343. */
  344. void
  345. xfs_lock_inodes(
  346. struct xfs_inode **ips,
  347. int inodes,
  348. uint lock_mode)
  349. {
  350. int attempts = 0;
  351. uint i;
  352. int j;
  353. bool try_lock;
  354. struct xfs_log_item *lp;
  355. /*
  356. * Currently supports between 2 and 5 inodes with exclusive locking. We
  357. * support an arbitrary depth of locking here, but absolute limits on
  358. * inodes depend on the type of locking and the limits placed by
  359. * lockdep annotations in xfs_lock_inumorder. These are all checked by
  360. * the asserts.
  361. */
  362. ASSERT(ips && inodes >= 2 && inodes <= 5);
  363. ASSERT(lock_mode & (XFS_IOLOCK_EXCL | XFS_MMAPLOCK_EXCL |
  364. XFS_ILOCK_EXCL));
  365. ASSERT(!(lock_mode & (XFS_IOLOCK_SHARED | XFS_MMAPLOCK_SHARED |
  366. XFS_ILOCK_SHARED)));
  367. ASSERT(!(lock_mode & XFS_MMAPLOCK_EXCL) ||
  368. inodes <= XFS_MMAPLOCK_MAX_SUBCLASS + 1);
  369. ASSERT(!(lock_mode & XFS_ILOCK_EXCL) ||
  370. inodes <= XFS_ILOCK_MAX_SUBCLASS + 1);
  371. if (lock_mode & XFS_IOLOCK_EXCL) {
  372. ASSERT(!(lock_mode & (XFS_MMAPLOCK_EXCL | XFS_ILOCK_EXCL)));
  373. } else if (lock_mode & XFS_MMAPLOCK_EXCL)
  374. ASSERT(!(lock_mode & XFS_ILOCK_EXCL));
  375. again:
  376. try_lock = false;
  377. i = 0;
  378. for (; i < inodes; i++) {
  379. ASSERT(ips[i]);
  380. if (i && (ips[i] == ips[i - 1])) /* Already locked */
  381. continue;
  382. /*
  383. * If try_lock is not set yet, make sure all locked inodes are
  384. * not in the AIL. If any are, set try_lock to be used later.
  385. */
  386. if (!try_lock) {
  387. for (j = (i - 1); j >= 0 && !try_lock; j--) {
  388. lp = &ips[j]->i_itemp->ili_item;
  389. if (lp && test_bit(XFS_LI_IN_AIL, &lp->li_flags))
  390. try_lock = true;
  391. }
  392. }
  393. /*
  394. * If any of the previous locks we have locked is in the AIL,
  395. * we must TRY to get the second and subsequent locks. If
  396. * we can't get any, we must release all we have
  397. * and try again.
  398. */
  399. if (!try_lock) {
  400. xfs_ilock(ips[i], xfs_lock_inumorder(lock_mode, i));
  401. continue;
  402. }
  403. /* try_lock means we have an inode locked that is in the AIL. */
  404. ASSERT(i != 0);
  405. if (xfs_ilock_nowait(ips[i], xfs_lock_inumorder(lock_mode, i)))
  406. continue;
  407. /*
  408. * Unlock all previous guys and try again. xfs_iunlock will try
  409. * to push the tail if the inode is in the AIL.
  410. */
  411. attempts++;
  412. for (j = i - 1; j >= 0; j--) {
  413. /*
  414. * Check to see if we've already unlocked this one. Not
  415. * the first one going back, and the inode ptr is the
  416. * same.
  417. */
  418. if (j != (i - 1) && ips[j] == ips[j + 1])
  419. continue;
  420. xfs_iunlock(ips[j], lock_mode);
  421. }
  422. if ((attempts % 5) == 0) {
  423. delay(1); /* Don't just spin the CPU */
  424. }
  425. goto again;
  426. }
  427. }
  428. /*
  429. * xfs_lock_two_inodes() can only be used to lock ilock. The iolock and
  430. * mmaplock must be double-locked separately since we use i_rwsem and
  431. * invalidate_lock for that. We now support taking one lock EXCL and the
  432. * other SHARED.
  433. */
  434. void
  435. xfs_lock_two_inodes(
  436. struct xfs_inode *ip0,
  437. uint ip0_mode,
  438. struct xfs_inode *ip1,
  439. uint ip1_mode)
  440. {
  441. int attempts = 0;
  442. struct xfs_log_item *lp;
  443. ASSERT(hweight32(ip0_mode) == 1);
  444. ASSERT(hweight32(ip1_mode) == 1);
  445. ASSERT(!(ip0_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL)));
  446. ASSERT(!(ip1_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL)));
  447. ASSERT(!(ip0_mode & (XFS_MMAPLOCK_SHARED|XFS_MMAPLOCK_EXCL)));
  448. ASSERT(!(ip1_mode & (XFS_MMAPLOCK_SHARED|XFS_MMAPLOCK_EXCL)));
  449. ASSERT(ip0->i_ino != ip1->i_ino);
  450. if (ip0->i_ino > ip1->i_ino) {
  451. swap(ip0, ip1);
  452. swap(ip0_mode, ip1_mode);
  453. }
  454. again:
  455. xfs_ilock(ip0, xfs_lock_inumorder(ip0_mode, 0));
  456. /*
  457. * If the first lock we have locked is in the AIL, we must TRY to get
  458. * the second lock. If we can't get it, we must release the first one
  459. * and try again.
  460. */
  461. lp = &ip0->i_itemp->ili_item;
  462. if (lp && test_bit(XFS_LI_IN_AIL, &lp->li_flags)) {
  463. if (!xfs_ilock_nowait(ip1, xfs_lock_inumorder(ip1_mode, 1))) {
  464. xfs_iunlock(ip0, ip0_mode);
  465. if ((++attempts % 5) == 0)
  466. delay(1); /* Don't just spin the CPU */
  467. goto again;
  468. }
  469. } else {
  470. xfs_ilock(ip1, xfs_lock_inumorder(ip1_mode, 1));
  471. }
  472. }
  473. /*
  474. * Lookups up an inode from "name". If ci_name is not NULL, then a CI match
  475. * is allowed, otherwise it has to be an exact match. If a CI match is found,
  476. * ci_name->name will point to a the actual name (caller must free) or
  477. * will be set to NULL if an exact match is found.
  478. */
  479. int
  480. xfs_lookup(
  481. struct xfs_inode *dp,
  482. const struct xfs_name *name,
  483. struct xfs_inode **ipp,
  484. struct xfs_name *ci_name)
  485. {
  486. xfs_ino_t inum;
  487. int error;
  488. trace_xfs_lookup(dp, name);
  489. if (xfs_is_shutdown(dp->i_mount))
  490. return -EIO;
  491. if (xfs_ifork_zapped(dp, XFS_DATA_FORK))
  492. return -EIO;
  493. error = xfs_dir_lookup(NULL, dp, name, &inum, ci_name);
  494. if (error)
  495. goto out_unlock;
  496. error = xfs_iget(dp->i_mount, NULL, inum, 0, 0, ipp);
  497. if (error)
  498. goto out_free_name;
  499. return 0;
  500. out_free_name:
  501. if (ci_name)
  502. kfree(ci_name->name);
  503. out_unlock:
  504. *ipp = NULL;
  505. return error;
  506. }
  507. /*
  508. * Initialise a newly allocated inode and return the in-core inode to the
  509. * caller locked exclusively.
  510. *
  511. * Caller is responsible for unlocking the inode manually upon return
  512. */
  513. int
  514. xfs_icreate(
  515. struct xfs_trans *tp,
  516. xfs_ino_t ino,
  517. const struct xfs_icreate_args *args,
  518. struct xfs_inode **ipp)
  519. {
  520. struct xfs_mount *mp = tp->t_mountp;
  521. struct xfs_inode *ip = NULL;
  522. int error;
  523. /*
  524. * Get the in-core inode with the lock held exclusively to prevent
  525. * others from looking at until we're done.
  526. */
  527. error = xfs_iget(mp, tp, ino, XFS_IGET_CREATE, XFS_ILOCK_EXCL, &ip);
  528. if (error)
  529. return error;
  530. ASSERT(ip != NULL);
  531. xfs_trans_ijoin(tp, ip, 0);
  532. xfs_inode_init(tp, args, ip);
  533. /* now that we have an i_mode we can setup the inode structure */
  534. xfs_setup_inode(ip);
  535. *ipp = ip;
  536. return 0;
  537. }
  538. /* Return dquots for the ids that will be assigned to a new file. */
  539. int
  540. xfs_icreate_dqalloc(
  541. const struct xfs_icreate_args *args,
  542. struct xfs_dquot **udqpp,
  543. struct xfs_dquot **gdqpp,
  544. struct xfs_dquot **pdqpp)
  545. {
  546. struct inode *dir = VFS_I(args->pip);
  547. kuid_t uid = GLOBAL_ROOT_UID;
  548. kgid_t gid = GLOBAL_ROOT_GID;
  549. prid_t prid = 0;
  550. unsigned int flags = XFS_QMOPT_QUOTALL;
  551. if (args->idmap) {
  552. /*
  553. * The uid/gid computation code must match what the VFS uses to
  554. * assign i_[ug]id. INHERIT adjusts the gid computation for
  555. * setgid/grpid systems.
  556. */
  557. uid = mapped_fsuid(args->idmap, i_user_ns(dir));
  558. gid = mapped_fsgid(args->idmap, i_user_ns(dir));
  559. prid = xfs_get_initial_prid(args->pip);
  560. flags |= XFS_QMOPT_INHERIT;
  561. }
  562. *udqpp = *gdqpp = *pdqpp = NULL;
  563. return xfs_qm_vop_dqalloc(args->pip, uid, gid, prid, flags, udqpp,
  564. gdqpp, pdqpp);
  565. }
  566. int
  567. xfs_create(
  568. const struct xfs_icreate_args *args,
  569. struct xfs_name *name,
  570. struct xfs_inode **ipp)
  571. {
  572. struct xfs_inode *dp = args->pip;
  573. struct xfs_dir_update du = {
  574. .dp = dp,
  575. .name = name,
  576. };
  577. struct xfs_mount *mp = dp->i_mount;
  578. struct xfs_trans *tp = NULL;
  579. struct xfs_dquot *udqp;
  580. struct xfs_dquot *gdqp;
  581. struct xfs_dquot *pdqp;
  582. struct xfs_trans_res *tres;
  583. xfs_ino_t ino;
  584. bool unlock_dp_on_error = false;
  585. bool is_dir = S_ISDIR(args->mode);
  586. uint resblks;
  587. int error;
  588. trace_xfs_create(dp, name);
  589. if (xfs_is_shutdown(mp))
  590. return -EIO;
  591. if (xfs_ifork_zapped(dp, XFS_DATA_FORK))
  592. return -EIO;
  593. /* Make sure that we have allocated dquot(s) on disk. */
  594. error = xfs_icreate_dqalloc(args, &udqp, &gdqp, &pdqp);
  595. if (error)
  596. return error;
  597. if (is_dir) {
  598. resblks = xfs_mkdir_space_res(mp, name->len);
  599. tres = &M_RES(mp)->tr_mkdir;
  600. } else {
  601. resblks = xfs_create_space_res(mp, name->len);
  602. tres = &M_RES(mp)->tr_create;
  603. }
  604. error = xfs_parent_start(mp, &du.ppargs);
  605. if (error)
  606. goto out_release_dquots;
  607. /*
  608. * Initially assume that the file does not exist and
  609. * reserve the resources for that case. If that is not
  610. * the case we'll drop the one we have and get a more
  611. * appropriate transaction later.
  612. */
  613. error = xfs_trans_alloc_icreate(mp, tres, udqp, gdqp, pdqp, resblks,
  614. &tp);
  615. if (error == -ENOSPC) {
  616. /* flush outstanding delalloc blocks and retry */
  617. xfs_flush_inodes(mp);
  618. error = xfs_trans_alloc_icreate(mp, tres, udqp, gdqp, pdqp,
  619. resblks, &tp);
  620. }
  621. if (error)
  622. goto out_parent;
  623. xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
  624. unlock_dp_on_error = true;
  625. /*
  626. * A newly created regular or special file just has one directory
  627. * entry pointing to them, but a directory also the "." entry
  628. * pointing to itself.
  629. */
  630. error = xfs_dialloc(&tp, args, &ino);
  631. if (!error)
  632. error = xfs_icreate(tp, ino, args, &du.ip);
  633. if (error)
  634. goto out_trans_cancel;
  635. /*
  636. * Now we join the directory inode to the transaction. We do not do it
  637. * earlier because xfs_dialloc might commit the previous transaction
  638. * (and release all the locks). An error from here on will result in
  639. * the transaction cancel unlocking dp so don't do it explicitly in the
  640. * error path.
  641. */
  642. xfs_trans_ijoin(tp, dp, 0);
  643. error = xfs_dir_create_child(tp, resblks, &du);
  644. if (error)
  645. goto out_trans_cancel;
  646. /*
  647. * If this is a synchronous mount, make sure that the
  648. * create transaction goes to disk before returning to
  649. * the user.
  650. */
  651. if (xfs_has_wsync(mp) || xfs_has_dirsync(mp))
  652. xfs_trans_set_sync(tp);
  653. /*
  654. * Attach the dquot(s) to the inodes and modify them incore.
  655. * These ids of the inode couldn't have changed since the new
  656. * inode has been locked ever since it was created.
  657. */
  658. xfs_qm_vop_create_dqattach(tp, du.ip, udqp, gdqp, pdqp);
  659. error = xfs_trans_commit(tp);
  660. if (error)
  661. goto out_release_inode;
  662. xfs_qm_dqrele(udqp);
  663. xfs_qm_dqrele(gdqp);
  664. xfs_qm_dqrele(pdqp);
  665. *ipp = du.ip;
  666. xfs_iunlock(du.ip, XFS_ILOCK_EXCL);
  667. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  668. xfs_parent_finish(mp, du.ppargs);
  669. return 0;
  670. out_trans_cancel:
  671. xfs_trans_cancel(tp);
  672. out_release_inode:
  673. /*
  674. * Wait until after the current transaction is aborted to finish the
  675. * setup of the inode and release the inode. This prevents recursive
  676. * transactions and deadlocks from xfs_inactive.
  677. */
  678. if (du.ip) {
  679. xfs_iunlock(du.ip, XFS_ILOCK_EXCL);
  680. xfs_finish_inode_setup(du.ip);
  681. xfs_irele(du.ip);
  682. }
  683. out_parent:
  684. xfs_parent_finish(mp, du.ppargs);
  685. out_release_dquots:
  686. xfs_qm_dqrele(udqp);
  687. xfs_qm_dqrele(gdqp);
  688. xfs_qm_dqrele(pdqp);
  689. if (unlock_dp_on_error)
  690. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  691. return error;
  692. }
  693. int
  694. xfs_create_tmpfile(
  695. const struct xfs_icreate_args *args,
  696. struct xfs_inode **ipp)
  697. {
  698. struct xfs_inode *dp = args->pip;
  699. struct xfs_mount *mp = dp->i_mount;
  700. struct xfs_inode *ip = NULL;
  701. struct xfs_trans *tp = NULL;
  702. struct xfs_dquot *udqp;
  703. struct xfs_dquot *gdqp;
  704. struct xfs_dquot *pdqp;
  705. struct xfs_trans_res *tres;
  706. xfs_ino_t ino;
  707. uint resblks;
  708. int error;
  709. ASSERT(args->flags & XFS_ICREATE_TMPFILE);
  710. if (xfs_is_shutdown(mp))
  711. return -EIO;
  712. /* Make sure that we have allocated dquot(s) on disk. */
  713. error = xfs_icreate_dqalloc(args, &udqp, &gdqp, &pdqp);
  714. if (error)
  715. return error;
  716. resblks = XFS_IALLOC_SPACE_RES(mp);
  717. tres = &M_RES(mp)->tr_create_tmpfile;
  718. error = xfs_trans_alloc_icreate(mp, tres, udqp, gdqp, pdqp, resblks,
  719. &tp);
  720. if (error)
  721. goto out_release_dquots;
  722. error = xfs_dialloc(&tp, args, &ino);
  723. if (!error)
  724. error = xfs_icreate(tp, ino, args, &ip);
  725. if (error)
  726. goto out_trans_cancel;
  727. if (xfs_has_wsync(mp))
  728. xfs_trans_set_sync(tp);
  729. /*
  730. * Attach the dquot(s) to the inodes and modify them incore.
  731. * These ids of the inode couldn't have changed since the new
  732. * inode has been locked ever since it was created.
  733. */
  734. xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp, pdqp);
  735. error = xfs_iunlink(tp, ip);
  736. if (error)
  737. goto out_trans_cancel;
  738. error = xfs_trans_commit(tp);
  739. if (error)
  740. goto out_release_inode;
  741. xfs_qm_dqrele(udqp);
  742. xfs_qm_dqrele(gdqp);
  743. xfs_qm_dqrele(pdqp);
  744. *ipp = ip;
  745. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  746. return 0;
  747. out_trans_cancel:
  748. xfs_trans_cancel(tp);
  749. out_release_inode:
  750. /*
  751. * Wait until after the current transaction is aborted to finish the
  752. * setup of the inode and release the inode. This prevents recursive
  753. * transactions and deadlocks from xfs_inactive.
  754. */
  755. if (ip) {
  756. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  757. xfs_finish_inode_setup(ip);
  758. xfs_irele(ip);
  759. }
  760. out_release_dquots:
  761. xfs_qm_dqrele(udqp);
  762. xfs_qm_dqrele(gdqp);
  763. xfs_qm_dqrele(pdqp);
  764. return error;
  765. }
  766. int
  767. xfs_link(
  768. struct xfs_inode *tdp,
  769. struct xfs_inode *sip,
  770. struct xfs_name *target_name)
  771. {
  772. struct xfs_dir_update du = {
  773. .dp = tdp,
  774. .name = target_name,
  775. .ip = sip,
  776. };
  777. struct xfs_mount *mp = tdp->i_mount;
  778. struct xfs_trans *tp;
  779. int error, nospace_error = 0;
  780. int resblks;
  781. trace_xfs_link(tdp, target_name);
  782. ASSERT(!S_ISDIR(VFS_I(sip)->i_mode));
  783. if (xfs_is_shutdown(mp))
  784. return -EIO;
  785. if (xfs_ifork_zapped(tdp, XFS_DATA_FORK))
  786. return -EIO;
  787. error = xfs_qm_dqattach(sip);
  788. if (error)
  789. goto std_return;
  790. error = xfs_qm_dqattach(tdp);
  791. if (error)
  792. goto std_return;
  793. error = xfs_parent_start(mp, &du.ppargs);
  794. if (error)
  795. goto std_return;
  796. resblks = xfs_link_space_res(mp, target_name->len);
  797. error = xfs_trans_alloc_dir(tdp, &M_RES(mp)->tr_link, sip, &resblks,
  798. &tp, &nospace_error);
  799. if (error)
  800. goto out_parent;
  801. /*
  802. * We don't allow reservationless or quotaless hardlinking when parent
  803. * pointers are enabled because we can't back out if the xattrs must
  804. * grow.
  805. */
  806. if (du.ppargs && nospace_error) {
  807. error = nospace_error;
  808. goto error_return;
  809. }
  810. /*
  811. * If we are using project inheritance, we only allow hard link
  812. * creation in our tree when the project IDs are the same; else
  813. * the tree quota mechanism could be circumvented.
  814. */
  815. if (unlikely((tdp->i_diflags & XFS_DIFLAG_PROJINHERIT) &&
  816. tdp->i_projid != sip->i_projid)) {
  817. /*
  818. * Project quota setup skips special files which can
  819. * leave inodes in a PROJINHERIT directory without a
  820. * project ID set. We need to allow links to be made
  821. * to these "project-less" inodes because userspace
  822. * expects them to succeed after project ID setup,
  823. * but everything else should be rejected.
  824. */
  825. if (!special_file(VFS_I(sip)->i_mode) ||
  826. sip->i_projid != 0) {
  827. error = -EXDEV;
  828. goto error_return;
  829. }
  830. }
  831. error = xfs_dir_add_child(tp, resblks, &du);
  832. if (error)
  833. goto error_return;
  834. /*
  835. * If this is a synchronous mount, make sure that the
  836. * link transaction goes to disk before returning to
  837. * the user.
  838. */
  839. if (xfs_has_wsync(mp) || xfs_has_dirsync(mp))
  840. xfs_trans_set_sync(tp);
  841. error = xfs_trans_commit(tp);
  842. xfs_iunlock(tdp, XFS_ILOCK_EXCL);
  843. xfs_iunlock(sip, XFS_ILOCK_EXCL);
  844. xfs_parent_finish(mp, du.ppargs);
  845. return error;
  846. error_return:
  847. xfs_trans_cancel(tp);
  848. xfs_iunlock(tdp, XFS_ILOCK_EXCL);
  849. xfs_iunlock(sip, XFS_ILOCK_EXCL);
  850. out_parent:
  851. xfs_parent_finish(mp, du.ppargs);
  852. std_return:
  853. if (error == -ENOSPC && nospace_error)
  854. error = nospace_error;
  855. return error;
  856. }
  857. /* Clear the reflink flag and the cowblocks tag if possible. */
  858. static void
  859. xfs_itruncate_clear_reflink_flags(
  860. struct xfs_inode *ip)
  861. {
  862. struct xfs_ifork *dfork;
  863. struct xfs_ifork *cfork;
  864. if (!xfs_is_reflink_inode(ip))
  865. return;
  866. dfork = xfs_ifork_ptr(ip, XFS_DATA_FORK);
  867. cfork = xfs_ifork_ptr(ip, XFS_COW_FORK);
  868. if (dfork->if_bytes == 0 && cfork->if_bytes == 0)
  869. ip->i_diflags2 &= ~XFS_DIFLAG2_REFLINK;
  870. if (cfork->if_bytes == 0)
  871. xfs_inode_clear_cowblocks_tag(ip);
  872. }
  873. /*
  874. * Free up the underlying blocks past new_size. The new size must be smaller
  875. * than the current size. This routine can be used both for the attribute and
  876. * data fork, and does not modify the inode size, which is left to the caller.
  877. *
  878. * The transaction passed to this routine must have made a permanent log
  879. * reservation of at least XFS_ITRUNCATE_LOG_RES. This routine may commit the
  880. * given transaction and start new ones, so make sure everything involved in
  881. * the transaction is tidy before calling here. Some transaction will be
  882. * returned to the caller to be committed. The incoming transaction must
  883. * already include the inode, and both inode locks must be held exclusively.
  884. * The inode must also be "held" within the transaction. On return the inode
  885. * will be "held" within the returned transaction. This routine does NOT
  886. * require any disk space to be reserved for it within the transaction.
  887. *
  888. * If we get an error, we must return with the inode locked and linked into the
  889. * current transaction. This keeps things simple for the higher level code,
  890. * because it always knows that the inode is locked and held in the transaction
  891. * that returns to it whether errors occur or not. We don't mark the inode
  892. * dirty on error so that transactions can be easily aborted if possible.
  893. */
  894. int
  895. xfs_itruncate_extents_flags(
  896. struct xfs_trans **tpp,
  897. struct xfs_inode *ip,
  898. int whichfork,
  899. xfs_fsize_t new_size,
  900. int flags)
  901. {
  902. struct xfs_mount *mp = ip->i_mount;
  903. struct xfs_trans *tp = *tpp;
  904. xfs_fileoff_t first_unmap_block;
  905. int error = 0;
  906. xfs_assert_ilocked(ip, XFS_ILOCK_EXCL);
  907. if (atomic_read(&VFS_I(ip)->i_count))
  908. xfs_assert_ilocked(ip, XFS_IOLOCK_EXCL);
  909. ASSERT(new_size <= XFS_ISIZE(ip));
  910. ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
  911. ASSERT(ip->i_itemp != NULL);
  912. ASSERT(ip->i_itemp->ili_lock_flags == 0);
  913. ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
  914. trace_xfs_itruncate_extents_start(ip, new_size);
  915. flags |= xfs_bmapi_aflag(whichfork);
  916. /*
  917. * Since it is possible for space to become allocated beyond
  918. * the end of the file (in a crash where the space is allocated
  919. * but the inode size is not yet updated), simply remove any
  920. * blocks which show up between the new EOF and the maximum
  921. * possible file size.
  922. *
  923. * We have to free all the blocks to the bmbt maximum offset, even if
  924. * the page cache can't scale that far.
  925. */
  926. first_unmap_block = XFS_B_TO_FSB(mp, (xfs_ufsize_t)new_size);
  927. if (!xfs_verify_fileoff(mp, first_unmap_block)) {
  928. WARN_ON_ONCE(first_unmap_block > XFS_MAX_FILEOFF);
  929. return 0;
  930. }
  931. error = xfs_bunmapi_range(&tp, ip, flags, first_unmap_block,
  932. XFS_MAX_FILEOFF);
  933. if (error)
  934. goto out;
  935. if (whichfork == XFS_DATA_FORK) {
  936. /* Remove all pending CoW reservations. */
  937. error = xfs_reflink_cancel_cow_blocks(ip, &tp,
  938. first_unmap_block, XFS_MAX_FILEOFF, true);
  939. if (error)
  940. goto out;
  941. xfs_itruncate_clear_reflink_flags(ip);
  942. }
  943. /*
  944. * Always re-log the inode so that our permanent transaction can keep
  945. * on rolling it forward in the log.
  946. */
  947. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  948. trace_xfs_itruncate_extents_end(ip, new_size);
  949. out:
  950. *tpp = tp;
  951. return error;
  952. }
  953. /*
  954. * Mark all the buffers attached to this directory stale. In theory we should
  955. * never be freeing a directory with any blocks at all, but this covers the
  956. * case where we've recovered a directory swap with a "temporary" directory
  957. * created by online repair and now need to dump it.
  958. */
  959. STATIC void
  960. xfs_inactive_dir(
  961. struct xfs_inode *dp)
  962. {
  963. struct xfs_iext_cursor icur;
  964. struct xfs_bmbt_irec got;
  965. struct xfs_mount *mp = dp->i_mount;
  966. struct xfs_da_geometry *geo = mp->m_dir_geo;
  967. struct xfs_ifork *ifp = xfs_ifork_ptr(dp, XFS_DATA_FORK);
  968. xfs_fileoff_t off;
  969. /*
  970. * Invalidate each directory block. All directory blocks are of
  971. * fsbcount length and alignment, so we only need to walk those same
  972. * offsets. We hold the only reference to this inode, so we must wait
  973. * for the buffer locks.
  974. */
  975. for_each_xfs_iext(ifp, &icur, &got) {
  976. for (off = round_up(got.br_startoff, geo->fsbcount);
  977. off < got.br_startoff + got.br_blockcount;
  978. off += geo->fsbcount) {
  979. struct xfs_buf *bp = NULL;
  980. xfs_fsblock_t fsbno;
  981. int error;
  982. fsbno = (off - got.br_startoff) + got.br_startblock;
  983. error = xfs_buf_incore(mp->m_ddev_targp,
  984. XFS_FSB_TO_DADDR(mp, fsbno),
  985. XFS_FSB_TO_BB(mp, geo->fsbcount),
  986. XBF_LIVESCAN, &bp);
  987. if (error)
  988. continue;
  989. xfs_buf_stale(bp);
  990. xfs_buf_relse(bp);
  991. }
  992. }
  993. }
  994. /*
  995. * xfs_inactive_truncate
  996. *
  997. * Called to perform a truncate when an inode becomes unlinked.
  998. */
  999. STATIC int
  1000. xfs_inactive_truncate(
  1001. struct xfs_inode *ip)
  1002. {
  1003. struct xfs_mount *mp = ip->i_mount;
  1004. struct xfs_trans *tp;
  1005. int error;
  1006. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, 0, 0, 0, &tp);
  1007. if (error) {
  1008. ASSERT(xfs_is_shutdown(mp));
  1009. return error;
  1010. }
  1011. xfs_ilock(ip, XFS_ILOCK_EXCL);
  1012. xfs_trans_ijoin(tp, ip, 0);
  1013. /*
  1014. * Log the inode size first to prevent stale data exposure in the event
  1015. * of a system crash before the truncate completes. See the related
  1016. * comment in xfs_vn_setattr_size() for details.
  1017. */
  1018. ip->i_disk_size = 0;
  1019. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  1020. error = xfs_itruncate_extents(&tp, ip, XFS_DATA_FORK, 0);
  1021. if (error)
  1022. goto error_trans_cancel;
  1023. ASSERT(ip->i_df.if_nextents == 0);
  1024. error = xfs_trans_commit(tp);
  1025. if (error)
  1026. goto error_unlock;
  1027. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1028. return 0;
  1029. error_trans_cancel:
  1030. xfs_trans_cancel(tp);
  1031. error_unlock:
  1032. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1033. return error;
  1034. }
  1035. /*
  1036. * xfs_inactive_ifree()
  1037. *
  1038. * Perform the inode free when an inode is unlinked.
  1039. */
  1040. STATIC int
  1041. xfs_inactive_ifree(
  1042. struct xfs_inode *ip)
  1043. {
  1044. struct xfs_mount *mp = ip->i_mount;
  1045. struct xfs_trans *tp;
  1046. int error;
  1047. /*
  1048. * We try to use a per-AG reservation for any block needed by the finobt
  1049. * tree, but as the finobt feature predates the per-AG reservation
  1050. * support a degraded file system might not have enough space for the
  1051. * reservation at mount time. In that case try to dip into the reserved
  1052. * pool and pray.
  1053. *
  1054. * Send a warning if the reservation does happen to fail, as the inode
  1055. * now remains allocated and sits on the unlinked list until the fs is
  1056. * repaired.
  1057. */
  1058. if (unlikely(mp->m_finobt_nores)) {
  1059. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ifree,
  1060. XFS_IFREE_SPACE_RES(mp), 0, XFS_TRANS_RESERVE,
  1061. &tp);
  1062. } else {
  1063. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ifree, 0, 0, 0, &tp);
  1064. }
  1065. if (error) {
  1066. if (error == -ENOSPC) {
  1067. xfs_warn_ratelimited(mp,
  1068. "Failed to remove inode(s) from unlinked list. "
  1069. "Please free space, unmount and run xfs_repair.");
  1070. } else {
  1071. ASSERT(xfs_is_shutdown(mp));
  1072. }
  1073. return error;
  1074. }
  1075. /*
  1076. * We do not hold the inode locked across the entire rolling transaction
  1077. * here. We only need to hold it for the first transaction that
  1078. * xfs_ifree() builds, which may mark the inode XFS_ISTALE if the
  1079. * underlying cluster buffer is freed. Relogging an XFS_ISTALE inode
  1080. * here breaks the relationship between cluster buffer invalidation and
  1081. * stale inode invalidation on cluster buffer item journal commit
  1082. * completion, and can result in leaving dirty stale inodes hanging
  1083. * around in memory.
  1084. *
  1085. * We have no need for serialising this inode operation against other
  1086. * operations - we freed the inode and hence reallocation is required
  1087. * and that will serialise on reallocating the space the deferops need
  1088. * to free. Hence we can unlock the inode on the first commit of
  1089. * the transaction rather than roll it right through the deferops. This
  1090. * avoids relogging the XFS_ISTALE inode.
  1091. *
  1092. * We check that xfs_ifree() hasn't grown an internal transaction roll
  1093. * by asserting that the inode is still locked when it returns.
  1094. */
  1095. xfs_ilock(ip, XFS_ILOCK_EXCL);
  1096. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  1097. error = xfs_ifree(tp, ip);
  1098. xfs_assert_ilocked(ip, XFS_ILOCK_EXCL);
  1099. if (error) {
  1100. /*
  1101. * If we fail to free the inode, shut down. The cancel
  1102. * might do that, we need to make sure. Otherwise the
  1103. * inode might be lost for a long time or forever.
  1104. */
  1105. if (!xfs_is_shutdown(mp)) {
  1106. xfs_notice(mp, "%s: xfs_ifree returned error %d",
  1107. __func__, error);
  1108. xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
  1109. }
  1110. xfs_trans_cancel(tp);
  1111. return error;
  1112. }
  1113. /*
  1114. * Credit the quota account(s). The inode is gone.
  1115. */
  1116. xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_ICOUNT, -1);
  1117. return xfs_trans_commit(tp);
  1118. }
  1119. /*
  1120. * Returns true if we need to update the on-disk metadata before we can free
  1121. * the memory used by this inode. Updates include freeing post-eof
  1122. * preallocations; freeing COW staging extents; and marking the inode free in
  1123. * the inobt if it is on the unlinked list.
  1124. */
  1125. bool
  1126. xfs_inode_needs_inactive(
  1127. struct xfs_inode *ip)
  1128. {
  1129. struct xfs_mount *mp = ip->i_mount;
  1130. struct xfs_ifork *cow_ifp = xfs_ifork_ptr(ip, XFS_COW_FORK);
  1131. /*
  1132. * If the inode is already free, then there can be nothing
  1133. * to clean up here.
  1134. */
  1135. if (VFS_I(ip)->i_mode == 0)
  1136. return false;
  1137. /*
  1138. * If this is a read-only mount, don't do this (would generate I/O)
  1139. * unless we're in log recovery and cleaning the iunlinked list.
  1140. */
  1141. if (xfs_is_readonly(mp) && !xlog_recovery_needed(mp->m_log))
  1142. return false;
  1143. /* If the log isn't running, push inodes straight to reclaim. */
  1144. if (xfs_is_shutdown(mp) || xfs_has_norecovery(mp))
  1145. return false;
  1146. /* Metadata inodes require explicit resource cleanup. */
  1147. if (xfs_is_metadata_inode(ip))
  1148. return false;
  1149. /* Want to clean out the cow blocks if there are any. */
  1150. if (cow_ifp && cow_ifp->if_bytes > 0)
  1151. return true;
  1152. /* Unlinked files must be freed. */
  1153. if (VFS_I(ip)->i_nlink == 0)
  1154. return true;
  1155. /*
  1156. * This file isn't being freed, so check if there are post-eof blocks
  1157. * to free.
  1158. *
  1159. * Note: don't bother with iolock here since lockdep complains about
  1160. * acquiring it in reclaim context. We have the only reference to the
  1161. * inode at this point anyways.
  1162. */
  1163. return xfs_can_free_eofblocks(ip);
  1164. }
  1165. /*
  1166. * Save health status somewhere, if we're dumping an inode with uncorrected
  1167. * errors and online repair isn't running.
  1168. */
  1169. static inline void
  1170. xfs_inactive_health(
  1171. struct xfs_inode *ip)
  1172. {
  1173. struct xfs_mount *mp = ip->i_mount;
  1174. struct xfs_perag *pag;
  1175. unsigned int sick;
  1176. unsigned int checked;
  1177. xfs_inode_measure_sickness(ip, &sick, &checked);
  1178. if (!sick)
  1179. return;
  1180. trace_xfs_inode_unfixed_corruption(ip, sick);
  1181. if (sick & XFS_SICK_INO_FORGET)
  1182. return;
  1183. pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
  1184. if (!pag) {
  1185. /* There had better still be a perag structure! */
  1186. ASSERT(0);
  1187. return;
  1188. }
  1189. xfs_ag_mark_sick(pag, XFS_SICK_AG_INODES);
  1190. xfs_perag_put(pag);
  1191. }
  1192. /*
  1193. * xfs_inactive
  1194. *
  1195. * This is called when the vnode reference count for the vnode
  1196. * goes to zero. If the file has been unlinked, then it must
  1197. * now be truncated. Also, we clear all of the read-ahead state
  1198. * kept for the inode here since the file is now closed.
  1199. */
  1200. int
  1201. xfs_inactive(
  1202. xfs_inode_t *ip)
  1203. {
  1204. struct xfs_mount *mp;
  1205. int error = 0;
  1206. int truncate = 0;
  1207. /*
  1208. * If the inode is already free, then there can be nothing
  1209. * to clean up here.
  1210. */
  1211. if (VFS_I(ip)->i_mode == 0) {
  1212. ASSERT(ip->i_df.if_broot_bytes == 0);
  1213. goto out;
  1214. }
  1215. mp = ip->i_mount;
  1216. ASSERT(!xfs_iflags_test(ip, XFS_IRECOVERY));
  1217. xfs_inactive_health(ip);
  1218. /*
  1219. * If this is a read-only mount, don't do this (would generate I/O)
  1220. * unless we're in log recovery and cleaning the iunlinked list.
  1221. */
  1222. if (xfs_is_readonly(mp) && !xlog_recovery_needed(mp->m_log))
  1223. goto out;
  1224. /* Metadata inodes require explicit resource cleanup. */
  1225. if (xfs_is_metadata_inode(ip))
  1226. goto out;
  1227. /* Try to clean out the cow blocks if there are any. */
  1228. if (xfs_inode_has_cow_data(ip)) {
  1229. error = xfs_reflink_cancel_cow_range(ip, 0, NULLFILEOFF, true);
  1230. if (error)
  1231. goto out;
  1232. }
  1233. if (VFS_I(ip)->i_nlink != 0) {
  1234. /*
  1235. * Note: don't bother with iolock here since lockdep complains
  1236. * about acquiring it in reclaim context. We have the only
  1237. * reference to the inode at this point anyways.
  1238. */
  1239. if (xfs_can_free_eofblocks(ip))
  1240. error = xfs_free_eofblocks(ip);
  1241. goto out;
  1242. }
  1243. if (S_ISREG(VFS_I(ip)->i_mode) &&
  1244. (ip->i_disk_size != 0 || XFS_ISIZE(ip) != 0 ||
  1245. xfs_inode_has_filedata(ip)))
  1246. truncate = 1;
  1247. if (xfs_iflags_test(ip, XFS_IQUOTAUNCHECKED)) {
  1248. /*
  1249. * If this inode is being inactivated during a quotacheck and
  1250. * has not yet been scanned by quotacheck, we /must/ remove
  1251. * the dquots from the inode before inactivation changes the
  1252. * block and inode counts. Most probably this is a result of
  1253. * reloading the incore iunlinked list to purge unrecovered
  1254. * unlinked inodes.
  1255. */
  1256. xfs_qm_dqdetach(ip);
  1257. } else {
  1258. error = xfs_qm_dqattach(ip);
  1259. if (error)
  1260. goto out;
  1261. }
  1262. if (S_ISDIR(VFS_I(ip)->i_mode) && ip->i_df.if_nextents > 0) {
  1263. xfs_inactive_dir(ip);
  1264. truncate = 1;
  1265. }
  1266. if (S_ISLNK(VFS_I(ip)->i_mode))
  1267. error = xfs_inactive_symlink(ip);
  1268. else if (truncate)
  1269. error = xfs_inactive_truncate(ip);
  1270. if (error)
  1271. goto out;
  1272. /*
  1273. * If there are attributes associated with the file then blow them away
  1274. * now. The code calls a routine that recursively deconstructs the
  1275. * attribute fork. If also blows away the in-core attribute fork.
  1276. */
  1277. if (xfs_inode_has_attr_fork(ip)) {
  1278. error = xfs_attr_inactive(ip);
  1279. if (error)
  1280. goto out;
  1281. }
  1282. ASSERT(ip->i_forkoff == 0);
  1283. /*
  1284. * Free the inode.
  1285. */
  1286. error = xfs_inactive_ifree(ip);
  1287. out:
  1288. /*
  1289. * We're done making metadata updates for this inode, so we can release
  1290. * the attached dquots.
  1291. */
  1292. xfs_qm_dqdetach(ip);
  1293. return error;
  1294. }
  1295. /*
  1296. * Find an inode on the unlinked list. This does not take references to the
  1297. * inode as we have existence guarantees by holding the AGI buffer lock and that
  1298. * only unlinked, referenced inodes can be on the unlinked inode list. If we
  1299. * don't find the inode in cache, then let the caller handle the situation.
  1300. */
  1301. struct xfs_inode *
  1302. xfs_iunlink_lookup(
  1303. struct xfs_perag *pag,
  1304. xfs_agino_t agino)
  1305. {
  1306. struct xfs_inode *ip;
  1307. rcu_read_lock();
  1308. ip = radix_tree_lookup(&pag->pag_ici_root, agino);
  1309. if (!ip) {
  1310. /* Caller can handle inode not being in memory. */
  1311. rcu_read_unlock();
  1312. return NULL;
  1313. }
  1314. /*
  1315. * Inode in RCU freeing limbo should not happen. Warn about this and
  1316. * let the caller handle the failure.
  1317. */
  1318. if (WARN_ON_ONCE(!ip->i_ino)) {
  1319. rcu_read_unlock();
  1320. return NULL;
  1321. }
  1322. ASSERT(!xfs_iflags_test(ip, XFS_IRECLAIMABLE | XFS_IRECLAIM));
  1323. rcu_read_unlock();
  1324. return ip;
  1325. }
  1326. /*
  1327. * Load the inode @next_agino into the cache and set its prev_unlinked pointer
  1328. * to @prev_agino. Caller must hold the AGI to synchronize with other changes
  1329. * to the unlinked list.
  1330. */
  1331. int
  1332. xfs_iunlink_reload_next(
  1333. struct xfs_trans *tp,
  1334. struct xfs_buf *agibp,
  1335. xfs_agino_t prev_agino,
  1336. xfs_agino_t next_agino)
  1337. {
  1338. struct xfs_perag *pag = agibp->b_pag;
  1339. struct xfs_mount *mp = pag->pag_mount;
  1340. struct xfs_inode *next_ip = NULL;
  1341. xfs_ino_t ino;
  1342. int error;
  1343. ASSERT(next_agino != NULLAGINO);
  1344. #ifdef DEBUG
  1345. rcu_read_lock();
  1346. next_ip = radix_tree_lookup(&pag->pag_ici_root, next_agino);
  1347. ASSERT(next_ip == NULL);
  1348. rcu_read_unlock();
  1349. #endif
  1350. xfs_info_ratelimited(mp,
  1351. "Found unrecovered unlinked inode 0x%x in AG 0x%x. Initiating recovery.",
  1352. next_agino, pag->pag_agno);
  1353. /*
  1354. * Use an untrusted lookup just to be cautious in case the AGI has been
  1355. * corrupted and now points at a free inode. That shouldn't happen,
  1356. * but we'd rather shut down now since we're already running in a weird
  1357. * situation.
  1358. */
  1359. ino = XFS_AGINO_TO_INO(mp, pag->pag_agno, next_agino);
  1360. error = xfs_iget(mp, tp, ino, XFS_IGET_UNTRUSTED, 0, &next_ip);
  1361. if (error) {
  1362. xfs_ag_mark_sick(pag, XFS_SICK_AG_AGI);
  1363. return error;
  1364. }
  1365. /* If this is not an unlinked inode, something is very wrong. */
  1366. if (VFS_I(next_ip)->i_nlink != 0) {
  1367. xfs_ag_mark_sick(pag, XFS_SICK_AG_AGI);
  1368. error = -EFSCORRUPTED;
  1369. goto rele;
  1370. }
  1371. next_ip->i_prev_unlinked = prev_agino;
  1372. trace_xfs_iunlink_reload_next(next_ip);
  1373. rele:
  1374. ASSERT(!(VFS_I(next_ip)->i_state & I_DONTCACHE));
  1375. if (xfs_is_quotacheck_running(mp) && next_ip)
  1376. xfs_iflags_set(next_ip, XFS_IQUOTAUNCHECKED);
  1377. xfs_irele(next_ip);
  1378. return error;
  1379. }
  1380. /*
  1381. * Look up the inode number specified and if it is not already marked XFS_ISTALE
  1382. * mark it stale. We should only find clean inodes in this lookup that aren't
  1383. * already stale.
  1384. */
  1385. static void
  1386. xfs_ifree_mark_inode_stale(
  1387. struct xfs_perag *pag,
  1388. struct xfs_inode *free_ip,
  1389. xfs_ino_t inum)
  1390. {
  1391. struct xfs_mount *mp = pag->pag_mount;
  1392. struct xfs_inode_log_item *iip;
  1393. struct xfs_inode *ip;
  1394. retry:
  1395. rcu_read_lock();
  1396. ip = radix_tree_lookup(&pag->pag_ici_root, XFS_INO_TO_AGINO(mp, inum));
  1397. /* Inode not in memory, nothing to do */
  1398. if (!ip) {
  1399. rcu_read_unlock();
  1400. return;
  1401. }
  1402. /*
  1403. * because this is an RCU protected lookup, we could find a recently
  1404. * freed or even reallocated inode during the lookup. We need to check
  1405. * under the i_flags_lock for a valid inode here. Skip it if it is not
  1406. * valid, the wrong inode or stale.
  1407. */
  1408. spin_lock(&ip->i_flags_lock);
  1409. if (ip->i_ino != inum || __xfs_iflags_test(ip, XFS_ISTALE))
  1410. goto out_iflags_unlock;
  1411. /*
  1412. * Don't try to lock/unlock the current inode, but we _cannot_ skip the
  1413. * other inodes that we did not find in the list attached to the buffer
  1414. * and are not already marked stale. If we can't lock it, back off and
  1415. * retry.
  1416. */
  1417. if (ip != free_ip) {
  1418. if (!xfs_ilock_nowait(ip, XFS_ILOCK_EXCL)) {
  1419. spin_unlock(&ip->i_flags_lock);
  1420. rcu_read_unlock();
  1421. delay(1);
  1422. goto retry;
  1423. }
  1424. }
  1425. ip->i_flags |= XFS_ISTALE;
  1426. /*
  1427. * If the inode is flushing, it is already attached to the buffer. All
  1428. * we needed to do here is mark the inode stale so buffer IO completion
  1429. * will remove it from the AIL.
  1430. */
  1431. iip = ip->i_itemp;
  1432. if (__xfs_iflags_test(ip, XFS_IFLUSHING)) {
  1433. ASSERT(!list_empty(&iip->ili_item.li_bio_list));
  1434. ASSERT(iip->ili_last_fields);
  1435. goto out_iunlock;
  1436. }
  1437. /*
  1438. * Inodes not attached to the buffer can be released immediately.
  1439. * Everything else has to go through xfs_iflush_abort() on journal
  1440. * commit as the flock synchronises removal of the inode from the
  1441. * cluster buffer against inode reclaim.
  1442. */
  1443. if (!iip || list_empty(&iip->ili_item.li_bio_list))
  1444. goto out_iunlock;
  1445. __xfs_iflags_set(ip, XFS_IFLUSHING);
  1446. spin_unlock(&ip->i_flags_lock);
  1447. rcu_read_unlock();
  1448. /* we have a dirty inode in memory that has not yet been flushed. */
  1449. spin_lock(&iip->ili_lock);
  1450. iip->ili_last_fields = iip->ili_fields;
  1451. iip->ili_fields = 0;
  1452. iip->ili_fsync_fields = 0;
  1453. spin_unlock(&iip->ili_lock);
  1454. ASSERT(iip->ili_last_fields);
  1455. if (ip != free_ip)
  1456. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1457. return;
  1458. out_iunlock:
  1459. if (ip != free_ip)
  1460. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1461. out_iflags_unlock:
  1462. spin_unlock(&ip->i_flags_lock);
  1463. rcu_read_unlock();
  1464. }
  1465. /*
  1466. * A big issue when freeing the inode cluster is that we _cannot_ skip any
  1467. * inodes that are in memory - they all must be marked stale and attached to
  1468. * the cluster buffer.
  1469. */
  1470. static int
  1471. xfs_ifree_cluster(
  1472. struct xfs_trans *tp,
  1473. struct xfs_perag *pag,
  1474. struct xfs_inode *free_ip,
  1475. struct xfs_icluster *xic)
  1476. {
  1477. struct xfs_mount *mp = free_ip->i_mount;
  1478. struct xfs_ino_geometry *igeo = M_IGEO(mp);
  1479. struct xfs_buf *bp;
  1480. xfs_daddr_t blkno;
  1481. xfs_ino_t inum = xic->first_ino;
  1482. int nbufs;
  1483. int i, j;
  1484. int ioffset;
  1485. int error;
  1486. nbufs = igeo->ialloc_blks / igeo->blocks_per_cluster;
  1487. for (j = 0; j < nbufs; j++, inum += igeo->inodes_per_cluster) {
  1488. /*
  1489. * The allocation bitmap tells us which inodes of the chunk were
  1490. * physically allocated. Skip the cluster if an inode falls into
  1491. * a sparse region.
  1492. */
  1493. ioffset = inum - xic->first_ino;
  1494. if ((xic->alloc & XFS_INOBT_MASK(ioffset)) == 0) {
  1495. ASSERT(ioffset % igeo->inodes_per_cluster == 0);
  1496. continue;
  1497. }
  1498. blkno = XFS_AGB_TO_DADDR(mp, XFS_INO_TO_AGNO(mp, inum),
  1499. XFS_INO_TO_AGBNO(mp, inum));
  1500. /*
  1501. * We obtain and lock the backing buffer first in the process
  1502. * here to ensure dirty inodes attached to the buffer remain in
  1503. * the flushing state while we mark them stale.
  1504. *
  1505. * If we scan the in-memory inodes first, then buffer IO can
  1506. * complete before we get a lock on it, and hence we may fail
  1507. * to mark all the active inodes on the buffer stale.
  1508. */
  1509. error = xfs_trans_get_buf(tp, mp->m_ddev_targp, blkno,
  1510. mp->m_bsize * igeo->blocks_per_cluster,
  1511. XBF_UNMAPPED, &bp);
  1512. if (error)
  1513. return error;
  1514. /*
  1515. * This buffer may not have been correctly initialised as we
  1516. * didn't read it from disk. That's not important because we are
  1517. * only using to mark the buffer as stale in the log, and to
  1518. * attach stale cached inodes on it.
  1519. *
  1520. * For the inode that triggered the cluster freeing, this
  1521. * attachment may occur in xfs_inode_item_precommit() after we
  1522. * have marked this buffer stale. If this buffer was not in
  1523. * memory before xfs_ifree_cluster() started, it will not be
  1524. * marked XBF_DONE and this will cause problems later in
  1525. * xfs_inode_item_precommit() when we trip over a (stale, !done)
  1526. * buffer to attached to the transaction.
  1527. *
  1528. * Hence we have to mark the buffer as XFS_DONE here. This is
  1529. * safe because we are also marking the buffer as XBF_STALE and
  1530. * XFS_BLI_STALE. That means it will never be dispatched for
  1531. * IO and it won't be unlocked until the cluster freeing has
  1532. * been committed to the journal and the buffer unpinned. If it
  1533. * is written, we want to know about it, and we want it to
  1534. * fail. We can acheive this by adding a write verifier to the
  1535. * buffer.
  1536. */
  1537. bp->b_flags |= XBF_DONE;
  1538. bp->b_ops = &xfs_inode_buf_ops;
  1539. /*
  1540. * Now we need to set all the cached clean inodes as XFS_ISTALE,
  1541. * too. This requires lookups, and will skip inodes that we've
  1542. * already marked XFS_ISTALE.
  1543. */
  1544. for (i = 0; i < igeo->inodes_per_cluster; i++)
  1545. xfs_ifree_mark_inode_stale(pag, free_ip, inum + i);
  1546. xfs_trans_stale_inode_buf(tp, bp);
  1547. xfs_trans_binval(tp, bp);
  1548. }
  1549. return 0;
  1550. }
  1551. /*
  1552. * This is called to return an inode to the inode free list. The inode should
  1553. * already be truncated to 0 length and have no pages associated with it. This
  1554. * routine also assumes that the inode is already a part of the transaction.
  1555. *
  1556. * The on-disk copy of the inode will have been added to the list of unlinked
  1557. * inodes in the AGI. We need to remove the inode from that list atomically with
  1558. * respect to freeing it here.
  1559. */
  1560. int
  1561. xfs_ifree(
  1562. struct xfs_trans *tp,
  1563. struct xfs_inode *ip)
  1564. {
  1565. struct xfs_mount *mp = ip->i_mount;
  1566. struct xfs_perag *pag;
  1567. struct xfs_icluster xic = { 0 };
  1568. struct xfs_inode_log_item *iip = ip->i_itemp;
  1569. int error;
  1570. xfs_assert_ilocked(ip, XFS_ILOCK_EXCL);
  1571. ASSERT(VFS_I(ip)->i_nlink == 0);
  1572. ASSERT(ip->i_df.if_nextents == 0);
  1573. ASSERT(ip->i_disk_size == 0 || !S_ISREG(VFS_I(ip)->i_mode));
  1574. ASSERT(ip->i_nblocks == 0);
  1575. pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
  1576. error = xfs_inode_uninit(tp, pag, ip, &xic);
  1577. if (error)
  1578. goto out;
  1579. if (xfs_iflags_test(ip, XFS_IPRESERVE_DM_FIELDS))
  1580. xfs_iflags_clear(ip, XFS_IPRESERVE_DM_FIELDS);
  1581. /* Don't attempt to replay owner changes for a deleted inode */
  1582. spin_lock(&iip->ili_lock);
  1583. iip->ili_fields &= ~(XFS_ILOG_AOWNER | XFS_ILOG_DOWNER);
  1584. spin_unlock(&iip->ili_lock);
  1585. if (xic.deleted)
  1586. error = xfs_ifree_cluster(tp, pag, ip, &xic);
  1587. out:
  1588. xfs_perag_put(pag);
  1589. return error;
  1590. }
  1591. /*
  1592. * This is called to unpin an inode. The caller must have the inode locked
  1593. * in at least shared mode so that the buffer cannot be subsequently pinned
  1594. * once someone is waiting for it to be unpinned.
  1595. */
  1596. static void
  1597. xfs_iunpin(
  1598. struct xfs_inode *ip)
  1599. {
  1600. xfs_assert_ilocked(ip, XFS_ILOCK_EXCL | XFS_ILOCK_SHARED);
  1601. trace_xfs_inode_unpin_nowait(ip, _RET_IP_);
  1602. /* Give the log a push to start the unpinning I/O */
  1603. xfs_log_force_seq(ip->i_mount, ip->i_itemp->ili_commit_seq, 0, NULL);
  1604. }
  1605. static void
  1606. __xfs_iunpin_wait(
  1607. struct xfs_inode *ip)
  1608. {
  1609. wait_queue_head_t *wq = bit_waitqueue(&ip->i_flags, __XFS_IPINNED_BIT);
  1610. DEFINE_WAIT_BIT(wait, &ip->i_flags, __XFS_IPINNED_BIT);
  1611. xfs_iunpin(ip);
  1612. do {
  1613. prepare_to_wait(wq, &wait.wq_entry, TASK_UNINTERRUPTIBLE);
  1614. if (xfs_ipincount(ip))
  1615. io_schedule();
  1616. } while (xfs_ipincount(ip));
  1617. finish_wait(wq, &wait.wq_entry);
  1618. }
  1619. void
  1620. xfs_iunpin_wait(
  1621. struct xfs_inode *ip)
  1622. {
  1623. if (xfs_ipincount(ip))
  1624. __xfs_iunpin_wait(ip);
  1625. }
  1626. /*
  1627. * Removing an inode from the namespace involves removing the directory entry
  1628. * and dropping the link count on the inode. Removing the directory entry can
  1629. * result in locking an AGF (directory blocks were freed) and removing a link
  1630. * count can result in placing the inode on an unlinked list which results in
  1631. * locking an AGI.
  1632. *
  1633. * The big problem here is that we have an ordering constraint on AGF and AGI
  1634. * locking - inode allocation locks the AGI, then can allocate a new extent for
  1635. * new inodes, locking the AGF after the AGI. Similarly, freeing the inode
  1636. * removes the inode from the unlinked list, requiring that we lock the AGI
  1637. * first, and then freeing the inode can result in an inode chunk being freed
  1638. * and hence freeing disk space requiring that we lock an AGF.
  1639. *
  1640. * Hence the ordering that is imposed by other parts of the code is AGI before
  1641. * AGF. This means we cannot remove the directory entry before we drop the inode
  1642. * reference count and put it on the unlinked list as this results in a lock
  1643. * order of AGF then AGI, and this can deadlock against inode allocation and
  1644. * freeing. Therefore we must drop the link counts before we remove the
  1645. * directory entry.
  1646. *
  1647. * This is still safe from a transactional point of view - it is not until we
  1648. * get to xfs_defer_finish() that we have the possibility of multiple
  1649. * transactions in this operation. Hence as long as we remove the directory
  1650. * entry and drop the link count in the first transaction of the remove
  1651. * operation, there are no transactional constraints on the ordering here.
  1652. */
  1653. int
  1654. xfs_remove(
  1655. struct xfs_inode *dp,
  1656. struct xfs_name *name,
  1657. struct xfs_inode *ip)
  1658. {
  1659. struct xfs_dir_update du = {
  1660. .dp = dp,
  1661. .name = name,
  1662. .ip = ip,
  1663. };
  1664. struct xfs_mount *mp = dp->i_mount;
  1665. struct xfs_trans *tp = NULL;
  1666. int is_dir = S_ISDIR(VFS_I(ip)->i_mode);
  1667. int dontcare;
  1668. int error = 0;
  1669. uint resblks;
  1670. trace_xfs_remove(dp, name);
  1671. if (xfs_is_shutdown(mp))
  1672. return -EIO;
  1673. if (xfs_ifork_zapped(dp, XFS_DATA_FORK))
  1674. return -EIO;
  1675. error = xfs_qm_dqattach(dp);
  1676. if (error)
  1677. goto std_return;
  1678. error = xfs_qm_dqattach(ip);
  1679. if (error)
  1680. goto std_return;
  1681. error = xfs_parent_start(mp, &du.ppargs);
  1682. if (error)
  1683. goto std_return;
  1684. /*
  1685. * We try to get the real space reservation first, allowing for
  1686. * directory btree deletion(s) implying possible bmap insert(s). If we
  1687. * can't get the space reservation then we use 0 instead, and avoid the
  1688. * bmap btree insert(s) in the directory code by, if the bmap insert
  1689. * tries to happen, instead trimming the LAST block from the directory.
  1690. *
  1691. * Ignore EDQUOT and ENOSPC being returned via nospace_error because
  1692. * the directory code can handle a reservationless update and we don't
  1693. * want to prevent a user from trying to free space by deleting things.
  1694. */
  1695. resblks = xfs_remove_space_res(mp, name->len);
  1696. error = xfs_trans_alloc_dir(dp, &M_RES(mp)->tr_remove, ip, &resblks,
  1697. &tp, &dontcare);
  1698. if (error) {
  1699. ASSERT(error != -ENOSPC);
  1700. goto out_parent;
  1701. }
  1702. error = xfs_dir_remove_child(tp, resblks, &du);
  1703. if (error)
  1704. goto out_trans_cancel;
  1705. /*
  1706. * If this is a synchronous mount, make sure that the
  1707. * remove transaction goes to disk before returning to
  1708. * the user.
  1709. */
  1710. if (xfs_has_wsync(mp) || xfs_has_dirsync(mp))
  1711. xfs_trans_set_sync(tp);
  1712. error = xfs_trans_commit(tp);
  1713. if (error)
  1714. goto out_unlock;
  1715. if (is_dir && xfs_inode_is_filestream(ip))
  1716. xfs_filestream_deassociate(ip);
  1717. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1718. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  1719. xfs_parent_finish(mp, du.ppargs);
  1720. return 0;
  1721. out_trans_cancel:
  1722. xfs_trans_cancel(tp);
  1723. out_unlock:
  1724. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1725. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  1726. out_parent:
  1727. xfs_parent_finish(mp, du.ppargs);
  1728. std_return:
  1729. return error;
  1730. }
  1731. static inline void
  1732. xfs_iunlock_rename(
  1733. struct xfs_inode **i_tab,
  1734. int num_inodes)
  1735. {
  1736. int i;
  1737. for (i = num_inodes - 1; i >= 0; i--) {
  1738. /* Skip duplicate inodes if src and target dps are the same */
  1739. if (!i_tab[i] || (i > 0 && i_tab[i] == i_tab[i - 1]))
  1740. continue;
  1741. xfs_iunlock(i_tab[i], XFS_ILOCK_EXCL);
  1742. }
  1743. }
  1744. /*
  1745. * Enter all inodes for a rename transaction into a sorted array.
  1746. */
  1747. #define __XFS_SORT_INODES 5
  1748. STATIC void
  1749. xfs_sort_for_rename(
  1750. struct xfs_inode *dp1, /* in: old (source) directory inode */
  1751. struct xfs_inode *dp2, /* in: new (target) directory inode */
  1752. struct xfs_inode *ip1, /* in: inode of old entry */
  1753. struct xfs_inode *ip2, /* in: inode of new entry */
  1754. struct xfs_inode *wip, /* in: whiteout inode */
  1755. struct xfs_inode **i_tab,/* out: sorted array of inodes */
  1756. int *num_inodes) /* in/out: inodes in array */
  1757. {
  1758. int i;
  1759. ASSERT(*num_inodes == __XFS_SORT_INODES);
  1760. memset(i_tab, 0, *num_inodes * sizeof(struct xfs_inode *));
  1761. /*
  1762. * i_tab contains a list of pointers to inodes. We initialize
  1763. * the table here & we'll sort it. We will then use it to
  1764. * order the acquisition of the inode locks.
  1765. *
  1766. * Note that the table may contain duplicates. e.g., dp1 == dp2.
  1767. */
  1768. i = 0;
  1769. i_tab[i++] = dp1;
  1770. i_tab[i++] = dp2;
  1771. i_tab[i++] = ip1;
  1772. if (ip2)
  1773. i_tab[i++] = ip2;
  1774. if (wip)
  1775. i_tab[i++] = wip;
  1776. *num_inodes = i;
  1777. xfs_sort_inodes(i_tab, *num_inodes);
  1778. }
  1779. void
  1780. xfs_sort_inodes(
  1781. struct xfs_inode **i_tab,
  1782. unsigned int num_inodes)
  1783. {
  1784. int i, j;
  1785. ASSERT(num_inodes <= __XFS_SORT_INODES);
  1786. /*
  1787. * Sort the elements via bubble sort. (Remember, there are at
  1788. * most 5 elements to sort, so this is adequate.)
  1789. */
  1790. for (i = 0; i < num_inodes; i++) {
  1791. for (j = 1; j < num_inodes; j++) {
  1792. if (i_tab[j]->i_ino < i_tab[j-1]->i_ino)
  1793. swap(i_tab[j], i_tab[j - 1]);
  1794. }
  1795. }
  1796. }
  1797. /*
  1798. * xfs_rename_alloc_whiteout()
  1799. *
  1800. * Return a referenced, unlinked, unlocked inode that can be used as a
  1801. * whiteout in a rename transaction. We use a tmpfile inode here so that if we
  1802. * crash between allocating the inode and linking it into the rename transaction
  1803. * recovery will free the inode and we won't leak it.
  1804. */
  1805. static int
  1806. xfs_rename_alloc_whiteout(
  1807. struct mnt_idmap *idmap,
  1808. struct xfs_name *src_name,
  1809. struct xfs_inode *dp,
  1810. struct xfs_inode **wip)
  1811. {
  1812. struct xfs_icreate_args args = {
  1813. .idmap = idmap,
  1814. .pip = dp,
  1815. .mode = S_IFCHR | WHITEOUT_MODE,
  1816. .flags = XFS_ICREATE_TMPFILE,
  1817. };
  1818. struct xfs_inode *tmpfile;
  1819. struct qstr name;
  1820. int error;
  1821. error = xfs_create_tmpfile(&args, &tmpfile);
  1822. if (error)
  1823. return error;
  1824. name.name = src_name->name;
  1825. name.len = src_name->len;
  1826. error = xfs_inode_init_security(VFS_I(tmpfile), VFS_I(dp), &name);
  1827. if (error) {
  1828. xfs_finish_inode_setup(tmpfile);
  1829. xfs_irele(tmpfile);
  1830. return error;
  1831. }
  1832. /*
  1833. * Prepare the tmpfile inode as if it were created through the VFS.
  1834. * Complete the inode setup and flag it as linkable. nlink is already
  1835. * zero, so we can skip the drop_nlink.
  1836. */
  1837. xfs_setup_iops(tmpfile);
  1838. xfs_finish_inode_setup(tmpfile);
  1839. VFS_I(tmpfile)->i_state |= I_LINKABLE;
  1840. *wip = tmpfile;
  1841. return 0;
  1842. }
  1843. /*
  1844. * xfs_rename
  1845. */
  1846. int
  1847. xfs_rename(
  1848. struct mnt_idmap *idmap,
  1849. struct xfs_inode *src_dp,
  1850. struct xfs_name *src_name,
  1851. struct xfs_inode *src_ip,
  1852. struct xfs_inode *target_dp,
  1853. struct xfs_name *target_name,
  1854. struct xfs_inode *target_ip,
  1855. unsigned int flags)
  1856. {
  1857. struct xfs_dir_update du_src = {
  1858. .dp = src_dp,
  1859. .name = src_name,
  1860. .ip = src_ip,
  1861. };
  1862. struct xfs_dir_update du_tgt = {
  1863. .dp = target_dp,
  1864. .name = target_name,
  1865. .ip = target_ip,
  1866. };
  1867. struct xfs_dir_update du_wip = { };
  1868. struct xfs_mount *mp = src_dp->i_mount;
  1869. struct xfs_trans *tp;
  1870. struct xfs_inode *inodes[__XFS_SORT_INODES];
  1871. int i;
  1872. int num_inodes = __XFS_SORT_INODES;
  1873. bool new_parent = (src_dp != target_dp);
  1874. bool src_is_directory = S_ISDIR(VFS_I(src_ip)->i_mode);
  1875. int spaceres;
  1876. bool retried = false;
  1877. int error, nospace_error = 0;
  1878. trace_xfs_rename(src_dp, target_dp, src_name, target_name);
  1879. if ((flags & RENAME_EXCHANGE) && !target_ip)
  1880. return -EINVAL;
  1881. /*
  1882. * If we are doing a whiteout operation, allocate the whiteout inode
  1883. * we will be placing at the target and ensure the type is set
  1884. * appropriately.
  1885. */
  1886. if (flags & RENAME_WHITEOUT) {
  1887. error = xfs_rename_alloc_whiteout(idmap, src_name, target_dp,
  1888. &du_wip.ip);
  1889. if (error)
  1890. return error;
  1891. /* setup target dirent info as whiteout */
  1892. src_name->type = XFS_DIR3_FT_CHRDEV;
  1893. }
  1894. xfs_sort_for_rename(src_dp, target_dp, src_ip, target_ip, du_wip.ip,
  1895. inodes, &num_inodes);
  1896. error = xfs_parent_start(mp, &du_src.ppargs);
  1897. if (error)
  1898. goto out_release_wip;
  1899. if (du_wip.ip) {
  1900. error = xfs_parent_start(mp, &du_wip.ppargs);
  1901. if (error)
  1902. goto out_src_ppargs;
  1903. }
  1904. if (target_ip) {
  1905. error = xfs_parent_start(mp, &du_tgt.ppargs);
  1906. if (error)
  1907. goto out_wip_ppargs;
  1908. }
  1909. retry:
  1910. nospace_error = 0;
  1911. spaceres = xfs_rename_space_res(mp, src_name->len, target_ip != NULL,
  1912. target_name->len, du_wip.ip != NULL);
  1913. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_rename, spaceres, 0, 0, &tp);
  1914. if (error == -ENOSPC) {
  1915. nospace_error = error;
  1916. spaceres = 0;
  1917. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_rename, 0, 0, 0,
  1918. &tp);
  1919. }
  1920. if (error)
  1921. goto out_tgt_ppargs;
  1922. /*
  1923. * We don't allow reservationless renaming when parent pointers are
  1924. * enabled because we can't back out if the xattrs must grow.
  1925. */
  1926. if (du_src.ppargs && nospace_error) {
  1927. error = nospace_error;
  1928. xfs_trans_cancel(tp);
  1929. goto out_tgt_ppargs;
  1930. }
  1931. /*
  1932. * Attach the dquots to the inodes
  1933. */
  1934. error = xfs_qm_vop_rename_dqattach(inodes);
  1935. if (error) {
  1936. xfs_trans_cancel(tp);
  1937. goto out_tgt_ppargs;
  1938. }
  1939. /*
  1940. * Lock all the participating inodes. Depending upon whether
  1941. * the target_name exists in the target directory, and
  1942. * whether the target directory is the same as the source
  1943. * directory, we can lock from 2 to 5 inodes.
  1944. */
  1945. xfs_lock_inodes(inodes, num_inodes, XFS_ILOCK_EXCL);
  1946. /*
  1947. * Join all the inodes to the transaction.
  1948. */
  1949. xfs_trans_ijoin(tp, src_dp, 0);
  1950. if (new_parent)
  1951. xfs_trans_ijoin(tp, target_dp, 0);
  1952. xfs_trans_ijoin(tp, src_ip, 0);
  1953. if (target_ip)
  1954. xfs_trans_ijoin(tp, target_ip, 0);
  1955. if (du_wip.ip)
  1956. xfs_trans_ijoin(tp, du_wip.ip, 0);
  1957. /*
  1958. * If we are using project inheritance, we only allow renames
  1959. * into our tree when the project IDs are the same; else the
  1960. * tree quota mechanism would be circumvented.
  1961. */
  1962. if (unlikely((target_dp->i_diflags & XFS_DIFLAG_PROJINHERIT) &&
  1963. target_dp->i_projid != src_ip->i_projid)) {
  1964. error = -EXDEV;
  1965. goto out_trans_cancel;
  1966. }
  1967. /* RENAME_EXCHANGE is unique from here on. */
  1968. if (flags & RENAME_EXCHANGE) {
  1969. error = xfs_dir_exchange_children(tp, &du_src, &du_tgt,
  1970. spaceres);
  1971. if (error)
  1972. goto out_trans_cancel;
  1973. goto out_commit;
  1974. }
  1975. /*
  1976. * Try to reserve quota to handle an expansion of the target directory.
  1977. * We'll allow the rename to continue in reservationless mode if we hit
  1978. * a space usage constraint. If we trigger reservationless mode, save
  1979. * the errno if there isn't any free space in the target directory.
  1980. */
  1981. if (spaceres != 0) {
  1982. error = xfs_trans_reserve_quota_nblks(tp, target_dp, spaceres,
  1983. 0, false);
  1984. if (error == -EDQUOT || error == -ENOSPC) {
  1985. if (!retried) {
  1986. xfs_trans_cancel(tp);
  1987. xfs_iunlock_rename(inodes, num_inodes);
  1988. xfs_blockgc_free_quota(target_dp, 0);
  1989. retried = true;
  1990. goto retry;
  1991. }
  1992. nospace_error = error;
  1993. spaceres = 0;
  1994. error = 0;
  1995. }
  1996. if (error)
  1997. goto out_trans_cancel;
  1998. }
  1999. /*
  2000. * We don't allow quotaless renaming when parent pointers are enabled
  2001. * because we can't back out if the xattrs must grow.
  2002. */
  2003. if (du_src.ppargs && nospace_error) {
  2004. error = nospace_error;
  2005. goto out_trans_cancel;
  2006. }
  2007. /*
  2008. * Lock the AGI buffers we need to handle bumping the nlink of the
  2009. * whiteout inode off the unlinked list and to handle dropping the
  2010. * nlink of the target inode. Per locking order rules, do this in
  2011. * increasing AG order and before directory block allocation tries to
  2012. * grab AGFs because we grab AGIs before AGFs.
  2013. *
  2014. * The (vfs) caller must ensure that if src is a directory then
  2015. * target_ip is either null or an empty directory.
  2016. */
  2017. for (i = 0; i < num_inodes && inodes[i] != NULL; i++) {
  2018. if (inodes[i] == du_wip.ip ||
  2019. (inodes[i] == target_ip &&
  2020. (VFS_I(target_ip)->i_nlink == 1 || src_is_directory))) {
  2021. struct xfs_perag *pag;
  2022. struct xfs_buf *bp;
  2023. pag = xfs_perag_get(mp,
  2024. XFS_INO_TO_AGNO(mp, inodes[i]->i_ino));
  2025. error = xfs_read_agi(pag, tp, 0, &bp);
  2026. xfs_perag_put(pag);
  2027. if (error)
  2028. goto out_trans_cancel;
  2029. }
  2030. }
  2031. error = xfs_dir_rename_children(tp, &du_src, &du_tgt, spaceres,
  2032. &du_wip);
  2033. if (error)
  2034. goto out_trans_cancel;
  2035. if (du_wip.ip) {
  2036. /*
  2037. * Now we have a real link, clear the "I'm a tmpfile" state
  2038. * flag from the inode so it doesn't accidentally get misused in
  2039. * future.
  2040. */
  2041. VFS_I(du_wip.ip)->i_state &= ~I_LINKABLE;
  2042. }
  2043. out_commit:
  2044. /*
  2045. * If this is a synchronous mount, make sure that the rename
  2046. * transaction goes to disk before returning to the user.
  2047. */
  2048. if (xfs_has_wsync(tp->t_mountp) || xfs_has_dirsync(tp->t_mountp))
  2049. xfs_trans_set_sync(tp);
  2050. error = xfs_trans_commit(tp);
  2051. nospace_error = 0;
  2052. goto out_unlock;
  2053. out_trans_cancel:
  2054. xfs_trans_cancel(tp);
  2055. out_unlock:
  2056. xfs_iunlock_rename(inodes, num_inodes);
  2057. out_tgt_ppargs:
  2058. xfs_parent_finish(mp, du_tgt.ppargs);
  2059. out_wip_ppargs:
  2060. xfs_parent_finish(mp, du_wip.ppargs);
  2061. out_src_ppargs:
  2062. xfs_parent_finish(mp, du_src.ppargs);
  2063. out_release_wip:
  2064. if (du_wip.ip)
  2065. xfs_irele(du_wip.ip);
  2066. if (error == -ENOSPC && nospace_error)
  2067. error = nospace_error;
  2068. return error;
  2069. }
  2070. static int
  2071. xfs_iflush(
  2072. struct xfs_inode *ip,
  2073. struct xfs_buf *bp)
  2074. {
  2075. struct xfs_inode_log_item *iip = ip->i_itemp;
  2076. struct xfs_dinode *dip;
  2077. struct xfs_mount *mp = ip->i_mount;
  2078. int error;
  2079. xfs_assert_ilocked(ip, XFS_ILOCK_EXCL | XFS_ILOCK_SHARED);
  2080. ASSERT(xfs_iflags_test(ip, XFS_IFLUSHING));
  2081. ASSERT(ip->i_df.if_format != XFS_DINODE_FMT_BTREE ||
  2082. ip->i_df.if_nextents > XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK));
  2083. ASSERT(iip->ili_item.li_buf == bp);
  2084. dip = xfs_buf_offset(bp, ip->i_imap.im_boffset);
  2085. /*
  2086. * We don't flush the inode if any of the following checks fail, but we
  2087. * do still update the log item and attach to the backing buffer as if
  2088. * the flush happened. This is a formality to facilitate predictable
  2089. * error handling as the caller will shutdown and fail the buffer.
  2090. */
  2091. error = -EFSCORRUPTED;
  2092. if (XFS_TEST_ERROR(dip->di_magic != cpu_to_be16(XFS_DINODE_MAGIC),
  2093. mp, XFS_ERRTAG_IFLUSH_1)) {
  2094. xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
  2095. "%s: Bad inode %llu magic number 0x%x, ptr "PTR_FMT,
  2096. __func__, ip->i_ino, be16_to_cpu(dip->di_magic), dip);
  2097. goto flush_out;
  2098. }
  2099. if (S_ISREG(VFS_I(ip)->i_mode)) {
  2100. if (XFS_TEST_ERROR(
  2101. ip->i_df.if_format != XFS_DINODE_FMT_EXTENTS &&
  2102. ip->i_df.if_format != XFS_DINODE_FMT_BTREE,
  2103. mp, XFS_ERRTAG_IFLUSH_3)) {
  2104. xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
  2105. "%s: Bad regular inode %llu, ptr "PTR_FMT,
  2106. __func__, ip->i_ino, ip);
  2107. goto flush_out;
  2108. }
  2109. } else if (S_ISDIR(VFS_I(ip)->i_mode)) {
  2110. if (XFS_TEST_ERROR(
  2111. ip->i_df.if_format != XFS_DINODE_FMT_EXTENTS &&
  2112. ip->i_df.if_format != XFS_DINODE_FMT_BTREE &&
  2113. ip->i_df.if_format != XFS_DINODE_FMT_LOCAL,
  2114. mp, XFS_ERRTAG_IFLUSH_4)) {
  2115. xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
  2116. "%s: Bad directory inode %llu, ptr "PTR_FMT,
  2117. __func__, ip->i_ino, ip);
  2118. goto flush_out;
  2119. }
  2120. }
  2121. if (XFS_TEST_ERROR(ip->i_df.if_nextents + xfs_ifork_nextents(&ip->i_af) >
  2122. ip->i_nblocks, mp, XFS_ERRTAG_IFLUSH_5)) {
  2123. xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
  2124. "%s: detected corrupt incore inode %llu, "
  2125. "total extents = %llu nblocks = %lld, ptr "PTR_FMT,
  2126. __func__, ip->i_ino,
  2127. ip->i_df.if_nextents + xfs_ifork_nextents(&ip->i_af),
  2128. ip->i_nblocks, ip);
  2129. goto flush_out;
  2130. }
  2131. if (XFS_TEST_ERROR(ip->i_forkoff > mp->m_sb.sb_inodesize,
  2132. mp, XFS_ERRTAG_IFLUSH_6)) {
  2133. xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
  2134. "%s: bad inode %llu, forkoff 0x%x, ptr "PTR_FMT,
  2135. __func__, ip->i_ino, ip->i_forkoff, ip);
  2136. goto flush_out;
  2137. }
  2138. /*
  2139. * Inode item log recovery for v2 inodes are dependent on the flushiter
  2140. * count for correct sequencing. We bump the flush iteration count so
  2141. * we can detect flushes which postdate a log record during recovery.
  2142. * This is redundant as we now log every change and hence this can't
  2143. * happen but we need to still do it to ensure backwards compatibility
  2144. * with old kernels that predate logging all inode changes.
  2145. */
  2146. if (!xfs_has_v3inodes(mp))
  2147. ip->i_flushiter++;
  2148. /*
  2149. * If there are inline format data / attr forks attached to this inode,
  2150. * make sure they are not corrupt.
  2151. */
  2152. if (ip->i_df.if_format == XFS_DINODE_FMT_LOCAL &&
  2153. xfs_ifork_verify_local_data(ip))
  2154. goto flush_out;
  2155. if (xfs_inode_has_attr_fork(ip) &&
  2156. ip->i_af.if_format == XFS_DINODE_FMT_LOCAL &&
  2157. xfs_ifork_verify_local_attr(ip))
  2158. goto flush_out;
  2159. /*
  2160. * Copy the dirty parts of the inode into the on-disk inode. We always
  2161. * copy out the core of the inode, because if the inode is dirty at all
  2162. * the core must be.
  2163. */
  2164. xfs_inode_to_disk(ip, dip, iip->ili_item.li_lsn);
  2165. /* Wrap, we never let the log put out DI_MAX_FLUSH */
  2166. if (!xfs_has_v3inodes(mp)) {
  2167. if (ip->i_flushiter == DI_MAX_FLUSH)
  2168. ip->i_flushiter = 0;
  2169. }
  2170. xfs_iflush_fork(ip, dip, iip, XFS_DATA_FORK);
  2171. if (xfs_inode_has_attr_fork(ip))
  2172. xfs_iflush_fork(ip, dip, iip, XFS_ATTR_FORK);
  2173. /*
  2174. * We've recorded everything logged in the inode, so we'd like to clear
  2175. * the ili_fields bits so we don't log and flush things unnecessarily.
  2176. * However, we can't stop logging all this information until the data
  2177. * we've copied into the disk buffer is written to disk. If we did we
  2178. * might overwrite the copy of the inode in the log with all the data
  2179. * after re-logging only part of it, and in the face of a crash we
  2180. * wouldn't have all the data we need to recover.
  2181. *
  2182. * What we do is move the bits to the ili_last_fields field. When
  2183. * logging the inode, these bits are moved back to the ili_fields field.
  2184. * In the xfs_buf_inode_iodone() routine we clear ili_last_fields, since
  2185. * we know that the information those bits represent is permanently on
  2186. * disk. As long as the flush completes before the inode is logged
  2187. * again, then both ili_fields and ili_last_fields will be cleared.
  2188. */
  2189. error = 0;
  2190. flush_out:
  2191. spin_lock(&iip->ili_lock);
  2192. iip->ili_last_fields = iip->ili_fields;
  2193. iip->ili_fields = 0;
  2194. iip->ili_fsync_fields = 0;
  2195. set_bit(XFS_LI_FLUSHING, &iip->ili_item.li_flags);
  2196. spin_unlock(&iip->ili_lock);
  2197. /*
  2198. * Store the current LSN of the inode so that we can tell whether the
  2199. * item has moved in the AIL from xfs_buf_inode_iodone().
  2200. */
  2201. xfs_trans_ail_copy_lsn(mp->m_ail, &iip->ili_flush_lsn,
  2202. &iip->ili_item.li_lsn);
  2203. /* generate the checksum. */
  2204. xfs_dinode_calc_crc(mp, dip);
  2205. if (error)
  2206. xfs_inode_mark_sick(ip, XFS_SICK_INO_CORE);
  2207. return error;
  2208. }
  2209. /*
  2210. * Non-blocking flush of dirty inode metadata into the backing buffer.
  2211. *
  2212. * The caller must have a reference to the inode and hold the cluster buffer
  2213. * locked. The function will walk across all the inodes on the cluster buffer it
  2214. * can find and lock without blocking, and flush them to the cluster buffer.
  2215. *
  2216. * On successful flushing of at least one inode, the caller must write out the
  2217. * buffer and release it. If no inodes are flushed, -EAGAIN will be returned and
  2218. * the caller needs to release the buffer. On failure, the filesystem will be
  2219. * shut down, the buffer will have been unlocked and released, and EFSCORRUPTED
  2220. * will be returned.
  2221. */
  2222. int
  2223. xfs_iflush_cluster(
  2224. struct xfs_buf *bp)
  2225. {
  2226. struct xfs_mount *mp = bp->b_mount;
  2227. struct xfs_log_item *lip, *n;
  2228. struct xfs_inode *ip;
  2229. struct xfs_inode_log_item *iip;
  2230. int clcount = 0;
  2231. int error = 0;
  2232. /*
  2233. * We must use the safe variant here as on shutdown xfs_iflush_abort()
  2234. * will remove itself from the list.
  2235. */
  2236. list_for_each_entry_safe(lip, n, &bp->b_li_list, li_bio_list) {
  2237. iip = (struct xfs_inode_log_item *)lip;
  2238. ip = iip->ili_inode;
  2239. /*
  2240. * Quick and dirty check to avoid locks if possible.
  2241. */
  2242. if (__xfs_iflags_test(ip, XFS_IRECLAIM | XFS_IFLUSHING))
  2243. continue;
  2244. if (xfs_ipincount(ip))
  2245. continue;
  2246. /*
  2247. * The inode is still attached to the buffer, which means it is
  2248. * dirty but reclaim might try to grab it. Check carefully for
  2249. * that, and grab the ilock while still holding the i_flags_lock
  2250. * to guarantee reclaim will not be able to reclaim this inode
  2251. * once we drop the i_flags_lock.
  2252. */
  2253. spin_lock(&ip->i_flags_lock);
  2254. ASSERT(!__xfs_iflags_test(ip, XFS_ISTALE));
  2255. if (__xfs_iflags_test(ip, XFS_IRECLAIM | XFS_IFLUSHING)) {
  2256. spin_unlock(&ip->i_flags_lock);
  2257. continue;
  2258. }
  2259. /*
  2260. * ILOCK will pin the inode against reclaim and prevent
  2261. * concurrent transactions modifying the inode while we are
  2262. * flushing the inode. If we get the lock, set the flushing
  2263. * state before we drop the i_flags_lock.
  2264. */
  2265. if (!xfs_ilock_nowait(ip, XFS_ILOCK_SHARED)) {
  2266. spin_unlock(&ip->i_flags_lock);
  2267. continue;
  2268. }
  2269. __xfs_iflags_set(ip, XFS_IFLUSHING);
  2270. spin_unlock(&ip->i_flags_lock);
  2271. /*
  2272. * Abort flushing this inode if we are shut down because the
  2273. * inode may not currently be in the AIL. This can occur when
  2274. * log I/O failure unpins the inode without inserting into the
  2275. * AIL, leaving a dirty/unpinned inode attached to the buffer
  2276. * that otherwise looks like it should be flushed.
  2277. */
  2278. if (xlog_is_shutdown(mp->m_log)) {
  2279. xfs_iunpin_wait(ip);
  2280. xfs_iflush_abort(ip);
  2281. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  2282. error = -EIO;
  2283. continue;
  2284. }
  2285. /* don't block waiting on a log force to unpin dirty inodes */
  2286. if (xfs_ipincount(ip)) {
  2287. xfs_iflags_clear(ip, XFS_IFLUSHING);
  2288. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  2289. continue;
  2290. }
  2291. if (!xfs_inode_clean(ip))
  2292. error = xfs_iflush(ip, bp);
  2293. else
  2294. xfs_iflags_clear(ip, XFS_IFLUSHING);
  2295. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  2296. if (error)
  2297. break;
  2298. clcount++;
  2299. }
  2300. if (error) {
  2301. /*
  2302. * Shutdown first so we kill the log before we release this
  2303. * buffer. If it is an INODE_ALLOC buffer and pins the tail
  2304. * of the log, failing it before the _log_ is shut down can
  2305. * result in the log tail being moved forward in the journal
  2306. * on disk because log writes can still be taking place. Hence
  2307. * unpinning the tail will allow the ICREATE intent to be
  2308. * removed from the log an recovery will fail with uninitialised
  2309. * inode cluster buffers.
  2310. */
  2311. xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
  2312. bp->b_flags |= XBF_ASYNC;
  2313. xfs_buf_ioend_fail(bp);
  2314. return error;
  2315. }
  2316. if (!clcount)
  2317. return -EAGAIN;
  2318. XFS_STATS_INC(mp, xs_icluster_flushcnt);
  2319. XFS_STATS_ADD(mp, xs_icluster_flushinode, clcount);
  2320. return 0;
  2321. }
  2322. /* Release an inode. */
  2323. void
  2324. xfs_irele(
  2325. struct xfs_inode *ip)
  2326. {
  2327. trace_xfs_irele(ip, _RET_IP_);
  2328. iput(VFS_I(ip));
  2329. }
  2330. /*
  2331. * Ensure all commited transactions touching the inode are written to the log.
  2332. */
  2333. int
  2334. xfs_log_force_inode(
  2335. struct xfs_inode *ip)
  2336. {
  2337. xfs_csn_t seq = 0;
  2338. xfs_ilock(ip, XFS_ILOCK_SHARED);
  2339. if (xfs_ipincount(ip))
  2340. seq = ip->i_itemp->ili_commit_seq;
  2341. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  2342. if (!seq)
  2343. return 0;
  2344. return xfs_log_force_seq(ip->i_mount, seq, XFS_LOG_SYNC, NULL);
  2345. }
  2346. /*
  2347. * Grab the exclusive iolock for a data copy from src to dest, making sure to
  2348. * abide vfs locking order (lowest pointer value goes first) and breaking the
  2349. * layout leases before proceeding. The loop is needed because we cannot call
  2350. * the blocking break_layout() with the iolocks held, and therefore have to
  2351. * back out both locks.
  2352. */
  2353. static int
  2354. xfs_iolock_two_inodes_and_break_layout(
  2355. struct inode *src,
  2356. struct inode *dest)
  2357. {
  2358. int error;
  2359. if (src > dest)
  2360. swap(src, dest);
  2361. retry:
  2362. /* Wait to break both inodes' layouts before we start locking. */
  2363. error = break_layout(src, true);
  2364. if (error)
  2365. return error;
  2366. if (src != dest) {
  2367. error = break_layout(dest, true);
  2368. if (error)
  2369. return error;
  2370. }
  2371. /* Lock one inode and make sure nobody got in and leased it. */
  2372. inode_lock(src);
  2373. error = break_layout(src, false);
  2374. if (error) {
  2375. inode_unlock(src);
  2376. if (error == -EWOULDBLOCK)
  2377. goto retry;
  2378. return error;
  2379. }
  2380. if (src == dest)
  2381. return 0;
  2382. /* Lock the other inode and make sure nobody got in and leased it. */
  2383. inode_lock_nested(dest, I_MUTEX_NONDIR2);
  2384. error = break_layout(dest, false);
  2385. if (error) {
  2386. inode_unlock(src);
  2387. inode_unlock(dest);
  2388. if (error == -EWOULDBLOCK)
  2389. goto retry;
  2390. return error;
  2391. }
  2392. return 0;
  2393. }
  2394. static int
  2395. xfs_mmaplock_two_inodes_and_break_dax_layout(
  2396. struct xfs_inode *ip1,
  2397. struct xfs_inode *ip2)
  2398. {
  2399. int error;
  2400. bool retry;
  2401. struct page *page;
  2402. if (ip1->i_ino > ip2->i_ino)
  2403. swap(ip1, ip2);
  2404. again:
  2405. retry = false;
  2406. /* Lock the first inode */
  2407. xfs_ilock(ip1, XFS_MMAPLOCK_EXCL);
  2408. error = xfs_break_dax_layouts(VFS_I(ip1), &retry);
  2409. if (error || retry) {
  2410. xfs_iunlock(ip1, XFS_MMAPLOCK_EXCL);
  2411. if (error == 0 && retry)
  2412. goto again;
  2413. return error;
  2414. }
  2415. if (ip1 == ip2)
  2416. return 0;
  2417. /* Nested lock the second inode */
  2418. xfs_ilock(ip2, xfs_lock_inumorder(XFS_MMAPLOCK_EXCL, 1));
  2419. /*
  2420. * We cannot use xfs_break_dax_layouts() directly here because it may
  2421. * need to unlock & lock the XFS_MMAPLOCK_EXCL which is not suitable
  2422. * for this nested lock case.
  2423. */
  2424. page = dax_layout_busy_page(VFS_I(ip2)->i_mapping);
  2425. if (page && page_ref_count(page) != 1) {
  2426. xfs_iunlock(ip2, XFS_MMAPLOCK_EXCL);
  2427. xfs_iunlock(ip1, XFS_MMAPLOCK_EXCL);
  2428. goto again;
  2429. }
  2430. return 0;
  2431. }
  2432. /*
  2433. * Lock two inodes so that userspace cannot initiate I/O via file syscalls or
  2434. * mmap activity.
  2435. */
  2436. int
  2437. xfs_ilock2_io_mmap(
  2438. struct xfs_inode *ip1,
  2439. struct xfs_inode *ip2)
  2440. {
  2441. int ret;
  2442. ret = xfs_iolock_two_inodes_and_break_layout(VFS_I(ip1), VFS_I(ip2));
  2443. if (ret)
  2444. return ret;
  2445. if (IS_DAX(VFS_I(ip1)) && IS_DAX(VFS_I(ip2))) {
  2446. ret = xfs_mmaplock_two_inodes_and_break_dax_layout(ip1, ip2);
  2447. if (ret) {
  2448. inode_unlock(VFS_I(ip2));
  2449. if (ip1 != ip2)
  2450. inode_unlock(VFS_I(ip1));
  2451. return ret;
  2452. }
  2453. } else
  2454. filemap_invalidate_lock_two(VFS_I(ip1)->i_mapping,
  2455. VFS_I(ip2)->i_mapping);
  2456. return 0;
  2457. }
  2458. /* Unlock both inodes to allow IO and mmap activity. */
  2459. void
  2460. xfs_iunlock2_io_mmap(
  2461. struct xfs_inode *ip1,
  2462. struct xfs_inode *ip2)
  2463. {
  2464. if (IS_DAX(VFS_I(ip1)) && IS_DAX(VFS_I(ip2))) {
  2465. xfs_iunlock(ip2, XFS_MMAPLOCK_EXCL);
  2466. if (ip1 != ip2)
  2467. xfs_iunlock(ip1, XFS_MMAPLOCK_EXCL);
  2468. } else
  2469. filemap_invalidate_unlock_two(VFS_I(ip1)->i_mapping,
  2470. VFS_I(ip2)->i_mapping);
  2471. inode_unlock(VFS_I(ip2));
  2472. if (ip1 != ip2)
  2473. inode_unlock(VFS_I(ip1));
  2474. }
  2475. /* Drop the MMAPLOCK and the IOLOCK after a remap completes. */
  2476. void
  2477. xfs_iunlock2_remapping(
  2478. struct xfs_inode *ip1,
  2479. struct xfs_inode *ip2)
  2480. {
  2481. xfs_iflags_clear(ip1, XFS_IREMAPPING);
  2482. if (ip1 != ip2)
  2483. xfs_iunlock(ip1, XFS_MMAPLOCK_SHARED);
  2484. xfs_iunlock(ip2, XFS_MMAPLOCK_EXCL);
  2485. if (ip1 != ip2)
  2486. inode_unlock_shared(VFS_I(ip1));
  2487. inode_unlock(VFS_I(ip2));
  2488. }
  2489. /*
  2490. * Reload the incore inode list for this inode. Caller should ensure that
  2491. * the link count cannot change, either by taking ILOCK_SHARED or otherwise
  2492. * preventing other threads from executing.
  2493. */
  2494. int
  2495. xfs_inode_reload_unlinked_bucket(
  2496. struct xfs_trans *tp,
  2497. struct xfs_inode *ip)
  2498. {
  2499. struct xfs_mount *mp = tp->t_mountp;
  2500. struct xfs_buf *agibp;
  2501. struct xfs_agi *agi;
  2502. struct xfs_perag *pag;
  2503. xfs_agnumber_t agno = XFS_INO_TO_AGNO(mp, ip->i_ino);
  2504. xfs_agino_t agino = XFS_INO_TO_AGINO(mp, ip->i_ino);
  2505. xfs_agino_t prev_agino, next_agino;
  2506. unsigned int bucket;
  2507. bool foundit = false;
  2508. int error;
  2509. /* Grab the first inode in the list */
  2510. pag = xfs_perag_get(mp, agno);
  2511. error = xfs_ialloc_read_agi(pag, tp, 0, &agibp);
  2512. xfs_perag_put(pag);
  2513. if (error)
  2514. return error;
  2515. /*
  2516. * We've taken ILOCK_SHARED and the AGI buffer lock to stabilize the
  2517. * incore unlinked list pointers for this inode. Check once more to
  2518. * see if we raced with anyone else to reload the unlinked list.
  2519. */
  2520. if (!xfs_inode_unlinked_incomplete(ip)) {
  2521. foundit = true;
  2522. goto out_agibp;
  2523. }
  2524. bucket = agino % XFS_AGI_UNLINKED_BUCKETS;
  2525. agi = agibp->b_addr;
  2526. trace_xfs_inode_reload_unlinked_bucket(ip);
  2527. xfs_info_ratelimited(mp,
  2528. "Found unrecovered unlinked inode 0x%x in AG 0x%x. Initiating list recovery.",
  2529. agino, agno);
  2530. prev_agino = NULLAGINO;
  2531. next_agino = be32_to_cpu(agi->agi_unlinked[bucket]);
  2532. while (next_agino != NULLAGINO) {
  2533. struct xfs_inode *next_ip = NULL;
  2534. /* Found this caller's inode, set its backlink. */
  2535. if (next_agino == agino) {
  2536. next_ip = ip;
  2537. next_ip->i_prev_unlinked = prev_agino;
  2538. foundit = true;
  2539. goto next_inode;
  2540. }
  2541. /* Try in-memory lookup first. */
  2542. next_ip = xfs_iunlink_lookup(pag, next_agino);
  2543. if (next_ip)
  2544. goto next_inode;
  2545. /* Inode not in memory, try reloading it. */
  2546. error = xfs_iunlink_reload_next(tp, agibp, prev_agino,
  2547. next_agino);
  2548. if (error)
  2549. break;
  2550. /* Grab the reloaded inode. */
  2551. next_ip = xfs_iunlink_lookup(pag, next_agino);
  2552. if (!next_ip) {
  2553. /* No incore inode at all? We reloaded it... */
  2554. ASSERT(next_ip != NULL);
  2555. error = -EFSCORRUPTED;
  2556. break;
  2557. }
  2558. next_inode:
  2559. prev_agino = next_agino;
  2560. next_agino = next_ip->i_next_unlinked;
  2561. }
  2562. out_agibp:
  2563. xfs_trans_brelse(tp, agibp);
  2564. /* Should have found this inode somewhere in the iunlinked bucket. */
  2565. if (!error && !foundit)
  2566. error = -EFSCORRUPTED;
  2567. return error;
  2568. }
  2569. /* Decide if this inode is missing its unlinked list and reload it. */
  2570. int
  2571. xfs_inode_reload_unlinked(
  2572. struct xfs_inode *ip)
  2573. {
  2574. struct xfs_trans *tp;
  2575. int error;
  2576. error = xfs_trans_alloc_empty(ip->i_mount, &tp);
  2577. if (error)
  2578. return error;
  2579. xfs_ilock(ip, XFS_ILOCK_SHARED);
  2580. if (xfs_inode_unlinked_incomplete(ip))
  2581. error = xfs_inode_reload_unlinked_bucket(tp, ip);
  2582. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  2583. xfs_trans_cancel(tp);
  2584. return error;
  2585. }
  2586. /* Has this inode fork been zapped by repair? */
  2587. bool
  2588. xfs_ifork_zapped(
  2589. const struct xfs_inode *ip,
  2590. int whichfork)
  2591. {
  2592. unsigned int datamask = 0;
  2593. switch (whichfork) {
  2594. case XFS_DATA_FORK:
  2595. switch (ip->i_vnode.i_mode & S_IFMT) {
  2596. case S_IFDIR:
  2597. datamask = XFS_SICK_INO_DIR_ZAPPED;
  2598. break;
  2599. case S_IFLNK:
  2600. datamask = XFS_SICK_INO_SYMLINK_ZAPPED;
  2601. break;
  2602. }
  2603. return ip->i_sick & (XFS_SICK_INO_BMBTD_ZAPPED | datamask);
  2604. case XFS_ATTR_FORK:
  2605. return ip->i_sick & XFS_SICK_INO_BMBTA_ZAPPED;
  2606. default:
  2607. return false;
  2608. }
  2609. }
  2610. /* Compute the number of data and realtime blocks used by a file. */
  2611. void
  2612. xfs_inode_count_blocks(
  2613. struct xfs_trans *tp,
  2614. struct xfs_inode *ip,
  2615. xfs_filblks_t *dblocks,
  2616. xfs_filblks_t *rblocks)
  2617. {
  2618. struct xfs_ifork *ifp = xfs_ifork_ptr(ip, XFS_DATA_FORK);
  2619. *rblocks = 0;
  2620. if (XFS_IS_REALTIME_INODE(ip))
  2621. xfs_bmap_count_leaves(ifp, rblocks);
  2622. *dblocks = ip->i_nblocks - *rblocks;
  2623. }
  2624. static void
  2625. xfs_wait_dax_page(
  2626. struct inode *inode)
  2627. {
  2628. struct xfs_inode *ip = XFS_I(inode);
  2629. xfs_iunlock(ip, XFS_MMAPLOCK_EXCL);
  2630. schedule();
  2631. xfs_ilock(ip, XFS_MMAPLOCK_EXCL);
  2632. }
  2633. int
  2634. xfs_break_dax_layouts(
  2635. struct inode *inode,
  2636. bool *retry)
  2637. {
  2638. struct page *page;
  2639. xfs_assert_ilocked(XFS_I(inode), XFS_MMAPLOCK_EXCL);
  2640. page = dax_layout_busy_page(inode->i_mapping);
  2641. if (!page)
  2642. return 0;
  2643. *retry = true;
  2644. return ___wait_var_event(&page->_refcount,
  2645. atomic_read(&page->_refcount) == 1, TASK_INTERRUPTIBLE,
  2646. 0, 0, xfs_wait_dax_page(inode));
  2647. }
  2648. int
  2649. xfs_break_layouts(
  2650. struct inode *inode,
  2651. uint *iolock,
  2652. enum layout_break_reason reason)
  2653. {
  2654. bool retry;
  2655. int error;
  2656. xfs_assert_ilocked(XFS_I(inode), XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL);
  2657. do {
  2658. retry = false;
  2659. switch (reason) {
  2660. case BREAK_UNMAP:
  2661. error = xfs_break_dax_layouts(inode, &retry);
  2662. if (error || retry)
  2663. break;
  2664. fallthrough;
  2665. case BREAK_WRITE:
  2666. error = xfs_break_leased_layouts(inode, iolock, &retry);
  2667. break;
  2668. default:
  2669. WARN_ON_ONCE(1);
  2670. error = -EINVAL;
  2671. }
  2672. } while (error == 0 && retry);
  2673. return error;
  2674. }
  2675. /* Returns the size of fundamental allocation unit for a file, in bytes. */
  2676. unsigned int
  2677. xfs_inode_alloc_unitsize(
  2678. struct xfs_inode *ip)
  2679. {
  2680. unsigned int blocks = 1;
  2681. if (XFS_IS_REALTIME_INODE(ip))
  2682. blocks = ip->i_mount->m_sb.sb_rextsize;
  2683. return XFS_FSB_TO_B(ip->i_mount, blocks);
  2684. }
  2685. /* Should we always be using copy on write for file writes? */
  2686. bool
  2687. xfs_is_always_cow_inode(
  2688. struct xfs_inode *ip)
  2689. {
  2690. return ip->i_mount->m_always_cow && xfs_has_reflink(ip->i_mount);
  2691. }