xfs_inode.c 98 KB

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