xfs_inode.c 99 KB

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