xfs_inode.c 104 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2000-2006 Silicon Graphics, Inc.
  4. * All Rights Reserved.
  5. */
  6. #include <linux/iversion.h>
  7. #include "xfs.h"
  8. #include "xfs_fs.h"
  9. #include "xfs_shared.h"
  10. #include "xfs_format.h"
  11. #include "xfs_log_format.h"
  12. #include "xfs_trans_resv.h"
  13. #include "xfs_sb.h"
  14. #include "xfs_mount.h"
  15. #include "xfs_defer.h"
  16. #include "xfs_inode.h"
  17. #include "xfs_dir2.h"
  18. #include "xfs_attr.h"
  19. #include "xfs_trans_space.h"
  20. #include "xfs_trans.h"
  21. #include "xfs_buf_item.h"
  22. #include "xfs_inode_item.h"
  23. #include "xfs_ialloc.h"
  24. #include "xfs_bmap.h"
  25. #include "xfs_bmap_util.h"
  26. #include "xfs_errortag.h"
  27. #include "xfs_error.h"
  28. #include "xfs_quota.h"
  29. #include "xfs_filestream.h"
  30. #include "xfs_trace.h"
  31. #include "xfs_icache.h"
  32. #include "xfs_symlink.h"
  33. #include "xfs_trans_priv.h"
  34. #include "xfs_log.h"
  35. #include "xfs_bmap_btree.h"
  36. #include "xfs_reflink.h"
  37. kmem_zone_t *xfs_inode_zone;
  38. /*
  39. * Used in xfs_itruncate_extents(). This is the maximum number of extents
  40. * freed from a file in a single transaction.
  41. */
  42. #define XFS_ITRUNC_MAX_EXTENTS 2
  43. STATIC int xfs_iunlink(struct xfs_trans *, struct xfs_inode *);
  44. STATIC int xfs_iunlink_remove(struct xfs_trans *, struct xfs_inode *);
  45. /*
  46. * helper function to extract extent size hint from inode
  47. */
  48. xfs_extlen_t
  49. xfs_get_extsz_hint(
  50. struct xfs_inode *ip)
  51. {
  52. /*
  53. * No point in aligning allocations if we need to COW to actually
  54. * write to them.
  55. */
  56. if (xfs_is_always_cow_inode(ip))
  57. return 0;
  58. if ((ip->i_d.di_flags & XFS_DIFLAG_EXTSIZE) && ip->i_d.di_extsize)
  59. return ip->i_d.di_extsize;
  60. if (XFS_IS_REALTIME_INODE(ip))
  61. return ip->i_mount->m_sb.sb_rextsize;
  62. return 0;
  63. }
  64. /*
  65. * Helper function to extract CoW extent size hint from inode.
  66. * Between the extent size hint and the CoW extent size hint, we
  67. * return the greater of the two. If the value is zero (automatic),
  68. * use the default size.
  69. */
  70. xfs_extlen_t
  71. xfs_get_cowextsz_hint(
  72. struct xfs_inode *ip)
  73. {
  74. xfs_extlen_t a, b;
  75. a = 0;
  76. if (ip->i_d.di_flags2 & XFS_DIFLAG2_COWEXTSIZE)
  77. a = ip->i_d.di_cowextsize;
  78. b = xfs_get_extsz_hint(ip);
  79. a = max(a, b);
  80. if (a == 0)
  81. return XFS_DEFAULT_COWEXTSZ_HINT;
  82. return a;
  83. }
  84. /*
  85. * These two are wrapper routines around the xfs_ilock() routine used to
  86. * centralize some grungy code. They are used in places that wish to lock the
  87. * inode solely for reading the extents. The reason these places can't just
  88. * call xfs_ilock(ip, XFS_ILOCK_SHARED) is that the inode lock also guards to
  89. * bringing in of the extents from disk for a file in b-tree format. If the
  90. * inode is in b-tree format, then we need to lock the inode exclusively until
  91. * the extents are read in. Locking it exclusively all the time would limit
  92. * our parallelism unnecessarily, though. What we do instead is check to see
  93. * if the extents have been read in yet, and only lock the inode exclusively
  94. * if they have not.
  95. *
  96. * The functions return a value which should be given to the corresponding
  97. * xfs_iunlock() call.
  98. */
  99. uint
  100. xfs_ilock_data_map_shared(
  101. struct xfs_inode *ip)
  102. {
  103. uint lock_mode = XFS_ILOCK_SHARED;
  104. if (ip->i_df.if_format == XFS_DINODE_FMT_BTREE &&
  105. (ip->i_df.if_flags & XFS_IFEXTENTS) == 0)
  106. lock_mode = XFS_ILOCK_EXCL;
  107. xfs_ilock(ip, lock_mode);
  108. return lock_mode;
  109. }
  110. uint
  111. xfs_ilock_attr_map_shared(
  112. struct xfs_inode *ip)
  113. {
  114. uint lock_mode = XFS_ILOCK_SHARED;
  115. if (ip->i_afp &&
  116. ip->i_afp->if_format == 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_lock locking order:
  135. *
  136. * i_rwsem -> page lock -> mmap_lock
  137. * mmap_lock -> i_mmap_lock -> page_lock
  138. *
  139. * The difference in mmap_lock 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_lock
  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_lock.
  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. struct xfs_inode **ips,
  402. int inodes,
  403. uint lock_mode)
  404. {
  405. int attempts = 0, i, j, try_lock;
  406. struct xfs_log_item *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 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 = &ips[j]->i_itemp->ili_item;
  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. struct xfs_log_item *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 = &ip0->i_itemp->ili_item;
  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. STATIC uint
  542. _xfs_dic2xflags(
  543. uint16_t di_flags,
  544. uint64_t di_flags2,
  545. bool has_attr)
  546. {
  547. uint flags = 0;
  548. if (di_flags & XFS_DIFLAG_ANY) {
  549. if (di_flags & XFS_DIFLAG_REALTIME)
  550. flags |= FS_XFLAG_REALTIME;
  551. if (di_flags & XFS_DIFLAG_PREALLOC)
  552. flags |= FS_XFLAG_PREALLOC;
  553. if (di_flags & XFS_DIFLAG_IMMUTABLE)
  554. flags |= FS_XFLAG_IMMUTABLE;
  555. if (di_flags & XFS_DIFLAG_APPEND)
  556. flags |= FS_XFLAG_APPEND;
  557. if (di_flags & XFS_DIFLAG_SYNC)
  558. flags |= FS_XFLAG_SYNC;
  559. if (di_flags & XFS_DIFLAG_NOATIME)
  560. flags |= FS_XFLAG_NOATIME;
  561. if (di_flags & XFS_DIFLAG_NODUMP)
  562. flags |= FS_XFLAG_NODUMP;
  563. if (di_flags & XFS_DIFLAG_RTINHERIT)
  564. flags |= FS_XFLAG_RTINHERIT;
  565. if (di_flags & XFS_DIFLAG_PROJINHERIT)
  566. flags |= FS_XFLAG_PROJINHERIT;
  567. if (di_flags & XFS_DIFLAG_NOSYMLINKS)
  568. flags |= FS_XFLAG_NOSYMLINKS;
  569. if (di_flags & XFS_DIFLAG_EXTSIZE)
  570. flags |= FS_XFLAG_EXTSIZE;
  571. if (di_flags & XFS_DIFLAG_EXTSZINHERIT)
  572. flags |= FS_XFLAG_EXTSZINHERIT;
  573. if (di_flags & XFS_DIFLAG_NODEFRAG)
  574. flags |= FS_XFLAG_NODEFRAG;
  575. if (di_flags & XFS_DIFLAG_FILESTREAM)
  576. flags |= FS_XFLAG_FILESTREAM;
  577. }
  578. if (di_flags2 & XFS_DIFLAG2_ANY) {
  579. if (di_flags2 & XFS_DIFLAG2_DAX)
  580. flags |= FS_XFLAG_DAX;
  581. if (di_flags2 & XFS_DIFLAG2_COWEXTSIZE)
  582. flags |= FS_XFLAG_COWEXTSIZE;
  583. }
  584. if (has_attr)
  585. flags |= FS_XFLAG_HASATTR;
  586. return flags;
  587. }
  588. uint
  589. xfs_ip2xflags(
  590. struct xfs_inode *ip)
  591. {
  592. struct xfs_icdinode *dic = &ip->i_d;
  593. return _xfs_dic2xflags(dic->di_flags, dic->di_flags2, XFS_IFORK_Q(ip));
  594. }
  595. /*
  596. * Lookups up an inode from "name". If ci_name is not NULL, then a CI match
  597. * is allowed, otherwise it has to be an exact match. If a CI match is found,
  598. * ci_name->name will point to a the actual name (caller must free) or
  599. * will be set to NULL if an exact match is found.
  600. */
  601. int
  602. xfs_lookup(
  603. xfs_inode_t *dp,
  604. struct xfs_name *name,
  605. xfs_inode_t **ipp,
  606. struct xfs_name *ci_name)
  607. {
  608. xfs_ino_t inum;
  609. int error;
  610. trace_xfs_lookup(dp, name);
  611. if (XFS_FORCED_SHUTDOWN(dp->i_mount))
  612. return -EIO;
  613. error = xfs_dir_lookup(NULL, dp, name, &inum, ci_name);
  614. if (error)
  615. goto out_unlock;
  616. error = xfs_iget(dp->i_mount, NULL, inum, 0, 0, ipp);
  617. if (error)
  618. goto out_free_name;
  619. return 0;
  620. out_free_name:
  621. if (ci_name)
  622. kmem_free(ci_name->name);
  623. out_unlock:
  624. *ipp = NULL;
  625. return error;
  626. }
  627. /* Propagate di_flags from a parent inode to a child inode. */
  628. static void
  629. xfs_inode_inherit_flags(
  630. struct xfs_inode *ip,
  631. const struct xfs_inode *pip)
  632. {
  633. unsigned int di_flags = 0;
  634. umode_t mode = VFS_I(ip)->i_mode;
  635. if (S_ISDIR(mode)) {
  636. if (pip->i_d.di_flags & XFS_DIFLAG_RTINHERIT)
  637. di_flags |= XFS_DIFLAG_RTINHERIT;
  638. if (pip->i_d.di_flags & XFS_DIFLAG_EXTSZINHERIT) {
  639. di_flags |= XFS_DIFLAG_EXTSZINHERIT;
  640. ip->i_d.di_extsize = pip->i_d.di_extsize;
  641. }
  642. if (pip->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
  643. di_flags |= XFS_DIFLAG_PROJINHERIT;
  644. } else if (S_ISREG(mode)) {
  645. if ((pip->i_d.di_flags & XFS_DIFLAG_RTINHERIT) &&
  646. xfs_sb_version_hasrealtime(&ip->i_mount->m_sb))
  647. di_flags |= XFS_DIFLAG_REALTIME;
  648. if (pip->i_d.di_flags & XFS_DIFLAG_EXTSZINHERIT) {
  649. di_flags |= XFS_DIFLAG_EXTSIZE;
  650. ip->i_d.di_extsize = pip->i_d.di_extsize;
  651. }
  652. }
  653. if ((pip->i_d.di_flags & XFS_DIFLAG_NOATIME) &&
  654. xfs_inherit_noatime)
  655. di_flags |= XFS_DIFLAG_NOATIME;
  656. if ((pip->i_d.di_flags & XFS_DIFLAG_NODUMP) &&
  657. xfs_inherit_nodump)
  658. di_flags |= XFS_DIFLAG_NODUMP;
  659. if ((pip->i_d.di_flags & XFS_DIFLAG_SYNC) &&
  660. xfs_inherit_sync)
  661. di_flags |= XFS_DIFLAG_SYNC;
  662. if ((pip->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) &&
  663. xfs_inherit_nosymlinks)
  664. di_flags |= XFS_DIFLAG_NOSYMLINKS;
  665. if ((pip->i_d.di_flags & XFS_DIFLAG_NODEFRAG) &&
  666. xfs_inherit_nodefrag)
  667. di_flags |= XFS_DIFLAG_NODEFRAG;
  668. if (pip->i_d.di_flags & XFS_DIFLAG_FILESTREAM)
  669. di_flags |= XFS_DIFLAG_FILESTREAM;
  670. ip->i_d.di_flags |= di_flags;
  671. }
  672. /* Propagate di_flags2 from a parent inode to a child inode. */
  673. static void
  674. xfs_inode_inherit_flags2(
  675. struct xfs_inode *ip,
  676. const struct xfs_inode *pip)
  677. {
  678. if (pip->i_d.di_flags2 & XFS_DIFLAG2_COWEXTSIZE) {
  679. ip->i_d.di_flags2 |= XFS_DIFLAG2_COWEXTSIZE;
  680. ip->i_d.di_cowextsize = pip->i_d.di_cowextsize;
  681. }
  682. if (pip->i_d.di_flags2 & XFS_DIFLAG2_DAX)
  683. ip->i_d.di_flags2 |= XFS_DIFLAG2_DAX;
  684. }
  685. /*
  686. * Allocate an inode on disk and return a copy of its in-core version.
  687. * The in-core inode is locked exclusively. Set mode, nlink, and rdev
  688. * appropriately within the inode. The uid and gid for the inode are
  689. * set according to the contents of the given cred structure.
  690. *
  691. * Use xfs_dialloc() to allocate the on-disk inode. If xfs_dialloc()
  692. * has a free inode available, call xfs_iget() to obtain the in-core
  693. * version of the allocated inode. Finally, fill in the inode and
  694. * log its initial contents. In this case, ialloc_context would be
  695. * set to NULL.
  696. *
  697. * If xfs_dialloc() does not have an available inode, it will replenish
  698. * its supply by doing an allocation. Since we can only do one
  699. * allocation within a transaction without deadlocks, we must commit
  700. * the current transaction before returning the inode itself.
  701. * In this case, therefore, we will set ialloc_context and return.
  702. * The caller should then commit the current transaction, start a new
  703. * transaction, and call xfs_ialloc() again to actually get the inode.
  704. *
  705. * To ensure that some other process does not grab the inode that
  706. * was allocated during the first call to xfs_ialloc(), this routine
  707. * also returns the [locked] bp pointing to the head of the freelist
  708. * as ialloc_context. The caller should hold this buffer across
  709. * the commit and pass it back into this routine on the second call.
  710. *
  711. * If we are allocating quota inodes, we do not have a parent inode
  712. * to attach to or associate with (i.e. pip == NULL) because they
  713. * are not linked into the directory structure - they are attached
  714. * directly to the superblock - and so have no parent.
  715. */
  716. static int
  717. xfs_ialloc(
  718. xfs_trans_t *tp,
  719. xfs_inode_t *pip,
  720. umode_t mode,
  721. xfs_nlink_t nlink,
  722. dev_t rdev,
  723. prid_t prid,
  724. xfs_buf_t **ialloc_context,
  725. xfs_inode_t **ipp)
  726. {
  727. struct xfs_mount *mp = tp->t_mountp;
  728. xfs_ino_t ino;
  729. xfs_inode_t *ip;
  730. uint flags;
  731. int error;
  732. struct timespec64 tv;
  733. struct inode *inode;
  734. /*
  735. * Call the space management code to pick
  736. * the on-disk inode to be allocated.
  737. */
  738. error = xfs_dialloc(tp, pip ? pip->i_ino : 0, mode,
  739. ialloc_context, &ino);
  740. if (error)
  741. return error;
  742. if (*ialloc_context || ino == NULLFSINO) {
  743. *ipp = NULL;
  744. return 0;
  745. }
  746. ASSERT(*ialloc_context == NULL);
  747. /*
  748. * Protect against obviously corrupt allocation btree records. Later
  749. * xfs_iget checks will catch re-allocation of other active in-memory
  750. * and on-disk inodes. If we don't catch reallocating the parent inode
  751. * here we will deadlock in xfs_iget() so we have to do these checks
  752. * first.
  753. */
  754. if ((pip && ino == pip->i_ino) || !xfs_verify_dir_ino(mp, ino)) {
  755. xfs_alert(mp, "Allocated a known in-use inode 0x%llx!", ino);
  756. return -EFSCORRUPTED;
  757. }
  758. /*
  759. * Get the in-core inode with the lock held exclusively.
  760. * This is because we're setting fields here we need
  761. * to prevent others from looking at until we're done.
  762. */
  763. error = xfs_iget(mp, tp, ino, XFS_IGET_CREATE,
  764. XFS_ILOCK_EXCL, &ip);
  765. if (error)
  766. return error;
  767. ASSERT(ip != NULL);
  768. inode = VFS_I(ip);
  769. inode->i_mode = mode;
  770. set_nlink(inode, nlink);
  771. inode->i_uid = current_fsuid();
  772. inode->i_rdev = rdev;
  773. ip->i_d.di_projid = prid;
  774. if (pip && XFS_INHERIT_GID(pip)) {
  775. inode->i_gid = VFS_I(pip)->i_gid;
  776. if ((VFS_I(pip)->i_mode & S_ISGID) && S_ISDIR(mode))
  777. inode->i_mode |= S_ISGID;
  778. } else {
  779. inode->i_gid = current_fsgid();
  780. }
  781. /*
  782. * If the group ID of the new file does not match the effective group
  783. * ID or one of the supplementary group IDs, the S_ISGID bit is cleared
  784. * (and only if the irix_sgid_inherit compatibility variable is set).
  785. */
  786. if (irix_sgid_inherit &&
  787. (inode->i_mode & S_ISGID) && !in_group_p(inode->i_gid))
  788. inode->i_mode &= ~S_ISGID;
  789. ip->i_d.di_size = 0;
  790. ip->i_df.if_nextents = 0;
  791. ASSERT(ip->i_d.di_nblocks == 0);
  792. tv = current_time(inode);
  793. inode->i_mtime = tv;
  794. inode->i_atime = tv;
  795. inode->i_ctime = tv;
  796. ip->i_d.di_extsize = 0;
  797. ip->i_d.di_dmevmask = 0;
  798. ip->i_d.di_dmstate = 0;
  799. ip->i_d.di_flags = 0;
  800. if (xfs_sb_version_has_v3inode(&mp->m_sb)) {
  801. inode_set_iversion(inode, 1);
  802. ip->i_d.di_flags2 = mp->m_ino_geo.new_diflags2;
  803. ip->i_d.di_cowextsize = 0;
  804. ip->i_d.di_crtime = tv;
  805. }
  806. flags = XFS_ILOG_CORE;
  807. switch (mode & S_IFMT) {
  808. case S_IFIFO:
  809. case S_IFCHR:
  810. case S_IFBLK:
  811. case S_IFSOCK:
  812. ip->i_df.if_format = XFS_DINODE_FMT_DEV;
  813. ip->i_df.if_flags = 0;
  814. flags |= XFS_ILOG_DEV;
  815. break;
  816. case S_IFREG:
  817. case S_IFDIR:
  818. if (pip && (pip->i_d.di_flags & XFS_DIFLAG_ANY))
  819. xfs_inode_inherit_flags(ip, pip);
  820. if (pip && (pip->i_d.di_flags2 & XFS_DIFLAG2_ANY))
  821. xfs_inode_inherit_flags2(ip, pip);
  822. /* FALLTHROUGH */
  823. case S_IFLNK:
  824. ip->i_df.if_format = XFS_DINODE_FMT_EXTENTS;
  825. ip->i_df.if_flags = XFS_IFEXTENTS;
  826. ip->i_df.if_bytes = 0;
  827. ip->i_df.if_u1.if_root = NULL;
  828. break;
  829. default:
  830. ASSERT(0);
  831. }
  832. /*
  833. * Log the new values stuffed into the inode.
  834. */
  835. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  836. xfs_trans_log_inode(tp, ip, flags);
  837. /* now that we have an i_mode we can setup the inode structure */
  838. xfs_setup_inode(ip);
  839. *ipp = ip;
  840. return 0;
  841. }
  842. /*
  843. * Allocates a new inode from disk and return a pointer to the
  844. * incore copy. This routine will internally commit the current
  845. * transaction and allocate a new one if the Space Manager needed
  846. * to do an allocation to replenish the inode free-list.
  847. *
  848. * This routine is designed to be called from xfs_create and
  849. * xfs_create_dir.
  850. *
  851. */
  852. int
  853. xfs_dir_ialloc(
  854. xfs_trans_t **tpp, /* input: current transaction;
  855. output: may be a new transaction. */
  856. xfs_inode_t *dp, /* directory within whose allocate
  857. the inode. */
  858. umode_t mode,
  859. xfs_nlink_t nlink,
  860. dev_t rdev,
  861. prid_t prid, /* project id */
  862. xfs_inode_t **ipp) /* pointer to inode; it will be
  863. locked. */
  864. {
  865. xfs_trans_t *tp;
  866. xfs_inode_t *ip;
  867. xfs_buf_t *ialloc_context = NULL;
  868. int code;
  869. void *dqinfo;
  870. uint tflags;
  871. tp = *tpp;
  872. ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
  873. /*
  874. * xfs_ialloc will return a pointer to an incore inode if
  875. * the Space Manager has an available inode on the free
  876. * list. Otherwise, it will do an allocation and replenish
  877. * the freelist. Since we can only do one allocation per
  878. * transaction without deadlocks, we will need to commit the
  879. * current transaction and start a new one. We will then
  880. * need to call xfs_ialloc again to get the inode.
  881. *
  882. * If xfs_ialloc did an allocation to replenish the freelist,
  883. * it returns the bp containing the head of the freelist as
  884. * ialloc_context. We will hold a lock on it across the
  885. * transaction commit so that no other process can steal
  886. * the inode(s) that we've just allocated.
  887. */
  888. code = xfs_ialloc(tp, dp, mode, nlink, rdev, prid, &ialloc_context,
  889. &ip);
  890. /*
  891. * Return an error if we were unable to allocate a new inode.
  892. * This should only happen if we run out of space on disk or
  893. * encounter a disk error.
  894. */
  895. if (code) {
  896. *ipp = NULL;
  897. return code;
  898. }
  899. if (!ialloc_context && !ip) {
  900. *ipp = NULL;
  901. return -ENOSPC;
  902. }
  903. /*
  904. * If the AGI buffer is non-NULL, then we were unable to get an
  905. * inode in one operation. We need to commit the current
  906. * transaction and call xfs_ialloc() again. It is guaranteed
  907. * to succeed the second time.
  908. */
  909. if (ialloc_context) {
  910. /*
  911. * Normally, xfs_trans_commit releases all the locks.
  912. * We call bhold to hang on to the ialloc_context across
  913. * the commit. Holding this buffer prevents any other
  914. * processes from doing any allocations in this
  915. * allocation group.
  916. */
  917. xfs_trans_bhold(tp, ialloc_context);
  918. /*
  919. * We want the quota changes to be associated with the next
  920. * transaction, NOT this one. So, detach the dqinfo from this
  921. * and attach it to the next transaction.
  922. */
  923. dqinfo = NULL;
  924. tflags = 0;
  925. if (tp->t_dqinfo) {
  926. dqinfo = (void *)tp->t_dqinfo;
  927. tp->t_dqinfo = NULL;
  928. tflags = tp->t_flags & XFS_TRANS_DQ_DIRTY;
  929. tp->t_flags &= ~(XFS_TRANS_DQ_DIRTY);
  930. }
  931. code = xfs_trans_roll(&tp);
  932. /*
  933. * Re-attach the quota info that we detached from prev trx.
  934. */
  935. if (dqinfo) {
  936. tp->t_dqinfo = dqinfo;
  937. tp->t_flags |= tflags;
  938. }
  939. if (code) {
  940. xfs_buf_relse(ialloc_context);
  941. *tpp = tp;
  942. *ipp = NULL;
  943. return code;
  944. }
  945. xfs_trans_bjoin(tp, ialloc_context);
  946. /*
  947. * Call ialloc again. Since we've locked out all
  948. * other allocations in this allocation group,
  949. * this call should always succeed.
  950. */
  951. code = xfs_ialloc(tp, dp, mode, nlink, rdev, prid,
  952. &ialloc_context, &ip);
  953. /*
  954. * If we get an error at this point, return to the caller
  955. * so that the current transaction can be aborted.
  956. */
  957. if (code) {
  958. *tpp = tp;
  959. *ipp = NULL;
  960. return code;
  961. }
  962. ASSERT(!ialloc_context && ip);
  963. }
  964. *ipp = ip;
  965. *tpp = tp;
  966. return 0;
  967. }
  968. /*
  969. * Decrement the link count on an inode & log the change. If this causes the
  970. * link count to go to zero, move the inode to AGI unlinked list so that it can
  971. * be freed when the last active reference goes away via xfs_inactive().
  972. */
  973. static int /* error */
  974. xfs_droplink(
  975. xfs_trans_t *tp,
  976. xfs_inode_t *ip)
  977. {
  978. xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_CHG);
  979. drop_nlink(VFS_I(ip));
  980. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  981. if (VFS_I(ip)->i_nlink)
  982. return 0;
  983. return xfs_iunlink(tp, ip);
  984. }
  985. /*
  986. * Increment the link count on an inode & log the change.
  987. */
  988. static void
  989. xfs_bumplink(
  990. xfs_trans_t *tp,
  991. xfs_inode_t *ip)
  992. {
  993. xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_CHG);
  994. inc_nlink(VFS_I(ip));
  995. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  996. }
  997. int
  998. xfs_create(
  999. xfs_inode_t *dp,
  1000. struct xfs_name *name,
  1001. umode_t mode,
  1002. dev_t rdev,
  1003. xfs_inode_t **ipp)
  1004. {
  1005. int is_dir = S_ISDIR(mode);
  1006. struct xfs_mount *mp = dp->i_mount;
  1007. struct xfs_inode *ip = NULL;
  1008. struct xfs_trans *tp = NULL;
  1009. int error;
  1010. bool unlock_dp_on_error = false;
  1011. prid_t prid;
  1012. struct xfs_dquot *udqp = NULL;
  1013. struct xfs_dquot *gdqp = NULL;
  1014. struct xfs_dquot *pdqp = NULL;
  1015. struct xfs_trans_res *tres;
  1016. uint resblks;
  1017. trace_xfs_create(dp, name);
  1018. if (XFS_FORCED_SHUTDOWN(mp))
  1019. return -EIO;
  1020. prid = xfs_get_initial_prid(dp);
  1021. /*
  1022. * Make sure that we have allocated dquot(s) on disk.
  1023. */
  1024. error = xfs_qm_vop_dqalloc(dp, current_fsuid(), current_fsgid(), prid,
  1025. XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT,
  1026. &udqp, &gdqp, &pdqp);
  1027. if (error)
  1028. return error;
  1029. if (is_dir) {
  1030. resblks = XFS_MKDIR_SPACE_RES(mp, name->len);
  1031. tres = &M_RES(mp)->tr_mkdir;
  1032. } else {
  1033. resblks = XFS_CREATE_SPACE_RES(mp, name->len);
  1034. tres = &M_RES(mp)->tr_create;
  1035. }
  1036. /*
  1037. * Initially assume that the file does not exist and
  1038. * reserve the resources for that case. If that is not
  1039. * the case we'll drop the one we have and get a more
  1040. * appropriate transaction later.
  1041. */
  1042. error = xfs_trans_alloc(mp, tres, resblks, 0, 0, &tp);
  1043. if (error == -ENOSPC) {
  1044. /* flush outstanding delalloc blocks and retry */
  1045. xfs_flush_inodes(mp);
  1046. error = xfs_trans_alloc(mp, tres, resblks, 0, 0, &tp);
  1047. }
  1048. if (error)
  1049. goto out_release_inode;
  1050. xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
  1051. unlock_dp_on_error = true;
  1052. /*
  1053. * Reserve disk quota and the inode.
  1054. */
  1055. error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp,
  1056. pdqp, resblks, 1, 0);
  1057. if (error)
  1058. goto out_trans_cancel;
  1059. /*
  1060. * A newly created regular or special file just has one directory
  1061. * entry pointing to them, but a directory also the "." entry
  1062. * pointing to itself.
  1063. */
  1064. error = xfs_dir_ialloc(&tp, dp, mode, is_dir ? 2 : 1, rdev, prid, &ip);
  1065. if (error)
  1066. goto out_trans_cancel;
  1067. /*
  1068. * Now we join the directory inode to the transaction. We do not do it
  1069. * earlier because xfs_dir_ialloc might commit the previous transaction
  1070. * (and release all the locks). An error from here on will result in
  1071. * the transaction cancel unlocking dp so don't do it explicitly in the
  1072. * error path.
  1073. */
  1074. xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
  1075. unlock_dp_on_error = false;
  1076. error = xfs_dir_createname(tp, dp, name, ip->i_ino,
  1077. resblks - XFS_IALLOC_SPACE_RES(mp));
  1078. if (error) {
  1079. ASSERT(error != -ENOSPC);
  1080. goto out_trans_cancel;
  1081. }
  1082. xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  1083. xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
  1084. if (is_dir) {
  1085. error = xfs_dir_init(tp, ip, dp);
  1086. if (error)
  1087. goto out_trans_cancel;
  1088. xfs_bumplink(tp, dp);
  1089. }
  1090. /*
  1091. * If this is a synchronous mount, make sure that the
  1092. * create transaction goes to disk before returning to
  1093. * the user.
  1094. */
  1095. if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
  1096. xfs_trans_set_sync(tp);
  1097. /*
  1098. * Attach the dquot(s) to the inodes and modify them incore.
  1099. * These ids of the inode couldn't have changed since the new
  1100. * inode has been locked ever since it was created.
  1101. */
  1102. xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp, pdqp);
  1103. error = xfs_trans_commit(tp);
  1104. if (error)
  1105. goto out_release_inode;
  1106. xfs_qm_dqrele(udqp);
  1107. xfs_qm_dqrele(gdqp);
  1108. xfs_qm_dqrele(pdqp);
  1109. *ipp = ip;
  1110. return 0;
  1111. out_trans_cancel:
  1112. xfs_trans_cancel(tp);
  1113. out_release_inode:
  1114. /*
  1115. * Wait until after the current transaction is aborted to finish the
  1116. * setup of the inode and release the inode. This prevents recursive
  1117. * transactions and deadlocks from xfs_inactive.
  1118. */
  1119. if (ip) {
  1120. xfs_finish_inode_setup(ip);
  1121. xfs_irele(ip);
  1122. }
  1123. xfs_qm_dqrele(udqp);
  1124. xfs_qm_dqrele(gdqp);
  1125. xfs_qm_dqrele(pdqp);
  1126. if (unlock_dp_on_error)
  1127. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  1128. return error;
  1129. }
  1130. int
  1131. xfs_create_tmpfile(
  1132. struct xfs_inode *dp,
  1133. umode_t mode,
  1134. struct xfs_inode **ipp)
  1135. {
  1136. struct xfs_mount *mp = dp->i_mount;
  1137. struct xfs_inode *ip = NULL;
  1138. struct xfs_trans *tp = NULL;
  1139. int error;
  1140. prid_t prid;
  1141. struct xfs_dquot *udqp = NULL;
  1142. struct xfs_dquot *gdqp = NULL;
  1143. struct xfs_dquot *pdqp = NULL;
  1144. struct xfs_trans_res *tres;
  1145. uint resblks;
  1146. if (XFS_FORCED_SHUTDOWN(mp))
  1147. return -EIO;
  1148. prid = xfs_get_initial_prid(dp);
  1149. /*
  1150. * Make sure that we have allocated dquot(s) on disk.
  1151. */
  1152. error = xfs_qm_vop_dqalloc(dp, current_fsuid(), current_fsgid(), prid,
  1153. XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT,
  1154. &udqp, &gdqp, &pdqp);
  1155. if (error)
  1156. return error;
  1157. resblks = XFS_IALLOC_SPACE_RES(mp);
  1158. tres = &M_RES(mp)->tr_create_tmpfile;
  1159. error = xfs_trans_alloc(mp, tres, resblks, 0, 0, &tp);
  1160. if (error)
  1161. goto out_release_inode;
  1162. error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp,
  1163. pdqp, resblks, 1, 0);
  1164. if (error)
  1165. goto out_trans_cancel;
  1166. error = xfs_dir_ialloc(&tp, dp, mode, 0, 0, prid, &ip);
  1167. if (error)
  1168. goto out_trans_cancel;
  1169. if (mp->m_flags & XFS_MOUNT_WSYNC)
  1170. xfs_trans_set_sync(tp);
  1171. /*
  1172. * Attach the dquot(s) to the inodes and modify them incore.
  1173. * These ids of the inode couldn't have changed since the new
  1174. * inode has been locked ever since it was created.
  1175. */
  1176. xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp, pdqp);
  1177. error = xfs_iunlink(tp, ip);
  1178. if (error)
  1179. goto out_trans_cancel;
  1180. error = xfs_trans_commit(tp);
  1181. if (error)
  1182. goto out_release_inode;
  1183. xfs_qm_dqrele(udqp);
  1184. xfs_qm_dqrele(gdqp);
  1185. xfs_qm_dqrele(pdqp);
  1186. *ipp = ip;
  1187. return 0;
  1188. out_trans_cancel:
  1189. xfs_trans_cancel(tp);
  1190. out_release_inode:
  1191. /*
  1192. * Wait until after the current transaction is aborted to finish the
  1193. * setup of the inode and release the inode. This prevents recursive
  1194. * transactions and deadlocks from xfs_inactive.
  1195. */
  1196. if (ip) {
  1197. xfs_finish_inode_setup(ip);
  1198. xfs_irele(ip);
  1199. }
  1200. xfs_qm_dqrele(udqp);
  1201. xfs_qm_dqrele(gdqp);
  1202. xfs_qm_dqrele(pdqp);
  1203. return error;
  1204. }
  1205. int
  1206. xfs_link(
  1207. xfs_inode_t *tdp,
  1208. xfs_inode_t *sip,
  1209. struct xfs_name *target_name)
  1210. {
  1211. xfs_mount_t *mp = tdp->i_mount;
  1212. xfs_trans_t *tp;
  1213. int error;
  1214. int resblks;
  1215. trace_xfs_link(tdp, target_name);
  1216. ASSERT(!S_ISDIR(VFS_I(sip)->i_mode));
  1217. if (XFS_FORCED_SHUTDOWN(mp))
  1218. return -EIO;
  1219. error = xfs_qm_dqattach(sip);
  1220. if (error)
  1221. goto std_return;
  1222. error = xfs_qm_dqattach(tdp);
  1223. if (error)
  1224. goto std_return;
  1225. resblks = XFS_LINK_SPACE_RES(mp, target_name->len);
  1226. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_link, resblks, 0, 0, &tp);
  1227. if (error == -ENOSPC) {
  1228. resblks = 0;
  1229. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_link, 0, 0, 0, &tp);
  1230. }
  1231. if (error)
  1232. goto std_return;
  1233. xfs_lock_two_inodes(sip, XFS_ILOCK_EXCL, tdp, XFS_ILOCK_EXCL);
  1234. xfs_trans_ijoin(tp, sip, XFS_ILOCK_EXCL);
  1235. xfs_trans_ijoin(tp, tdp, XFS_ILOCK_EXCL);
  1236. /*
  1237. * If we are using project inheritance, we only allow hard link
  1238. * creation in our tree when the project IDs are the same; else
  1239. * the tree quota mechanism could be circumvented.
  1240. */
  1241. if (unlikely((tdp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
  1242. tdp->i_d.di_projid != sip->i_d.di_projid)) {
  1243. error = -EXDEV;
  1244. goto error_return;
  1245. }
  1246. if (!resblks) {
  1247. error = xfs_dir_canenter(tp, tdp, target_name);
  1248. if (error)
  1249. goto error_return;
  1250. }
  1251. /*
  1252. * Handle initial link state of O_TMPFILE inode
  1253. */
  1254. if (VFS_I(sip)->i_nlink == 0) {
  1255. error = xfs_iunlink_remove(tp, sip);
  1256. if (error)
  1257. goto error_return;
  1258. }
  1259. error = xfs_dir_createname(tp, tdp, target_name, sip->i_ino,
  1260. resblks);
  1261. if (error)
  1262. goto error_return;
  1263. xfs_trans_ichgtime(tp, tdp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  1264. xfs_trans_log_inode(tp, tdp, XFS_ILOG_CORE);
  1265. xfs_bumplink(tp, sip);
  1266. /*
  1267. * If this is a synchronous mount, make sure that the
  1268. * link transaction goes to disk before returning to
  1269. * the user.
  1270. */
  1271. if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
  1272. xfs_trans_set_sync(tp);
  1273. return xfs_trans_commit(tp);
  1274. error_return:
  1275. xfs_trans_cancel(tp);
  1276. std_return:
  1277. return error;
  1278. }
  1279. /* Clear the reflink flag and the cowblocks tag if possible. */
  1280. static void
  1281. xfs_itruncate_clear_reflink_flags(
  1282. struct xfs_inode *ip)
  1283. {
  1284. struct xfs_ifork *dfork;
  1285. struct xfs_ifork *cfork;
  1286. if (!xfs_is_reflink_inode(ip))
  1287. return;
  1288. dfork = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
  1289. cfork = XFS_IFORK_PTR(ip, XFS_COW_FORK);
  1290. if (dfork->if_bytes == 0 && cfork->if_bytes == 0)
  1291. ip->i_d.di_flags2 &= ~XFS_DIFLAG2_REFLINK;
  1292. if (cfork->if_bytes == 0)
  1293. xfs_inode_clear_cowblocks_tag(ip);
  1294. }
  1295. /*
  1296. * Free up the underlying blocks past new_size. The new size must be smaller
  1297. * than the current size. This routine can be used both for the attribute and
  1298. * data fork, and does not modify the inode size, which is left to the caller.
  1299. *
  1300. * The transaction passed to this routine must have made a permanent log
  1301. * reservation of at least XFS_ITRUNCATE_LOG_RES. This routine may commit the
  1302. * given transaction and start new ones, so make sure everything involved in
  1303. * the transaction is tidy before calling here. Some transaction will be
  1304. * returned to the caller to be committed. The incoming transaction must
  1305. * already include the inode, and both inode locks must be held exclusively.
  1306. * The inode must also be "held" within the transaction. On return the inode
  1307. * will be "held" within the returned transaction. This routine does NOT
  1308. * require any disk space to be reserved for it within the transaction.
  1309. *
  1310. * If we get an error, we must return with the inode locked and linked into the
  1311. * current transaction. This keeps things simple for the higher level code,
  1312. * because it always knows that the inode is locked and held in the transaction
  1313. * that returns to it whether errors occur or not. We don't mark the inode
  1314. * dirty on error so that transactions can be easily aborted if possible.
  1315. */
  1316. int
  1317. xfs_itruncate_extents_flags(
  1318. struct xfs_trans **tpp,
  1319. struct xfs_inode *ip,
  1320. int whichfork,
  1321. xfs_fsize_t new_size,
  1322. int flags)
  1323. {
  1324. struct xfs_mount *mp = ip->i_mount;
  1325. struct xfs_trans *tp = *tpp;
  1326. xfs_fileoff_t first_unmap_block;
  1327. xfs_filblks_t unmap_len;
  1328. int error = 0;
  1329. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  1330. ASSERT(!atomic_read(&VFS_I(ip)->i_count) ||
  1331. xfs_isilocked(ip, XFS_IOLOCK_EXCL));
  1332. ASSERT(new_size <= XFS_ISIZE(ip));
  1333. ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
  1334. ASSERT(ip->i_itemp != NULL);
  1335. ASSERT(ip->i_itemp->ili_lock_flags == 0);
  1336. ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
  1337. trace_xfs_itruncate_extents_start(ip, new_size);
  1338. flags |= xfs_bmapi_aflag(whichfork);
  1339. /*
  1340. * Since it is possible for space to become allocated beyond
  1341. * the end of the file (in a crash where the space is allocated
  1342. * but the inode size is not yet updated), simply remove any
  1343. * blocks which show up between the new EOF and the maximum
  1344. * possible file size.
  1345. *
  1346. * We have to free all the blocks to the bmbt maximum offset, even if
  1347. * the page cache can't scale that far.
  1348. */
  1349. first_unmap_block = XFS_B_TO_FSB(mp, (xfs_ufsize_t)new_size);
  1350. if (first_unmap_block >= XFS_MAX_FILEOFF) {
  1351. WARN_ON_ONCE(first_unmap_block > XFS_MAX_FILEOFF);
  1352. return 0;
  1353. }
  1354. unmap_len = XFS_MAX_FILEOFF - first_unmap_block + 1;
  1355. while (unmap_len > 0) {
  1356. ASSERT(tp->t_firstblock == NULLFSBLOCK);
  1357. error = __xfs_bunmapi(tp, ip, first_unmap_block, &unmap_len,
  1358. flags, XFS_ITRUNC_MAX_EXTENTS);
  1359. if (error)
  1360. goto out;
  1361. /* free the just unmapped extents */
  1362. error = xfs_defer_finish(&tp);
  1363. if (error)
  1364. goto out;
  1365. }
  1366. if (whichfork == XFS_DATA_FORK) {
  1367. /* Remove all pending CoW reservations. */
  1368. error = xfs_reflink_cancel_cow_blocks(ip, &tp,
  1369. first_unmap_block, XFS_MAX_FILEOFF, true);
  1370. if (error)
  1371. goto out;
  1372. xfs_itruncate_clear_reflink_flags(ip);
  1373. }
  1374. /*
  1375. * Always re-log the inode so that our permanent transaction can keep
  1376. * on rolling it forward in the log.
  1377. */
  1378. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  1379. trace_xfs_itruncate_extents_end(ip, new_size);
  1380. out:
  1381. *tpp = tp;
  1382. return error;
  1383. }
  1384. int
  1385. xfs_release(
  1386. xfs_inode_t *ip)
  1387. {
  1388. xfs_mount_t *mp = ip->i_mount;
  1389. int error;
  1390. if (!S_ISREG(VFS_I(ip)->i_mode) || (VFS_I(ip)->i_mode == 0))
  1391. return 0;
  1392. /* If this is a read-only mount, don't do this (would generate I/O) */
  1393. if (mp->m_flags & XFS_MOUNT_RDONLY)
  1394. return 0;
  1395. if (!XFS_FORCED_SHUTDOWN(mp)) {
  1396. int truncated;
  1397. /*
  1398. * If we previously truncated this file and removed old data
  1399. * in the process, we want to initiate "early" writeout on
  1400. * the last close. This is an attempt to combat the notorious
  1401. * NULL files problem which is particularly noticeable from a
  1402. * truncate down, buffered (re-)write (delalloc), followed by
  1403. * a crash. What we are effectively doing here is
  1404. * significantly reducing the time window where we'd otherwise
  1405. * be exposed to that problem.
  1406. */
  1407. truncated = xfs_iflags_test_and_clear(ip, XFS_ITRUNCATED);
  1408. if (truncated) {
  1409. xfs_iflags_clear(ip, XFS_IDIRTY_RELEASE);
  1410. if (ip->i_delayed_blks > 0) {
  1411. error = filemap_flush(VFS_I(ip)->i_mapping);
  1412. if (error)
  1413. return error;
  1414. }
  1415. }
  1416. }
  1417. if (VFS_I(ip)->i_nlink == 0)
  1418. return 0;
  1419. if (xfs_can_free_eofblocks(ip, false)) {
  1420. /*
  1421. * Check if the inode is being opened, written and closed
  1422. * frequently and we have delayed allocation blocks outstanding
  1423. * (e.g. streaming writes from the NFS server), truncating the
  1424. * blocks past EOF will cause fragmentation to occur.
  1425. *
  1426. * In this case don't do the truncation, but we have to be
  1427. * careful how we detect this case. Blocks beyond EOF show up as
  1428. * i_delayed_blks even when the inode is clean, so we need to
  1429. * truncate them away first before checking for a dirty release.
  1430. * Hence on the first dirty close we will still remove the
  1431. * speculative allocation, but after that we will leave it in
  1432. * place.
  1433. */
  1434. if (xfs_iflags_test(ip, XFS_IDIRTY_RELEASE))
  1435. return 0;
  1436. /*
  1437. * If we can't get the iolock just skip truncating the blocks
  1438. * past EOF because we could deadlock with the mmap_lock
  1439. * otherwise. We'll get another chance to drop them once the
  1440. * last reference to the inode is dropped, so we'll never leak
  1441. * blocks permanently.
  1442. */
  1443. if (xfs_ilock_nowait(ip, XFS_IOLOCK_EXCL)) {
  1444. error = xfs_free_eofblocks(ip);
  1445. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  1446. if (error)
  1447. return error;
  1448. }
  1449. /* delalloc blocks after truncation means it really is dirty */
  1450. if (ip->i_delayed_blks)
  1451. xfs_iflags_set(ip, XFS_IDIRTY_RELEASE);
  1452. }
  1453. return 0;
  1454. }
  1455. /*
  1456. * xfs_inactive_truncate
  1457. *
  1458. * Called to perform a truncate when an inode becomes unlinked.
  1459. */
  1460. STATIC int
  1461. xfs_inactive_truncate(
  1462. struct xfs_inode *ip)
  1463. {
  1464. struct xfs_mount *mp = ip->i_mount;
  1465. struct xfs_trans *tp;
  1466. int error;
  1467. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, 0, 0, 0, &tp);
  1468. if (error) {
  1469. ASSERT(XFS_FORCED_SHUTDOWN(mp));
  1470. return error;
  1471. }
  1472. xfs_ilock(ip, XFS_ILOCK_EXCL);
  1473. xfs_trans_ijoin(tp, ip, 0);
  1474. /*
  1475. * Log the inode size first to prevent stale data exposure in the event
  1476. * of a system crash before the truncate completes. See the related
  1477. * comment in xfs_vn_setattr_size() for details.
  1478. */
  1479. ip->i_d.di_size = 0;
  1480. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  1481. error = xfs_itruncate_extents(&tp, ip, XFS_DATA_FORK, 0);
  1482. if (error)
  1483. goto error_trans_cancel;
  1484. ASSERT(ip->i_df.if_nextents == 0);
  1485. error = xfs_trans_commit(tp);
  1486. if (error)
  1487. goto error_unlock;
  1488. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1489. return 0;
  1490. error_trans_cancel:
  1491. xfs_trans_cancel(tp);
  1492. error_unlock:
  1493. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1494. return error;
  1495. }
  1496. /*
  1497. * xfs_inactive_ifree()
  1498. *
  1499. * Perform the inode free when an inode is unlinked.
  1500. */
  1501. STATIC int
  1502. xfs_inactive_ifree(
  1503. struct xfs_inode *ip)
  1504. {
  1505. struct xfs_mount *mp = ip->i_mount;
  1506. struct xfs_trans *tp;
  1507. int error;
  1508. /*
  1509. * We try to use a per-AG reservation for any block needed by the finobt
  1510. * tree, but as the finobt feature predates the per-AG reservation
  1511. * support a degraded file system might not have enough space for the
  1512. * reservation at mount time. In that case try to dip into the reserved
  1513. * pool and pray.
  1514. *
  1515. * Send a warning if the reservation does happen to fail, as the inode
  1516. * now remains allocated and sits on the unlinked list until the fs is
  1517. * repaired.
  1518. */
  1519. if (unlikely(mp->m_finobt_nores)) {
  1520. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ifree,
  1521. XFS_IFREE_SPACE_RES(mp), 0, XFS_TRANS_RESERVE,
  1522. &tp);
  1523. } else {
  1524. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ifree, 0, 0, 0, &tp);
  1525. }
  1526. if (error) {
  1527. if (error == -ENOSPC) {
  1528. xfs_warn_ratelimited(mp,
  1529. "Failed to remove inode(s) from unlinked list. "
  1530. "Please free space, unmount and run xfs_repair.");
  1531. } else {
  1532. ASSERT(XFS_FORCED_SHUTDOWN(mp));
  1533. }
  1534. return error;
  1535. }
  1536. /*
  1537. * We do not hold the inode locked across the entire rolling transaction
  1538. * here. We only need to hold it for the first transaction that
  1539. * xfs_ifree() builds, which may mark the inode XFS_ISTALE if the
  1540. * underlying cluster buffer is freed. Relogging an XFS_ISTALE inode
  1541. * here breaks the relationship between cluster buffer invalidation and
  1542. * stale inode invalidation on cluster buffer item journal commit
  1543. * completion, and can result in leaving dirty stale inodes hanging
  1544. * around in memory.
  1545. *
  1546. * We have no need for serialising this inode operation against other
  1547. * operations - we freed the inode and hence reallocation is required
  1548. * and that will serialise on reallocating the space the deferops need
  1549. * to free. Hence we can unlock the inode on the first commit of
  1550. * the transaction rather than roll it right through the deferops. This
  1551. * avoids relogging the XFS_ISTALE inode.
  1552. *
  1553. * We check that xfs_ifree() hasn't grown an internal transaction roll
  1554. * by asserting that the inode is still locked when it returns.
  1555. */
  1556. xfs_ilock(ip, XFS_ILOCK_EXCL);
  1557. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  1558. error = xfs_ifree(tp, ip);
  1559. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  1560. if (error) {
  1561. /*
  1562. * If we fail to free the inode, shut down. The cancel
  1563. * might do that, we need to make sure. Otherwise the
  1564. * inode might be lost for a long time or forever.
  1565. */
  1566. if (!XFS_FORCED_SHUTDOWN(mp)) {
  1567. xfs_notice(mp, "%s: xfs_ifree returned error %d",
  1568. __func__, error);
  1569. xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
  1570. }
  1571. xfs_trans_cancel(tp);
  1572. return error;
  1573. }
  1574. /*
  1575. * Credit the quota account(s). The inode is gone.
  1576. */
  1577. xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_ICOUNT, -1);
  1578. /*
  1579. * Just ignore errors at this point. There is nothing we can do except
  1580. * to try to keep going. Make sure it's not a silent error.
  1581. */
  1582. error = xfs_trans_commit(tp);
  1583. if (error)
  1584. xfs_notice(mp, "%s: xfs_trans_commit returned error %d",
  1585. __func__, error);
  1586. return 0;
  1587. }
  1588. /*
  1589. * xfs_inactive
  1590. *
  1591. * This is called when the vnode reference count for the vnode
  1592. * goes to zero. If the file has been unlinked, then it must
  1593. * now be truncated. Also, we clear all of the read-ahead state
  1594. * kept for the inode here since the file is now closed.
  1595. */
  1596. void
  1597. xfs_inactive(
  1598. xfs_inode_t *ip)
  1599. {
  1600. struct xfs_mount *mp;
  1601. int error;
  1602. int truncate = 0;
  1603. /*
  1604. * If the inode is already free, then there can be nothing
  1605. * to clean up here.
  1606. */
  1607. if (VFS_I(ip)->i_mode == 0) {
  1608. ASSERT(ip->i_df.if_broot_bytes == 0);
  1609. return;
  1610. }
  1611. mp = ip->i_mount;
  1612. ASSERT(!xfs_iflags_test(ip, XFS_IRECOVERY));
  1613. /* If this is a read-only mount, don't do this (would generate I/O) */
  1614. if (mp->m_flags & XFS_MOUNT_RDONLY)
  1615. return;
  1616. /* Try to clean out the cow blocks if there are any. */
  1617. if (xfs_inode_has_cow_data(ip))
  1618. xfs_reflink_cancel_cow_range(ip, 0, NULLFILEOFF, true);
  1619. if (VFS_I(ip)->i_nlink != 0) {
  1620. /*
  1621. * force is true because we are evicting an inode from the
  1622. * cache. Post-eof blocks must be freed, lest we end up with
  1623. * broken free space accounting.
  1624. *
  1625. * Note: don't bother with iolock here since lockdep complains
  1626. * about acquiring it in reclaim context. We have the only
  1627. * reference to the inode at this point anyways.
  1628. */
  1629. if (xfs_can_free_eofblocks(ip, true))
  1630. xfs_free_eofblocks(ip);
  1631. return;
  1632. }
  1633. if (S_ISREG(VFS_I(ip)->i_mode) &&
  1634. (ip->i_d.di_size != 0 || XFS_ISIZE(ip) != 0 ||
  1635. ip->i_df.if_nextents > 0 || ip->i_delayed_blks > 0))
  1636. truncate = 1;
  1637. error = xfs_qm_dqattach(ip);
  1638. if (error)
  1639. return;
  1640. if (S_ISLNK(VFS_I(ip)->i_mode))
  1641. error = xfs_inactive_symlink(ip);
  1642. else if (truncate)
  1643. error = xfs_inactive_truncate(ip);
  1644. if (error)
  1645. return;
  1646. /*
  1647. * If there are attributes associated with the file then blow them away
  1648. * now. The code calls a routine that recursively deconstructs the
  1649. * attribute fork. If also blows away the in-core attribute fork.
  1650. */
  1651. if (XFS_IFORK_Q(ip)) {
  1652. error = xfs_attr_inactive(ip);
  1653. if (error)
  1654. return;
  1655. }
  1656. ASSERT(!ip->i_afp);
  1657. ASSERT(ip->i_d.di_forkoff == 0);
  1658. /*
  1659. * Free the inode.
  1660. */
  1661. error = xfs_inactive_ifree(ip);
  1662. if (error)
  1663. return;
  1664. /*
  1665. * Release the dquots held by inode, if any.
  1666. */
  1667. xfs_qm_dqdetach(ip);
  1668. }
  1669. /*
  1670. * In-Core Unlinked List Lookups
  1671. * =============================
  1672. *
  1673. * Every inode is supposed to be reachable from some other piece of metadata
  1674. * with the exception of the root directory. Inodes with a connection to a
  1675. * file descriptor but not linked from anywhere in the on-disk directory tree
  1676. * are collectively known as unlinked inodes, though the filesystem itself
  1677. * maintains links to these inodes so that on-disk metadata are consistent.
  1678. *
  1679. * XFS implements a per-AG on-disk hash table of unlinked inodes. The AGI
  1680. * header contains a number of buckets that point to an inode, and each inode
  1681. * record has a pointer to the next inode in the hash chain. This
  1682. * singly-linked list causes scaling problems in the iunlink remove function
  1683. * because we must walk that list to find the inode that points to the inode
  1684. * being removed from the unlinked hash bucket list.
  1685. *
  1686. * What if we modelled the unlinked list as a collection of records capturing
  1687. * "X.next_unlinked = Y" relations? If we indexed those records on Y, we'd
  1688. * have a fast way to look up unlinked list predecessors, which avoids the
  1689. * slow list walk. That's exactly what we do here (in-core) with a per-AG
  1690. * rhashtable.
  1691. *
  1692. * Because this is a backref cache, we ignore operational failures since the
  1693. * iunlink code can fall back to the slow bucket walk. The only errors that
  1694. * should bubble out are for obviously incorrect situations.
  1695. *
  1696. * All users of the backref cache MUST hold the AGI buffer lock to serialize
  1697. * access or have otherwise provided for concurrency control.
  1698. */
  1699. /* Capture a "X.next_unlinked = Y" relationship. */
  1700. struct xfs_iunlink {
  1701. struct rhash_head iu_rhash_head;
  1702. xfs_agino_t iu_agino; /* X */
  1703. xfs_agino_t iu_next_unlinked; /* Y */
  1704. };
  1705. /* Unlinked list predecessor lookup hashtable construction */
  1706. static int
  1707. xfs_iunlink_obj_cmpfn(
  1708. struct rhashtable_compare_arg *arg,
  1709. const void *obj)
  1710. {
  1711. const xfs_agino_t *key = arg->key;
  1712. const struct xfs_iunlink *iu = obj;
  1713. if (iu->iu_next_unlinked != *key)
  1714. return 1;
  1715. return 0;
  1716. }
  1717. static const struct rhashtable_params xfs_iunlink_hash_params = {
  1718. .min_size = XFS_AGI_UNLINKED_BUCKETS,
  1719. .key_len = sizeof(xfs_agino_t),
  1720. .key_offset = offsetof(struct xfs_iunlink,
  1721. iu_next_unlinked),
  1722. .head_offset = offsetof(struct xfs_iunlink, iu_rhash_head),
  1723. .automatic_shrinking = true,
  1724. .obj_cmpfn = xfs_iunlink_obj_cmpfn,
  1725. };
  1726. /*
  1727. * Return X, where X.next_unlinked == @agino. Returns NULLAGINO if no such
  1728. * relation is found.
  1729. */
  1730. static xfs_agino_t
  1731. xfs_iunlink_lookup_backref(
  1732. struct xfs_perag *pag,
  1733. xfs_agino_t agino)
  1734. {
  1735. struct xfs_iunlink *iu;
  1736. iu = rhashtable_lookup_fast(&pag->pagi_unlinked_hash, &agino,
  1737. xfs_iunlink_hash_params);
  1738. return iu ? iu->iu_agino : NULLAGINO;
  1739. }
  1740. /*
  1741. * Take ownership of an iunlink cache entry and insert it into the hash table.
  1742. * If successful, the entry will be owned by the cache; if not, it is freed.
  1743. * Either way, the caller does not own @iu after this call.
  1744. */
  1745. static int
  1746. xfs_iunlink_insert_backref(
  1747. struct xfs_perag *pag,
  1748. struct xfs_iunlink *iu)
  1749. {
  1750. int error;
  1751. error = rhashtable_insert_fast(&pag->pagi_unlinked_hash,
  1752. &iu->iu_rhash_head, xfs_iunlink_hash_params);
  1753. /*
  1754. * Fail loudly if there already was an entry because that's a sign of
  1755. * corruption of in-memory data. Also fail loudly if we see an error
  1756. * code we didn't anticipate from the rhashtable code. Currently we
  1757. * only anticipate ENOMEM.
  1758. */
  1759. if (error) {
  1760. WARN(error != -ENOMEM, "iunlink cache insert error %d", error);
  1761. kmem_free(iu);
  1762. }
  1763. /*
  1764. * Absorb any runtime errors that aren't a result of corruption because
  1765. * this is a cache and we can always fall back to bucket list scanning.
  1766. */
  1767. if (error != 0 && error != -EEXIST)
  1768. error = 0;
  1769. return error;
  1770. }
  1771. /* Remember that @prev_agino.next_unlinked = @this_agino. */
  1772. static int
  1773. xfs_iunlink_add_backref(
  1774. struct xfs_perag *pag,
  1775. xfs_agino_t prev_agino,
  1776. xfs_agino_t this_agino)
  1777. {
  1778. struct xfs_iunlink *iu;
  1779. if (XFS_TEST_ERROR(false, pag->pag_mount, XFS_ERRTAG_IUNLINK_FALLBACK))
  1780. return 0;
  1781. iu = kmem_zalloc(sizeof(*iu), KM_NOFS);
  1782. iu->iu_agino = prev_agino;
  1783. iu->iu_next_unlinked = this_agino;
  1784. return xfs_iunlink_insert_backref(pag, iu);
  1785. }
  1786. /*
  1787. * Replace X.next_unlinked = @agino with X.next_unlinked = @next_unlinked.
  1788. * If @next_unlinked is NULLAGINO, we drop the backref and exit. If there
  1789. * wasn't any such entry then we don't bother.
  1790. */
  1791. static int
  1792. xfs_iunlink_change_backref(
  1793. struct xfs_perag *pag,
  1794. xfs_agino_t agino,
  1795. xfs_agino_t next_unlinked)
  1796. {
  1797. struct xfs_iunlink *iu;
  1798. int error;
  1799. /* Look up the old entry; if there wasn't one then exit. */
  1800. iu = rhashtable_lookup_fast(&pag->pagi_unlinked_hash, &agino,
  1801. xfs_iunlink_hash_params);
  1802. if (!iu)
  1803. return 0;
  1804. /*
  1805. * Remove the entry. This shouldn't ever return an error, but if we
  1806. * couldn't remove the old entry we don't want to add it again to the
  1807. * hash table, and if the entry disappeared on us then someone's
  1808. * violated the locking rules and we need to fail loudly. Either way
  1809. * we cannot remove the inode because internal state is or would have
  1810. * been corrupt.
  1811. */
  1812. error = rhashtable_remove_fast(&pag->pagi_unlinked_hash,
  1813. &iu->iu_rhash_head, xfs_iunlink_hash_params);
  1814. if (error)
  1815. return error;
  1816. /* If there is no new next entry just free our item and return. */
  1817. if (next_unlinked == NULLAGINO) {
  1818. kmem_free(iu);
  1819. return 0;
  1820. }
  1821. /* Update the entry and re-add it to the hash table. */
  1822. iu->iu_next_unlinked = next_unlinked;
  1823. return xfs_iunlink_insert_backref(pag, iu);
  1824. }
  1825. /* Set up the in-core predecessor structures. */
  1826. int
  1827. xfs_iunlink_init(
  1828. struct xfs_perag *pag)
  1829. {
  1830. return rhashtable_init(&pag->pagi_unlinked_hash,
  1831. &xfs_iunlink_hash_params);
  1832. }
  1833. /* Free the in-core predecessor structures. */
  1834. static void
  1835. xfs_iunlink_free_item(
  1836. void *ptr,
  1837. void *arg)
  1838. {
  1839. struct xfs_iunlink *iu = ptr;
  1840. bool *freed_anything = arg;
  1841. *freed_anything = true;
  1842. kmem_free(iu);
  1843. }
  1844. void
  1845. xfs_iunlink_destroy(
  1846. struct xfs_perag *pag)
  1847. {
  1848. bool freed_anything = false;
  1849. rhashtable_free_and_destroy(&pag->pagi_unlinked_hash,
  1850. xfs_iunlink_free_item, &freed_anything);
  1851. ASSERT(freed_anything == false || XFS_FORCED_SHUTDOWN(pag->pag_mount));
  1852. }
  1853. /*
  1854. * Point the AGI unlinked bucket at an inode and log the results. The caller
  1855. * is responsible for validating the old value.
  1856. */
  1857. STATIC int
  1858. xfs_iunlink_update_bucket(
  1859. struct xfs_trans *tp,
  1860. xfs_agnumber_t agno,
  1861. struct xfs_buf *agibp,
  1862. unsigned int bucket_index,
  1863. xfs_agino_t new_agino)
  1864. {
  1865. struct xfs_agi *agi = agibp->b_addr;
  1866. xfs_agino_t old_value;
  1867. int offset;
  1868. ASSERT(xfs_verify_agino_or_null(tp->t_mountp, agno, new_agino));
  1869. old_value = be32_to_cpu(agi->agi_unlinked[bucket_index]);
  1870. trace_xfs_iunlink_update_bucket(tp->t_mountp, agno, bucket_index,
  1871. old_value, new_agino);
  1872. /*
  1873. * We should never find the head of the list already set to the value
  1874. * passed in because either we're adding or removing ourselves from the
  1875. * head of the list.
  1876. */
  1877. if (old_value == new_agino) {
  1878. xfs_buf_mark_corrupt(agibp);
  1879. return -EFSCORRUPTED;
  1880. }
  1881. agi->agi_unlinked[bucket_index] = cpu_to_be32(new_agino);
  1882. offset = offsetof(struct xfs_agi, agi_unlinked) +
  1883. (sizeof(xfs_agino_t) * bucket_index);
  1884. xfs_trans_log_buf(tp, agibp, offset, offset + sizeof(xfs_agino_t) - 1);
  1885. return 0;
  1886. }
  1887. /* Set an on-disk inode's next_unlinked pointer. */
  1888. STATIC void
  1889. xfs_iunlink_update_dinode(
  1890. struct xfs_trans *tp,
  1891. xfs_agnumber_t agno,
  1892. xfs_agino_t agino,
  1893. struct xfs_buf *ibp,
  1894. struct xfs_dinode *dip,
  1895. struct xfs_imap *imap,
  1896. xfs_agino_t next_agino)
  1897. {
  1898. struct xfs_mount *mp = tp->t_mountp;
  1899. int offset;
  1900. ASSERT(xfs_verify_agino_or_null(mp, agno, next_agino));
  1901. trace_xfs_iunlink_update_dinode(mp, agno, agino,
  1902. be32_to_cpu(dip->di_next_unlinked), next_agino);
  1903. dip->di_next_unlinked = cpu_to_be32(next_agino);
  1904. offset = imap->im_boffset +
  1905. offsetof(struct xfs_dinode, di_next_unlinked);
  1906. /* need to recalc the inode CRC if appropriate */
  1907. xfs_dinode_calc_crc(mp, dip);
  1908. xfs_trans_inode_buf(tp, ibp);
  1909. xfs_trans_log_buf(tp, ibp, offset, offset + sizeof(xfs_agino_t) - 1);
  1910. }
  1911. /* Set an in-core inode's unlinked pointer and return the old value. */
  1912. STATIC int
  1913. xfs_iunlink_update_inode(
  1914. struct xfs_trans *tp,
  1915. struct xfs_inode *ip,
  1916. xfs_agnumber_t agno,
  1917. xfs_agino_t next_agino,
  1918. xfs_agino_t *old_next_agino)
  1919. {
  1920. struct xfs_mount *mp = tp->t_mountp;
  1921. struct xfs_dinode *dip;
  1922. struct xfs_buf *ibp;
  1923. xfs_agino_t old_value;
  1924. int error;
  1925. ASSERT(xfs_verify_agino_or_null(mp, agno, next_agino));
  1926. error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp, 0);
  1927. if (error)
  1928. return error;
  1929. /* Make sure the old pointer isn't garbage. */
  1930. old_value = be32_to_cpu(dip->di_next_unlinked);
  1931. if (!xfs_verify_agino_or_null(mp, agno, old_value)) {
  1932. xfs_inode_verifier_error(ip, -EFSCORRUPTED, __func__, dip,
  1933. sizeof(*dip), __this_address);
  1934. error = -EFSCORRUPTED;
  1935. goto out;
  1936. }
  1937. /*
  1938. * Since we're updating a linked list, we should never find that the
  1939. * current pointer is the same as the new value, unless we're
  1940. * terminating the list.
  1941. */
  1942. *old_next_agino = old_value;
  1943. if (old_value == next_agino) {
  1944. if (next_agino != NULLAGINO) {
  1945. xfs_inode_verifier_error(ip, -EFSCORRUPTED, __func__,
  1946. dip, sizeof(*dip), __this_address);
  1947. error = -EFSCORRUPTED;
  1948. }
  1949. goto out;
  1950. }
  1951. /* Ok, update the new pointer. */
  1952. xfs_iunlink_update_dinode(tp, agno, XFS_INO_TO_AGINO(mp, ip->i_ino),
  1953. ibp, dip, &ip->i_imap, next_agino);
  1954. return 0;
  1955. out:
  1956. xfs_trans_brelse(tp, ibp);
  1957. return error;
  1958. }
  1959. /*
  1960. * This is called when the inode's link count has gone to 0 or we are creating
  1961. * a tmpfile via O_TMPFILE. The inode @ip must have nlink == 0.
  1962. *
  1963. * We place the on-disk inode on a list in the AGI. It will be pulled from this
  1964. * list when the inode is freed.
  1965. */
  1966. STATIC int
  1967. xfs_iunlink(
  1968. struct xfs_trans *tp,
  1969. struct xfs_inode *ip)
  1970. {
  1971. struct xfs_mount *mp = tp->t_mountp;
  1972. struct xfs_agi *agi;
  1973. struct xfs_buf *agibp;
  1974. xfs_agino_t next_agino;
  1975. xfs_agnumber_t agno = XFS_INO_TO_AGNO(mp, ip->i_ino);
  1976. xfs_agino_t agino = XFS_INO_TO_AGINO(mp, ip->i_ino);
  1977. short bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS;
  1978. int error;
  1979. ASSERT(VFS_I(ip)->i_nlink == 0);
  1980. ASSERT(VFS_I(ip)->i_mode != 0);
  1981. trace_xfs_iunlink(ip);
  1982. /* Get the agi buffer first. It ensures lock ordering on the list. */
  1983. error = xfs_read_agi(mp, tp, agno, &agibp);
  1984. if (error)
  1985. return error;
  1986. agi = agibp->b_addr;
  1987. /*
  1988. * Get the index into the agi hash table for the list this inode will
  1989. * go on. Make sure the pointer isn't garbage and that this inode
  1990. * isn't already on the list.
  1991. */
  1992. next_agino = be32_to_cpu(agi->agi_unlinked[bucket_index]);
  1993. if (next_agino == agino ||
  1994. !xfs_verify_agino_or_null(mp, agno, next_agino)) {
  1995. xfs_buf_mark_corrupt(agibp);
  1996. return -EFSCORRUPTED;
  1997. }
  1998. if (next_agino != NULLAGINO) {
  1999. xfs_agino_t old_agino;
  2000. /*
  2001. * There is already another inode in the bucket, so point this
  2002. * inode to the current head of the list.
  2003. */
  2004. error = xfs_iunlink_update_inode(tp, ip, agno, next_agino,
  2005. &old_agino);
  2006. if (error)
  2007. return error;
  2008. ASSERT(old_agino == NULLAGINO);
  2009. /*
  2010. * agino has been unlinked, add a backref from the next inode
  2011. * back to agino.
  2012. */
  2013. error = xfs_iunlink_add_backref(agibp->b_pag, agino, next_agino);
  2014. if (error)
  2015. return error;
  2016. }
  2017. /* Point the head of the list to point to this inode. */
  2018. return xfs_iunlink_update_bucket(tp, agno, agibp, bucket_index, agino);
  2019. }
  2020. /* Return the imap, dinode pointer, and buffer for an inode. */
  2021. STATIC int
  2022. xfs_iunlink_map_ino(
  2023. struct xfs_trans *tp,
  2024. xfs_agnumber_t agno,
  2025. xfs_agino_t agino,
  2026. struct xfs_imap *imap,
  2027. struct xfs_dinode **dipp,
  2028. struct xfs_buf **bpp)
  2029. {
  2030. struct xfs_mount *mp = tp->t_mountp;
  2031. int error;
  2032. imap->im_blkno = 0;
  2033. error = xfs_imap(mp, tp, XFS_AGINO_TO_INO(mp, agno, agino), imap, 0);
  2034. if (error) {
  2035. xfs_warn(mp, "%s: xfs_imap returned error %d.",
  2036. __func__, error);
  2037. return error;
  2038. }
  2039. error = xfs_imap_to_bp(mp, tp, imap, dipp, bpp, 0);
  2040. if (error) {
  2041. xfs_warn(mp, "%s: xfs_imap_to_bp returned error %d.",
  2042. __func__, error);
  2043. return error;
  2044. }
  2045. return 0;
  2046. }
  2047. /*
  2048. * Walk the unlinked chain from @head_agino until we find the inode that
  2049. * points to @target_agino. Return the inode number, map, dinode pointer,
  2050. * and inode cluster buffer of that inode as @agino, @imap, @dipp, and @bpp.
  2051. *
  2052. * @tp, @pag, @head_agino, and @target_agino are input parameters.
  2053. * @agino, @imap, @dipp, and @bpp are all output parameters.
  2054. *
  2055. * Do not call this function if @target_agino is the head of the list.
  2056. */
  2057. STATIC int
  2058. xfs_iunlink_map_prev(
  2059. struct xfs_trans *tp,
  2060. xfs_agnumber_t agno,
  2061. xfs_agino_t head_agino,
  2062. xfs_agino_t target_agino,
  2063. xfs_agino_t *agino,
  2064. struct xfs_imap *imap,
  2065. struct xfs_dinode **dipp,
  2066. struct xfs_buf **bpp,
  2067. struct xfs_perag *pag)
  2068. {
  2069. struct xfs_mount *mp = tp->t_mountp;
  2070. xfs_agino_t next_agino;
  2071. int error;
  2072. ASSERT(head_agino != target_agino);
  2073. *bpp = NULL;
  2074. /* See if our backref cache can find it faster. */
  2075. *agino = xfs_iunlink_lookup_backref(pag, target_agino);
  2076. if (*agino != NULLAGINO) {
  2077. error = xfs_iunlink_map_ino(tp, agno, *agino, imap, dipp, bpp);
  2078. if (error)
  2079. return error;
  2080. if (be32_to_cpu((*dipp)->di_next_unlinked) == target_agino)
  2081. return 0;
  2082. /*
  2083. * If we get here the cache contents were corrupt, so drop the
  2084. * buffer and fall back to walking the bucket list.
  2085. */
  2086. xfs_trans_brelse(tp, *bpp);
  2087. *bpp = NULL;
  2088. WARN_ON_ONCE(1);
  2089. }
  2090. trace_xfs_iunlink_map_prev_fallback(mp, agno);
  2091. /* Otherwise, walk the entire bucket until we find it. */
  2092. next_agino = head_agino;
  2093. while (next_agino != target_agino) {
  2094. xfs_agino_t unlinked_agino;
  2095. if (*bpp)
  2096. xfs_trans_brelse(tp, *bpp);
  2097. *agino = next_agino;
  2098. error = xfs_iunlink_map_ino(tp, agno, next_agino, imap, dipp,
  2099. bpp);
  2100. if (error)
  2101. return error;
  2102. unlinked_agino = be32_to_cpu((*dipp)->di_next_unlinked);
  2103. /*
  2104. * Make sure this pointer is valid and isn't an obvious
  2105. * infinite loop.
  2106. */
  2107. if (!xfs_verify_agino(mp, agno, unlinked_agino) ||
  2108. next_agino == unlinked_agino) {
  2109. XFS_CORRUPTION_ERROR(__func__,
  2110. XFS_ERRLEVEL_LOW, mp,
  2111. *dipp, sizeof(**dipp));
  2112. error = -EFSCORRUPTED;
  2113. return error;
  2114. }
  2115. next_agino = unlinked_agino;
  2116. }
  2117. return 0;
  2118. }
  2119. /*
  2120. * Pull the on-disk inode from the AGI unlinked list.
  2121. */
  2122. STATIC int
  2123. xfs_iunlink_remove(
  2124. struct xfs_trans *tp,
  2125. struct xfs_inode *ip)
  2126. {
  2127. struct xfs_mount *mp = tp->t_mountp;
  2128. struct xfs_agi *agi;
  2129. struct xfs_buf *agibp;
  2130. struct xfs_buf *last_ibp;
  2131. struct xfs_dinode *last_dip = NULL;
  2132. xfs_agnumber_t agno = XFS_INO_TO_AGNO(mp, ip->i_ino);
  2133. xfs_agino_t agino = XFS_INO_TO_AGINO(mp, ip->i_ino);
  2134. xfs_agino_t next_agino;
  2135. xfs_agino_t head_agino;
  2136. short bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS;
  2137. int error;
  2138. trace_xfs_iunlink_remove(ip);
  2139. /* Get the agi buffer first. It ensures lock ordering on the list. */
  2140. error = xfs_read_agi(mp, tp, agno, &agibp);
  2141. if (error)
  2142. return error;
  2143. agi = agibp->b_addr;
  2144. /*
  2145. * Get the index into the agi hash table for the list this inode will
  2146. * go on. Make sure the head pointer isn't garbage.
  2147. */
  2148. head_agino = be32_to_cpu(agi->agi_unlinked[bucket_index]);
  2149. if (!xfs_verify_agino(mp, agno, head_agino)) {
  2150. XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
  2151. agi, sizeof(*agi));
  2152. return -EFSCORRUPTED;
  2153. }
  2154. /*
  2155. * Set our inode's next_unlinked pointer to NULL and then return
  2156. * the old pointer value so that we can update whatever was previous
  2157. * to us in the list to point to whatever was next in the list.
  2158. */
  2159. error = xfs_iunlink_update_inode(tp, ip, agno, NULLAGINO, &next_agino);
  2160. if (error)
  2161. return error;
  2162. /*
  2163. * If there was a backref pointing from the next inode back to this
  2164. * one, remove it because we've removed this inode from the list.
  2165. *
  2166. * Later, if this inode was in the middle of the list we'll update
  2167. * this inode's backref to point from the next inode.
  2168. */
  2169. if (next_agino != NULLAGINO) {
  2170. error = xfs_iunlink_change_backref(agibp->b_pag, next_agino,
  2171. NULLAGINO);
  2172. if (error)
  2173. return error;
  2174. }
  2175. if (head_agino != agino) {
  2176. struct xfs_imap imap;
  2177. xfs_agino_t prev_agino;
  2178. /* We need to search the list for the inode being freed. */
  2179. error = xfs_iunlink_map_prev(tp, agno, head_agino, agino,
  2180. &prev_agino, &imap, &last_dip, &last_ibp,
  2181. agibp->b_pag);
  2182. if (error)
  2183. return error;
  2184. /* Point the previous inode on the list to the next inode. */
  2185. xfs_iunlink_update_dinode(tp, agno, prev_agino, last_ibp,
  2186. last_dip, &imap, next_agino);
  2187. /*
  2188. * Now we deal with the backref for this inode. If this inode
  2189. * pointed at a real inode, change the backref that pointed to
  2190. * us to point to our old next. If this inode was the end of
  2191. * the list, delete the backref that pointed to us. Note that
  2192. * change_backref takes care of deleting the backref if
  2193. * next_agino is NULLAGINO.
  2194. */
  2195. return xfs_iunlink_change_backref(agibp->b_pag, agino,
  2196. next_agino);
  2197. }
  2198. /* Point the head of the list to the next unlinked inode. */
  2199. return xfs_iunlink_update_bucket(tp, agno, agibp, bucket_index,
  2200. next_agino);
  2201. }
  2202. /*
  2203. * Look up the inode number specified and if it is not already marked XFS_ISTALE
  2204. * mark it stale. We should only find clean inodes in this lookup that aren't
  2205. * already stale.
  2206. */
  2207. static void
  2208. xfs_ifree_mark_inode_stale(
  2209. struct xfs_buf *bp,
  2210. struct xfs_inode *free_ip,
  2211. xfs_ino_t inum)
  2212. {
  2213. struct xfs_mount *mp = bp->b_mount;
  2214. struct xfs_perag *pag = bp->b_pag;
  2215. struct xfs_inode_log_item *iip;
  2216. struct xfs_inode *ip;
  2217. retry:
  2218. rcu_read_lock();
  2219. ip = radix_tree_lookup(&pag->pag_ici_root, XFS_INO_TO_AGINO(mp, inum));
  2220. /* Inode not in memory, nothing to do */
  2221. if (!ip) {
  2222. rcu_read_unlock();
  2223. return;
  2224. }
  2225. /*
  2226. * because this is an RCU protected lookup, we could find a recently
  2227. * freed or even reallocated inode during the lookup. We need to check
  2228. * under the i_flags_lock for a valid inode here. Skip it if it is not
  2229. * valid, the wrong inode or stale.
  2230. */
  2231. spin_lock(&ip->i_flags_lock);
  2232. if (ip->i_ino != inum || __xfs_iflags_test(ip, XFS_ISTALE))
  2233. goto out_iflags_unlock;
  2234. /*
  2235. * Don't try to lock/unlock the current inode, but we _cannot_ skip the
  2236. * other inodes that we did not find in the list attached to the buffer
  2237. * and are not already marked stale. If we can't lock it, back off and
  2238. * retry.
  2239. */
  2240. if (ip != free_ip) {
  2241. if (!xfs_ilock_nowait(ip, XFS_ILOCK_EXCL)) {
  2242. spin_unlock(&ip->i_flags_lock);
  2243. rcu_read_unlock();
  2244. delay(1);
  2245. goto retry;
  2246. }
  2247. }
  2248. ip->i_flags |= XFS_ISTALE;
  2249. /*
  2250. * If the inode is flushing, it is already attached to the buffer. All
  2251. * we needed to do here is mark the inode stale so buffer IO completion
  2252. * will remove it from the AIL.
  2253. */
  2254. iip = ip->i_itemp;
  2255. if (__xfs_iflags_test(ip, XFS_IFLUSHING)) {
  2256. ASSERT(!list_empty(&iip->ili_item.li_bio_list));
  2257. ASSERT(iip->ili_last_fields);
  2258. goto out_iunlock;
  2259. }
  2260. /*
  2261. * Inodes not attached to the buffer can be released immediately.
  2262. * Everything else has to go through xfs_iflush_abort() on journal
  2263. * commit as the flock synchronises removal of the inode from the
  2264. * cluster buffer against inode reclaim.
  2265. */
  2266. if (!iip || list_empty(&iip->ili_item.li_bio_list))
  2267. goto out_iunlock;
  2268. __xfs_iflags_set(ip, XFS_IFLUSHING);
  2269. spin_unlock(&ip->i_flags_lock);
  2270. rcu_read_unlock();
  2271. /* we have a dirty inode in memory that has not yet been flushed. */
  2272. spin_lock(&iip->ili_lock);
  2273. iip->ili_last_fields = iip->ili_fields;
  2274. iip->ili_fields = 0;
  2275. iip->ili_fsync_fields = 0;
  2276. spin_unlock(&iip->ili_lock);
  2277. ASSERT(iip->ili_last_fields);
  2278. if (ip != free_ip)
  2279. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  2280. return;
  2281. out_iunlock:
  2282. if (ip != free_ip)
  2283. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  2284. out_iflags_unlock:
  2285. spin_unlock(&ip->i_flags_lock);
  2286. rcu_read_unlock();
  2287. }
  2288. /*
  2289. * A big issue when freeing the inode cluster is that we _cannot_ skip any
  2290. * inodes that are in memory - they all must be marked stale and attached to
  2291. * the cluster buffer.
  2292. */
  2293. STATIC int
  2294. xfs_ifree_cluster(
  2295. struct xfs_inode *free_ip,
  2296. struct xfs_trans *tp,
  2297. struct xfs_icluster *xic)
  2298. {
  2299. struct xfs_mount *mp = free_ip->i_mount;
  2300. struct xfs_ino_geometry *igeo = M_IGEO(mp);
  2301. struct xfs_buf *bp;
  2302. xfs_daddr_t blkno;
  2303. xfs_ino_t inum = xic->first_ino;
  2304. int nbufs;
  2305. int i, j;
  2306. int ioffset;
  2307. int error;
  2308. nbufs = igeo->ialloc_blks / igeo->blocks_per_cluster;
  2309. for (j = 0; j < nbufs; j++, inum += igeo->inodes_per_cluster) {
  2310. /*
  2311. * The allocation bitmap tells us which inodes of the chunk were
  2312. * physically allocated. Skip the cluster if an inode falls into
  2313. * a sparse region.
  2314. */
  2315. ioffset = inum - xic->first_ino;
  2316. if ((xic->alloc & XFS_INOBT_MASK(ioffset)) == 0) {
  2317. ASSERT(ioffset % igeo->inodes_per_cluster == 0);
  2318. continue;
  2319. }
  2320. blkno = XFS_AGB_TO_DADDR(mp, XFS_INO_TO_AGNO(mp, inum),
  2321. XFS_INO_TO_AGBNO(mp, inum));
  2322. /*
  2323. * We obtain and lock the backing buffer first in the process
  2324. * here to ensure dirty inodes attached to the buffer remain in
  2325. * the flushing state while we mark them stale.
  2326. *
  2327. * If we scan the in-memory inodes first, then buffer IO can
  2328. * complete before we get a lock on it, and hence we may fail
  2329. * to mark all the active inodes on the buffer stale.
  2330. */
  2331. error = xfs_trans_get_buf(tp, mp->m_ddev_targp, blkno,
  2332. mp->m_bsize * igeo->blocks_per_cluster,
  2333. XBF_UNMAPPED, &bp);
  2334. if (error)
  2335. return error;
  2336. /*
  2337. * This buffer may not have been correctly initialised as we
  2338. * didn't read it from disk. That's not important because we are
  2339. * only using to mark the buffer as stale in the log, and to
  2340. * attach stale cached inodes on it. That means it will never be
  2341. * dispatched for IO. If it is, we want to know about it, and we
  2342. * want it to fail. We can acheive this by adding a write
  2343. * verifier to the buffer.
  2344. */
  2345. bp->b_ops = &xfs_inode_buf_ops;
  2346. /*
  2347. * Now we need to set all the cached clean inodes as XFS_ISTALE,
  2348. * too. This requires lookups, and will skip inodes that we've
  2349. * already marked XFS_ISTALE.
  2350. */
  2351. for (i = 0; i < igeo->inodes_per_cluster; i++)
  2352. xfs_ifree_mark_inode_stale(bp, free_ip, inum + i);
  2353. xfs_trans_stale_inode_buf(tp, bp);
  2354. xfs_trans_binval(tp, bp);
  2355. }
  2356. return 0;
  2357. }
  2358. /*
  2359. * This is called to return an inode to the inode free list.
  2360. * The inode should already be truncated to 0 length and have
  2361. * no pages associated with it. This routine also assumes that
  2362. * the inode is already a part of the transaction.
  2363. *
  2364. * The on-disk copy of the inode will have been added to the list
  2365. * of unlinked inodes in the AGI. We need to remove the inode from
  2366. * that list atomically with respect to freeing it here.
  2367. */
  2368. int
  2369. xfs_ifree(
  2370. struct xfs_trans *tp,
  2371. struct xfs_inode *ip)
  2372. {
  2373. int error;
  2374. struct xfs_icluster xic = { 0 };
  2375. struct xfs_inode_log_item *iip = ip->i_itemp;
  2376. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  2377. ASSERT(VFS_I(ip)->i_nlink == 0);
  2378. ASSERT(ip->i_df.if_nextents == 0);
  2379. ASSERT(ip->i_d.di_size == 0 || !S_ISREG(VFS_I(ip)->i_mode));
  2380. ASSERT(ip->i_d.di_nblocks == 0);
  2381. /*
  2382. * Pull the on-disk inode from the AGI unlinked list.
  2383. */
  2384. error = xfs_iunlink_remove(tp, ip);
  2385. if (error)
  2386. return error;
  2387. error = xfs_difree(tp, ip->i_ino, &xic);
  2388. if (error)
  2389. return error;
  2390. /*
  2391. * Free any local-format data sitting around before we reset the
  2392. * data fork to extents format. Note that the attr fork data has
  2393. * already been freed by xfs_attr_inactive.
  2394. */
  2395. if (ip->i_df.if_format == XFS_DINODE_FMT_LOCAL) {
  2396. kmem_free(ip->i_df.if_u1.if_data);
  2397. ip->i_df.if_u1.if_data = NULL;
  2398. ip->i_df.if_bytes = 0;
  2399. }
  2400. VFS_I(ip)->i_mode = 0; /* mark incore inode as free */
  2401. ip->i_d.di_flags = 0;
  2402. ip->i_d.di_flags2 = ip->i_mount->m_ino_geo.new_diflags2;
  2403. ip->i_d.di_dmevmask = 0;
  2404. ip->i_d.di_forkoff = 0; /* mark the attr fork not in use */
  2405. ip->i_df.if_format = XFS_DINODE_FMT_EXTENTS;
  2406. /* Don't attempt to replay owner changes for a deleted inode */
  2407. spin_lock(&iip->ili_lock);
  2408. iip->ili_fields &= ~(XFS_ILOG_AOWNER | XFS_ILOG_DOWNER);
  2409. spin_unlock(&iip->ili_lock);
  2410. /*
  2411. * Bump the generation count so no one will be confused
  2412. * by reincarnations of this inode.
  2413. */
  2414. VFS_I(ip)->i_generation++;
  2415. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  2416. if (xic.deleted)
  2417. error = xfs_ifree_cluster(ip, tp, &xic);
  2418. return error;
  2419. }
  2420. /*
  2421. * This is called to unpin an inode. The caller must have the inode locked
  2422. * in at least shared mode so that the buffer cannot be subsequently pinned
  2423. * once someone is waiting for it to be unpinned.
  2424. */
  2425. static void
  2426. xfs_iunpin(
  2427. struct xfs_inode *ip)
  2428. {
  2429. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
  2430. trace_xfs_inode_unpin_nowait(ip, _RET_IP_);
  2431. /* Give the log a push to start the unpinning I/O */
  2432. xfs_log_force_lsn(ip->i_mount, ip->i_itemp->ili_last_lsn, 0, NULL);
  2433. }
  2434. static void
  2435. __xfs_iunpin_wait(
  2436. struct xfs_inode *ip)
  2437. {
  2438. wait_queue_head_t *wq = bit_waitqueue(&ip->i_flags, __XFS_IPINNED_BIT);
  2439. DEFINE_WAIT_BIT(wait, &ip->i_flags, __XFS_IPINNED_BIT);
  2440. xfs_iunpin(ip);
  2441. do {
  2442. prepare_to_wait(wq, &wait.wq_entry, TASK_UNINTERRUPTIBLE);
  2443. if (xfs_ipincount(ip))
  2444. io_schedule();
  2445. } while (xfs_ipincount(ip));
  2446. finish_wait(wq, &wait.wq_entry);
  2447. }
  2448. void
  2449. xfs_iunpin_wait(
  2450. struct xfs_inode *ip)
  2451. {
  2452. if (xfs_ipincount(ip))
  2453. __xfs_iunpin_wait(ip);
  2454. }
  2455. /*
  2456. * Removing an inode from the namespace involves removing the directory entry
  2457. * and dropping the link count on the inode. Removing the directory entry can
  2458. * result in locking an AGF (directory blocks were freed) and removing a link
  2459. * count can result in placing the inode on an unlinked list which results in
  2460. * locking an AGI.
  2461. *
  2462. * The big problem here is that we have an ordering constraint on AGF and AGI
  2463. * locking - inode allocation locks the AGI, then can allocate a new extent for
  2464. * new inodes, locking the AGF after the AGI. Similarly, freeing the inode
  2465. * removes the inode from the unlinked list, requiring that we lock the AGI
  2466. * first, and then freeing the inode can result in an inode chunk being freed
  2467. * and hence freeing disk space requiring that we lock an AGF.
  2468. *
  2469. * Hence the ordering that is imposed by other parts of the code is AGI before
  2470. * AGF. This means we cannot remove the directory entry before we drop the inode
  2471. * reference count and put it on the unlinked list as this results in a lock
  2472. * order of AGF then AGI, and this can deadlock against inode allocation and
  2473. * freeing. Therefore we must drop the link counts before we remove the
  2474. * directory entry.
  2475. *
  2476. * This is still safe from a transactional point of view - it is not until we
  2477. * get to xfs_defer_finish() that we have the possibility of multiple
  2478. * transactions in this operation. Hence as long as we remove the directory
  2479. * entry and drop the link count in the first transaction of the remove
  2480. * operation, there are no transactional constraints on the ordering here.
  2481. */
  2482. int
  2483. xfs_remove(
  2484. xfs_inode_t *dp,
  2485. struct xfs_name *name,
  2486. xfs_inode_t *ip)
  2487. {
  2488. xfs_mount_t *mp = dp->i_mount;
  2489. xfs_trans_t *tp = NULL;
  2490. int is_dir = S_ISDIR(VFS_I(ip)->i_mode);
  2491. int error = 0;
  2492. uint resblks;
  2493. trace_xfs_remove(dp, name);
  2494. if (XFS_FORCED_SHUTDOWN(mp))
  2495. return -EIO;
  2496. error = xfs_qm_dqattach(dp);
  2497. if (error)
  2498. goto std_return;
  2499. error = xfs_qm_dqattach(ip);
  2500. if (error)
  2501. goto std_return;
  2502. /*
  2503. * We try to get the real space reservation first,
  2504. * allowing for directory btree deletion(s) implying
  2505. * possible bmap insert(s). If we can't get the space
  2506. * reservation then we use 0 instead, and avoid the bmap
  2507. * btree insert(s) in the directory code by, if the bmap
  2508. * insert tries to happen, instead trimming the LAST
  2509. * block from the directory.
  2510. */
  2511. resblks = XFS_REMOVE_SPACE_RES(mp);
  2512. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_remove, resblks, 0, 0, &tp);
  2513. if (error == -ENOSPC) {
  2514. resblks = 0;
  2515. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_remove, 0, 0, 0,
  2516. &tp);
  2517. }
  2518. if (error) {
  2519. ASSERT(error != -ENOSPC);
  2520. goto std_return;
  2521. }
  2522. xfs_lock_two_inodes(dp, XFS_ILOCK_EXCL, ip, XFS_ILOCK_EXCL);
  2523. xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
  2524. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  2525. /*
  2526. * If we're removing a directory perform some additional validation.
  2527. */
  2528. if (is_dir) {
  2529. ASSERT(VFS_I(ip)->i_nlink >= 2);
  2530. if (VFS_I(ip)->i_nlink != 2) {
  2531. error = -ENOTEMPTY;
  2532. goto out_trans_cancel;
  2533. }
  2534. if (!xfs_dir_isempty(ip)) {
  2535. error = -ENOTEMPTY;
  2536. goto out_trans_cancel;
  2537. }
  2538. /* Drop the link from ip's "..". */
  2539. error = xfs_droplink(tp, dp);
  2540. if (error)
  2541. goto out_trans_cancel;
  2542. /* Drop the "." link from ip to self. */
  2543. error = xfs_droplink(tp, ip);
  2544. if (error)
  2545. goto out_trans_cancel;
  2546. } else {
  2547. /*
  2548. * When removing a non-directory we need to log the parent
  2549. * inode here. For a directory this is done implicitly
  2550. * by the xfs_droplink call for the ".." entry.
  2551. */
  2552. xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
  2553. }
  2554. xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  2555. /* Drop the link from dp to ip. */
  2556. error = xfs_droplink(tp, ip);
  2557. if (error)
  2558. goto out_trans_cancel;
  2559. error = xfs_dir_removename(tp, dp, name, ip->i_ino, resblks);
  2560. if (error) {
  2561. ASSERT(error != -ENOENT);
  2562. goto out_trans_cancel;
  2563. }
  2564. /*
  2565. * If this is a synchronous mount, make sure that the
  2566. * remove transaction goes to disk before returning to
  2567. * the user.
  2568. */
  2569. if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
  2570. xfs_trans_set_sync(tp);
  2571. error = xfs_trans_commit(tp);
  2572. if (error)
  2573. goto std_return;
  2574. if (is_dir && xfs_inode_is_filestream(ip))
  2575. xfs_filestream_deassociate(ip);
  2576. return 0;
  2577. out_trans_cancel:
  2578. xfs_trans_cancel(tp);
  2579. std_return:
  2580. return error;
  2581. }
  2582. /*
  2583. * Enter all inodes for a rename transaction into a sorted array.
  2584. */
  2585. #define __XFS_SORT_INODES 5
  2586. STATIC void
  2587. xfs_sort_for_rename(
  2588. struct xfs_inode *dp1, /* in: old (source) directory inode */
  2589. struct xfs_inode *dp2, /* in: new (target) directory inode */
  2590. struct xfs_inode *ip1, /* in: inode of old entry */
  2591. struct xfs_inode *ip2, /* in: inode of new entry */
  2592. struct xfs_inode *wip, /* in: whiteout inode */
  2593. struct xfs_inode **i_tab,/* out: sorted array of inodes */
  2594. int *num_inodes) /* in/out: inodes in array */
  2595. {
  2596. int i, j;
  2597. ASSERT(*num_inodes == __XFS_SORT_INODES);
  2598. memset(i_tab, 0, *num_inodes * sizeof(struct xfs_inode *));
  2599. /*
  2600. * i_tab contains a list of pointers to inodes. We initialize
  2601. * the table here & we'll sort it. We will then use it to
  2602. * order the acquisition of the inode locks.
  2603. *
  2604. * Note that the table may contain duplicates. e.g., dp1 == dp2.
  2605. */
  2606. i = 0;
  2607. i_tab[i++] = dp1;
  2608. i_tab[i++] = dp2;
  2609. i_tab[i++] = ip1;
  2610. if (ip2)
  2611. i_tab[i++] = ip2;
  2612. if (wip)
  2613. i_tab[i++] = wip;
  2614. *num_inodes = i;
  2615. /*
  2616. * Sort the elements via bubble sort. (Remember, there are at
  2617. * most 5 elements to sort, so this is adequate.)
  2618. */
  2619. for (i = 0; i < *num_inodes; i++) {
  2620. for (j = 1; j < *num_inodes; j++) {
  2621. if (i_tab[j]->i_ino < i_tab[j-1]->i_ino) {
  2622. struct xfs_inode *temp = i_tab[j];
  2623. i_tab[j] = i_tab[j-1];
  2624. i_tab[j-1] = temp;
  2625. }
  2626. }
  2627. }
  2628. }
  2629. static int
  2630. xfs_finish_rename(
  2631. struct xfs_trans *tp)
  2632. {
  2633. /*
  2634. * If this is a synchronous mount, make sure that the rename transaction
  2635. * goes to disk before returning to the user.
  2636. */
  2637. if (tp->t_mountp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
  2638. xfs_trans_set_sync(tp);
  2639. return xfs_trans_commit(tp);
  2640. }
  2641. /*
  2642. * xfs_cross_rename()
  2643. *
  2644. * responsible for handling RENAME_EXCHANGE flag in renameat2() sytemcall
  2645. */
  2646. STATIC int
  2647. xfs_cross_rename(
  2648. struct xfs_trans *tp,
  2649. struct xfs_inode *dp1,
  2650. struct xfs_name *name1,
  2651. struct xfs_inode *ip1,
  2652. struct xfs_inode *dp2,
  2653. struct xfs_name *name2,
  2654. struct xfs_inode *ip2,
  2655. int spaceres)
  2656. {
  2657. int error = 0;
  2658. int ip1_flags = 0;
  2659. int ip2_flags = 0;
  2660. int dp2_flags = 0;
  2661. /* Swap inode number for dirent in first parent */
  2662. error = xfs_dir_replace(tp, dp1, name1, ip2->i_ino, spaceres);
  2663. if (error)
  2664. goto out_trans_abort;
  2665. /* Swap inode number for dirent in second parent */
  2666. error = xfs_dir_replace(tp, dp2, name2, ip1->i_ino, spaceres);
  2667. if (error)
  2668. goto out_trans_abort;
  2669. /*
  2670. * If we're renaming one or more directories across different parents,
  2671. * update the respective ".." entries (and link counts) to match the new
  2672. * parents.
  2673. */
  2674. if (dp1 != dp2) {
  2675. dp2_flags = XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG;
  2676. if (S_ISDIR(VFS_I(ip2)->i_mode)) {
  2677. error = xfs_dir_replace(tp, ip2, &xfs_name_dotdot,
  2678. dp1->i_ino, spaceres);
  2679. if (error)
  2680. goto out_trans_abort;
  2681. /* transfer ip2 ".." reference to dp1 */
  2682. if (!S_ISDIR(VFS_I(ip1)->i_mode)) {
  2683. error = xfs_droplink(tp, dp2);
  2684. if (error)
  2685. goto out_trans_abort;
  2686. xfs_bumplink(tp, dp1);
  2687. }
  2688. /*
  2689. * Although ip1 isn't changed here, userspace needs
  2690. * to be warned about the change, so that applications
  2691. * relying on it (like backup ones), will properly
  2692. * notify the change
  2693. */
  2694. ip1_flags |= XFS_ICHGTIME_CHG;
  2695. ip2_flags |= XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG;
  2696. }
  2697. if (S_ISDIR(VFS_I(ip1)->i_mode)) {
  2698. error = xfs_dir_replace(tp, ip1, &xfs_name_dotdot,
  2699. dp2->i_ino, spaceres);
  2700. if (error)
  2701. goto out_trans_abort;
  2702. /* transfer ip1 ".." reference to dp2 */
  2703. if (!S_ISDIR(VFS_I(ip2)->i_mode)) {
  2704. error = xfs_droplink(tp, dp1);
  2705. if (error)
  2706. goto out_trans_abort;
  2707. xfs_bumplink(tp, dp2);
  2708. }
  2709. /*
  2710. * Although ip2 isn't changed here, userspace needs
  2711. * to be warned about the change, so that applications
  2712. * relying on it (like backup ones), will properly
  2713. * notify the change
  2714. */
  2715. ip1_flags |= XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG;
  2716. ip2_flags |= XFS_ICHGTIME_CHG;
  2717. }
  2718. }
  2719. if (ip1_flags) {
  2720. xfs_trans_ichgtime(tp, ip1, ip1_flags);
  2721. xfs_trans_log_inode(tp, ip1, XFS_ILOG_CORE);
  2722. }
  2723. if (ip2_flags) {
  2724. xfs_trans_ichgtime(tp, ip2, ip2_flags);
  2725. xfs_trans_log_inode(tp, ip2, XFS_ILOG_CORE);
  2726. }
  2727. if (dp2_flags) {
  2728. xfs_trans_ichgtime(tp, dp2, dp2_flags);
  2729. xfs_trans_log_inode(tp, dp2, XFS_ILOG_CORE);
  2730. }
  2731. xfs_trans_ichgtime(tp, dp1, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  2732. xfs_trans_log_inode(tp, dp1, XFS_ILOG_CORE);
  2733. return xfs_finish_rename(tp);
  2734. out_trans_abort:
  2735. xfs_trans_cancel(tp);
  2736. return error;
  2737. }
  2738. /*
  2739. * xfs_rename_alloc_whiteout()
  2740. *
  2741. * Return a referenced, unlinked, unlocked inode that can be used as a
  2742. * whiteout in a rename transaction. We use a tmpfile inode here so that if we
  2743. * crash between allocating the inode and linking it into the rename transaction
  2744. * recovery will free the inode and we won't leak it.
  2745. */
  2746. static int
  2747. xfs_rename_alloc_whiteout(
  2748. struct xfs_inode *dp,
  2749. struct xfs_inode **wip)
  2750. {
  2751. struct xfs_inode *tmpfile;
  2752. int error;
  2753. error = xfs_create_tmpfile(dp, S_IFCHR | WHITEOUT_MODE, &tmpfile);
  2754. if (error)
  2755. return error;
  2756. /*
  2757. * Prepare the tmpfile inode as if it were created through the VFS.
  2758. * Complete the inode setup and flag it as linkable. nlink is already
  2759. * zero, so we can skip the drop_nlink.
  2760. */
  2761. xfs_setup_iops(tmpfile);
  2762. xfs_finish_inode_setup(tmpfile);
  2763. VFS_I(tmpfile)->i_state |= I_LINKABLE;
  2764. *wip = tmpfile;
  2765. return 0;
  2766. }
  2767. /*
  2768. * xfs_rename
  2769. */
  2770. int
  2771. xfs_rename(
  2772. struct xfs_inode *src_dp,
  2773. struct xfs_name *src_name,
  2774. struct xfs_inode *src_ip,
  2775. struct xfs_inode *target_dp,
  2776. struct xfs_name *target_name,
  2777. struct xfs_inode *target_ip,
  2778. unsigned int flags)
  2779. {
  2780. struct xfs_mount *mp = src_dp->i_mount;
  2781. struct xfs_trans *tp;
  2782. struct xfs_inode *wip = NULL; /* whiteout inode */
  2783. struct xfs_inode *inodes[__XFS_SORT_INODES];
  2784. struct xfs_buf *agibp;
  2785. int num_inodes = __XFS_SORT_INODES;
  2786. bool new_parent = (src_dp != target_dp);
  2787. bool src_is_directory = S_ISDIR(VFS_I(src_ip)->i_mode);
  2788. int spaceres;
  2789. int error;
  2790. trace_xfs_rename(src_dp, target_dp, src_name, target_name);
  2791. if ((flags & RENAME_EXCHANGE) && !target_ip)
  2792. return -EINVAL;
  2793. /*
  2794. * If we are doing a whiteout operation, allocate the whiteout inode
  2795. * we will be placing at the target and ensure the type is set
  2796. * appropriately.
  2797. */
  2798. if (flags & RENAME_WHITEOUT) {
  2799. ASSERT(!(flags & (RENAME_NOREPLACE | RENAME_EXCHANGE)));
  2800. error = xfs_rename_alloc_whiteout(target_dp, &wip);
  2801. if (error)
  2802. return error;
  2803. /* setup target dirent info as whiteout */
  2804. src_name->type = XFS_DIR3_FT_CHRDEV;
  2805. }
  2806. xfs_sort_for_rename(src_dp, target_dp, src_ip, target_ip, wip,
  2807. inodes, &num_inodes);
  2808. spaceres = XFS_RENAME_SPACE_RES(mp, target_name->len);
  2809. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_rename, spaceres, 0, 0, &tp);
  2810. if (error == -ENOSPC) {
  2811. spaceres = 0;
  2812. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_rename, 0, 0, 0,
  2813. &tp);
  2814. }
  2815. if (error)
  2816. goto out_release_wip;
  2817. /*
  2818. * Attach the dquots to the inodes
  2819. */
  2820. error = xfs_qm_vop_rename_dqattach(inodes);
  2821. if (error)
  2822. goto out_trans_cancel;
  2823. /*
  2824. * Lock all the participating inodes. Depending upon whether
  2825. * the target_name exists in the target directory, and
  2826. * whether the target directory is the same as the source
  2827. * directory, we can lock from 2 to 4 inodes.
  2828. */
  2829. xfs_lock_inodes(inodes, num_inodes, XFS_ILOCK_EXCL);
  2830. /*
  2831. * Join all the inodes to the transaction. From this point on,
  2832. * we can rely on either trans_commit or trans_cancel to unlock
  2833. * them.
  2834. */
  2835. xfs_trans_ijoin(tp, src_dp, XFS_ILOCK_EXCL);
  2836. if (new_parent)
  2837. xfs_trans_ijoin(tp, target_dp, XFS_ILOCK_EXCL);
  2838. xfs_trans_ijoin(tp, src_ip, XFS_ILOCK_EXCL);
  2839. if (target_ip)
  2840. xfs_trans_ijoin(tp, target_ip, XFS_ILOCK_EXCL);
  2841. if (wip)
  2842. xfs_trans_ijoin(tp, wip, XFS_ILOCK_EXCL);
  2843. /*
  2844. * If we are using project inheritance, we only allow renames
  2845. * into our tree when the project IDs are the same; else the
  2846. * tree quota mechanism would be circumvented.
  2847. */
  2848. if (unlikely((target_dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
  2849. target_dp->i_d.di_projid != src_ip->i_d.di_projid)) {
  2850. error = -EXDEV;
  2851. goto out_trans_cancel;
  2852. }
  2853. /* RENAME_EXCHANGE is unique from here on. */
  2854. if (flags & RENAME_EXCHANGE)
  2855. return xfs_cross_rename(tp, src_dp, src_name, src_ip,
  2856. target_dp, target_name, target_ip,
  2857. spaceres);
  2858. /*
  2859. * Check for expected errors before we dirty the transaction
  2860. * so we can return an error without a transaction abort.
  2861. */
  2862. if (target_ip == NULL) {
  2863. /*
  2864. * If there's no space reservation, check the entry will
  2865. * fit before actually inserting it.
  2866. */
  2867. if (!spaceres) {
  2868. error = xfs_dir_canenter(tp, target_dp, target_name);
  2869. if (error)
  2870. goto out_trans_cancel;
  2871. }
  2872. } else {
  2873. /*
  2874. * If target exists and it's a directory, check that whether
  2875. * it can be destroyed.
  2876. */
  2877. if (S_ISDIR(VFS_I(target_ip)->i_mode) &&
  2878. (!xfs_dir_isempty(target_ip) ||
  2879. (VFS_I(target_ip)->i_nlink > 2))) {
  2880. error = -EEXIST;
  2881. goto out_trans_cancel;
  2882. }
  2883. }
  2884. /*
  2885. * Directory entry creation below may acquire the AGF. Remove
  2886. * the whiteout from the unlinked list first to preserve correct
  2887. * AGI/AGF locking order. This dirties the transaction so failures
  2888. * after this point will abort and log recovery will clean up the
  2889. * mess.
  2890. *
  2891. * For whiteouts, we need to bump the link count on the whiteout
  2892. * inode. After this point, we have a real link, clear the tmpfile
  2893. * state flag from the inode so it doesn't accidentally get misused
  2894. * in future.
  2895. */
  2896. if (wip) {
  2897. ASSERT(VFS_I(wip)->i_nlink == 0);
  2898. error = xfs_iunlink_remove(tp, wip);
  2899. if (error)
  2900. goto out_trans_cancel;
  2901. xfs_bumplink(tp, wip);
  2902. VFS_I(wip)->i_state &= ~I_LINKABLE;
  2903. }
  2904. /*
  2905. * Set up the target.
  2906. */
  2907. if (target_ip == NULL) {
  2908. /*
  2909. * If target does not exist and the rename crosses
  2910. * directories, adjust the target directory link count
  2911. * to account for the ".." reference from the new entry.
  2912. */
  2913. error = xfs_dir_createname(tp, target_dp, target_name,
  2914. src_ip->i_ino, spaceres);
  2915. if (error)
  2916. goto out_trans_cancel;
  2917. xfs_trans_ichgtime(tp, target_dp,
  2918. XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  2919. if (new_parent && src_is_directory) {
  2920. xfs_bumplink(tp, target_dp);
  2921. }
  2922. } else { /* target_ip != NULL */
  2923. /*
  2924. * Link the source inode under the target name.
  2925. * If the source inode is a directory and we are moving
  2926. * it across directories, its ".." entry will be
  2927. * inconsistent until we replace that down below.
  2928. *
  2929. * In case there is already an entry with the same
  2930. * name at the destination directory, remove it first.
  2931. */
  2932. /*
  2933. * Check whether the replace operation will need to allocate
  2934. * blocks. This happens when the shortform directory lacks
  2935. * space and we have to convert it to a block format directory.
  2936. * When more blocks are necessary, we must lock the AGI first
  2937. * to preserve locking order (AGI -> AGF).
  2938. */
  2939. if (xfs_dir2_sf_replace_needblock(target_dp, src_ip->i_ino)) {
  2940. error = xfs_read_agi(mp, tp,
  2941. XFS_INO_TO_AGNO(mp, target_ip->i_ino),
  2942. &agibp);
  2943. if (error)
  2944. goto out_trans_cancel;
  2945. }
  2946. error = xfs_dir_replace(tp, target_dp, target_name,
  2947. src_ip->i_ino, spaceres);
  2948. if (error)
  2949. goto out_trans_cancel;
  2950. xfs_trans_ichgtime(tp, target_dp,
  2951. XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  2952. /*
  2953. * Decrement the link count on the target since the target
  2954. * dir no longer points to it.
  2955. */
  2956. error = xfs_droplink(tp, target_ip);
  2957. if (error)
  2958. goto out_trans_cancel;
  2959. if (src_is_directory) {
  2960. /*
  2961. * Drop the link from the old "." entry.
  2962. */
  2963. error = xfs_droplink(tp, target_ip);
  2964. if (error)
  2965. goto out_trans_cancel;
  2966. }
  2967. } /* target_ip != NULL */
  2968. /*
  2969. * Remove the source.
  2970. */
  2971. if (new_parent && src_is_directory) {
  2972. /*
  2973. * Rewrite the ".." entry to point to the new
  2974. * directory.
  2975. */
  2976. error = xfs_dir_replace(tp, src_ip, &xfs_name_dotdot,
  2977. target_dp->i_ino, spaceres);
  2978. ASSERT(error != -EEXIST);
  2979. if (error)
  2980. goto out_trans_cancel;
  2981. }
  2982. /*
  2983. * We always want to hit the ctime on the source inode.
  2984. *
  2985. * This isn't strictly required by the standards since the source
  2986. * inode isn't really being changed, but old unix file systems did
  2987. * it and some incremental backup programs won't work without it.
  2988. */
  2989. xfs_trans_ichgtime(tp, src_ip, XFS_ICHGTIME_CHG);
  2990. xfs_trans_log_inode(tp, src_ip, XFS_ILOG_CORE);
  2991. /*
  2992. * Adjust the link count on src_dp. This is necessary when
  2993. * renaming a directory, either within one parent when
  2994. * the target existed, or across two parent directories.
  2995. */
  2996. if (src_is_directory && (new_parent || target_ip != NULL)) {
  2997. /*
  2998. * Decrement link count on src_directory since the
  2999. * entry that's moved no longer points to it.
  3000. */
  3001. error = xfs_droplink(tp, src_dp);
  3002. if (error)
  3003. goto out_trans_cancel;
  3004. }
  3005. /*
  3006. * For whiteouts, we only need to update the source dirent with the
  3007. * inode number of the whiteout inode rather than removing it
  3008. * altogether.
  3009. */
  3010. if (wip) {
  3011. error = xfs_dir_replace(tp, src_dp, src_name, wip->i_ino,
  3012. spaceres);
  3013. } else
  3014. error = xfs_dir_removename(tp, src_dp, src_name, src_ip->i_ino,
  3015. spaceres);
  3016. if (error)
  3017. goto out_trans_cancel;
  3018. xfs_trans_ichgtime(tp, src_dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  3019. xfs_trans_log_inode(tp, src_dp, XFS_ILOG_CORE);
  3020. if (new_parent)
  3021. xfs_trans_log_inode(tp, target_dp, XFS_ILOG_CORE);
  3022. error = xfs_finish_rename(tp);
  3023. if (wip)
  3024. xfs_irele(wip);
  3025. return error;
  3026. out_trans_cancel:
  3027. xfs_trans_cancel(tp);
  3028. out_release_wip:
  3029. if (wip)
  3030. xfs_irele(wip);
  3031. return error;
  3032. }
  3033. static int
  3034. xfs_iflush(
  3035. struct xfs_inode *ip,
  3036. struct xfs_buf *bp)
  3037. {
  3038. struct xfs_inode_log_item *iip = ip->i_itemp;
  3039. struct xfs_dinode *dip;
  3040. struct xfs_mount *mp = ip->i_mount;
  3041. int error;
  3042. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
  3043. ASSERT(xfs_iflags_test(ip, XFS_IFLUSHING));
  3044. ASSERT(ip->i_df.if_format != XFS_DINODE_FMT_BTREE ||
  3045. ip->i_df.if_nextents > XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK));
  3046. ASSERT(iip->ili_item.li_buf == bp);
  3047. dip = xfs_buf_offset(bp, ip->i_imap.im_boffset);
  3048. /*
  3049. * We don't flush the inode if any of the following checks fail, but we
  3050. * do still update the log item and attach to the backing buffer as if
  3051. * the flush happened. This is a formality to facilitate predictable
  3052. * error handling as the caller will shutdown and fail the buffer.
  3053. */
  3054. error = -EFSCORRUPTED;
  3055. if (XFS_TEST_ERROR(dip->di_magic != cpu_to_be16(XFS_DINODE_MAGIC),
  3056. mp, XFS_ERRTAG_IFLUSH_1)) {
  3057. xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
  3058. "%s: Bad inode %Lu magic number 0x%x, ptr "PTR_FMT,
  3059. __func__, ip->i_ino, be16_to_cpu(dip->di_magic), dip);
  3060. goto flush_out;
  3061. }
  3062. if (S_ISREG(VFS_I(ip)->i_mode)) {
  3063. if (XFS_TEST_ERROR(
  3064. ip->i_df.if_format != XFS_DINODE_FMT_EXTENTS &&
  3065. ip->i_df.if_format != XFS_DINODE_FMT_BTREE,
  3066. mp, XFS_ERRTAG_IFLUSH_3)) {
  3067. xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
  3068. "%s: Bad regular inode %Lu, ptr "PTR_FMT,
  3069. __func__, ip->i_ino, ip);
  3070. goto flush_out;
  3071. }
  3072. } else if (S_ISDIR(VFS_I(ip)->i_mode)) {
  3073. if (XFS_TEST_ERROR(
  3074. ip->i_df.if_format != XFS_DINODE_FMT_EXTENTS &&
  3075. ip->i_df.if_format != XFS_DINODE_FMT_BTREE &&
  3076. ip->i_df.if_format != XFS_DINODE_FMT_LOCAL,
  3077. mp, XFS_ERRTAG_IFLUSH_4)) {
  3078. xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
  3079. "%s: Bad directory inode %Lu, ptr "PTR_FMT,
  3080. __func__, ip->i_ino, ip);
  3081. goto flush_out;
  3082. }
  3083. }
  3084. if (XFS_TEST_ERROR(ip->i_df.if_nextents + xfs_ifork_nextents(ip->i_afp) >
  3085. ip->i_d.di_nblocks, mp, XFS_ERRTAG_IFLUSH_5)) {
  3086. xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
  3087. "%s: detected corrupt incore inode %Lu, "
  3088. "total extents = %d, nblocks = %Ld, ptr "PTR_FMT,
  3089. __func__, ip->i_ino,
  3090. ip->i_df.if_nextents + xfs_ifork_nextents(ip->i_afp),
  3091. ip->i_d.di_nblocks, ip);
  3092. goto flush_out;
  3093. }
  3094. if (XFS_TEST_ERROR(ip->i_d.di_forkoff > mp->m_sb.sb_inodesize,
  3095. mp, XFS_ERRTAG_IFLUSH_6)) {
  3096. xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
  3097. "%s: bad inode %Lu, forkoff 0x%x, ptr "PTR_FMT,
  3098. __func__, ip->i_ino, ip->i_d.di_forkoff, ip);
  3099. goto flush_out;
  3100. }
  3101. /*
  3102. * Inode item log recovery for v2 inodes are dependent on the
  3103. * di_flushiter count for correct sequencing. We bump the flush
  3104. * iteration count so we can detect flushes which postdate a log record
  3105. * during recovery. This is redundant as we now log every change and
  3106. * hence this can't happen but we need to still do it to ensure
  3107. * backwards compatibility with old kernels that predate logging all
  3108. * inode changes.
  3109. */
  3110. if (!xfs_sb_version_has_v3inode(&mp->m_sb))
  3111. ip->i_d.di_flushiter++;
  3112. /*
  3113. * If there are inline format data / attr forks attached to this inode,
  3114. * make sure they are not corrupt.
  3115. */
  3116. if (ip->i_df.if_format == XFS_DINODE_FMT_LOCAL &&
  3117. xfs_ifork_verify_local_data(ip))
  3118. goto flush_out;
  3119. if (ip->i_afp && ip->i_afp->if_format == XFS_DINODE_FMT_LOCAL &&
  3120. xfs_ifork_verify_local_attr(ip))
  3121. goto flush_out;
  3122. /*
  3123. * Copy the dirty parts of the inode into the on-disk inode. We always
  3124. * copy out the core of the inode, because if the inode is dirty at all
  3125. * the core must be.
  3126. */
  3127. xfs_inode_to_disk(ip, dip, iip->ili_item.li_lsn);
  3128. /* Wrap, we never let the log put out DI_MAX_FLUSH */
  3129. if (ip->i_d.di_flushiter == DI_MAX_FLUSH)
  3130. ip->i_d.di_flushiter = 0;
  3131. xfs_iflush_fork(ip, dip, iip, XFS_DATA_FORK);
  3132. if (XFS_IFORK_Q(ip))
  3133. xfs_iflush_fork(ip, dip, iip, XFS_ATTR_FORK);
  3134. /*
  3135. * We've recorded everything logged in the inode, so we'd like to clear
  3136. * the ili_fields bits so we don't log and flush things unnecessarily.
  3137. * However, we can't stop logging all this information until the data
  3138. * we've copied into the disk buffer is written to disk. If we did we
  3139. * might overwrite the copy of the inode in the log with all the data
  3140. * after re-logging only part of it, and in the face of a crash we
  3141. * wouldn't have all the data we need to recover.
  3142. *
  3143. * What we do is move the bits to the ili_last_fields field. When
  3144. * logging the inode, these bits are moved back to the ili_fields field.
  3145. * In the xfs_buf_inode_iodone() routine we clear ili_last_fields, since
  3146. * we know that the information those bits represent is permanently on
  3147. * disk. As long as the flush completes before the inode is logged
  3148. * again, then both ili_fields and ili_last_fields will be cleared.
  3149. */
  3150. error = 0;
  3151. flush_out:
  3152. spin_lock(&iip->ili_lock);
  3153. iip->ili_last_fields = iip->ili_fields;
  3154. iip->ili_fields = 0;
  3155. iip->ili_fsync_fields = 0;
  3156. spin_unlock(&iip->ili_lock);
  3157. /*
  3158. * Store the current LSN of the inode so that we can tell whether the
  3159. * item has moved in the AIL from xfs_buf_inode_iodone().
  3160. */
  3161. xfs_trans_ail_copy_lsn(mp->m_ail, &iip->ili_flush_lsn,
  3162. &iip->ili_item.li_lsn);
  3163. /* generate the checksum. */
  3164. xfs_dinode_calc_crc(mp, dip);
  3165. return error;
  3166. }
  3167. /*
  3168. * Non-blocking flush of dirty inode metadata into the backing buffer.
  3169. *
  3170. * The caller must have a reference to the inode and hold the cluster buffer
  3171. * locked. The function will walk across all the inodes on the cluster buffer it
  3172. * can find and lock without blocking, and flush them to the cluster buffer.
  3173. *
  3174. * On successful flushing of at least one inode, the caller must write out the
  3175. * buffer and release it. If no inodes are flushed, -EAGAIN will be returned and
  3176. * the caller needs to release the buffer. On failure, the filesystem will be
  3177. * shut down, the buffer will have been unlocked and released, and EFSCORRUPTED
  3178. * will be returned.
  3179. */
  3180. int
  3181. xfs_iflush_cluster(
  3182. struct xfs_buf *bp)
  3183. {
  3184. struct xfs_mount *mp = bp->b_mount;
  3185. struct xfs_log_item *lip, *n;
  3186. struct xfs_inode *ip;
  3187. struct xfs_inode_log_item *iip;
  3188. int clcount = 0;
  3189. int error = 0;
  3190. /*
  3191. * We must use the safe variant here as on shutdown xfs_iflush_abort()
  3192. * can remove itself from the list.
  3193. */
  3194. list_for_each_entry_safe(lip, n, &bp->b_li_list, li_bio_list) {
  3195. iip = (struct xfs_inode_log_item *)lip;
  3196. ip = iip->ili_inode;
  3197. /*
  3198. * Quick and dirty check to avoid locks if possible.
  3199. */
  3200. if (__xfs_iflags_test(ip, XFS_IRECLAIM | XFS_IFLUSHING))
  3201. continue;
  3202. if (xfs_ipincount(ip))
  3203. continue;
  3204. /*
  3205. * The inode is still attached to the buffer, which means it is
  3206. * dirty but reclaim might try to grab it. Check carefully for
  3207. * that, and grab the ilock while still holding the i_flags_lock
  3208. * to guarantee reclaim will not be able to reclaim this inode
  3209. * once we drop the i_flags_lock.
  3210. */
  3211. spin_lock(&ip->i_flags_lock);
  3212. ASSERT(!__xfs_iflags_test(ip, XFS_ISTALE));
  3213. if (__xfs_iflags_test(ip, XFS_IRECLAIM | XFS_IFLUSHING)) {
  3214. spin_unlock(&ip->i_flags_lock);
  3215. continue;
  3216. }
  3217. /*
  3218. * ILOCK will pin the inode against reclaim and prevent
  3219. * concurrent transactions modifying the inode while we are
  3220. * flushing the inode. If we get the lock, set the flushing
  3221. * state before we drop the i_flags_lock.
  3222. */
  3223. if (!xfs_ilock_nowait(ip, XFS_ILOCK_SHARED)) {
  3224. spin_unlock(&ip->i_flags_lock);
  3225. continue;
  3226. }
  3227. __xfs_iflags_set(ip, XFS_IFLUSHING);
  3228. spin_unlock(&ip->i_flags_lock);
  3229. /*
  3230. * Abort flushing this inode if we are shut down because the
  3231. * inode may not currently be in the AIL. This can occur when
  3232. * log I/O failure unpins the inode without inserting into the
  3233. * AIL, leaving a dirty/unpinned inode attached to the buffer
  3234. * that otherwise looks like it should be flushed.
  3235. */
  3236. if (XFS_FORCED_SHUTDOWN(mp)) {
  3237. xfs_iunpin_wait(ip);
  3238. xfs_iflush_abort(ip);
  3239. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  3240. error = -EIO;
  3241. continue;
  3242. }
  3243. /* don't block waiting on a log force to unpin dirty inodes */
  3244. if (xfs_ipincount(ip)) {
  3245. xfs_iflags_clear(ip, XFS_IFLUSHING);
  3246. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  3247. continue;
  3248. }
  3249. if (!xfs_inode_clean(ip))
  3250. error = xfs_iflush(ip, bp);
  3251. else
  3252. xfs_iflags_clear(ip, XFS_IFLUSHING);
  3253. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  3254. if (error)
  3255. break;
  3256. clcount++;
  3257. }
  3258. if (error) {
  3259. bp->b_flags |= XBF_ASYNC;
  3260. xfs_buf_ioend_fail(bp);
  3261. xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
  3262. return error;
  3263. }
  3264. if (!clcount)
  3265. return -EAGAIN;
  3266. XFS_STATS_INC(mp, xs_icluster_flushcnt);
  3267. XFS_STATS_ADD(mp, xs_icluster_flushinode, clcount);
  3268. return 0;
  3269. }
  3270. /* Release an inode. */
  3271. void
  3272. xfs_irele(
  3273. struct xfs_inode *ip)
  3274. {
  3275. trace_xfs_irele(ip, _RET_IP_);
  3276. iput(VFS_I(ip));
  3277. }
  3278. /*
  3279. * Ensure all commited transactions touching the inode are written to the log.
  3280. */
  3281. int
  3282. xfs_log_force_inode(
  3283. struct xfs_inode *ip)
  3284. {
  3285. xfs_lsn_t lsn = 0;
  3286. xfs_ilock(ip, XFS_ILOCK_SHARED);
  3287. if (xfs_ipincount(ip))
  3288. lsn = ip->i_itemp->ili_last_lsn;
  3289. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  3290. if (!lsn)
  3291. return 0;
  3292. return xfs_log_force_lsn(ip->i_mount, lsn, XFS_LOG_SYNC, NULL);
  3293. }
  3294. /*
  3295. * Grab the exclusive iolock for a data copy from src to dest, making sure to
  3296. * abide vfs locking order (lowest pointer value goes first) and breaking the
  3297. * layout leases before proceeding. The loop is needed because we cannot call
  3298. * the blocking break_layout() with the iolocks held, and therefore have to
  3299. * back out both locks.
  3300. */
  3301. static int
  3302. xfs_iolock_two_inodes_and_break_layout(
  3303. struct inode *src,
  3304. struct inode *dest)
  3305. {
  3306. int error;
  3307. if (src > dest)
  3308. swap(src, dest);
  3309. retry:
  3310. /* Wait to break both inodes' layouts before we start locking. */
  3311. error = break_layout(src, true);
  3312. if (error)
  3313. return error;
  3314. if (src != dest) {
  3315. error = break_layout(dest, true);
  3316. if (error)
  3317. return error;
  3318. }
  3319. /* Lock one inode and make sure nobody got in and leased it. */
  3320. inode_lock(src);
  3321. error = break_layout(src, false);
  3322. if (error) {
  3323. inode_unlock(src);
  3324. if (error == -EWOULDBLOCK)
  3325. goto retry;
  3326. return error;
  3327. }
  3328. if (src == dest)
  3329. return 0;
  3330. /* Lock the other inode and make sure nobody got in and leased it. */
  3331. inode_lock_nested(dest, I_MUTEX_NONDIR2);
  3332. error = break_layout(dest, false);
  3333. if (error) {
  3334. inode_unlock(src);
  3335. inode_unlock(dest);
  3336. if (error == -EWOULDBLOCK)
  3337. goto retry;
  3338. return error;
  3339. }
  3340. return 0;
  3341. }
  3342. /*
  3343. * Lock two inodes so that userspace cannot initiate I/O via file syscalls or
  3344. * mmap activity.
  3345. */
  3346. int
  3347. xfs_ilock2_io_mmap(
  3348. struct xfs_inode *ip1,
  3349. struct xfs_inode *ip2)
  3350. {
  3351. int ret;
  3352. ret = xfs_iolock_two_inodes_and_break_layout(VFS_I(ip1), VFS_I(ip2));
  3353. if (ret)
  3354. return ret;
  3355. if (ip1 == ip2)
  3356. xfs_ilock(ip1, XFS_MMAPLOCK_EXCL);
  3357. else
  3358. xfs_lock_two_inodes(ip1, XFS_MMAPLOCK_EXCL,
  3359. ip2, XFS_MMAPLOCK_EXCL);
  3360. return 0;
  3361. }
  3362. /* Unlock both inodes to allow IO and mmap activity. */
  3363. void
  3364. xfs_iunlock2_io_mmap(
  3365. struct xfs_inode *ip1,
  3366. struct xfs_inode *ip2)
  3367. {
  3368. bool same_inode = (ip1 == ip2);
  3369. xfs_iunlock(ip2, XFS_MMAPLOCK_EXCL);
  3370. if (!same_inode)
  3371. xfs_iunlock(ip1, XFS_MMAPLOCK_EXCL);
  3372. inode_unlock(VFS_I(ip2));
  3373. if (!same_inode)
  3374. inode_unlock(VFS_I(ip1));
  3375. }