xfs_inode.c 94 KB

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