xfs_inode.c 100 KB

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