xfs_vnodeops.c 107 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357
  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 "xfs.h"
  19. #include "xfs_fs.h"
  20. #include "xfs_types.h"
  21. #include "xfs_bit.h"
  22. #include "xfs_log.h"
  23. #include "xfs_inum.h"
  24. #include "xfs_trans.h"
  25. #include "xfs_sb.h"
  26. #include "xfs_ag.h"
  27. #include "xfs_dir2.h"
  28. #include "xfs_dmapi.h"
  29. #include "xfs_mount.h"
  30. #include "xfs_da_btree.h"
  31. #include "xfs_bmap_btree.h"
  32. #include "xfs_alloc_btree.h"
  33. #include "xfs_ialloc_btree.h"
  34. #include "xfs_dir2_sf.h"
  35. #include "xfs_attr_sf.h"
  36. #include "xfs_dinode.h"
  37. #include "xfs_inode.h"
  38. #include "xfs_inode_item.h"
  39. #include "xfs_itable.h"
  40. #include "xfs_btree.h"
  41. #include "xfs_ialloc.h"
  42. #include "xfs_alloc.h"
  43. #include "xfs_bmap.h"
  44. #include "xfs_attr.h"
  45. #include "xfs_rw.h"
  46. #include "xfs_error.h"
  47. #include "xfs_quota.h"
  48. #include "xfs_utils.h"
  49. #include "xfs_rtalloc.h"
  50. #include "xfs_trans_space.h"
  51. #include "xfs_log_priv.h"
  52. #include "xfs_filestream.h"
  53. #include "xfs_vnodeops.h"
  54. int
  55. xfs_open(
  56. xfs_inode_t *ip)
  57. {
  58. int mode;
  59. if (XFS_FORCED_SHUTDOWN(ip->i_mount))
  60. return XFS_ERROR(EIO);
  61. /*
  62. * If it's a directory with any blocks, read-ahead block 0
  63. * as we're almost certain to have the next operation be a read there.
  64. */
  65. if (S_ISDIR(ip->i_d.di_mode) && ip->i_d.di_nextents > 0) {
  66. mode = xfs_ilock_map_shared(ip);
  67. if (ip->i_d.di_nextents > 0)
  68. (void)xfs_da_reada_buf(NULL, ip, 0, XFS_DATA_FORK);
  69. xfs_iunlock(ip, mode);
  70. }
  71. return 0;
  72. }
  73. /*
  74. * xfs_getattr
  75. */
  76. int
  77. xfs_getattr(
  78. xfs_inode_t *ip,
  79. bhv_vattr_t *vap,
  80. int flags)
  81. {
  82. bhv_vnode_t *vp = XFS_ITOV(ip);
  83. xfs_mount_t *mp = ip->i_mount;
  84. xfs_itrace_entry(ip);
  85. if (XFS_FORCED_SHUTDOWN(mp))
  86. return XFS_ERROR(EIO);
  87. if (!(flags & ATTR_LAZY))
  88. xfs_ilock(ip, XFS_ILOCK_SHARED);
  89. vap->va_size = XFS_ISIZE(ip);
  90. if (vap->va_mask == XFS_AT_SIZE)
  91. goto all_done;
  92. vap->va_nblocks =
  93. XFS_FSB_TO_BB(mp, ip->i_d.di_nblocks + ip->i_delayed_blks);
  94. vap->va_nodeid = ip->i_ino;
  95. #if XFS_BIG_INUMS
  96. vap->va_nodeid += mp->m_inoadd;
  97. #endif
  98. vap->va_nlink = ip->i_d.di_nlink;
  99. /*
  100. * Quick exit for non-stat callers
  101. */
  102. if ((vap->va_mask &
  103. ~(XFS_AT_SIZE|XFS_AT_FSID|XFS_AT_NODEID|
  104. XFS_AT_NLINK|XFS_AT_BLKSIZE)) == 0)
  105. goto all_done;
  106. /*
  107. * Copy from in-core inode.
  108. */
  109. vap->va_mode = ip->i_d.di_mode;
  110. vap->va_uid = ip->i_d.di_uid;
  111. vap->va_gid = ip->i_d.di_gid;
  112. vap->va_projid = ip->i_d.di_projid;
  113. /*
  114. * Check vnode type block/char vs. everything else.
  115. */
  116. switch (ip->i_d.di_mode & S_IFMT) {
  117. case S_IFBLK:
  118. case S_IFCHR:
  119. vap->va_rdev = ip->i_df.if_u2.if_rdev;
  120. vap->va_blocksize = BLKDEV_IOSIZE;
  121. break;
  122. default:
  123. vap->va_rdev = 0;
  124. if (!(XFS_IS_REALTIME_INODE(ip))) {
  125. vap->va_blocksize = xfs_preferred_iosize(mp);
  126. } else {
  127. /*
  128. * If the file blocks are being allocated from a
  129. * realtime partition, then return the inode's
  130. * realtime extent size or the realtime volume's
  131. * extent size.
  132. */
  133. vap->va_blocksize =
  134. xfs_get_extsz_hint(ip) << mp->m_sb.sb_blocklog;
  135. }
  136. break;
  137. }
  138. vn_atime_to_timespec(vp, &vap->va_atime);
  139. vap->va_mtime.tv_sec = ip->i_d.di_mtime.t_sec;
  140. vap->va_mtime.tv_nsec = ip->i_d.di_mtime.t_nsec;
  141. vap->va_ctime.tv_sec = ip->i_d.di_ctime.t_sec;
  142. vap->va_ctime.tv_nsec = ip->i_d.di_ctime.t_nsec;
  143. /*
  144. * Exit for stat callers. See if any of the rest of the fields
  145. * to be filled in are needed.
  146. */
  147. if ((vap->va_mask &
  148. (XFS_AT_XFLAGS|XFS_AT_EXTSIZE|XFS_AT_NEXTENTS|XFS_AT_ANEXTENTS|
  149. XFS_AT_GENCOUNT|XFS_AT_VCODE)) == 0)
  150. goto all_done;
  151. /*
  152. * Convert di_flags to xflags.
  153. */
  154. vap->va_xflags = xfs_ip2xflags(ip);
  155. /*
  156. * Exit for inode revalidate. See if any of the rest of
  157. * the fields to be filled in are needed.
  158. */
  159. if ((vap->va_mask &
  160. (XFS_AT_EXTSIZE|XFS_AT_NEXTENTS|XFS_AT_ANEXTENTS|
  161. XFS_AT_GENCOUNT|XFS_AT_VCODE)) == 0)
  162. goto all_done;
  163. vap->va_extsize = ip->i_d.di_extsize << mp->m_sb.sb_blocklog;
  164. vap->va_nextents =
  165. (ip->i_df.if_flags & XFS_IFEXTENTS) ?
  166. ip->i_df.if_bytes / sizeof(xfs_bmbt_rec_t) :
  167. ip->i_d.di_nextents;
  168. if (ip->i_afp)
  169. vap->va_anextents =
  170. (ip->i_afp->if_flags & XFS_IFEXTENTS) ?
  171. ip->i_afp->if_bytes / sizeof(xfs_bmbt_rec_t) :
  172. ip->i_d.di_anextents;
  173. else
  174. vap->va_anextents = 0;
  175. vap->va_gen = ip->i_d.di_gen;
  176. all_done:
  177. if (!(flags & ATTR_LAZY))
  178. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  179. return 0;
  180. }
  181. /*
  182. * xfs_setattr
  183. */
  184. int
  185. xfs_setattr(
  186. xfs_inode_t *ip,
  187. bhv_vattr_t *vap,
  188. int flags,
  189. cred_t *credp)
  190. {
  191. bhv_vnode_t *vp = XFS_ITOV(ip);
  192. xfs_mount_t *mp = ip->i_mount;
  193. xfs_trans_t *tp;
  194. int mask;
  195. int code;
  196. uint lock_flags;
  197. uint commit_flags=0;
  198. uid_t uid=0, iuid=0;
  199. gid_t gid=0, igid=0;
  200. int timeflags = 0;
  201. xfs_prid_t projid=0, iprojid=0;
  202. int mandlock_before, mandlock_after;
  203. struct xfs_dquot *udqp, *gdqp, *olddquot1, *olddquot2;
  204. int file_owner;
  205. int need_iolock = 1;
  206. xfs_itrace_entry(ip);
  207. if (mp->m_flags & XFS_MOUNT_RDONLY)
  208. return XFS_ERROR(EROFS);
  209. /*
  210. * Cannot set certain attributes.
  211. */
  212. mask = vap->va_mask;
  213. if (mask & XFS_AT_NOSET) {
  214. return XFS_ERROR(EINVAL);
  215. }
  216. if (XFS_FORCED_SHUTDOWN(mp))
  217. return XFS_ERROR(EIO);
  218. /*
  219. * Timestamps do not need to be logged and hence do not
  220. * need to be done within a transaction.
  221. */
  222. if (mask & XFS_AT_UPDTIMES) {
  223. ASSERT((mask & ~XFS_AT_UPDTIMES) == 0);
  224. timeflags = ((mask & XFS_AT_UPDATIME) ? XFS_ICHGTIME_ACC : 0) |
  225. ((mask & XFS_AT_UPDCTIME) ? XFS_ICHGTIME_CHG : 0) |
  226. ((mask & XFS_AT_UPDMTIME) ? XFS_ICHGTIME_MOD : 0);
  227. xfs_ichgtime(ip, timeflags);
  228. return 0;
  229. }
  230. olddquot1 = olddquot2 = NULL;
  231. udqp = gdqp = NULL;
  232. /*
  233. * If disk quotas is on, we make sure that the dquots do exist on disk,
  234. * before we start any other transactions. Trying to do this later
  235. * is messy. We don't care to take a readlock to look at the ids
  236. * in inode here, because we can't hold it across the trans_reserve.
  237. * If the IDs do change before we take the ilock, we're covered
  238. * because the i_*dquot fields will get updated anyway.
  239. */
  240. if (XFS_IS_QUOTA_ON(mp) &&
  241. (mask & (XFS_AT_UID|XFS_AT_GID|XFS_AT_PROJID))) {
  242. uint qflags = 0;
  243. if ((mask & XFS_AT_UID) && XFS_IS_UQUOTA_ON(mp)) {
  244. uid = vap->va_uid;
  245. qflags |= XFS_QMOPT_UQUOTA;
  246. } else {
  247. uid = ip->i_d.di_uid;
  248. }
  249. if ((mask & XFS_AT_GID) && XFS_IS_GQUOTA_ON(mp)) {
  250. gid = vap->va_gid;
  251. qflags |= XFS_QMOPT_GQUOTA;
  252. } else {
  253. gid = ip->i_d.di_gid;
  254. }
  255. if ((mask & XFS_AT_PROJID) && XFS_IS_PQUOTA_ON(mp)) {
  256. projid = vap->va_projid;
  257. qflags |= XFS_QMOPT_PQUOTA;
  258. } else {
  259. projid = ip->i_d.di_projid;
  260. }
  261. /*
  262. * We take a reference when we initialize udqp and gdqp,
  263. * so it is important that we never blindly double trip on
  264. * the same variable. See xfs_create() for an example.
  265. */
  266. ASSERT(udqp == NULL);
  267. ASSERT(gdqp == NULL);
  268. code = XFS_QM_DQVOPALLOC(mp, ip, uid, gid, projid, qflags,
  269. &udqp, &gdqp);
  270. if (code)
  271. return code;
  272. }
  273. /*
  274. * For the other attributes, we acquire the inode lock and
  275. * first do an error checking pass.
  276. */
  277. tp = NULL;
  278. lock_flags = XFS_ILOCK_EXCL;
  279. if (flags & ATTR_NOLOCK)
  280. need_iolock = 0;
  281. if (!(mask & XFS_AT_SIZE)) {
  282. if ((mask != (XFS_AT_CTIME|XFS_AT_ATIME|XFS_AT_MTIME)) ||
  283. (mp->m_flags & XFS_MOUNT_WSYNC)) {
  284. tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_NOT_SIZE);
  285. commit_flags = 0;
  286. if ((code = xfs_trans_reserve(tp, 0,
  287. XFS_ICHANGE_LOG_RES(mp), 0,
  288. 0, 0))) {
  289. lock_flags = 0;
  290. goto error_return;
  291. }
  292. }
  293. } else {
  294. if (DM_EVENT_ENABLED(ip, DM_EVENT_TRUNCATE) &&
  295. !(flags & ATTR_DMI)) {
  296. int dmflags = AT_DELAY_FLAG(flags) | DM_SEM_FLAG_WR;
  297. code = XFS_SEND_DATA(mp, DM_EVENT_TRUNCATE, vp,
  298. vap->va_size, 0, dmflags, NULL);
  299. if (code) {
  300. lock_flags = 0;
  301. goto error_return;
  302. }
  303. }
  304. if (need_iolock)
  305. lock_flags |= XFS_IOLOCK_EXCL;
  306. }
  307. xfs_ilock(ip, lock_flags);
  308. /* boolean: are we the file owner? */
  309. file_owner = (current_fsuid(credp) == ip->i_d.di_uid);
  310. /*
  311. * Change various properties of a file.
  312. * Only the owner or users with CAP_FOWNER
  313. * capability may do these things.
  314. */
  315. if (mask &
  316. (XFS_AT_MODE|XFS_AT_XFLAGS|XFS_AT_EXTSIZE|XFS_AT_UID|
  317. XFS_AT_GID|XFS_AT_PROJID)) {
  318. /*
  319. * CAP_FOWNER overrides the following restrictions:
  320. *
  321. * The user ID of the calling process must be equal
  322. * to the file owner ID, except in cases where the
  323. * CAP_FSETID capability is applicable.
  324. */
  325. if (!file_owner && !capable(CAP_FOWNER)) {
  326. code = XFS_ERROR(EPERM);
  327. goto error_return;
  328. }
  329. /*
  330. * CAP_FSETID overrides the following restrictions:
  331. *
  332. * The effective user ID of the calling process shall match
  333. * the file owner when setting the set-user-ID and
  334. * set-group-ID bits on that file.
  335. *
  336. * The effective group ID or one of the supplementary group
  337. * IDs of the calling process shall match the group owner of
  338. * the file when setting the set-group-ID bit on that file
  339. */
  340. if (mask & XFS_AT_MODE) {
  341. mode_t m = 0;
  342. if ((vap->va_mode & S_ISUID) && !file_owner)
  343. m |= S_ISUID;
  344. if ((vap->va_mode & S_ISGID) &&
  345. !in_group_p((gid_t)ip->i_d.di_gid))
  346. m |= S_ISGID;
  347. #if 0
  348. /* Linux allows this, Irix doesn't. */
  349. if ((vap->va_mode & S_ISVTX) && !VN_ISDIR(vp))
  350. m |= S_ISVTX;
  351. #endif
  352. if (m && !capable(CAP_FSETID))
  353. vap->va_mode &= ~m;
  354. }
  355. }
  356. /*
  357. * Change file ownership. Must be the owner or privileged.
  358. * If the system was configured with the "restricted_chown"
  359. * option, the owner is not permitted to give away the file,
  360. * and can change the group id only to a group of which he
  361. * or she is a member.
  362. */
  363. if (mask & (XFS_AT_UID|XFS_AT_GID|XFS_AT_PROJID)) {
  364. /*
  365. * These IDs could have changed since we last looked at them.
  366. * But, we're assured that if the ownership did change
  367. * while we didn't have the inode locked, inode's dquot(s)
  368. * would have changed also.
  369. */
  370. iuid = ip->i_d.di_uid;
  371. iprojid = ip->i_d.di_projid;
  372. igid = ip->i_d.di_gid;
  373. gid = (mask & XFS_AT_GID) ? vap->va_gid : igid;
  374. uid = (mask & XFS_AT_UID) ? vap->va_uid : iuid;
  375. projid = (mask & XFS_AT_PROJID) ? (xfs_prid_t)vap->va_projid :
  376. iprojid;
  377. /*
  378. * CAP_CHOWN overrides the following restrictions:
  379. *
  380. * If _POSIX_CHOWN_RESTRICTED is defined, this capability
  381. * shall override the restriction that a process cannot
  382. * change the user ID of a file it owns and the restriction
  383. * that the group ID supplied to the chown() function
  384. * shall be equal to either the group ID or one of the
  385. * supplementary group IDs of the calling process.
  386. */
  387. if (restricted_chown &&
  388. (iuid != uid || (igid != gid &&
  389. !in_group_p((gid_t)gid))) &&
  390. !capable(CAP_CHOWN)) {
  391. code = XFS_ERROR(EPERM);
  392. goto error_return;
  393. }
  394. /*
  395. * Do a quota reservation only if uid/projid/gid is actually
  396. * going to change.
  397. */
  398. if ((XFS_IS_UQUOTA_ON(mp) && iuid != uid) ||
  399. (XFS_IS_PQUOTA_ON(mp) && iprojid != projid) ||
  400. (XFS_IS_GQUOTA_ON(mp) && igid != gid)) {
  401. ASSERT(tp);
  402. code = XFS_QM_DQVOPCHOWNRESV(mp, tp, ip, udqp, gdqp,
  403. capable(CAP_FOWNER) ?
  404. XFS_QMOPT_FORCE_RES : 0);
  405. if (code) /* out of quota */
  406. goto error_return;
  407. }
  408. }
  409. /*
  410. * Truncate file. Must have write permission and not be a directory.
  411. */
  412. if (mask & XFS_AT_SIZE) {
  413. /* Short circuit the truncate case for zero length files */
  414. if ((vap->va_size == 0) &&
  415. (ip->i_size == 0) && (ip->i_d.di_nextents == 0)) {
  416. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  417. lock_flags &= ~XFS_ILOCK_EXCL;
  418. if (mask & XFS_AT_CTIME)
  419. xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  420. code = 0;
  421. goto error_return;
  422. }
  423. if (VN_ISDIR(vp)) {
  424. code = XFS_ERROR(EISDIR);
  425. goto error_return;
  426. } else if (!VN_ISREG(vp)) {
  427. code = XFS_ERROR(EINVAL);
  428. goto error_return;
  429. }
  430. /*
  431. * Make sure that the dquots are attached to the inode.
  432. */
  433. if ((code = XFS_QM_DQATTACH(mp, ip, XFS_QMOPT_ILOCKED)))
  434. goto error_return;
  435. }
  436. /*
  437. * Change file access or modified times.
  438. */
  439. if (mask & (XFS_AT_ATIME|XFS_AT_MTIME)) {
  440. if (!file_owner) {
  441. if ((flags & ATTR_UTIME) &&
  442. !capable(CAP_FOWNER)) {
  443. code = XFS_ERROR(EPERM);
  444. goto error_return;
  445. }
  446. }
  447. }
  448. /*
  449. * Change extent size or realtime flag.
  450. */
  451. if (mask & (XFS_AT_EXTSIZE|XFS_AT_XFLAGS)) {
  452. /*
  453. * Can't change extent size if any extents are allocated.
  454. */
  455. if (ip->i_d.di_nextents && (mask & XFS_AT_EXTSIZE) &&
  456. ((ip->i_d.di_extsize << mp->m_sb.sb_blocklog) !=
  457. vap->va_extsize) ) {
  458. code = XFS_ERROR(EINVAL); /* EFBIG? */
  459. goto error_return;
  460. }
  461. /*
  462. * Can't change realtime flag if any extents are allocated.
  463. */
  464. if ((ip->i_d.di_nextents || ip->i_delayed_blks) &&
  465. (mask & XFS_AT_XFLAGS) &&
  466. (XFS_IS_REALTIME_INODE(ip)) !=
  467. (vap->va_xflags & XFS_XFLAG_REALTIME)) {
  468. code = XFS_ERROR(EINVAL); /* EFBIG? */
  469. goto error_return;
  470. }
  471. /*
  472. * Extent size must be a multiple of the appropriate block
  473. * size, if set at all.
  474. */
  475. if ((mask & XFS_AT_EXTSIZE) && vap->va_extsize != 0) {
  476. xfs_extlen_t size;
  477. if (XFS_IS_REALTIME_INODE(ip) ||
  478. ((mask & XFS_AT_XFLAGS) &&
  479. (vap->va_xflags & XFS_XFLAG_REALTIME))) {
  480. size = mp->m_sb.sb_rextsize <<
  481. mp->m_sb.sb_blocklog;
  482. } else {
  483. size = mp->m_sb.sb_blocksize;
  484. }
  485. if (vap->va_extsize % size) {
  486. code = XFS_ERROR(EINVAL);
  487. goto error_return;
  488. }
  489. }
  490. /*
  491. * If realtime flag is set then must have realtime data.
  492. */
  493. if ((mask & XFS_AT_XFLAGS) &&
  494. (vap->va_xflags & XFS_XFLAG_REALTIME)) {
  495. if ((mp->m_sb.sb_rblocks == 0) ||
  496. (mp->m_sb.sb_rextsize == 0) ||
  497. (ip->i_d.di_extsize % mp->m_sb.sb_rextsize)) {
  498. code = XFS_ERROR(EINVAL);
  499. goto error_return;
  500. }
  501. }
  502. /*
  503. * Can't modify an immutable/append-only file unless
  504. * we have appropriate permission.
  505. */
  506. if ((mask & XFS_AT_XFLAGS) &&
  507. (ip->i_d.di_flags &
  508. (XFS_DIFLAG_IMMUTABLE|XFS_DIFLAG_APPEND) ||
  509. (vap->va_xflags &
  510. (XFS_XFLAG_IMMUTABLE | XFS_XFLAG_APPEND))) &&
  511. !capable(CAP_LINUX_IMMUTABLE)) {
  512. code = XFS_ERROR(EPERM);
  513. goto error_return;
  514. }
  515. }
  516. /*
  517. * Now we can make the changes. Before we join the inode
  518. * to the transaction, if XFS_AT_SIZE is set then take care of
  519. * the part of the truncation that must be done without the
  520. * inode lock. This needs to be done before joining the inode
  521. * to the transaction, because the inode cannot be unlocked
  522. * once it is a part of the transaction.
  523. */
  524. if (mask & XFS_AT_SIZE) {
  525. code = 0;
  526. if ((vap->va_size > ip->i_size) &&
  527. (flags & ATTR_NOSIZETOK) == 0) {
  528. code = xfs_igrow_start(ip, vap->va_size, credp);
  529. }
  530. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  531. /*
  532. * We are going to log the inode size change in this
  533. * transaction so any previous writes that are beyond the on
  534. * disk EOF and the new EOF that have not been written out need
  535. * to be written here. If we do not write the data out, we
  536. * expose ourselves to the null files problem.
  537. *
  538. * Only flush from the on disk size to the smaller of the in
  539. * memory file size or the new size as that's the range we
  540. * really care about here and prevents waiting for other data
  541. * not within the range we care about here.
  542. */
  543. if (!code &&
  544. (ip->i_size != ip->i_d.di_size) &&
  545. (vap->va_size > ip->i_d.di_size)) {
  546. code = xfs_flush_pages(ip,
  547. ip->i_d.di_size, vap->va_size,
  548. XFS_B_ASYNC, FI_NONE);
  549. }
  550. /* wait for all I/O to complete */
  551. vn_iowait(ip);
  552. if (!code)
  553. code = xfs_itruncate_data(ip, vap->va_size);
  554. if (code) {
  555. ASSERT(tp == NULL);
  556. lock_flags &= ~XFS_ILOCK_EXCL;
  557. ASSERT(lock_flags == XFS_IOLOCK_EXCL);
  558. goto error_return;
  559. }
  560. tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_SIZE);
  561. if ((code = xfs_trans_reserve(tp, 0,
  562. XFS_ITRUNCATE_LOG_RES(mp), 0,
  563. XFS_TRANS_PERM_LOG_RES,
  564. XFS_ITRUNCATE_LOG_COUNT))) {
  565. xfs_trans_cancel(tp, 0);
  566. if (need_iolock)
  567. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  568. return code;
  569. }
  570. commit_flags = XFS_TRANS_RELEASE_LOG_RES;
  571. xfs_ilock(ip, XFS_ILOCK_EXCL);
  572. }
  573. if (tp) {
  574. xfs_trans_ijoin(tp, ip, lock_flags);
  575. xfs_trans_ihold(tp, ip);
  576. }
  577. /* determine whether mandatory locking mode changes */
  578. mandlock_before = MANDLOCK(vp, ip->i_d.di_mode);
  579. /*
  580. * Truncate file. Must have write permission and not be a directory.
  581. */
  582. if (mask & XFS_AT_SIZE) {
  583. if (vap->va_size > ip->i_size) {
  584. xfs_igrow_finish(tp, ip, vap->va_size,
  585. !(flags & ATTR_DMI));
  586. } else if ((vap->va_size <= ip->i_size) ||
  587. ((vap->va_size == 0) && ip->i_d.di_nextents)) {
  588. /*
  589. * signal a sync transaction unless
  590. * we're truncating an already unlinked
  591. * file on a wsync filesystem
  592. */
  593. code = xfs_itruncate_finish(&tp, ip,
  594. (xfs_fsize_t)vap->va_size,
  595. XFS_DATA_FORK,
  596. ((ip->i_d.di_nlink != 0 ||
  597. !(mp->m_flags & XFS_MOUNT_WSYNC))
  598. ? 1 : 0));
  599. if (code)
  600. goto abort_return;
  601. /*
  602. * Truncated "down", so we're removing references
  603. * to old data here - if we now delay flushing for
  604. * a long time, we expose ourselves unduly to the
  605. * notorious NULL files problem. So, we mark this
  606. * vnode and flush it when the file is closed, and
  607. * do not wait the usual (long) time for writeout.
  608. */
  609. xfs_iflags_set(ip, XFS_ITRUNCATED);
  610. }
  611. /*
  612. * Have to do this even if the file's size doesn't change.
  613. */
  614. timeflags |= XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG;
  615. }
  616. /*
  617. * Change file access modes.
  618. */
  619. if (mask & XFS_AT_MODE) {
  620. ip->i_d.di_mode &= S_IFMT;
  621. ip->i_d.di_mode |= vap->va_mode & ~S_IFMT;
  622. xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
  623. timeflags |= XFS_ICHGTIME_CHG;
  624. }
  625. /*
  626. * Change file ownership. Must be the owner or privileged.
  627. * If the system was configured with the "restricted_chown"
  628. * option, the owner is not permitted to give away the file,
  629. * and can change the group id only to a group of which he
  630. * or she is a member.
  631. */
  632. if (mask & (XFS_AT_UID|XFS_AT_GID|XFS_AT_PROJID)) {
  633. /*
  634. * CAP_FSETID overrides the following restrictions:
  635. *
  636. * The set-user-ID and set-group-ID bits of a file will be
  637. * cleared upon successful return from chown()
  638. */
  639. if ((ip->i_d.di_mode & (S_ISUID|S_ISGID)) &&
  640. !capable(CAP_FSETID)) {
  641. ip->i_d.di_mode &= ~(S_ISUID|S_ISGID);
  642. }
  643. /*
  644. * Change the ownerships and register quota modifications
  645. * in the transaction.
  646. */
  647. if (iuid != uid) {
  648. if (XFS_IS_UQUOTA_ON(mp)) {
  649. ASSERT(mask & XFS_AT_UID);
  650. ASSERT(udqp);
  651. olddquot1 = XFS_QM_DQVOPCHOWN(mp, tp, ip,
  652. &ip->i_udquot, udqp);
  653. }
  654. ip->i_d.di_uid = uid;
  655. }
  656. if (igid != gid) {
  657. if (XFS_IS_GQUOTA_ON(mp)) {
  658. ASSERT(!XFS_IS_PQUOTA_ON(mp));
  659. ASSERT(mask & XFS_AT_GID);
  660. ASSERT(gdqp);
  661. olddquot2 = XFS_QM_DQVOPCHOWN(mp, tp, ip,
  662. &ip->i_gdquot, gdqp);
  663. }
  664. ip->i_d.di_gid = gid;
  665. }
  666. if (iprojid != projid) {
  667. if (XFS_IS_PQUOTA_ON(mp)) {
  668. ASSERT(!XFS_IS_GQUOTA_ON(mp));
  669. ASSERT(mask & XFS_AT_PROJID);
  670. ASSERT(gdqp);
  671. olddquot2 = XFS_QM_DQVOPCHOWN(mp, tp, ip,
  672. &ip->i_gdquot, gdqp);
  673. }
  674. ip->i_d.di_projid = projid;
  675. /*
  676. * We may have to rev the inode as well as
  677. * the superblock version number since projids didn't
  678. * exist before DINODE_VERSION_2 and SB_VERSION_NLINK.
  679. */
  680. if (ip->i_d.di_version == XFS_DINODE_VERSION_1)
  681. xfs_bump_ino_vers2(tp, ip);
  682. }
  683. xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
  684. timeflags |= XFS_ICHGTIME_CHG;
  685. }
  686. /*
  687. * Change file access or modified times.
  688. */
  689. if (mask & (XFS_AT_ATIME|XFS_AT_MTIME)) {
  690. if (mask & XFS_AT_ATIME) {
  691. ip->i_d.di_atime.t_sec = vap->va_atime.tv_sec;
  692. ip->i_d.di_atime.t_nsec = vap->va_atime.tv_nsec;
  693. ip->i_update_core = 1;
  694. timeflags &= ~XFS_ICHGTIME_ACC;
  695. }
  696. if (mask & XFS_AT_MTIME) {
  697. ip->i_d.di_mtime.t_sec = vap->va_mtime.tv_sec;
  698. ip->i_d.di_mtime.t_nsec = vap->va_mtime.tv_nsec;
  699. timeflags &= ~XFS_ICHGTIME_MOD;
  700. timeflags |= XFS_ICHGTIME_CHG;
  701. }
  702. if (tp && (flags & ATTR_UTIME))
  703. xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
  704. }
  705. /*
  706. * Change XFS-added attributes.
  707. */
  708. if (mask & (XFS_AT_EXTSIZE|XFS_AT_XFLAGS)) {
  709. if (mask & XFS_AT_EXTSIZE) {
  710. /*
  711. * Converting bytes to fs blocks.
  712. */
  713. ip->i_d.di_extsize = vap->va_extsize >>
  714. mp->m_sb.sb_blocklog;
  715. }
  716. if (mask & XFS_AT_XFLAGS) {
  717. uint di_flags;
  718. /* can't set PREALLOC this way, just preserve it */
  719. di_flags = (ip->i_d.di_flags & XFS_DIFLAG_PREALLOC);
  720. if (vap->va_xflags & XFS_XFLAG_IMMUTABLE)
  721. di_flags |= XFS_DIFLAG_IMMUTABLE;
  722. if (vap->va_xflags & XFS_XFLAG_APPEND)
  723. di_flags |= XFS_DIFLAG_APPEND;
  724. if (vap->va_xflags & XFS_XFLAG_SYNC)
  725. di_flags |= XFS_DIFLAG_SYNC;
  726. if (vap->va_xflags & XFS_XFLAG_NOATIME)
  727. di_flags |= XFS_DIFLAG_NOATIME;
  728. if (vap->va_xflags & XFS_XFLAG_NODUMP)
  729. di_flags |= XFS_DIFLAG_NODUMP;
  730. if (vap->va_xflags & XFS_XFLAG_PROJINHERIT)
  731. di_flags |= XFS_DIFLAG_PROJINHERIT;
  732. if (vap->va_xflags & XFS_XFLAG_NODEFRAG)
  733. di_flags |= XFS_DIFLAG_NODEFRAG;
  734. if (vap->va_xflags & XFS_XFLAG_FILESTREAM)
  735. di_flags |= XFS_DIFLAG_FILESTREAM;
  736. if ((ip->i_d.di_mode & S_IFMT) == S_IFDIR) {
  737. if (vap->va_xflags & XFS_XFLAG_RTINHERIT)
  738. di_flags |= XFS_DIFLAG_RTINHERIT;
  739. if (vap->va_xflags & XFS_XFLAG_NOSYMLINKS)
  740. di_flags |= XFS_DIFLAG_NOSYMLINKS;
  741. if (vap->va_xflags & XFS_XFLAG_EXTSZINHERIT)
  742. di_flags |= XFS_DIFLAG_EXTSZINHERIT;
  743. } else if ((ip->i_d.di_mode & S_IFMT) == S_IFREG) {
  744. if (vap->va_xflags & XFS_XFLAG_REALTIME)
  745. di_flags |= XFS_DIFLAG_REALTIME;
  746. if (vap->va_xflags & XFS_XFLAG_EXTSIZE)
  747. di_flags |= XFS_DIFLAG_EXTSIZE;
  748. }
  749. ip->i_d.di_flags = di_flags;
  750. }
  751. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  752. timeflags |= XFS_ICHGTIME_CHG;
  753. }
  754. /*
  755. * Change file inode change time only if XFS_AT_CTIME set
  756. * AND we have been called by a DMI function.
  757. */
  758. if ( (flags & ATTR_DMI) && (mask & XFS_AT_CTIME) ) {
  759. ip->i_d.di_ctime.t_sec = vap->va_ctime.tv_sec;
  760. ip->i_d.di_ctime.t_nsec = vap->va_ctime.tv_nsec;
  761. ip->i_update_core = 1;
  762. timeflags &= ~XFS_ICHGTIME_CHG;
  763. }
  764. /*
  765. * Send out timestamp changes that need to be set to the
  766. * current time. Not done when called by a DMI function.
  767. */
  768. if (timeflags && !(flags & ATTR_DMI))
  769. xfs_ichgtime(ip, timeflags);
  770. XFS_STATS_INC(xs_ig_attrchg);
  771. /*
  772. * If this is a synchronous mount, make sure that the
  773. * transaction goes to disk before returning to the user.
  774. * This is slightly sub-optimal in that truncates require
  775. * two sync transactions instead of one for wsync filesystems.
  776. * One for the truncate and one for the timestamps since we
  777. * don't want to change the timestamps unless we're sure the
  778. * truncate worked. Truncates are less than 1% of the laddis
  779. * mix so this probably isn't worth the trouble to optimize.
  780. */
  781. code = 0;
  782. if (tp) {
  783. if (mp->m_flags & XFS_MOUNT_WSYNC)
  784. xfs_trans_set_sync(tp);
  785. code = xfs_trans_commit(tp, commit_flags);
  786. }
  787. /*
  788. * If the (regular) file's mandatory locking mode changed, then
  789. * notify the vnode. We do this under the inode lock to prevent
  790. * racing calls to vop_vnode_change.
  791. */
  792. mandlock_after = MANDLOCK(vp, ip->i_d.di_mode);
  793. xfs_iunlock(ip, lock_flags);
  794. /*
  795. * Release any dquot(s) the inode had kept before chown.
  796. */
  797. XFS_QM_DQRELE(mp, olddquot1);
  798. XFS_QM_DQRELE(mp, olddquot2);
  799. XFS_QM_DQRELE(mp, udqp);
  800. XFS_QM_DQRELE(mp, gdqp);
  801. if (code) {
  802. return code;
  803. }
  804. if (DM_EVENT_ENABLED(ip, DM_EVENT_ATTRIBUTE) &&
  805. !(flags & ATTR_DMI)) {
  806. (void) XFS_SEND_NAMESP(mp, DM_EVENT_ATTRIBUTE, vp, DM_RIGHT_NULL,
  807. NULL, DM_RIGHT_NULL, NULL, NULL,
  808. 0, 0, AT_DELAY_FLAG(flags));
  809. }
  810. return 0;
  811. abort_return:
  812. commit_flags |= XFS_TRANS_ABORT;
  813. /* FALLTHROUGH */
  814. error_return:
  815. XFS_QM_DQRELE(mp, udqp);
  816. XFS_QM_DQRELE(mp, gdqp);
  817. if (tp) {
  818. xfs_trans_cancel(tp, commit_flags);
  819. }
  820. if (lock_flags != 0) {
  821. xfs_iunlock(ip, lock_flags);
  822. }
  823. return code;
  824. }
  825. /*
  826. * The maximum pathlen is 1024 bytes. Since the minimum file system
  827. * blocksize is 512 bytes, we can get a max of 2 extents back from
  828. * bmapi.
  829. */
  830. #define SYMLINK_MAPS 2
  831. STATIC int
  832. xfs_readlink_bmap(
  833. xfs_inode_t *ip,
  834. char *link)
  835. {
  836. xfs_mount_t *mp = ip->i_mount;
  837. int pathlen = ip->i_d.di_size;
  838. int nmaps = SYMLINK_MAPS;
  839. xfs_bmbt_irec_t mval[SYMLINK_MAPS];
  840. xfs_daddr_t d;
  841. int byte_cnt;
  842. int n;
  843. xfs_buf_t *bp;
  844. int error = 0;
  845. error = xfs_bmapi(NULL, ip, 0, XFS_B_TO_FSB(mp, pathlen), 0, NULL, 0,
  846. mval, &nmaps, NULL, NULL);
  847. if (error)
  848. goto out;
  849. for (n = 0; n < nmaps; n++) {
  850. d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
  851. byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
  852. bp = xfs_buf_read(mp->m_ddev_targp, d, BTOBB(byte_cnt), 0);
  853. error = XFS_BUF_GETERROR(bp);
  854. if (error) {
  855. xfs_ioerror_alert("xfs_readlink",
  856. ip->i_mount, bp, XFS_BUF_ADDR(bp));
  857. xfs_buf_relse(bp);
  858. goto out;
  859. }
  860. if (pathlen < byte_cnt)
  861. byte_cnt = pathlen;
  862. pathlen -= byte_cnt;
  863. memcpy(link, XFS_BUF_PTR(bp), byte_cnt);
  864. xfs_buf_relse(bp);
  865. }
  866. link[ip->i_d.di_size] = '\0';
  867. error = 0;
  868. out:
  869. return error;
  870. }
  871. int
  872. xfs_readlink(
  873. xfs_inode_t *ip,
  874. char *link)
  875. {
  876. xfs_mount_t *mp = ip->i_mount;
  877. int pathlen;
  878. int error = 0;
  879. xfs_itrace_entry(ip);
  880. if (XFS_FORCED_SHUTDOWN(mp))
  881. return XFS_ERROR(EIO);
  882. xfs_ilock(ip, XFS_ILOCK_SHARED);
  883. ASSERT((ip->i_d.di_mode & S_IFMT) == S_IFLNK);
  884. ASSERT(ip->i_d.di_size <= MAXPATHLEN);
  885. pathlen = ip->i_d.di_size;
  886. if (!pathlen)
  887. goto out;
  888. if (ip->i_df.if_flags & XFS_IFINLINE) {
  889. memcpy(link, ip->i_df.if_u1.if_data, pathlen);
  890. link[pathlen] = '\0';
  891. } else {
  892. error = xfs_readlink_bmap(ip, link);
  893. }
  894. out:
  895. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  896. return error;
  897. }
  898. /*
  899. * xfs_fsync
  900. *
  901. * This is called to sync the inode and its data out to disk.
  902. * We need to hold the I/O lock while flushing the data, and
  903. * the inode lock while flushing the inode. The inode lock CANNOT
  904. * be held while flushing the data, so acquire after we're done
  905. * with that.
  906. */
  907. int
  908. xfs_fsync(
  909. xfs_inode_t *ip,
  910. int flag,
  911. xfs_off_t start,
  912. xfs_off_t stop)
  913. {
  914. xfs_trans_t *tp;
  915. int error;
  916. int log_flushed = 0, changed = 1;
  917. xfs_itrace_entry(ip);
  918. ASSERT(start >= 0 && stop >= -1);
  919. if (XFS_FORCED_SHUTDOWN(ip->i_mount))
  920. return XFS_ERROR(EIO);
  921. if (flag & FSYNC_DATA)
  922. filemap_fdatawait(vn_to_inode(XFS_ITOV(ip))->i_mapping);
  923. /*
  924. * We always need to make sure that the required inode state
  925. * is safe on disk. The vnode might be clean but because
  926. * of committed transactions that haven't hit the disk yet.
  927. * Likewise, there could be unflushed non-transactional
  928. * changes to the inode core that have to go to disk.
  929. *
  930. * The following code depends on one assumption: that
  931. * any transaction that changes an inode logs the core
  932. * because it has to change some field in the inode core
  933. * (typically nextents or nblocks). That assumption
  934. * implies that any transactions against an inode will
  935. * catch any non-transactional updates. If inode-altering
  936. * transactions exist that violate this assumption, the
  937. * code breaks. Right now, it figures that if the involved
  938. * update_* field is clear and the inode is unpinned, the
  939. * inode is clean. Either it's been flushed or it's been
  940. * committed and the commit has hit the disk unpinning the inode.
  941. * (Note that xfs_inode_item_format() called at commit clears
  942. * the update_* fields.)
  943. */
  944. xfs_ilock(ip, XFS_ILOCK_SHARED);
  945. /* If we are flushing data then we care about update_size
  946. * being set, otherwise we care about update_core
  947. */
  948. if ((flag & FSYNC_DATA) ?
  949. (ip->i_update_size == 0) :
  950. (ip->i_update_core == 0)) {
  951. /*
  952. * Timestamps/size haven't changed since last inode
  953. * flush or inode transaction commit. That means
  954. * either nothing got written or a transaction
  955. * committed which caught the updates. If the
  956. * latter happened and the transaction hasn't
  957. * hit the disk yet, the inode will be still
  958. * be pinned. If it is, force the log.
  959. */
  960. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  961. if (xfs_ipincount(ip)) {
  962. _xfs_log_force(ip->i_mount, (xfs_lsn_t)0,
  963. XFS_LOG_FORCE |
  964. ((flag & FSYNC_WAIT)
  965. ? XFS_LOG_SYNC : 0),
  966. &log_flushed);
  967. } else {
  968. /*
  969. * If the inode is not pinned and nothing
  970. * has changed we don't need to flush the
  971. * cache.
  972. */
  973. changed = 0;
  974. }
  975. error = 0;
  976. } else {
  977. /*
  978. * Kick off a transaction to log the inode
  979. * core to get the updates. Make it
  980. * sync if FSYNC_WAIT is passed in (which
  981. * is done by everybody but specfs). The
  982. * sync transaction will also force the log.
  983. */
  984. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  985. tp = xfs_trans_alloc(ip->i_mount, XFS_TRANS_FSYNC_TS);
  986. if ((error = xfs_trans_reserve(tp, 0,
  987. XFS_FSYNC_TS_LOG_RES(ip->i_mount),
  988. 0, 0, 0))) {
  989. xfs_trans_cancel(tp, 0);
  990. return error;
  991. }
  992. xfs_ilock(ip, XFS_ILOCK_EXCL);
  993. /*
  994. * Note - it's possible that we might have pushed
  995. * ourselves out of the way during trans_reserve
  996. * which would flush the inode. But there's no
  997. * guarantee that the inode buffer has actually
  998. * gone out yet (it's delwri). Plus the buffer
  999. * could be pinned anyway if it's part of an
  1000. * inode in another recent transaction. So we
  1001. * play it safe and fire off the transaction anyway.
  1002. */
  1003. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  1004. xfs_trans_ihold(tp, ip);
  1005. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  1006. if (flag & FSYNC_WAIT)
  1007. xfs_trans_set_sync(tp);
  1008. error = _xfs_trans_commit(tp, 0, &log_flushed);
  1009. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1010. }
  1011. if ((ip->i_mount->m_flags & XFS_MOUNT_BARRIER) && changed) {
  1012. /*
  1013. * If the log write didn't issue an ordered tag we need
  1014. * to flush the disk cache for the data device now.
  1015. */
  1016. if (!log_flushed)
  1017. xfs_blkdev_issue_flush(ip->i_mount->m_ddev_targp);
  1018. /*
  1019. * If this inode is on the RT dev we need to flush that
  1020. * cache as well.
  1021. */
  1022. if (XFS_IS_REALTIME_INODE(ip))
  1023. xfs_blkdev_issue_flush(ip->i_mount->m_rtdev_targp);
  1024. }
  1025. return error;
  1026. }
  1027. /*
  1028. * This is called by xfs_inactive to free any blocks beyond eof
  1029. * when the link count isn't zero and by xfs_dm_punch_hole() when
  1030. * punching a hole to EOF.
  1031. */
  1032. int
  1033. xfs_free_eofblocks(
  1034. xfs_mount_t *mp,
  1035. xfs_inode_t *ip,
  1036. int flags)
  1037. {
  1038. xfs_trans_t *tp;
  1039. int error;
  1040. xfs_fileoff_t end_fsb;
  1041. xfs_fileoff_t last_fsb;
  1042. xfs_filblks_t map_len;
  1043. int nimaps;
  1044. xfs_bmbt_irec_t imap;
  1045. int use_iolock = (flags & XFS_FREE_EOF_LOCK);
  1046. /*
  1047. * Figure out if there are any blocks beyond the end
  1048. * of the file. If not, then there is nothing to do.
  1049. */
  1050. end_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)ip->i_size));
  1051. last_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp));
  1052. map_len = last_fsb - end_fsb;
  1053. if (map_len <= 0)
  1054. return 0;
  1055. nimaps = 1;
  1056. xfs_ilock(ip, XFS_ILOCK_SHARED);
  1057. error = xfs_bmapi(NULL, ip, end_fsb, map_len, 0,
  1058. NULL, 0, &imap, &nimaps, NULL, NULL);
  1059. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  1060. if (!error && (nimaps != 0) &&
  1061. (imap.br_startblock != HOLESTARTBLOCK ||
  1062. ip->i_delayed_blks)) {
  1063. /*
  1064. * Attach the dquots to the inode up front.
  1065. */
  1066. if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
  1067. return error;
  1068. /*
  1069. * There are blocks after the end of file.
  1070. * Free them up now by truncating the file to
  1071. * its current size.
  1072. */
  1073. tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
  1074. /*
  1075. * Do the xfs_itruncate_start() call before
  1076. * reserving any log space because
  1077. * itruncate_start will call into the buffer
  1078. * cache and we can't
  1079. * do that within a transaction.
  1080. */
  1081. if (use_iolock)
  1082. xfs_ilock(ip, XFS_IOLOCK_EXCL);
  1083. error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE,
  1084. ip->i_size);
  1085. if (error) {
  1086. xfs_trans_cancel(tp, 0);
  1087. if (use_iolock)
  1088. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  1089. return error;
  1090. }
  1091. error = xfs_trans_reserve(tp, 0,
  1092. XFS_ITRUNCATE_LOG_RES(mp),
  1093. 0, XFS_TRANS_PERM_LOG_RES,
  1094. XFS_ITRUNCATE_LOG_COUNT);
  1095. if (error) {
  1096. ASSERT(XFS_FORCED_SHUTDOWN(mp));
  1097. xfs_trans_cancel(tp, 0);
  1098. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  1099. return error;
  1100. }
  1101. xfs_ilock(ip, XFS_ILOCK_EXCL);
  1102. xfs_trans_ijoin(tp, ip,
  1103. XFS_IOLOCK_EXCL |
  1104. XFS_ILOCK_EXCL);
  1105. xfs_trans_ihold(tp, ip);
  1106. error = xfs_itruncate_finish(&tp, ip,
  1107. ip->i_size,
  1108. XFS_DATA_FORK,
  1109. 0);
  1110. /*
  1111. * If we get an error at this point we
  1112. * simply don't bother truncating the file.
  1113. */
  1114. if (error) {
  1115. xfs_trans_cancel(tp,
  1116. (XFS_TRANS_RELEASE_LOG_RES |
  1117. XFS_TRANS_ABORT));
  1118. } else {
  1119. error = xfs_trans_commit(tp,
  1120. XFS_TRANS_RELEASE_LOG_RES);
  1121. }
  1122. xfs_iunlock(ip, (use_iolock ? (XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL)
  1123. : XFS_ILOCK_EXCL));
  1124. }
  1125. return error;
  1126. }
  1127. /*
  1128. * Free a symlink that has blocks associated with it.
  1129. */
  1130. STATIC int
  1131. xfs_inactive_symlink_rmt(
  1132. xfs_inode_t *ip,
  1133. xfs_trans_t **tpp)
  1134. {
  1135. xfs_buf_t *bp;
  1136. int committed;
  1137. int done;
  1138. int error;
  1139. xfs_fsblock_t first_block;
  1140. xfs_bmap_free_t free_list;
  1141. int i;
  1142. xfs_mount_t *mp;
  1143. xfs_bmbt_irec_t mval[SYMLINK_MAPS];
  1144. int nmaps;
  1145. xfs_trans_t *ntp;
  1146. int size;
  1147. xfs_trans_t *tp;
  1148. tp = *tpp;
  1149. mp = ip->i_mount;
  1150. ASSERT(ip->i_d.di_size > XFS_IFORK_DSIZE(ip));
  1151. /*
  1152. * We're freeing a symlink that has some
  1153. * blocks allocated to it. Free the
  1154. * blocks here. We know that we've got
  1155. * either 1 or 2 extents and that we can
  1156. * free them all in one bunmapi call.
  1157. */
  1158. ASSERT(ip->i_d.di_nextents > 0 && ip->i_d.di_nextents <= 2);
  1159. if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
  1160. XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
  1161. ASSERT(XFS_FORCED_SHUTDOWN(mp));
  1162. xfs_trans_cancel(tp, 0);
  1163. *tpp = NULL;
  1164. return error;
  1165. }
  1166. /*
  1167. * Lock the inode, fix the size, and join it to the transaction.
  1168. * Hold it so in the normal path, we still have it locked for
  1169. * the second transaction. In the error paths we need it
  1170. * held so the cancel won't rele it, see below.
  1171. */
  1172. xfs_ilock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
  1173. size = (int)ip->i_d.di_size;
  1174. ip->i_d.di_size = 0;
  1175. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
  1176. xfs_trans_ihold(tp, ip);
  1177. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  1178. /*
  1179. * Find the block(s) so we can inval and unmap them.
  1180. */
  1181. done = 0;
  1182. XFS_BMAP_INIT(&free_list, &first_block);
  1183. nmaps = ARRAY_SIZE(mval);
  1184. if ((error = xfs_bmapi(tp, ip, 0, XFS_B_TO_FSB(mp, size),
  1185. XFS_BMAPI_METADATA, &first_block, 0, mval, &nmaps,
  1186. &free_list, NULL)))
  1187. goto error0;
  1188. /*
  1189. * Invalidate the block(s).
  1190. */
  1191. for (i = 0; i < nmaps; i++) {
  1192. bp = xfs_trans_get_buf(tp, mp->m_ddev_targp,
  1193. XFS_FSB_TO_DADDR(mp, mval[i].br_startblock),
  1194. XFS_FSB_TO_BB(mp, mval[i].br_blockcount), 0);
  1195. xfs_trans_binval(tp, bp);
  1196. }
  1197. /*
  1198. * Unmap the dead block(s) to the free_list.
  1199. */
  1200. if ((error = xfs_bunmapi(tp, ip, 0, size, XFS_BMAPI_METADATA, nmaps,
  1201. &first_block, &free_list, NULL, &done)))
  1202. goto error1;
  1203. ASSERT(done);
  1204. /*
  1205. * Commit the first transaction. This logs the EFI and the inode.
  1206. */
  1207. if ((error = xfs_bmap_finish(&tp, &free_list, &committed)))
  1208. goto error1;
  1209. /*
  1210. * The transaction must have been committed, since there were
  1211. * actually extents freed by xfs_bunmapi. See xfs_bmap_finish.
  1212. * The new tp has the extent freeing and EFDs.
  1213. */
  1214. ASSERT(committed);
  1215. /*
  1216. * The first xact was committed, so add the inode to the new one.
  1217. * Mark it dirty so it will be logged and moved forward in the log as
  1218. * part of every commit.
  1219. */
  1220. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
  1221. xfs_trans_ihold(tp, ip);
  1222. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  1223. /*
  1224. * Get a new, empty transaction to return to our caller.
  1225. */
  1226. ntp = xfs_trans_dup(tp);
  1227. /*
  1228. * Commit the transaction containing extent freeing and EFDs.
  1229. * If we get an error on the commit here or on the reserve below,
  1230. * we need to unlock the inode since the new transaction doesn't
  1231. * have the inode attached.
  1232. */
  1233. error = xfs_trans_commit(tp, 0);
  1234. tp = ntp;
  1235. if (error) {
  1236. ASSERT(XFS_FORCED_SHUTDOWN(mp));
  1237. goto error0;
  1238. }
  1239. /*
  1240. * Remove the memory for extent descriptions (just bookkeeping).
  1241. */
  1242. if (ip->i_df.if_bytes)
  1243. xfs_idata_realloc(ip, -ip->i_df.if_bytes, XFS_DATA_FORK);
  1244. ASSERT(ip->i_df.if_bytes == 0);
  1245. /*
  1246. * Put an itruncate log reservation in the new transaction
  1247. * for our caller.
  1248. */
  1249. if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
  1250. XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
  1251. ASSERT(XFS_FORCED_SHUTDOWN(mp));
  1252. goto error0;
  1253. }
  1254. /*
  1255. * Return with the inode locked but not joined to the transaction.
  1256. */
  1257. *tpp = tp;
  1258. return 0;
  1259. error1:
  1260. xfs_bmap_cancel(&free_list);
  1261. error0:
  1262. /*
  1263. * Have to come here with the inode locked and either
  1264. * (held and in the transaction) or (not in the transaction).
  1265. * If the inode isn't held then cancel would iput it, but
  1266. * that's wrong since this is inactive and the vnode ref
  1267. * count is 0 already.
  1268. * Cancel won't do anything to the inode if held, but it still
  1269. * needs to be locked until the cancel is done, if it was
  1270. * joined to the transaction.
  1271. */
  1272. xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
  1273. xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
  1274. *tpp = NULL;
  1275. return error;
  1276. }
  1277. STATIC int
  1278. xfs_inactive_symlink_local(
  1279. xfs_inode_t *ip,
  1280. xfs_trans_t **tpp)
  1281. {
  1282. int error;
  1283. ASSERT(ip->i_d.di_size <= XFS_IFORK_DSIZE(ip));
  1284. /*
  1285. * We're freeing a symlink which fit into
  1286. * the inode. Just free the memory used
  1287. * to hold the old symlink.
  1288. */
  1289. error = xfs_trans_reserve(*tpp, 0,
  1290. XFS_ITRUNCATE_LOG_RES(ip->i_mount),
  1291. 0, XFS_TRANS_PERM_LOG_RES,
  1292. XFS_ITRUNCATE_LOG_COUNT);
  1293. if (error) {
  1294. xfs_trans_cancel(*tpp, 0);
  1295. *tpp = NULL;
  1296. return error;
  1297. }
  1298. xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
  1299. /*
  1300. * Zero length symlinks _can_ exist.
  1301. */
  1302. if (ip->i_df.if_bytes > 0) {
  1303. xfs_idata_realloc(ip,
  1304. -(ip->i_df.if_bytes),
  1305. XFS_DATA_FORK);
  1306. ASSERT(ip->i_df.if_bytes == 0);
  1307. }
  1308. return 0;
  1309. }
  1310. STATIC int
  1311. xfs_inactive_attrs(
  1312. xfs_inode_t *ip,
  1313. xfs_trans_t **tpp)
  1314. {
  1315. xfs_trans_t *tp;
  1316. int error;
  1317. xfs_mount_t *mp;
  1318. ASSERT(ismrlocked(&ip->i_iolock, MR_UPDATE));
  1319. tp = *tpp;
  1320. mp = ip->i_mount;
  1321. ASSERT(ip->i_d.di_forkoff != 0);
  1322. xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  1323. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1324. error = xfs_attr_inactive(ip);
  1325. if (error) {
  1326. *tpp = NULL;
  1327. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  1328. return error; /* goto out */
  1329. }
  1330. tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
  1331. error = xfs_trans_reserve(tp, 0,
  1332. XFS_IFREE_LOG_RES(mp),
  1333. 0, XFS_TRANS_PERM_LOG_RES,
  1334. XFS_INACTIVE_LOG_COUNT);
  1335. if (error) {
  1336. ASSERT(XFS_FORCED_SHUTDOWN(mp));
  1337. xfs_trans_cancel(tp, 0);
  1338. *tpp = NULL;
  1339. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  1340. return error;
  1341. }
  1342. xfs_ilock(ip, XFS_ILOCK_EXCL);
  1343. xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
  1344. xfs_trans_ihold(tp, ip);
  1345. xfs_idestroy_fork(ip, XFS_ATTR_FORK);
  1346. ASSERT(ip->i_d.di_anextents == 0);
  1347. *tpp = tp;
  1348. return 0;
  1349. }
  1350. int
  1351. xfs_release(
  1352. xfs_inode_t *ip)
  1353. {
  1354. bhv_vnode_t *vp = XFS_ITOV(ip);
  1355. xfs_mount_t *mp = ip->i_mount;
  1356. int error;
  1357. if (!VN_ISREG(vp) || (ip->i_d.di_mode == 0))
  1358. return 0;
  1359. /* If this is a read-only mount, don't do this (would generate I/O) */
  1360. if (mp->m_flags & XFS_MOUNT_RDONLY)
  1361. return 0;
  1362. if (!XFS_FORCED_SHUTDOWN(mp)) {
  1363. int truncated;
  1364. /*
  1365. * If we are using filestreams, and we have an unlinked
  1366. * file that we are processing the last close on, then nothing
  1367. * will be able to reopen and write to this file. Purge this
  1368. * inode from the filestreams cache so that it doesn't delay
  1369. * teardown of the inode.
  1370. */
  1371. if ((ip->i_d.di_nlink == 0) && xfs_inode_is_filestream(ip))
  1372. xfs_filestream_deassociate(ip);
  1373. /*
  1374. * If we previously truncated this file and removed old data
  1375. * in the process, we want to initiate "early" writeout on
  1376. * the last close. This is an attempt to combat the notorious
  1377. * NULL files problem which is particularly noticable from a
  1378. * truncate down, buffered (re-)write (delalloc), followed by
  1379. * a crash. What we are effectively doing here is
  1380. * significantly reducing the time window where we'd otherwise
  1381. * be exposed to that problem.
  1382. */
  1383. truncated = xfs_iflags_test_and_clear(ip, XFS_ITRUNCATED);
  1384. if (truncated && VN_DIRTY(vp) && ip->i_delayed_blks > 0)
  1385. xfs_flush_pages(ip, 0, -1, XFS_B_ASYNC, FI_NONE);
  1386. }
  1387. if (ip->i_d.di_nlink != 0) {
  1388. if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
  1389. ((ip->i_size > 0) || (VN_CACHED(vp) > 0 ||
  1390. ip->i_delayed_blks > 0)) &&
  1391. (ip->i_df.if_flags & XFS_IFEXTENTS)) &&
  1392. (!(ip->i_d.di_flags &
  1393. (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)))) {
  1394. error = xfs_free_eofblocks(mp, ip, XFS_FREE_EOF_LOCK);
  1395. if (error)
  1396. return error;
  1397. }
  1398. }
  1399. return 0;
  1400. }
  1401. /*
  1402. * xfs_inactive
  1403. *
  1404. * This is called when the vnode reference count for the vnode
  1405. * goes to zero. If the file has been unlinked, then it must
  1406. * now be truncated. Also, we clear all of the read-ahead state
  1407. * kept for the inode here since the file is now closed.
  1408. */
  1409. int
  1410. xfs_inactive(
  1411. xfs_inode_t *ip)
  1412. {
  1413. bhv_vnode_t *vp = XFS_ITOV(ip);
  1414. xfs_bmap_free_t free_list;
  1415. xfs_fsblock_t first_block;
  1416. int committed;
  1417. xfs_trans_t *tp;
  1418. xfs_mount_t *mp;
  1419. int error;
  1420. int truncate;
  1421. xfs_itrace_entry(ip);
  1422. /*
  1423. * If the inode is already free, then there can be nothing
  1424. * to clean up here.
  1425. */
  1426. if (ip->i_d.di_mode == 0 || VN_BAD(vp)) {
  1427. ASSERT(ip->i_df.if_real_bytes == 0);
  1428. ASSERT(ip->i_df.if_broot_bytes == 0);
  1429. return VN_INACTIVE_CACHE;
  1430. }
  1431. /*
  1432. * Only do a truncate if it's a regular file with
  1433. * some actual space in it. It's OK to look at the
  1434. * inode's fields without the lock because we're the
  1435. * only one with a reference to the inode.
  1436. */
  1437. truncate = ((ip->i_d.di_nlink == 0) &&
  1438. ((ip->i_d.di_size != 0) || (ip->i_size != 0) ||
  1439. (ip->i_d.di_nextents > 0) || (ip->i_delayed_blks > 0)) &&
  1440. ((ip->i_d.di_mode & S_IFMT) == S_IFREG));
  1441. mp = ip->i_mount;
  1442. if (ip->i_d.di_nlink == 0 && DM_EVENT_ENABLED(ip, DM_EVENT_DESTROY)) {
  1443. (void) XFS_SEND_DESTROY(mp, vp, DM_RIGHT_NULL);
  1444. }
  1445. error = 0;
  1446. /* If this is a read-only mount, don't do this (would generate I/O) */
  1447. if (mp->m_flags & XFS_MOUNT_RDONLY)
  1448. goto out;
  1449. if (ip->i_d.di_nlink != 0) {
  1450. if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
  1451. ((ip->i_size > 0) || (VN_CACHED(vp) > 0 ||
  1452. ip->i_delayed_blks > 0)) &&
  1453. (ip->i_df.if_flags & XFS_IFEXTENTS) &&
  1454. (!(ip->i_d.di_flags &
  1455. (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)) ||
  1456. (ip->i_delayed_blks != 0)))) {
  1457. error = xfs_free_eofblocks(mp, ip, XFS_FREE_EOF_LOCK);
  1458. if (error)
  1459. return VN_INACTIVE_CACHE;
  1460. }
  1461. goto out;
  1462. }
  1463. ASSERT(ip->i_d.di_nlink == 0);
  1464. if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
  1465. return VN_INACTIVE_CACHE;
  1466. tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
  1467. if (truncate) {
  1468. /*
  1469. * Do the xfs_itruncate_start() call before
  1470. * reserving any log space because itruncate_start
  1471. * will call into the buffer cache and we can't
  1472. * do that within a transaction.
  1473. */
  1474. xfs_ilock(ip, XFS_IOLOCK_EXCL);
  1475. error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE, 0);
  1476. if (error) {
  1477. xfs_trans_cancel(tp, 0);
  1478. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  1479. return VN_INACTIVE_CACHE;
  1480. }
  1481. error = xfs_trans_reserve(tp, 0,
  1482. XFS_ITRUNCATE_LOG_RES(mp),
  1483. 0, XFS_TRANS_PERM_LOG_RES,
  1484. XFS_ITRUNCATE_LOG_COUNT);
  1485. if (error) {
  1486. /* Don't call itruncate_cleanup */
  1487. ASSERT(XFS_FORCED_SHUTDOWN(mp));
  1488. xfs_trans_cancel(tp, 0);
  1489. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  1490. return VN_INACTIVE_CACHE;
  1491. }
  1492. xfs_ilock(ip, XFS_ILOCK_EXCL);
  1493. xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
  1494. xfs_trans_ihold(tp, ip);
  1495. /*
  1496. * normally, we have to run xfs_itruncate_finish sync.
  1497. * But if filesystem is wsync and we're in the inactive
  1498. * path, then we know that nlink == 0, and that the
  1499. * xaction that made nlink == 0 is permanently committed
  1500. * since xfs_remove runs as a synchronous transaction.
  1501. */
  1502. error = xfs_itruncate_finish(&tp, ip, 0, XFS_DATA_FORK,
  1503. (!(mp->m_flags & XFS_MOUNT_WSYNC) ? 1 : 0));
  1504. if (error) {
  1505. xfs_trans_cancel(tp,
  1506. XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
  1507. xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
  1508. return VN_INACTIVE_CACHE;
  1509. }
  1510. } else if ((ip->i_d.di_mode & S_IFMT) == S_IFLNK) {
  1511. /*
  1512. * If we get an error while cleaning up a
  1513. * symlink we bail out.
  1514. */
  1515. error = (ip->i_d.di_size > XFS_IFORK_DSIZE(ip)) ?
  1516. xfs_inactive_symlink_rmt(ip, &tp) :
  1517. xfs_inactive_symlink_local(ip, &tp);
  1518. if (error) {
  1519. ASSERT(tp == NULL);
  1520. return VN_INACTIVE_CACHE;
  1521. }
  1522. xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
  1523. xfs_trans_ihold(tp, ip);
  1524. } else {
  1525. error = xfs_trans_reserve(tp, 0,
  1526. XFS_IFREE_LOG_RES(mp),
  1527. 0, XFS_TRANS_PERM_LOG_RES,
  1528. XFS_INACTIVE_LOG_COUNT);
  1529. if (error) {
  1530. ASSERT(XFS_FORCED_SHUTDOWN(mp));
  1531. xfs_trans_cancel(tp, 0);
  1532. return VN_INACTIVE_CACHE;
  1533. }
  1534. xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
  1535. xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
  1536. xfs_trans_ihold(tp, ip);
  1537. }
  1538. /*
  1539. * If there are attributes associated with the file
  1540. * then blow them away now. The code calls a routine
  1541. * that recursively deconstructs the attribute fork.
  1542. * We need to just commit the current transaction
  1543. * because we can't use it for xfs_attr_inactive().
  1544. */
  1545. if (ip->i_d.di_anextents > 0) {
  1546. error = xfs_inactive_attrs(ip, &tp);
  1547. /*
  1548. * If we got an error, the transaction is already
  1549. * cancelled, and the inode is unlocked. Just get out.
  1550. */
  1551. if (error)
  1552. return VN_INACTIVE_CACHE;
  1553. } else if (ip->i_afp) {
  1554. xfs_idestroy_fork(ip, XFS_ATTR_FORK);
  1555. }
  1556. /*
  1557. * Free the inode.
  1558. */
  1559. XFS_BMAP_INIT(&free_list, &first_block);
  1560. error = xfs_ifree(tp, ip, &free_list);
  1561. if (error) {
  1562. /*
  1563. * If we fail to free the inode, shut down. The cancel
  1564. * might do that, we need to make sure. Otherwise the
  1565. * inode might be lost for a long time or forever.
  1566. */
  1567. if (!XFS_FORCED_SHUTDOWN(mp)) {
  1568. cmn_err(CE_NOTE,
  1569. "xfs_inactive: xfs_ifree() returned an error = %d on %s",
  1570. error, mp->m_fsname);
  1571. xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
  1572. }
  1573. xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
  1574. } else {
  1575. /*
  1576. * Credit the quota account(s). The inode is gone.
  1577. */
  1578. XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, XFS_TRANS_DQ_ICOUNT, -1);
  1579. /*
  1580. * Just ignore errors at this point. There is
  1581. * nothing we can do except to try to keep going.
  1582. */
  1583. (void) xfs_bmap_finish(&tp, &free_list, &committed);
  1584. (void) xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  1585. }
  1586. /*
  1587. * Release the dquots held by inode, if any.
  1588. */
  1589. XFS_QM_DQDETACH(mp, ip);
  1590. xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
  1591. out:
  1592. return VN_INACTIVE_CACHE;
  1593. }
  1594. int
  1595. xfs_lookup(
  1596. xfs_inode_t *dp,
  1597. bhv_vname_t *dentry,
  1598. bhv_vnode_t **vpp)
  1599. {
  1600. xfs_inode_t *ip;
  1601. xfs_ino_t e_inum;
  1602. int error;
  1603. uint lock_mode;
  1604. xfs_itrace_entry(dp);
  1605. if (XFS_FORCED_SHUTDOWN(dp->i_mount))
  1606. return XFS_ERROR(EIO);
  1607. lock_mode = xfs_ilock_map_shared(dp);
  1608. error = xfs_dir_lookup_int(dp, lock_mode, dentry, &e_inum, &ip);
  1609. if (!error) {
  1610. *vpp = XFS_ITOV(ip);
  1611. xfs_itrace_ref(ip);
  1612. }
  1613. xfs_iunlock_map_shared(dp, lock_mode);
  1614. return error;
  1615. }
  1616. int
  1617. xfs_create(
  1618. xfs_inode_t *dp,
  1619. bhv_vname_t *dentry,
  1620. mode_t mode,
  1621. xfs_dev_t rdev,
  1622. bhv_vnode_t **vpp,
  1623. cred_t *credp)
  1624. {
  1625. char *name = VNAME(dentry);
  1626. xfs_mount_t *mp = dp->i_mount;
  1627. bhv_vnode_t *dir_vp = XFS_ITOV(dp);
  1628. xfs_inode_t *ip;
  1629. bhv_vnode_t *vp = NULL;
  1630. xfs_trans_t *tp;
  1631. int error;
  1632. xfs_bmap_free_t free_list;
  1633. xfs_fsblock_t first_block;
  1634. boolean_t unlock_dp_on_error = B_FALSE;
  1635. int dm_event_sent = 0;
  1636. uint cancel_flags;
  1637. int committed;
  1638. xfs_prid_t prid;
  1639. struct xfs_dquot *udqp, *gdqp;
  1640. uint resblks;
  1641. int namelen;
  1642. ASSERT(!*vpp);
  1643. xfs_itrace_entry(dp);
  1644. namelen = VNAMELEN(dentry);
  1645. if (DM_EVENT_ENABLED(dp, DM_EVENT_CREATE)) {
  1646. error = XFS_SEND_NAMESP(mp, DM_EVENT_CREATE,
  1647. dir_vp, DM_RIGHT_NULL, NULL,
  1648. DM_RIGHT_NULL, name, NULL,
  1649. mode, 0, 0);
  1650. if (error)
  1651. return error;
  1652. dm_event_sent = 1;
  1653. }
  1654. if (XFS_FORCED_SHUTDOWN(mp))
  1655. return XFS_ERROR(EIO);
  1656. /* Return through std_return after this point. */
  1657. udqp = gdqp = NULL;
  1658. if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
  1659. prid = dp->i_d.di_projid;
  1660. else
  1661. prid = (xfs_prid_t)dfltprid;
  1662. /*
  1663. * Make sure that we have allocated dquot(s) on disk.
  1664. */
  1665. error = XFS_QM_DQVOPALLOC(mp, dp,
  1666. current_fsuid(credp), current_fsgid(credp), prid,
  1667. XFS_QMOPT_QUOTALL|XFS_QMOPT_INHERIT, &udqp, &gdqp);
  1668. if (error)
  1669. goto std_return;
  1670. ip = NULL;
  1671. tp = xfs_trans_alloc(mp, XFS_TRANS_CREATE);
  1672. cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
  1673. resblks = XFS_CREATE_SPACE_RES(mp, namelen);
  1674. /*
  1675. * Initially assume that the file does not exist and
  1676. * reserve the resources for that case. If that is not
  1677. * the case we'll drop the one we have and get a more
  1678. * appropriate transaction later.
  1679. */
  1680. error = xfs_trans_reserve(tp, resblks, XFS_CREATE_LOG_RES(mp), 0,
  1681. XFS_TRANS_PERM_LOG_RES, XFS_CREATE_LOG_COUNT);
  1682. if (error == ENOSPC) {
  1683. resblks = 0;
  1684. error = xfs_trans_reserve(tp, 0, XFS_CREATE_LOG_RES(mp), 0,
  1685. XFS_TRANS_PERM_LOG_RES, XFS_CREATE_LOG_COUNT);
  1686. }
  1687. if (error) {
  1688. cancel_flags = 0;
  1689. goto error_return;
  1690. }
  1691. xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
  1692. unlock_dp_on_error = B_TRUE;
  1693. XFS_BMAP_INIT(&free_list, &first_block);
  1694. ASSERT(ip == NULL);
  1695. /*
  1696. * Reserve disk quota and the inode.
  1697. */
  1698. error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
  1699. if (error)
  1700. goto error_return;
  1701. if (resblks == 0 && (error = xfs_dir_canenter(tp, dp, name, namelen)))
  1702. goto error_return;
  1703. error = xfs_dir_ialloc(&tp, dp, mode, 1,
  1704. rdev, credp, prid, resblks > 0,
  1705. &ip, &committed);
  1706. if (error) {
  1707. if (error == ENOSPC)
  1708. goto error_return;
  1709. goto abort_return;
  1710. }
  1711. xfs_itrace_ref(ip);
  1712. /*
  1713. * At this point, we've gotten a newly allocated inode.
  1714. * It is locked (and joined to the transaction).
  1715. */
  1716. ASSERT(ismrlocked (&ip->i_lock, MR_UPDATE));
  1717. /*
  1718. * Now we join the directory inode to the transaction. We do not do it
  1719. * earlier because xfs_dir_ialloc might commit the previous transaction
  1720. * (and release all the locks). An error from here on will result in
  1721. * the transaction cancel unlocking dp so don't do it explicitly in the
  1722. * error path.
  1723. */
  1724. VN_HOLD(dir_vp);
  1725. xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
  1726. unlock_dp_on_error = B_FALSE;
  1727. error = xfs_dir_createname(tp, dp, name, namelen, ip->i_ino,
  1728. &first_block, &free_list, resblks ?
  1729. resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
  1730. if (error) {
  1731. ASSERT(error != ENOSPC);
  1732. goto abort_return;
  1733. }
  1734. xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  1735. xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
  1736. /*
  1737. * If this is a synchronous mount, make sure that the
  1738. * create transaction goes to disk before returning to
  1739. * the user.
  1740. */
  1741. if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
  1742. xfs_trans_set_sync(tp);
  1743. }
  1744. dp->i_gen++;
  1745. /*
  1746. * Attach the dquot(s) to the inodes and modify them incore.
  1747. * These ids of the inode couldn't have changed since the new
  1748. * inode has been locked ever since it was created.
  1749. */
  1750. XFS_QM_DQVOPCREATE(mp, tp, ip, udqp, gdqp);
  1751. /*
  1752. * xfs_trans_commit normally decrements the vnode ref count
  1753. * when it unlocks the inode. Since we want to return the
  1754. * vnode to the caller, we bump the vnode ref count now.
  1755. */
  1756. IHOLD(ip);
  1757. vp = XFS_ITOV(ip);
  1758. error = xfs_bmap_finish(&tp, &free_list, &committed);
  1759. if (error) {
  1760. xfs_bmap_cancel(&free_list);
  1761. goto abort_rele;
  1762. }
  1763. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  1764. if (error) {
  1765. IRELE(ip);
  1766. tp = NULL;
  1767. goto error_return;
  1768. }
  1769. XFS_QM_DQRELE(mp, udqp);
  1770. XFS_QM_DQRELE(mp, gdqp);
  1771. *vpp = vp;
  1772. /* Fallthrough to std_return with error = 0 */
  1773. std_return:
  1774. if ((*vpp || (error != 0 && dm_event_sent != 0)) &&
  1775. DM_EVENT_ENABLED(dp, DM_EVENT_POSTCREATE)) {
  1776. (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTCREATE,
  1777. dir_vp, DM_RIGHT_NULL,
  1778. *vpp ? vp:NULL,
  1779. DM_RIGHT_NULL, name, NULL,
  1780. mode, error, 0);
  1781. }
  1782. return error;
  1783. abort_return:
  1784. cancel_flags |= XFS_TRANS_ABORT;
  1785. /* FALLTHROUGH */
  1786. error_return:
  1787. if (tp != NULL)
  1788. xfs_trans_cancel(tp, cancel_flags);
  1789. XFS_QM_DQRELE(mp, udqp);
  1790. XFS_QM_DQRELE(mp, gdqp);
  1791. if (unlock_dp_on_error)
  1792. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  1793. goto std_return;
  1794. abort_rele:
  1795. /*
  1796. * Wait until after the current transaction is aborted to
  1797. * release the inode. This prevents recursive transactions
  1798. * and deadlocks from xfs_inactive.
  1799. */
  1800. cancel_flags |= XFS_TRANS_ABORT;
  1801. xfs_trans_cancel(tp, cancel_flags);
  1802. IRELE(ip);
  1803. XFS_QM_DQRELE(mp, udqp);
  1804. XFS_QM_DQRELE(mp, gdqp);
  1805. goto std_return;
  1806. }
  1807. #ifdef DEBUG
  1808. /*
  1809. * Some counters to see if (and how often) we are hitting some deadlock
  1810. * prevention code paths.
  1811. */
  1812. int xfs_rm_locks;
  1813. int xfs_rm_lock_delays;
  1814. int xfs_rm_attempts;
  1815. #endif
  1816. /*
  1817. * The following routine will lock the inodes associated with the
  1818. * directory and the named entry in the directory. The locks are
  1819. * acquired in increasing inode number.
  1820. *
  1821. * If the entry is "..", then only the directory is locked. The
  1822. * vnode ref count will still include that from the .. entry in
  1823. * this case.
  1824. *
  1825. * There is a deadlock we need to worry about. If the locked directory is
  1826. * in the AIL, it might be blocking up the log. The next inode we lock
  1827. * could be already locked by another thread waiting for log space (e.g
  1828. * a permanent log reservation with a long running transaction (see
  1829. * xfs_itruncate_finish)). To solve this, we must check if the directory
  1830. * is in the ail and use lock_nowait. If we can't lock, we need to
  1831. * drop the inode lock on the directory and try again. xfs_iunlock will
  1832. * potentially push the tail if we were holding up the log.
  1833. */
  1834. STATIC int
  1835. xfs_lock_dir_and_entry(
  1836. xfs_inode_t *dp,
  1837. xfs_inode_t *ip) /* inode of entry 'name' */
  1838. {
  1839. int attempts;
  1840. xfs_ino_t e_inum;
  1841. xfs_inode_t *ips[2];
  1842. xfs_log_item_t *lp;
  1843. #ifdef DEBUG
  1844. xfs_rm_locks++;
  1845. #endif
  1846. attempts = 0;
  1847. again:
  1848. xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
  1849. e_inum = ip->i_ino;
  1850. xfs_itrace_ref(ip);
  1851. /*
  1852. * We want to lock in increasing inum. Since we've already
  1853. * acquired the lock on the directory, we may need to release
  1854. * if if the inum of the entry turns out to be less.
  1855. */
  1856. if (e_inum > dp->i_ino) {
  1857. /*
  1858. * We are already in the right order, so just
  1859. * lock on the inode of the entry.
  1860. * We need to use nowait if dp is in the AIL.
  1861. */
  1862. lp = (xfs_log_item_t *)dp->i_itemp;
  1863. if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
  1864. if (!xfs_ilock_nowait(ip, XFS_ILOCK_EXCL)) {
  1865. attempts++;
  1866. #ifdef DEBUG
  1867. xfs_rm_attempts++;
  1868. #endif
  1869. /*
  1870. * Unlock dp and try again.
  1871. * xfs_iunlock will try to push the tail
  1872. * if the inode is in the AIL.
  1873. */
  1874. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  1875. if ((attempts % 5) == 0) {
  1876. delay(1); /* Don't just spin the CPU */
  1877. #ifdef DEBUG
  1878. xfs_rm_lock_delays++;
  1879. #endif
  1880. }
  1881. goto again;
  1882. }
  1883. } else {
  1884. xfs_ilock(ip, XFS_ILOCK_EXCL);
  1885. }
  1886. } else if (e_inum < dp->i_ino) {
  1887. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  1888. ips[0] = ip;
  1889. ips[1] = dp;
  1890. xfs_lock_inodes(ips, 2, 0, XFS_ILOCK_EXCL);
  1891. }
  1892. /* else e_inum == dp->i_ino */
  1893. /* This can happen if we're asked to lock /x/..
  1894. * the entry is "..", which is also the parent directory.
  1895. */
  1896. return 0;
  1897. }
  1898. #ifdef DEBUG
  1899. int xfs_locked_n;
  1900. int xfs_small_retries;
  1901. int xfs_middle_retries;
  1902. int xfs_lots_retries;
  1903. int xfs_lock_delays;
  1904. #endif
  1905. /*
  1906. * Bump the subclass so xfs_lock_inodes() acquires each lock with
  1907. * a different value
  1908. */
  1909. static inline int
  1910. xfs_lock_inumorder(int lock_mode, int subclass)
  1911. {
  1912. if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL))
  1913. lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_IOLOCK_SHIFT;
  1914. if (lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL))
  1915. lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_ILOCK_SHIFT;
  1916. return lock_mode;
  1917. }
  1918. /*
  1919. * The following routine will lock n inodes in exclusive mode.
  1920. * We assume the caller calls us with the inodes in i_ino order.
  1921. *
  1922. * We need to detect deadlock where an inode that we lock
  1923. * is in the AIL and we start waiting for another inode that is locked
  1924. * by a thread in a long running transaction (such as truncate). This can
  1925. * result in deadlock since the long running trans might need to wait
  1926. * for the inode we just locked in order to push the tail and free space
  1927. * in the log.
  1928. */
  1929. void
  1930. xfs_lock_inodes(
  1931. xfs_inode_t **ips,
  1932. int inodes,
  1933. int first_locked,
  1934. uint lock_mode)
  1935. {
  1936. int attempts = 0, i, j, try_lock;
  1937. xfs_log_item_t *lp;
  1938. ASSERT(ips && (inodes >= 2)); /* we need at least two */
  1939. if (first_locked) {
  1940. try_lock = 1;
  1941. i = 1;
  1942. } else {
  1943. try_lock = 0;
  1944. i = 0;
  1945. }
  1946. again:
  1947. for (; i < inodes; i++) {
  1948. ASSERT(ips[i]);
  1949. if (i && (ips[i] == ips[i-1])) /* Already locked */
  1950. continue;
  1951. /*
  1952. * If try_lock is not set yet, make sure all locked inodes
  1953. * are not in the AIL.
  1954. * If any are, set try_lock to be used later.
  1955. */
  1956. if (!try_lock) {
  1957. for (j = (i - 1); j >= 0 && !try_lock; j--) {
  1958. lp = (xfs_log_item_t *)ips[j]->i_itemp;
  1959. if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
  1960. try_lock++;
  1961. }
  1962. }
  1963. }
  1964. /*
  1965. * If any of the previous locks we have locked is in the AIL,
  1966. * we must TRY to get the second and subsequent locks. If
  1967. * we can't get any, we must release all we have
  1968. * and try again.
  1969. */
  1970. if (try_lock) {
  1971. /* try_lock must be 0 if i is 0. */
  1972. /*
  1973. * try_lock means we have an inode locked
  1974. * that is in the AIL.
  1975. */
  1976. ASSERT(i != 0);
  1977. if (!xfs_ilock_nowait(ips[i], xfs_lock_inumorder(lock_mode, i))) {
  1978. attempts++;
  1979. /*
  1980. * Unlock all previous guys and try again.
  1981. * xfs_iunlock will try to push the tail
  1982. * if the inode is in the AIL.
  1983. */
  1984. for(j = i - 1; j >= 0; j--) {
  1985. /*
  1986. * Check to see if we've already
  1987. * unlocked this one.
  1988. * Not the first one going back,
  1989. * and the inode ptr is the same.
  1990. */
  1991. if ((j != (i - 1)) && ips[j] ==
  1992. ips[j+1])
  1993. continue;
  1994. xfs_iunlock(ips[j], lock_mode);
  1995. }
  1996. if ((attempts % 5) == 0) {
  1997. delay(1); /* Don't just spin the CPU */
  1998. #ifdef DEBUG
  1999. xfs_lock_delays++;
  2000. #endif
  2001. }
  2002. i = 0;
  2003. try_lock = 0;
  2004. goto again;
  2005. }
  2006. } else {
  2007. xfs_ilock(ips[i], xfs_lock_inumorder(lock_mode, i));
  2008. }
  2009. }
  2010. #ifdef DEBUG
  2011. if (attempts) {
  2012. if (attempts < 5) xfs_small_retries++;
  2013. else if (attempts < 100) xfs_middle_retries++;
  2014. else xfs_lots_retries++;
  2015. } else {
  2016. xfs_locked_n++;
  2017. }
  2018. #endif
  2019. }
  2020. #ifdef DEBUG
  2021. #define REMOVE_DEBUG_TRACE(x) {remove_which_error_return = (x);}
  2022. int remove_which_error_return = 0;
  2023. #else /* ! DEBUG */
  2024. #define REMOVE_DEBUG_TRACE(x)
  2025. #endif /* ! DEBUG */
  2026. int
  2027. xfs_remove(
  2028. xfs_inode_t *dp,
  2029. bhv_vname_t *dentry)
  2030. {
  2031. bhv_vnode_t *dir_vp = XFS_ITOV(dp);
  2032. char *name = VNAME(dentry);
  2033. xfs_mount_t *mp = dp->i_mount;
  2034. xfs_inode_t *ip;
  2035. xfs_trans_t *tp = NULL;
  2036. int error = 0;
  2037. xfs_bmap_free_t free_list;
  2038. xfs_fsblock_t first_block;
  2039. int cancel_flags;
  2040. int committed;
  2041. int dm_di_mode = 0;
  2042. int link_zero;
  2043. uint resblks;
  2044. int namelen;
  2045. xfs_itrace_entry(dp);
  2046. if (XFS_FORCED_SHUTDOWN(mp))
  2047. return XFS_ERROR(EIO);
  2048. namelen = VNAMELEN(dentry);
  2049. if (!xfs_get_dir_entry(dentry, &ip)) {
  2050. dm_di_mode = ip->i_d.di_mode;
  2051. IRELE(ip);
  2052. }
  2053. if (DM_EVENT_ENABLED(dp, DM_EVENT_REMOVE)) {
  2054. error = XFS_SEND_NAMESP(mp, DM_EVENT_REMOVE, dir_vp,
  2055. DM_RIGHT_NULL, NULL, DM_RIGHT_NULL,
  2056. name, NULL, dm_di_mode, 0, 0);
  2057. if (error)
  2058. return error;
  2059. }
  2060. /* From this point on, return through std_return */
  2061. ip = NULL;
  2062. /*
  2063. * We need to get a reference to ip before we get our log
  2064. * reservation. The reason for this is that we cannot call
  2065. * xfs_iget for an inode for which we do not have a reference
  2066. * once we've acquired a log reservation. This is because the
  2067. * inode we are trying to get might be in xfs_inactive going
  2068. * for a log reservation. Since we'll have to wait for the
  2069. * inactive code to complete before returning from xfs_iget,
  2070. * we need to make sure that we don't have log space reserved
  2071. * when we call xfs_iget. Instead we get an unlocked reference
  2072. * to the inode before getting our log reservation.
  2073. */
  2074. error = xfs_get_dir_entry(dentry, &ip);
  2075. if (error) {
  2076. REMOVE_DEBUG_TRACE(__LINE__);
  2077. goto std_return;
  2078. }
  2079. dm_di_mode = ip->i_d.di_mode;
  2080. xfs_itrace_entry(ip);
  2081. xfs_itrace_ref(ip);
  2082. error = XFS_QM_DQATTACH(mp, dp, 0);
  2083. if (!error && dp != ip)
  2084. error = XFS_QM_DQATTACH(mp, ip, 0);
  2085. if (error) {
  2086. REMOVE_DEBUG_TRACE(__LINE__);
  2087. IRELE(ip);
  2088. goto std_return;
  2089. }
  2090. tp = xfs_trans_alloc(mp, XFS_TRANS_REMOVE);
  2091. cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
  2092. /*
  2093. * We try to get the real space reservation first,
  2094. * allowing for directory btree deletion(s) implying
  2095. * possible bmap insert(s). If we can't get the space
  2096. * reservation then we use 0 instead, and avoid the bmap
  2097. * btree insert(s) in the directory code by, if the bmap
  2098. * insert tries to happen, instead trimming the LAST
  2099. * block from the directory.
  2100. */
  2101. resblks = XFS_REMOVE_SPACE_RES(mp);
  2102. error = xfs_trans_reserve(tp, resblks, XFS_REMOVE_LOG_RES(mp), 0,
  2103. XFS_TRANS_PERM_LOG_RES, XFS_REMOVE_LOG_COUNT);
  2104. if (error == ENOSPC) {
  2105. resblks = 0;
  2106. error = xfs_trans_reserve(tp, 0, XFS_REMOVE_LOG_RES(mp), 0,
  2107. XFS_TRANS_PERM_LOG_RES, XFS_REMOVE_LOG_COUNT);
  2108. }
  2109. if (error) {
  2110. ASSERT(error != ENOSPC);
  2111. REMOVE_DEBUG_TRACE(__LINE__);
  2112. xfs_trans_cancel(tp, 0);
  2113. IRELE(ip);
  2114. return error;
  2115. }
  2116. error = xfs_lock_dir_and_entry(dp, ip);
  2117. if (error) {
  2118. REMOVE_DEBUG_TRACE(__LINE__);
  2119. xfs_trans_cancel(tp, cancel_flags);
  2120. IRELE(ip);
  2121. goto std_return;
  2122. }
  2123. /*
  2124. * At this point, we've gotten both the directory and the entry
  2125. * inodes locked.
  2126. */
  2127. xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
  2128. if (dp != ip) {
  2129. /*
  2130. * Increment vnode ref count only in this case since
  2131. * there's an extra vnode reference in the case where
  2132. * dp == ip.
  2133. */
  2134. IHOLD(dp);
  2135. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  2136. }
  2137. /*
  2138. * Entry must exist since we did a lookup in xfs_lock_dir_and_entry.
  2139. */
  2140. XFS_BMAP_INIT(&free_list, &first_block);
  2141. error = xfs_dir_removename(tp, dp, name, namelen, ip->i_ino,
  2142. &first_block, &free_list, 0);
  2143. if (error) {
  2144. ASSERT(error != ENOENT);
  2145. REMOVE_DEBUG_TRACE(__LINE__);
  2146. goto error1;
  2147. }
  2148. xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  2149. dp->i_gen++;
  2150. xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
  2151. error = xfs_droplink(tp, ip);
  2152. if (error) {
  2153. REMOVE_DEBUG_TRACE(__LINE__);
  2154. goto error1;
  2155. }
  2156. /* Determine if this is the last link while
  2157. * we are in the transaction.
  2158. */
  2159. link_zero = (ip)->i_d.di_nlink==0;
  2160. /*
  2161. * Take an extra ref on the inode so that it doesn't
  2162. * go to xfs_inactive() from within the commit.
  2163. */
  2164. IHOLD(ip);
  2165. /*
  2166. * If this is a synchronous mount, make sure that the
  2167. * remove transaction goes to disk before returning to
  2168. * the user.
  2169. */
  2170. if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
  2171. xfs_trans_set_sync(tp);
  2172. }
  2173. error = xfs_bmap_finish(&tp, &free_list, &committed);
  2174. if (error) {
  2175. REMOVE_DEBUG_TRACE(__LINE__);
  2176. goto error_rele;
  2177. }
  2178. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  2179. if (error) {
  2180. IRELE(ip);
  2181. goto std_return;
  2182. }
  2183. /*
  2184. * If we are using filestreams, kill the stream association.
  2185. * If the file is still open it may get a new one but that
  2186. * will get killed on last close in xfs_close() so we don't
  2187. * have to worry about that.
  2188. */
  2189. if (link_zero && xfs_inode_is_filestream(ip))
  2190. xfs_filestream_deassociate(ip);
  2191. xfs_itrace_exit(ip);
  2192. IRELE(ip);
  2193. /* Fall through to std_return with error = 0 */
  2194. std_return:
  2195. if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTREMOVE)) {
  2196. (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTREMOVE,
  2197. dir_vp, DM_RIGHT_NULL,
  2198. NULL, DM_RIGHT_NULL,
  2199. name, NULL, dm_di_mode, error, 0);
  2200. }
  2201. return error;
  2202. error1:
  2203. xfs_bmap_cancel(&free_list);
  2204. cancel_flags |= XFS_TRANS_ABORT;
  2205. xfs_trans_cancel(tp, cancel_flags);
  2206. goto std_return;
  2207. error_rele:
  2208. /*
  2209. * In this case make sure to not release the inode until after
  2210. * the current transaction is aborted. Releasing it beforehand
  2211. * can cause us to go to xfs_inactive and start a recursive
  2212. * transaction which can easily deadlock with the current one.
  2213. */
  2214. xfs_bmap_cancel(&free_list);
  2215. cancel_flags |= XFS_TRANS_ABORT;
  2216. xfs_trans_cancel(tp, cancel_flags);
  2217. IRELE(ip);
  2218. goto std_return;
  2219. }
  2220. int
  2221. xfs_link(
  2222. xfs_inode_t *tdp,
  2223. bhv_vnode_t *src_vp,
  2224. bhv_vname_t *dentry)
  2225. {
  2226. bhv_vnode_t *target_dir_vp = XFS_ITOV(tdp);
  2227. xfs_mount_t *mp = tdp->i_mount;
  2228. xfs_inode_t *sip = xfs_vtoi(src_vp);
  2229. xfs_trans_t *tp;
  2230. xfs_inode_t *ips[2];
  2231. int error;
  2232. xfs_bmap_free_t free_list;
  2233. xfs_fsblock_t first_block;
  2234. int cancel_flags;
  2235. int committed;
  2236. int resblks;
  2237. char *target_name = VNAME(dentry);
  2238. int target_namelen;
  2239. xfs_itrace_entry(tdp);
  2240. xfs_itrace_entry(xfs_vtoi(src_vp));
  2241. target_namelen = VNAMELEN(dentry);
  2242. ASSERT(!VN_ISDIR(src_vp));
  2243. if (XFS_FORCED_SHUTDOWN(mp))
  2244. return XFS_ERROR(EIO);
  2245. if (DM_EVENT_ENABLED(tdp, DM_EVENT_LINK)) {
  2246. error = XFS_SEND_NAMESP(mp, DM_EVENT_LINK,
  2247. target_dir_vp, DM_RIGHT_NULL,
  2248. src_vp, DM_RIGHT_NULL,
  2249. target_name, NULL, 0, 0, 0);
  2250. if (error)
  2251. return error;
  2252. }
  2253. /* Return through std_return after this point. */
  2254. error = XFS_QM_DQATTACH(mp, sip, 0);
  2255. if (!error && sip != tdp)
  2256. error = XFS_QM_DQATTACH(mp, tdp, 0);
  2257. if (error)
  2258. goto std_return;
  2259. tp = xfs_trans_alloc(mp, XFS_TRANS_LINK);
  2260. cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
  2261. resblks = XFS_LINK_SPACE_RES(mp, target_namelen);
  2262. error = xfs_trans_reserve(tp, resblks, XFS_LINK_LOG_RES(mp), 0,
  2263. XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
  2264. if (error == ENOSPC) {
  2265. resblks = 0;
  2266. error = xfs_trans_reserve(tp, 0, XFS_LINK_LOG_RES(mp), 0,
  2267. XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
  2268. }
  2269. if (error) {
  2270. cancel_flags = 0;
  2271. goto error_return;
  2272. }
  2273. if (sip->i_ino < tdp->i_ino) {
  2274. ips[0] = sip;
  2275. ips[1] = tdp;
  2276. } else {
  2277. ips[0] = tdp;
  2278. ips[1] = sip;
  2279. }
  2280. xfs_lock_inodes(ips, 2, 0, XFS_ILOCK_EXCL);
  2281. /*
  2282. * Increment vnode ref counts since xfs_trans_commit &
  2283. * xfs_trans_cancel will both unlock the inodes and
  2284. * decrement the associated ref counts.
  2285. */
  2286. VN_HOLD(src_vp);
  2287. VN_HOLD(target_dir_vp);
  2288. xfs_trans_ijoin(tp, sip, XFS_ILOCK_EXCL);
  2289. xfs_trans_ijoin(tp, tdp, XFS_ILOCK_EXCL);
  2290. /*
  2291. * If the source has too many links, we can't make any more to it.
  2292. */
  2293. if (sip->i_d.di_nlink >= XFS_MAXLINK) {
  2294. error = XFS_ERROR(EMLINK);
  2295. goto error_return;
  2296. }
  2297. /*
  2298. * If we are using project inheritance, we only allow hard link
  2299. * creation in our tree when the project IDs are the same; else
  2300. * the tree quota mechanism could be circumvented.
  2301. */
  2302. if (unlikely((tdp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
  2303. (tdp->i_d.di_projid != sip->i_d.di_projid))) {
  2304. error = XFS_ERROR(EXDEV);
  2305. goto error_return;
  2306. }
  2307. if (resblks == 0 &&
  2308. (error = xfs_dir_canenter(tp, tdp, target_name, target_namelen)))
  2309. goto error_return;
  2310. XFS_BMAP_INIT(&free_list, &first_block);
  2311. error = xfs_dir_createname(tp, tdp, target_name, target_namelen,
  2312. sip->i_ino, &first_block, &free_list,
  2313. resblks);
  2314. if (error)
  2315. goto abort_return;
  2316. xfs_ichgtime(tdp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  2317. tdp->i_gen++;
  2318. xfs_trans_log_inode(tp, tdp, XFS_ILOG_CORE);
  2319. error = xfs_bumplink(tp, sip);
  2320. if (error)
  2321. goto abort_return;
  2322. /*
  2323. * If this is a synchronous mount, make sure that the
  2324. * link 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. }
  2330. error = xfs_bmap_finish (&tp, &free_list, &committed);
  2331. if (error) {
  2332. xfs_bmap_cancel(&free_list);
  2333. goto abort_return;
  2334. }
  2335. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  2336. if (error)
  2337. goto std_return;
  2338. /* Fall through to std_return with error = 0. */
  2339. std_return:
  2340. if (DM_EVENT_ENABLED(sip, DM_EVENT_POSTLINK)) {
  2341. (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTLINK,
  2342. target_dir_vp, DM_RIGHT_NULL,
  2343. src_vp, DM_RIGHT_NULL,
  2344. target_name, NULL, 0, error, 0);
  2345. }
  2346. return error;
  2347. abort_return:
  2348. cancel_flags |= XFS_TRANS_ABORT;
  2349. /* FALLTHROUGH */
  2350. error_return:
  2351. xfs_trans_cancel(tp, cancel_flags);
  2352. goto std_return;
  2353. }
  2354. int
  2355. xfs_mkdir(
  2356. xfs_inode_t *dp,
  2357. bhv_vname_t *dentry,
  2358. mode_t mode,
  2359. bhv_vnode_t **vpp,
  2360. cred_t *credp)
  2361. {
  2362. bhv_vnode_t *dir_vp = XFS_ITOV(dp);
  2363. char *dir_name = VNAME(dentry);
  2364. int dir_namelen = VNAMELEN(dentry);
  2365. xfs_mount_t *mp = dp->i_mount;
  2366. xfs_inode_t *cdp; /* inode of created dir */
  2367. bhv_vnode_t *cvp; /* vnode of created dir */
  2368. xfs_trans_t *tp;
  2369. int cancel_flags;
  2370. int error;
  2371. int committed;
  2372. xfs_bmap_free_t free_list;
  2373. xfs_fsblock_t first_block;
  2374. boolean_t unlock_dp_on_error = B_FALSE;
  2375. boolean_t created = B_FALSE;
  2376. int dm_event_sent = 0;
  2377. xfs_prid_t prid;
  2378. struct xfs_dquot *udqp, *gdqp;
  2379. uint resblks;
  2380. if (XFS_FORCED_SHUTDOWN(mp))
  2381. return XFS_ERROR(EIO);
  2382. tp = NULL;
  2383. if (DM_EVENT_ENABLED(dp, DM_EVENT_CREATE)) {
  2384. error = XFS_SEND_NAMESP(mp, DM_EVENT_CREATE,
  2385. dir_vp, DM_RIGHT_NULL, NULL,
  2386. DM_RIGHT_NULL, dir_name, NULL,
  2387. mode, 0, 0);
  2388. if (error)
  2389. return error;
  2390. dm_event_sent = 1;
  2391. }
  2392. /* Return through std_return after this point. */
  2393. xfs_itrace_entry(dp);
  2394. mp = dp->i_mount;
  2395. udqp = gdqp = NULL;
  2396. if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
  2397. prid = dp->i_d.di_projid;
  2398. else
  2399. prid = (xfs_prid_t)dfltprid;
  2400. /*
  2401. * Make sure that we have allocated dquot(s) on disk.
  2402. */
  2403. error = XFS_QM_DQVOPALLOC(mp, dp,
  2404. current_fsuid(credp), current_fsgid(credp), prid,
  2405. XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
  2406. if (error)
  2407. goto std_return;
  2408. tp = xfs_trans_alloc(mp, XFS_TRANS_MKDIR);
  2409. cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
  2410. resblks = XFS_MKDIR_SPACE_RES(mp, dir_namelen);
  2411. error = xfs_trans_reserve(tp, resblks, XFS_MKDIR_LOG_RES(mp), 0,
  2412. XFS_TRANS_PERM_LOG_RES, XFS_MKDIR_LOG_COUNT);
  2413. if (error == ENOSPC) {
  2414. resblks = 0;
  2415. error = xfs_trans_reserve(tp, 0, XFS_MKDIR_LOG_RES(mp), 0,
  2416. XFS_TRANS_PERM_LOG_RES,
  2417. XFS_MKDIR_LOG_COUNT);
  2418. }
  2419. if (error) {
  2420. cancel_flags = 0;
  2421. goto error_return;
  2422. }
  2423. xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
  2424. unlock_dp_on_error = B_TRUE;
  2425. /*
  2426. * Check for directory link count overflow.
  2427. */
  2428. if (dp->i_d.di_nlink >= XFS_MAXLINK) {
  2429. error = XFS_ERROR(EMLINK);
  2430. goto error_return;
  2431. }
  2432. /*
  2433. * Reserve disk quota and the inode.
  2434. */
  2435. error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
  2436. if (error)
  2437. goto error_return;
  2438. if (resblks == 0 &&
  2439. (error = xfs_dir_canenter(tp, dp, dir_name, dir_namelen)))
  2440. goto error_return;
  2441. /*
  2442. * create the directory inode.
  2443. */
  2444. error = xfs_dir_ialloc(&tp, dp, mode, 2,
  2445. 0, credp, prid, resblks > 0,
  2446. &cdp, NULL);
  2447. if (error) {
  2448. if (error == ENOSPC)
  2449. goto error_return;
  2450. goto abort_return;
  2451. }
  2452. xfs_itrace_ref(cdp);
  2453. /*
  2454. * Now we add the directory inode to the transaction.
  2455. * We waited until now since xfs_dir_ialloc might start
  2456. * a new transaction. Had we joined the transaction
  2457. * earlier, the locks might have gotten released. An error
  2458. * from here on will result in the transaction cancel
  2459. * unlocking dp so don't do it explicitly in the error path.
  2460. */
  2461. VN_HOLD(dir_vp);
  2462. xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
  2463. unlock_dp_on_error = B_FALSE;
  2464. XFS_BMAP_INIT(&free_list, &first_block);
  2465. error = xfs_dir_createname(tp, dp, dir_name, dir_namelen, cdp->i_ino,
  2466. &first_block, &free_list, resblks ?
  2467. resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
  2468. if (error) {
  2469. ASSERT(error != ENOSPC);
  2470. goto error1;
  2471. }
  2472. xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  2473. /*
  2474. * Bump the in memory version number of the parent directory
  2475. * so that other processes accessing it will recognize that
  2476. * the directory has changed.
  2477. */
  2478. dp->i_gen++;
  2479. error = xfs_dir_init(tp, cdp, dp);
  2480. if (error)
  2481. goto error2;
  2482. cdp->i_gen = 1;
  2483. error = xfs_bumplink(tp, dp);
  2484. if (error)
  2485. goto error2;
  2486. cvp = XFS_ITOV(cdp);
  2487. created = B_TRUE;
  2488. *vpp = cvp;
  2489. IHOLD(cdp);
  2490. /*
  2491. * Attach the dquots to the new inode and modify the icount incore.
  2492. */
  2493. XFS_QM_DQVOPCREATE(mp, tp, cdp, udqp, gdqp);
  2494. /*
  2495. * If this is a synchronous mount, make sure that the
  2496. * mkdir transaction goes to disk before returning to
  2497. * the user.
  2498. */
  2499. if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
  2500. xfs_trans_set_sync(tp);
  2501. }
  2502. error = xfs_bmap_finish(&tp, &free_list, &committed);
  2503. if (error) {
  2504. IRELE(cdp);
  2505. goto error2;
  2506. }
  2507. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  2508. XFS_QM_DQRELE(mp, udqp);
  2509. XFS_QM_DQRELE(mp, gdqp);
  2510. if (error) {
  2511. IRELE(cdp);
  2512. }
  2513. /* Fall through to std_return with error = 0 or errno from
  2514. * xfs_trans_commit. */
  2515. std_return:
  2516. if ((created || (error != 0 && dm_event_sent != 0)) &&
  2517. DM_EVENT_ENABLED(dp, DM_EVENT_POSTCREATE)) {
  2518. (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTCREATE,
  2519. dir_vp, DM_RIGHT_NULL,
  2520. created ? XFS_ITOV(cdp):NULL,
  2521. DM_RIGHT_NULL,
  2522. dir_name, NULL,
  2523. mode, error, 0);
  2524. }
  2525. return error;
  2526. error2:
  2527. error1:
  2528. xfs_bmap_cancel(&free_list);
  2529. abort_return:
  2530. cancel_flags |= XFS_TRANS_ABORT;
  2531. error_return:
  2532. xfs_trans_cancel(tp, cancel_flags);
  2533. XFS_QM_DQRELE(mp, udqp);
  2534. XFS_QM_DQRELE(mp, gdqp);
  2535. if (unlock_dp_on_error)
  2536. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  2537. goto std_return;
  2538. }
  2539. int
  2540. xfs_rmdir(
  2541. xfs_inode_t *dp,
  2542. bhv_vname_t *dentry)
  2543. {
  2544. bhv_vnode_t *dir_vp = XFS_ITOV(dp);
  2545. char *name = VNAME(dentry);
  2546. int namelen = VNAMELEN(dentry);
  2547. xfs_mount_t *mp = dp->i_mount;
  2548. xfs_inode_t *cdp; /* child directory */
  2549. xfs_trans_t *tp;
  2550. int error;
  2551. xfs_bmap_free_t free_list;
  2552. xfs_fsblock_t first_block;
  2553. int cancel_flags;
  2554. int committed;
  2555. int dm_di_mode = S_IFDIR;
  2556. int last_cdp_link;
  2557. uint resblks;
  2558. xfs_itrace_entry(dp);
  2559. if (XFS_FORCED_SHUTDOWN(mp))
  2560. return XFS_ERROR(EIO);
  2561. if (!xfs_get_dir_entry(dentry, &cdp)) {
  2562. dm_di_mode = cdp->i_d.di_mode;
  2563. IRELE(cdp);
  2564. }
  2565. if (DM_EVENT_ENABLED(dp, DM_EVENT_REMOVE)) {
  2566. error = XFS_SEND_NAMESP(mp, DM_EVENT_REMOVE,
  2567. dir_vp, DM_RIGHT_NULL,
  2568. NULL, DM_RIGHT_NULL,
  2569. name, NULL, dm_di_mode, 0, 0);
  2570. if (error)
  2571. return XFS_ERROR(error);
  2572. }
  2573. /* Return through std_return after this point. */
  2574. cdp = NULL;
  2575. /*
  2576. * We need to get a reference to cdp before we get our log
  2577. * reservation. The reason for this is that we cannot call
  2578. * xfs_iget for an inode for which we do not have a reference
  2579. * once we've acquired a log reservation. This is because the
  2580. * inode we are trying to get might be in xfs_inactive going
  2581. * for a log reservation. Since we'll have to wait for the
  2582. * inactive code to complete before returning from xfs_iget,
  2583. * we need to make sure that we don't have log space reserved
  2584. * when we call xfs_iget. Instead we get an unlocked reference
  2585. * to the inode before getting our log reservation.
  2586. */
  2587. error = xfs_get_dir_entry(dentry, &cdp);
  2588. if (error) {
  2589. REMOVE_DEBUG_TRACE(__LINE__);
  2590. goto std_return;
  2591. }
  2592. mp = dp->i_mount;
  2593. dm_di_mode = cdp->i_d.di_mode;
  2594. /*
  2595. * Get the dquots for the inodes.
  2596. */
  2597. error = XFS_QM_DQATTACH(mp, dp, 0);
  2598. if (!error && dp != cdp)
  2599. error = XFS_QM_DQATTACH(mp, cdp, 0);
  2600. if (error) {
  2601. IRELE(cdp);
  2602. REMOVE_DEBUG_TRACE(__LINE__);
  2603. goto std_return;
  2604. }
  2605. tp = xfs_trans_alloc(mp, XFS_TRANS_RMDIR);
  2606. cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
  2607. /*
  2608. * We try to get the real space reservation first,
  2609. * allowing for directory btree deletion(s) implying
  2610. * possible bmap insert(s). If we can't get the space
  2611. * reservation then we use 0 instead, and avoid the bmap
  2612. * btree insert(s) in the directory code by, if the bmap
  2613. * insert tries to happen, instead trimming the LAST
  2614. * block from the directory.
  2615. */
  2616. resblks = XFS_REMOVE_SPACE_RES(mp);
  2617. error = xfs_trans_reserve(tp, resblks, XFS_REMOVE_LOG_RES(mp), 0,
  2618. XFS_TRANS_PERM_LOG_RES, XFS_DEFAULT_LOG_COUNT);
  2619. if (error == ENOSPC) {
  2620. resblks = 0;
  2621. error = xfs_trans_reserve(tp, 0, XFS_REMOVE_LOG_RES(mp), 0,
  2622. XFS_TRANS_PERM_LOG_RES, XFS_DEFAULT_LOG_COUNT);
  2623. }
  2624. if (error) {
  2625. ASSERT(error != ENOSPC);
  2626. cancel_flags = 0;
  2627. IRELE(cdp);
  2628. goto error_return;
  2629. }
  2630. XFS_BMAP_INIT(&free_list, &first_block);
  2631. /*
  2632. * Now lock the child directory inode and the parent directory
  2633. * inode in the proper order. This will take care of validating
  2634. * that the directory entry for the child directory inode has
  2635. * not changed while we were obtaining a log reservation.
  2636. */
  2637. error = xfs_lock_dir_and_entry(dp, cdp);
  2638. if (error) {
  2639. xfs_trans_cancel(tp, cancel_flags);
  2640. IRELE(cdp);
  2641. goto std_return;
  2642. }
  2643. xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
  2644. if (dp != cdp) {
  2645. /*
  2646. * Only increment the parent directory vnode count if
  2647. * we didn't bump it in looking up cdp. The only time
  2648. * we don't bump it is when we're looking up ".".
  2649. */
  2650. VN_HOLD(dir_vp);
  2651. }
  2652. xfs_itrace_ref(cdp);
  2653. xfs_trans_ijoin(tp, cdp, XFS_ILOCK_EXCL);
  2654. ASSERT(cdp->i_d.di_nlink >= 2);
  2655. if (cdp->i_d.di_nlink != 2) {
  2656. error = XFS_ERROR(ENOTEMPTY);
  2657. goto error_return;
  2658. }
  2659. if (!xfs_dir_isempty(cdp)) {
  2660. error = XFS_ERROR(ENOTEMPTY);
  2661. goto error_return;
  2662. }
  2663. error = xfs_dir_removename(tp, dp, name, namelen, cdp->i_ino,
  2664. &first_block, &free_list, resblks);
  2665. if (error)
  2666. goto error1;
  2667. xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  2668. /*
  2669. * Bump the in memory generation count on the parent
  2670. * directory so that other can know that it has changed.
  2671. */
  2672. dp->i_gen++;
  2673. /*
  2674. * Drop the link from cdp's "..".
  2675. */
  2676. error = xfs_droplink(tp, dp);
  2677. if (error) {
  2678. goto error1;
  2679. }
  2680. /*
  2681. * Drop the link from dp to cdp.
  2682. */
  2683. error = xfs_droplink(tp, cdp);
  2684. if (error) {
  2685. goto error1;
  2686. }
  2687. /*
  2688. * Drop the "." link from cdp to self.
  2689. */
  2690. error = xfs_droplink(tp, cdp);
  2691. if (error) {
  2692. goto error1;
  2693. }
  2694. /* Determine these before committing transaction */
  2695. last_cdp_link = (cdp)->i_d.di_nlink==0;
  2696. /*
  2697. * Take an extra ref on the child vnode so that it
  2698. * does not go to xfs_inactive() from within the commit.
  2699. */
  2700. IHOLD(cdp);
  2701. /*
  2702. * If this is a synchronous mount, make sure that the
  2703. * rmdir transaction goes to disk before returning to
  2704. * the user.
  2705. */
  2706. if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
  2707. xfs_trans_set_sync(tp);
  2708. }
  2709. error = xfs_bmap_finish (&tp, &free_list, &committed);
  2710. if (error) {
  2711. xfs_bmap_cancel(&free_list);
  2712. xfs_trans_cancel(tp, (XFS_TRANS_RELEASE_LOG_RES |
  2713. XFS_TRANS_ABORT));
  2714. IRELE(cdp);
  2715. goto std_return;
  2716. }
  2717. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  2718. if (error) {
  2719. IRELE(cdp);
  2720. goto std_return;
  2721. }
  2722. IRELE(cdp);
  2723. /* Fall through to std_return with error = 0 or the errno
  2724. * from xfs_trans_commit. */
  2725. std_return:
  2726. if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTREMOVE)) {
  2727. (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTREMOVE,
  2728. dir_vp, DM_RIGHT_NULL,
  2729. NULL, DM_RIGHT_NULL,
  2730. name, NULL, dm_di_mode,
  2731. error, 0);
  2732. }
  2733. return error;
  2734. error1:
  2735. xfs_bmap_cancel(&free_list);
  2736. cancel_flags |= XFS_TRANS_ABORT;
  2737. /* FALLTHROUGH */
  2738. error_return:
  2739. xfs_trans_cancel(tp, cancel_flags);
  2740. goto std_return;
  2741. }
  2742. int
  2743. xfs_symlink(
  2744. xfs_inode_t *dp,
  2745. bhv_vname_t *dentry,
  2746. char *target_path,
  2747. mode_t mode,
  2748. bhv_vnode_t **vpp,
  2749. cred_t *credp)
  2750. {
  2751. bhv_vnode_t *dir_vp = XFS_ITOV(dp);
  2752. xfs_mount_t *mp = dp->i_mount;
  2753. xfs_trans_t *tp;
  2754. xfs_inode_t *ip;
  2755. int error;
  2756. int pathlen;
  2757. xfs_bmap_free_t free_list;
  2758. xfs_fsblock_t first_block;
  2759. boolean_t unlock_dp_on_error = B_FALSE;
  2760. uint cancel_flags;
  2761. int committed;
  2762. xfs_fileoff_t first_fsb;
  2763. xfs_filblks_t fs_blocks;
  2764. int nmaps;
  2765. xfs_bmbt_irec_t mval[SYMLINK_MAPS];
  2766. xfs_daddr_t d;
  2767. char *cur_chunk;
  2768. int byte_cnt;
  2769. int n;
  2770. xfs_buf_t *bp;
  2771. xfs_prid_t prid;
  2772. struct xfs_dquot *udqp, *gdqp;
  2773. uint resblks;
  2774. char *link_name = VNAME(dentry);
  2775. int link_namelen;
  2776. *vpp = NULL;
  2777. error = 0;
  2778. ip = NULL;
  2779. tp = NULL;
  2780. xfs_itrace_entry(dp);
  2781. if (XFS_FORCED_SHUTDOWN(mp))
  2782. return XFS_ERROR(EIO);
  2783. link_namelen = VNAMELEN(dentry);
  2784. /*
  2785. * Check component lengths of the target path name.
  2786. */
  2787. pathlen = strlen(target_path);
  2788. if (pathlen >= MAXPATHLEN) /* total string too long */
  2789. return XFS_ERROR(ENAMETOOLONG);
  2790. if (pathlen >= MAXNAMELEN) { /* is any component too long? */
  2791. int len, total;
  2792. char *path;
  2793. for (total = 0, path = target_path; total < pathlen;) {
  2794. /*
  2795. * Skip any slashes.
  2796. */
  2797. while(*path == '/') {
  2798. total++;
  2799. path++;
  2800. }
  2801. /*
  2802. * Count up to the next slash or end of path.
  2803. * Error out if the component is bigger than MAXNAMELEN.
  2804. */
  2805. for(len = 0; *path != '/' && total < pathlen;total++, path++) {
  2806. if (++len >= MAXNAMELEN) {
  2807. error = ENAMETOOLONG;
  2808. return error;
  2809. }
  2810. }
  2811. }
  2812. }
  2813. if (DM_EVENT_ENABLED(dp, DM_EVENT_SYMLINK)) {
  2814. error = XFS_SEND_NAMESP(mp, DM_EVENT_SYMLINK, dir_vp,
  2815. DM_RIGHT_NULL, NULL, DM_RIGHT_NULL,
  2816. link_name, target_path, 0, 0, 0);
  2817. if (error)
  2818. return error;
  2819. }
  2820. /* Return through std_return after this point. */
  2821. udqp = gdqp = NULL;
  2822. if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
  2823. prid = dp->i_d.di_projid;
  2824. else
  2825. prid = (xfs_prid_t)dfltprid;
  2826. /*
  2827. * Make sure that we have allocated dquot(s) on disk.
  2828. */
  2829. error = XFS_QM_DQVOPALLOC(mp, dp,
  2830. current_fsuid(credp), current_fsgid(credp), prid,
  2831. XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
  2832. if (error)
  2833. goto std_return;
  2834. tp = xfs_trans_alloc(mp, XFS_TRANS_SYMLINK);
  2835. cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
  2836. /*
  2837. * The symlink will fit into the inode data fork?
  2838. * There can't be any attributes so we get the whole variable part.
  2839. */
  2840. if (pathlen <= XFS_LITINO(mp))
  2841. fs_blocks = 0;
  2842. else
  2843. fs_blocks = XFS_B_TO_FSB(mp, pathlen);
  2844. resblks = XFS_SYMLINK_SPACE_RES(mp, link_namelen, fs_blocks);
  2845. error = xfs_trans_reserve(tp, resblks, XFS_SYMLINK_LOG_RES(mp), 0,
  2846. XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
  2847. if (error == ENOSPC && fs_blocks == 0) {
  2848. resblks = 0;
  2849. error = xfs_trans_reserve(tp, 0, XFS_SYMLINK_LOG_RES(mp), 0,
  2850. XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
  2851. }
  2852. if (error) {
  2853. cancel_flags = 0;
  2854. goto error_return;
  2855. }
  2856. xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
  2857. unlock_dp_on_error = B_TRUE;
  2858. /*
  2859. * Check whether the directory allows new symlinks or not.
  2860. */
  2861. if (dp->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) {
  2862. error = XFS_ERROR(EPERM);
  2863. goto error_return;
  2864. }
  2865. /*
  2866. * Reserve disk quota : blocks and inode.
  2867. */
  2868. error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
  2869. if (error)
  2870. goto error_return;
  2871. /*
  2872. * Check for ability to enter directory entry, if no space reserved.
  2873. */
  2874. if (resblks == 0 &&
  2875. (error = xfs_dir_canenter(tp, dp, link_name, link_namelen)))
  2876. goto error_return;
  2877. /*
  2878. * Initialize the bmap freelist prior to calling either
  2879. * bmapi or the directory create code.
  2880. */
  2881. XFS_BMAP_INIT(&free_list, &first_block);
  2882. /*
  2883. * Allocate an inode for the symlink.
  2884. */
  2885. error = xfs_dir_ialloc(&tp, dp, S_IFLNK | (mode & ~S_IFMT),
  2886. 1, 0, credp, prid, resblks > 0, &ip, NULL);
  2887. if (error) {
  2888. if (error == ENOSPC)
  2889. goto error_return;
  2890. goto error1;
  2891. }
  2892. xfs_itrace_ref(ip);
  2893. /*
  2894. * An error after we've joined dp to the transaction will result in the
  2895. * transaction cancel unlocking dp so don't do it explicitly in the
  2896. * error path.
  2897. */
  2898. VN_HOLD(dir_vp);
  2899. xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
  2900. unlock_dp_on_error = B_FALSE;
  2901. /*
  2902. * Also attach the dquot(s) to it, if applicable.
  2903. */
  2904. XFS_QM_DQVOPCREATE(mp, tp, ip, udqp, gdqp);
  2905. if (resblks)
  2906. resblks -= XFS_IALLOC_SPACE_RES(mp);
  2907. /*
  2908. * If the symlink will fit into the inode, write it inline.
  2909. */
  2910. if (pathlen <= XFS_IFORK_DSIZE(ip)) {
  2911. xfs_idata_realloc(ip, pathlen, XFS_DATA_FORK);
  2912. memcpy(ip->i_df.if_u1.if_data, target_path, pathlen);
  2913. ip->i_d.di_size = pathlen;
  2914. /*
  2915. * The inode was initially created in extent format.
  2916. */
  2917. ip->i_df.if_flags &= ~(XFS_IFEXTENTS | XFS_IFBROOT);
  2918. ip->i_df.if_flags |= XFS_IFINLINE;
  2919. ip->i_d.di_format = XFS_DINODE_FMT_LOCAL;
  2920. xfs_trans_log_inode(tp, ip, XFS_ILOG_DDATA | XFS_ILOG_CORE);
  2921. } else {
  2922. first_fsb = 0;
  2923. nmaps = SYMLINK_MAPS;
  2924. error = xfs_bmapi(tp, ip, first_fsb, fs_blocks,
  2925. XFS_BMAPI_WRITE | XFS_BMAPI_METADATA,
  2926. &first_block, resblks, mval, &nmaps,
  2927. &free_list, NULL);
  2928. if (error) {
  2929. goto error1;
  2930. }
  2931. if (resblks)
  2932. resblks -= fs_blocks;
  2933. ip->i_d.di_size = pathlen;
  2934. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  2935. cur_chunk = target_path;
  2936. for (n = 0; n < nmaps; n++) {
  2937. d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
  2938. byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
  2939. bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
  2940. BTOBB(byte_cnt), 0);
  2941. ASSERT(bp && !XFS_BUF_GETERROR(bp));
  2942. if (pathlen < byte_cnt) {
  2943. byte_cnt = pathlen;
  2944. }
  2945. pathlen -= byte_cnt;
  2946. memcpy(XFS_BUF_PTR(bp), cur_chunk, byte_cnt);
  2947. cur_chunk += byte_cnt;
  2948. xfs_trans_log_buf(tp, bp, 0, byte_cnt - 1);
  2949. }
  2950. }
  2951. /*
  2952. * Create the directory entry for the symlink.
  2953. */
  2954. error = xfs_dir_createname(tp, dp, link_name, link_namelen, ip->i_ino,
  2955. &first_block, &free_list, resblks);
  2956. if (error)
  2957. goto error1;
  2958. xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  2959. xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
  2960. /*
  2961. * Bump the in memory version number of the parent directory
  2962. * so that other processes accessing it will recognize that
  2963. * the directory has changed.
  2964. */
  2965. dp->i_gen++;
  2966. /*
  2967. * If this is a synchronous mount, make sure that the
  2968. * symlink transaction goes to disk before returning to
  2969. * the user.
  2970. */
  2971. if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
  2972. xfs_trans_set_sync(tp);
  2973. }
  2974. /*
  2975. * xfs_trans_commit normally decrements the vnode ref count
  2976. * when it unlocks the inode. Since we want to return the
  2977. * vnode to the caller, we bump the vnode ref count now.
  2978. */
  2979. IHOLD(ip);
  2980. error = xfs_bmap_finish(&tp, &free_list, &committed);
  2981. if (error) {
  2982. goto error2;
  2983. }
  2984. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  2985. XFS_QM_DQRELE(mp, udqp);
  2986. XFS_QM_DQRELE(mp, gdqp);
  2987. /* Fall through to std_return with error = 0 or errno from
  2988. * xfs_trans_commit */
  2989. std_return:
  2990. if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTSYMLINK)) {
  2991. (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTSYMLINK,
  2992. dir_vp, DM_RIGHT_NULL,
  2993. error ? NULL : XFS_ITOV(ip),
  2994. DM_RIGHT_NULL, link_name, target_path,
  2995. 0, error, 0);
  2996. }
  2997. if (!error) {
  2998. bhv_vnode_t *vp;
  2999. ASSERT(ip);
  3000. vp = XFS_ITOV(ip);
  3001. *vpp = vp;
  3002. }
  3003. return error;
  3004. error2:
  3005. IRELE(ip);
  3006. error1:
  3007. xfs_bmap_cancel(&free_list);
  3008. cancel_flags |= XFS_TRANS_ABORT;
  3009. error_return:
  3010. xfs_trans_cancel(tp, cancel_flags);
  3011. XFS_QM_DQRELE(mp, udqp);
  3012. XFS_QM_DQRELE(mp, gdqp);
  3013. if (unlock_dp_on_error)
  3014. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  3015. goto std_return;
  3016. }
  3017. int
  3018. xfs_rwlock(
  3019. xfs_inode_t *ip,
  3020. bhv_vrwlock_t locktype)
  3021. {
  3022. if (S_ISDIR(ip->i_d.di_mode))
  3023. return 1;
  3024. if (locktype == VRWLOCK_WRITE) {
  3025. xfs_ilock(ip, XFS_IOLOCK_EXCL);
  3026. } else if (locktype == VRWLOCK_TRY_READ) {
  3027. return xfs_ilock_nowait(ip, XFS_IOLOCK_SHARED);
  3028. } else if (locktype == VRWLOCK_TRY_WRITE) {
  3029. return xfs_ilock_nowait(ip, XFS_IOLOCK_EXCL);
  3030. } else {
  3031. ASSERT((locktype == VRWLOCK_READ) ||
  3032. (locktype == VRWLOCK_WRITE_DIRECT));
  3033. xfs_ilock(ip, XFS_IOLOCK_SHARED);
  3034. }
  3035. return 1;
  3036. }
  3037. void
  3038. xfs_rwunlock(
  3039. xfs_inode_t *ip,
  3040. bhv_vrwlock_t locktype)
  3041. {
  3042. if (S_ISDIR(ip->i_d.di_mode))
  3043. return;
  3044. if (locktype == VRWLOCK_WRITE) {
  3045. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  3046. } else {
  3047. ASSERT((locktype == VRWLOCK_READ) ||
  3048. (locktype == VRWLOCK_WRITE_DIRECT));
  3049. xfs_iunlock(ip, XFS_IOLOCK_SHARED);
  3050. }
  3051. return;
  3052. }
  3053. int
  3054. xfs_inode_flush(
  3055. xfs_inode_t *ip,
  3056. int flags)
  3057. {
  3058. xfs_mount_t *mp = ip->i_mount;
  3059. xfs_inode_log_item_t *iip = ip->i_itemp;
  3060. int error = 0;
  3061. if (XFS_FORCED_SHUTDOWN(mp))
  3062. return XFS_ERROR(EIO);
  3063. /*
  3064. * Bypass inodes which have already been cleaned by
  3065. * the inode flush clustering code inside xfs_iflush
  3066. */
  3067. if ((ip->i_update_core == 0) &&
  3068. ((iip == NULL) || !(iip->ili_format.ilf_fields & XFS_ILOG_ALL)))
  3069. return 0;
  3070. /*
  3071. * We make this non-blocking if the inode is contended,
  3072. * return EAGAIN to indicate to the caller that they
  3073. * did not succeed. This prevents the flush path from
  3074. * blocking on inodes inside another operation right
  3075. * now, they get caught later by xfs_sync.
  3076. */
  3077. if (flags & FLUSH_SYNC) {
  3078. xfs_ilock(ip, XFS_ILOCK_SHARED);
  3079. xfs_iflock(ip);
  3080. } else if (xfs_ilock_nowait(ip, XFS_ILOCK_SHARED)) {
  3081. if (xfs_ipincount(ip) || !xfs_iflock_nowait(ip)) {
  3082. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  3083. return EAGAIN;
  3084. }
  3085. } else {
  3086. return EAGAIN;
  3087. }
  3088. error = xfs_iflush(ip, (flags & FLUSH_SYNC) ? XFS_IFLUSH_SYNC
  3089. : XFS_IFLUSH_ASYNC_NOBLOCK);
  3090. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  3091. return error;
  3092. }
  3093. int
  3094. xfs_set_dmattrs(
  3095. xfs_inode_t *ip,
  3096. u_int evmask,
  3097. u_int16_t state)
  3098. {
  3099. xfs_mount_t *mp = ip->i_mount;
  3100. xfs_trans_t *tp;
  3101. int error;
  3102. if (!capable(CAP_SYS_ADMIN))
  3103. return XFS_ERROR(EPERM);
  3104. if (XFS_FORCED_SHUTDOWN(mp))
  3105. return XFS_ERROR(EIO);
  3106. tp = xfs_trans_alloc(mp, XFS_TRANS_SET_DMATTRS);
  3107. error = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES (mp), 0, 0, 0);
  3108. if (error) {
  3109. xfs_trans_cancel(tp, 0);
  3110. return error;
  3111. }
  3112. xfs_ilock(ip, XFS_ILOCK_EXCL);
  3113. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  3114. ip->i_d.di_dmevmask = evmask;
  3115. ip->i_d.di_dmstate = state;
  3116. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  3117. IHOLD(ip);
  3118. error = xfs_trans_commit(tp, 0);
  3119. return error;
  3120. }
  3121. int
  3122. xfs_reclaim(
  3123. xfs_inode_t *ip)
  3124. {
  3125. bhv_vnode_t *vp = XFS_ITOV(ip);
  3126. xfs_itrace_entry(ip);
  3127. ASSERT(!VN_MAPPED(vp));
  3128. /* bad inode, get out here ASAP */
  3129. if (VN_BAD(vp)) {
  3130. xfs_ireclaim(ip);
  3131. return 0;
  3132. }
  3133. vn_iowait(ip);
  3134. ASSERT(XFS_FORCED_SHUTDOWN(ip->i_mount) || ip->i_delayed_blks == 0);
  3135. /*
  3136. * Make sure the atime in the XFS inode is correct before freeing the
  3137. * Linux inode.
  3138. */
  3139. xfs_synchronize_atime(ip);
  3140. /*
  3141. * If we have nothing to flush with this inode then complete the
  3142. * teardown now, otherwise break the link between the xfs inode and the
  3143. * linux inode and clean up the xfs inode later. This avoids flushing
  3144. * the inode to disk during the delete operation itself.
  3145. *
  3146. * When breaking the link, we need to set the XFS_IRECLAIMABLE flag
  3147. * first to ensure that xfs_iunpin() will never see an xfs inode
  3148. * that has a linux inode being reclaimed. Synchronisation is provided
  3149. * by the i_flags_lock.
  3150. */
  3151. if (!ip->i_update_core && (ip->i_itemp == NULL)) {
  3152. xfs_ilock(ip, XFS_ILOCK_EXCL);
  3153. xfs_iflock(ip);
  3154. return xfs_finish_reclaim(ip, 1, XFS_IFLUSH_DELWRI_ELSE_SYNC);
  3155. } else {
  3156. xfs_mount_t *mp = ip->i_mount;
  3157. /* Protect sync and unpin from us */
  3158. XFS_MOUNT_ILOCK(mp);
  3159. spin_lock(&ip->i_flags_lock);
  3160. __xfs_iflags_set(ip, XFS_IRECLAIMABLE);
  3161. vn_to_inode(vp)->i_private = NULL;
  3162. ip->i_vnode = NULL;
  3163. spin_unlock(&ip->i_flags_lock);
  3164. list_add_tail(&ip->i_reclaim, &mp->m_del_inodes);
  3165. XFS_MOUNT_IUNLOCK(mp);
  3166. }
  3167. return 0;
  3168. }
  3169. int
  3170. xfs_finish_reclaim(
  3171. xfs_inode_t *ip,
  3172. int locked,
  3173. int sync_mode)
  3174. {
  3175. xfs_perag_t *pag = xfs_get_perag(ip->i_mount, ip->i_ino);
  3176. bhv_vnode_t *vp = XFS_ITOV_NULL(ip);
  3177. int error;
  3178. if (vp && VN_BAD(vp))
  3179. goto reclaim;
  3180. /* The hash lock here protects a thread in xfs_iget_core from
  3181. * racing with us on linking the inode back with a vnode.
  3182. * Once we have the XFS_IRECLAIM flag set it will not touch
  3183. * us.
  3184. */
  3185. write_lock(&pag->pag_ici_lock);
  3186. spin_lock(&ip->i_flags_lock);
  3187. if (__xfs_iflags_test(ip, XFS_IRECLAIM) ||
  3188. (!__xfs_iflags_test(ip, XFS_IRECLAIMABLE) && vp == NULL)) {
  3189. spin_unlock(&ip->i_flags_lock);
  3190. write_unlock(&pag->pag_ici_lock);
  3191. if (locked) {
  3192. xfs_ifunlock(ip);
  3193. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  3194. }
  3195. return 1;
  3196. }
  3197. __xfs_iflags_set(ip, XFS_IRECLAIM);
  3198. spin_unlock(&ip->i_flags_lock);
  3199. write_unlock(&pag->pag_ici_lock);
  3200. xfs_put_perag(ip->i_mount, pag);
  3201. /*
  3202. * If the inode is still dirty, then flush it out. If the inode
  3203. * is not in the AIL, then it will be OK to flush it delwri as
  3204. * long as xfs_iflush() does not keep any references to the inode.
  3205. * We leave that decision up to xfs_iflush() since it has the
  3206. * knowledge of whether it's OK to simply do a delwri flush of
  3207. * the inode or whether we need to wait until the inode is
  3208. * pulled from the AIL.
  3209. * We get the flush lock regardless, though, just to make sure
  3210. * we don't free it while it is being flushed.
  3211. */
  3212. if (!locked) {
  3213. xfs_ilock(ip, XFS_ILOCK_EXCL);
  3214. xfs_iflock(ip);
  3215. }
  3216. if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
  3217. if (ip->i_update_core ||
  3218. ((ip->i_itemp != NULL) &&
  3219. (ip->i_itemp->ili_format.ilf_fields != 0))) {
  3220. error = xfs_iflush(ip, sync_mode);
  3221. /*
  3222. * If we hit an error, typically because of filesystem
  3223. * shutdown, we don't need to let vn_reclaim to know
  3224. * because we're gonna reclaim the inode anyway.
  3225. */
  3226. if (error) {
  3227. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  3228. goto reclaim;
  3229. }
  3230. xfs_iflock(ip); /* synchronize with xfs_iflush_done */
  3231. }
  3232. ASSERT(ip->i_update_core == 0);
  3233. ASSERT(ip->i_itemp == NULL ||
  3234. ip->i_itemp->ili_format.ilf_fields == 0);
  3235. }
  3236. xfs_ifunlock(ip);
  3237. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  3238. reclaim:
  3239. xfs_ireclaim(ip);
  3240. return 0;
  3241. }
  3242. int
  3243. xfs_finish_reclaim_all(xfs_mount_t *mp, int noblock)
  3244. {
  3245. int purged;
  3246. xfs_inode_t *ip, *n;
  3247. int done = 0;
  3248. while (!done) {
  3249. purged = 0;
  3250. XFS_MOUNT_ILOCK(mp);
  3251. list_for_each_entry_safe(ip, n, &mp->m_del_inodes, i_reclaim) {
  3252. if (noblock) {
  3253. if (xfs_ilock_nowait(ip, XFS_ILOCK_EXCL) == 0)
  3254. continue;
  3255. if (xfs_ipincount(ip) ||
  3256. !xfs_iflock_nowait(ip)) {
  3257. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  3258. continue;
  3259. }
  3260. }
  3261. XFS_MOUNT_IUNLOCK(mp);
  3262. if (xfs_finish_reclaim(ip, noblock,
  3263. XFS_IFLUSH_DELWRI_ELSE_ASYNC))
  3264. delay(1);
  3265. purged = 1;
  3266. break;
  3267. }
  3268. done = !purged;
  3269. }
  3270. XFS_MOUNT_IUNLOCK(mp);
  3271. return 0;
  3272. }
  3273. /*
  3274. * xfs_alloc_file_space()
  3275. * This routine allocates disk space for the given file.
  3276. *
  3277. * If alloc_type == 0, this request is for an ALLOCSP type
  3278. * request which will change the file size. In this case, no
  3279. * DMAPI event will be generated by the call. A TRUNCATE event
  3280. * will be generated later by xfs_setattr.
  3281. *
  3282. * If alloc_type != 0, this request is for a RESVSP type
  3283. * request, and a DMAPI DM_EVENT_WRITE will be generated if the
  3284. * lower block boundary byte address is less than the file's
  3285. * length.
  3286. *
  3287. * RETURNS:
  3288. * 0 on success
  3289. * errno on error
  3290. *
  3291. */
  3292. STATIC int
  3293. xfs_alloc_file_space(
  3294. xfs_inode_t *ip,
  3295. xfs_off_t offset,
  3296. xfs_off_t len,
  3297. int alloc_type,
  3298. int attr_flags)
  3299. {
  3300. xfs_mount_t *mp = ip->i_mount;
  3301. xfs_off_t count;
  3302. xfs_filblks_t allocated_fsb;
  3303. xfs_filblks_t allocatesize_fsb;
  3304. xfs_extlen_t extsz, temp;
  3305. xfs_fileoff_t startoffset_fsb;
  3306. xfs_fsblock_t firstfsb;
  3307. int nimaps;
  3308. int bmapi_flag;
  3309. int quota_flag;
  3310. int rt;
  3311. xfs_trans_t *tp;
  3312. xfs_bmbt_irec_t imaps[1], *imapp;
  3313. xfs_bmap_free_t free_list;
  3314. uint qblocks, resblks, resrtextents;
  3315. int committed;
  3316. int error;
  3317. xfs_itrace_entry(ip);
  3318. if (XFS_FORCED_SHUTDOWN(mp))
  3319. return XFS_ERROR(EIO);
  3320. if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
  3321. return error;
  3322. if (len <= 0)
  3323. return XFS_ERROR(EINVAL);
  3324. rt = XFS_IS_REALTIME_INODE(ip);
  3325. extsz = xfs_get_extsz_hint(ip);
  3326. count = len;
  3327. imapp = &imaps[0];
  3328. nimaps = 1;
  3329. bmapi_flag = XFS_BMAPI_WRITE | (alloc_type ? XFS_BMAPI_PREALLOC : 0);
  3330. startoffset_fsb = XFS_B_TO_FSBT(mp, offset);
  3331. allocatesize_fsb = XFS_B_TO_FSB(mp, count);
  3332. /* Generate a DMAPI event if needed. */
  3333. if (alloc_type != 0 && offset < ip->i_size &&
  3334. (attr_flags&ATTR_DMI) == 0 &&
  3335. DM_EVENT_ENABLED(ip, DM_EVENT_WRITE)) {
  3336. xfs_off_t end_dmi_offset;
  3337. end_dmi_offset = offset+len;
  3338. if (end_dmi_offset > ip->i_size)
  3339. end_dmi_offset = ip->i_size;
  3340. error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, XFS_ITOV(ip),
  3341. offset, end_dmi_offset - offset,
  3342. 0, NULL);
  3343. if (error)
  3344. return error;
  3345. }
  3346. /*
  3347. * Allocate file space until done or until there is an error
  3348. */
  3349. retry:
  3350. while (allocatesize_fsb && !error) {
  3351. xfs_fileoff_t s, e;
  3352. /*
  3353. * Determine space reservations for data/realtime.
  3354. */
  3355. if (unlikely(extsz)) {
  3356. s = startoffset_fsb;
  3357. do_div(s, extsz);
  3358. s *= extsz;
  3359. e = startoffset_fsb + allocatesize_fsb;
  3360. if ((temp = do_mod(startoffset_fsb, extsz)))
  3361. e += temp;
  3362. if ((temp = do_mod(e, extsz)))
  3363. e += extsz - temp;
  3364. } else {
  3365. s = 0;
  3366. e = allocatesize_fsb;
  3367. }
  3368. if (unlikely(rt)) {
  3369. resrtextents = qblocks = (uint)(e - s);
  3370. resrtextents /= mp->m_sb.sb_rextsize;
  3371. resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
  3372. quota_flag = XFS_QMOPT_RES_RTBLKS;
  3373. } else {
  3374. resrtextents = 0;
  3375. resblks = qblocks = \
  3376. XFS_DIOSTRAT_SPACE_RES(mp, (uint)(e - s));
  3377. quota_flag = XFS_QMOPT_RES_REGBLKS;
  3378. }
  3379. /*
  3380. * Allocate and setup the transaction.
  3381. */
  3382. tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
  3383. error = xfs_trans_reserve(tp, resblks,
  3384. XFS_WRITE_LOG_RES(mp), resrtextents,
  3385. XFS_TRANS_PERM_LOG_RES,
  3386. XFS_WRITE_LOG_COUNT);
  3387. /*
  3388. * Check for running out of space
  3389. */
  3390. if (error) {
  3391. /*
  3392. * Free the transaction structure.
  3393. */
  3394. ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
  3395. xfs_trans_cancel(tp, 0);
  3396. break;
  3397. }
  3398. xfs_ilock(ip, XFS_ILOCK_EXCL);
  3399. error = XFS_TRANS_RESERVE_QUOTA_NBLKS(mp, tp, ip,
  3400. qblocks, 0, quota_flag);
  3401. if (error)
  3402. goto error1;
  3403. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  3404. xfs_trans_ihold(tp, ip);
  3405. /*
  3406. * Issue the xfs_bmapi() call to allocate the blocks
  3407. */
  3408. XFS_BMAP_INIT(&free_list, &firstfsb);
  3409. error = xfs_bmapi(tp, ip, startoffset_fsb,
  3410. allocatesize_fsb, bmapi_flag,
  3411. &firstfsb, 0, imapp, &nimaps,
  3412. &free_list, NULL);
  3413. if (error) {
  3414. goto error0;
  3415. }
  3416. /*
  3417. * Complete the transaction
  3418. */
  3419. error = xfs_bmap_finish(&tp, &free_list, &committed);
  3420. if (error) {
  3421. goto error0;
  3422. }
  3423. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  3424. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  3425. if (error) {
  3426. break;
  3427. }
  3428. allocated_fsb = imapp->br_blockcount;
  3429. if (nimaps == 0) {
  3430. error = XFS_ERROR(ENOSPC);
  3431. break;
  3432. }
  3433. startoffset_fsb += allocated_fsb;
  3434. allocatesize_fsb -= allocated_fsb;
  3435. }
  3436. dmapi_enospc_check:
  3437. if (error == ENOSPC && (attr_flags & ATTR_DMI) == 0 &&
  3438. DM_EVENT_ENABLED(ip, DM_EVENT_NOSPACE)) {
  3439. error = XFS_SEND_NAMESP(mp, DM_EVENT_NOSPACE,
  3440. XFS_ITOV(ip), DM_RIGHT_NULL,
  3441. XFS_ITOV(ip), DM_RIGHT_NULL,
  3442. NULL, NULL, 0, 0, 0); /* Delay flag intentionally unused */
  3443. if (error == 0)
  3444. goto retry; /* Maybe DMAPI app. has made space */
  3445. /* else fall through with error from XFS_SEND_DATA */
  3446. }
  3447. return error;
  3448. error0: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
  3449. xfs_bmap_cancel(&free_list);
  3450. XFS_TRANS_UNRESERVE_QUOTA_NBLKS(mp, tp, ip, qblocks, 0, quota_flag);
  3451. error1: /* Just cancel transaction */
  3452. xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
  3453. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  3454. goto dmapi_enospc_check;
  3455. }
  3456. /*
  3457. * Zero file bytes between startoff and endoff inclusive.
  3458. * The iolock is held exclusive and no blocks are buffered.
  3459. */
  3460. STATIC int
  3461. xfs_zero_remaining_bytes(
  3462. xfs_inode_t *ip,
  3463. xfs_off_t startoff,
  3464. xfs_off_t endoff)
  3465. {
  3466. xfs_bmbt_irec_t imap;
  3467. xfs_fileoff_t offset_fsb;
  3468. xfs_off_t lastoffset;
  3469. xfs_off_t offset;
  3470. xfs_buf_t *bp;
  3471. xfs_mount_t *mp = ip->i_mount;
  3472. int nimap;
  3473. int error = 0;
  3474. bp = xfs_buf_get_noaddr(mp->m_sb.sb_blocksize,
  3475. XFS_IS_REALTIME_INODE(ip) ?
  3476. mp->m_rtdev_targp : mp->m_ddev_targp);
  3477. for (offset = startoff; offset <= endoff; offset = lastoffset + 1) {
  3478. offset_fsb = XFS_B_TO_FSBT(mp, offset);
  3479. nimap = 1;
  3480. error = xfs_bmapi(NULL, ip, offset_fsb, 1, 0,
  3481. NULL, 0, &imap, &nimap, NULL, NULL);
  3482. if (error || nimap < 1)
  3483. break;
  3484. ASSERT(imap.br_blockcount >= 1);
  3485. ASSERT(imap.br_startoff == offset_fsb);
  3486. lastoffset = XFS_FSB_TO_B(mp, imap.br_startoff + 1) - 1;
  3487. if (lastoffset > endoff)
  3488. lastoffset = endoff;
  3489. if (imap.br_startblock == HOLESTARTBLOCK)
  3490. continue;
  3491. ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
  3492. if (imap.br_state == XFS_EXT_UNWRITTEN)
  3493. continue;
  3494. XFS_BUF_UNDONE(bp);
  3495. XFS_BUF_UNWRITE(bp);
  3496. XFS_BUF_READ(bp);
  3497. XFS_BUF_SET_ADDR(bp, XFS_FSB_TO_DB(ip, imap.br_startblock));
  3498. xfsbdstrat(mp, bp);
  3499. if ((error = xfs_iowait(bp))) {
  3500. xfs_ioerror_alert("xfs_zero_remaining_bytes(read)",
  3501. mp, bp, XFS_BUF_ADDR(bp));
  3502. break;
  3503. }
  3504. memset(XFS_BUF_PTR(bp) +
  3505. (offset - XFS_FSB_TO_B(mp, imap.br_startoff)),
  3506. 0, lastoffset - offset + 1);
  3507. XFS_BUF_UNDONE(bp);
  3508. XFS_BUF_UNREAD(bp);
  3509. XFS_BUF_WRITE(bp);
  3510. xfsbdstrat(mp, bp);
  3511. if ((error = xfs_iowait(bp))) {
  3512. xfs_ioerror_alert("xfs_zero_remaining_bytes(write)",
  3513. mp, bp, XFS_BUF_ADDR(bp));
  3514. break;
  3515. }
  3516. }
  3517. xfs_buf_free(bp);
  3518. return error;
  3519. }
  3520. /*
  3521. * xfs_free_file_space()
  3522. * This routine frees disk space for the given file.
  3523. *
  3524. * This routine is only called by xfs_change_file_space
  3525. * for an UNRESVSP type call.
  3526. *
  3527. * RETURNS:
  3528. * 0 on success
  3529. * errno on error
  3530. *
  3531. */
  3532. STATIC int
  3533. xfs_free_file_space(
  3534. xfs_inode_t *ip,
  3535. xfs_off_t offset,
  3536. xfs_off_t len,
  3537. int attr_flags)
  3538. {
  3539. bhv_vnode_t *vp;
  3540. int committed;
  3541. int done;
  3542. xfs_off_t end_dmi_offset;
  3543. xfs_fileoff_t endoffset_fsb;
  3544. int error;
  3545. xfs_fsblock_t firstfsb;
  3546. xfs_bmap_free_t free_list;
  3547. xfs_bmbt_irec_t imap;
  3548. xfs_off_t ioffset;
  3549. xfs_extlen_t mod=0;
  3550. xfs_mount_t *mp;
  3551. int nimap;
  3552. uint resblks;
  3553. uint rounding;
  3554. int rt;
  3555. xfs_fileoff_t startoffset_fsb;
  3556. xfs_trans_t *tp;
  3557. int need_iolock = 1;
  3558. vp = XFS_ITOV(ip);
  3559. mp = ip->i_mount;
  3560. xfs_itrace_entry(ip);
  3561. if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
  3562. return error;
  3563. error = 0;
  3564. if (len <= 0) /* if nothing being freed */
  3565. return error;
  3566. rt = XFS_IS_REALTIME_INODE(ip);
  3567. startoffset_fsb = XFS_B_TO_FSB(mp, offset);
  3568. end_dmi_offset = offset + len;
  3569. endoffset_fsb = XFS_B_TO_FSBT(mp, end_dmi_offset);
  3570. if (offset < ip->i_size && (attr_flags & ATTR_DMI) == 0 &&
  3571. DM_EVENT_ENABLED(ip, DM_EVENT_WRITE)) {
  3572. if (end_dmi_offset > ip->i_size)
  3573. end_dmi_offset = ip->i_size;
  3574. error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, vp,
  3575. offset, end_dmi_offset - offset,
  3576. AT_DELAY_FLAG(attr_flags), NULL);
  3577. if (error)
  3578. return error;
  3579. }
  3580. if (attr_flags & ATTR_NOLOCK)
  3581. need_iolock = 0;
  3582. if (need_iolock) {
  3583. xfs_ilock(ip, XFS_IOLOCK_EXCL);
  3584. vn_iowait(ip); /* wait for the completion of any pending DIOs */
  3585. }
  3586. rounding = max_t(uint, 1 << mp->m_sb.sb_blocklog, PAGE_CACHE_SIZE);
  3587. ioffset = offset & ~(rounding - 1);
  3588. if (VN_CACHED(vp) != 0) {
  3589. xfs_inval_cached_trace(ip, ioffset, -1, ioffset, -1);
  3590. error = xfs_flushinval_pages(ip, ioffset, -1, FI_REMAPF_LOCKED);
  3591. if (error)
  3592. goto out_unlock_iolock;
  3593. }
  3594. /*
  3595. * Need to zero the stuff we're not freeing, on disk.
  3596. * If its a realtime file & can't use unwritten extents then we
  3597. * actually need to zero the extent edges. Otherwise xfs_bunmapi
  3598. * will take care of it for us.
  3599. */
  3600. if (rt && !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
  3601. nimap = 1;
  3602. error = xfs_bmapi(NULL, ip, startoffset_fsb,
  3603. 1, 0, NULL, 0, &imap, &nimap, NULL, NULL);
  3604. if (error)
  3605. goto out_unlock_iolock;
  3606. ASSERT(nimap == 0 || nimap == 1);
  3607. if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
  3608. xfs_daddr_t block;
  3609. ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
  3610. block = imap.br_startblock;
  3611. mod = do_div(block, mp->m_sb.sb_rextsize);
  3612. if (mod)
  3613. startoffset_fsb += mp->m_sb.sb_rextsize - mod;
  3614. }
  3615. nimap = 1;
  3616. error = xfs_bmapi(NULL, ip, endoffset_fsb - 1,
  3617. 1, 0, NULL, 0, &imap, &nimap, NULL, NULL);
  3618. if (error)
  3619. goto out_unlock_iolock;
  3620. ASSERT(nimap == 0 || nimap == 1);
  3621. if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
  3622. ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
  3623. mod++;
  3624. if (mod && (mod != mp->m_sb.sb_rextsize))
  3625. endoffset_fsb -= mod;
  3626. }
  3627. }
  3628. if ((done = (endoffset_fsb <= startoffset_fsb)))
  3629. /*
  3630. * One contiguous piece to clear
  3631. */
  3632. error = xfs_zero_remaining_bytes(ip, offset, offset + len - 1);
  3633. else {
  3634. /*
  3635. * Some full blocks, possibly two pieces to clear
  3636. */
  3637. if (offset < XFS_FSB_TO_B(mp, startoffset_fsb))
  3638. error = xfs_zero_remaining_bytes(ip, offset,
  3639. XFS_FSB_TO_B(mp, startoffset_fsb) - 1);
  3640. if (!error &&
  3641. XFS_FSB_TO_B(mp, endoffset_fsb) < offset + len)
  3642. error = xfs_zero_remaining_bytes(ip,
  3643. XFS_FSB_TO_B(mp, endoffset_fsb),
  3644. offset + len - 1);
  3645. }
  3646. /*
  3647. * free file space until done or until there is an error
  3648. */
  3649. resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
  3650. while (!error && !done) {
  3651. /*
  3652. * allocate and setup the transaction. Allow this
  3653. * transaction to dip into the reserve blocks to ensure
  3654. * the freeing of the space succeeds at ENOSPC.
  3655. */
  3656. tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
  3657. tp->t_flags |= XFS_TRANS_RESERVE;
  3658. error = xfs_trans_reserve(tp,
  3659. resblks,
  3660. XFS_WRITE_LOG_RES(mp),
  3661. 0,
  3662. XFS_TRANS_PERM_LOG_RES,
  3663. XFS_WRITE_LOG_COUNT);
  3664. /*
  3665. * check for running out of space
  3666. */
  3667. if (error) {
  3668. /*
  3669. * Free the transaction structure.
  3670. */
  3671. ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
  3672. xfs_trans_cancel(tp, 0);
  3673. break;
  3674. }
  3675. xfs_ilock(ip, XFS_ILOCK_EXCL);
  3676. error = XFS_TRANS_RESERVE_QUOTA(mp, tp,
  3677. ip->i_udquot, ip->i_gdquot, resblks, 0,
  3678. XFS_QMOPT_RES_REGBLKS);
  3679. if (error)
  3680. goto error1;
  3681. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  3682. xfs_trans_ihold(tp, ip);
  3683. /*
  3684. * issue the bunmapi() call to free the blocks
  3685. */
  3686. XFS_BMAP_INIT(&free_list, &firstfsb);
  3687. error = xfs_bunmapi(tp, ip, startoffset_fsb,
  3688. endoffset_fsb - startoffset_fsb,
  3689. 0, 2, &firstfsb, &free_list, NULL, &done);
  3690. if (error) {
  3691. goto error0;
  3692. }
  3693. /*
  3694. * complete the transaction
  3695. */
  3696. error = xfs_bmap_finish(&tp, &free_list, &committed);
  3697. if (error) {
  3698. goto error0;
  3699. }
  3700. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  3701. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  3702. }
  3703. out_unlock_iolock:
  3704. if (need_iolock)
  3705. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  3706. return error;
  3707. error0:
  3708. xfs_bmap_cancel(&free_list);
  3709. error1:
  3710. xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
  3711. xfs_iunlock(ip, need_iolock ? (XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL) :
  3712. XFS_ILOCK_EXCL);
  3713. return error;
  3714. }
  3715. /*
  3716. * xfs_change_file_space()
  3717. * This routine allocates or frees disk space for the given file.
  3718. * The user specified parameters are checked for alignment and size
  3719. * limitations.
  3720. *
  3721. * RETURNS:
  3722. * 0 on success
  3723. * errno on error
  3724. *
  3725. */
  3726. int
  3727. xfs_change_file_space(
  3728. xfs_inode_t *ip,
  3729. int cmd,
  3730. xfs_flock64_t *bf,
  3731. xfs_off_t offset,
  3732. cred_t *credp,
  3733. int attr_flags)
  3734. {
  3735. xfs_mount_t *mp = ip->i_mount;
  3736. int clrprealloc;
  3737. int error;
  3738. xfs_fsize_t fsize;
  3739. int setprealloc;
  3740. xfs_off_t startoffset;
  3741. xfs_off_t llen;
  3742. xfs_trans_t *tp;
  3743. bhv_vattr_t va;
  3744. xfs_itrace_entry(ip);
  3745. if (!S_ISREG(ip->i_d.di_mode))
  3746. return XFS_ERROR(EINVAL);
  3747. switch (bf->l_whence) {
  3748. case 0: /*SEEK_SET*/
  3749. break;
  3750. case 1: /*SEEK_CUR*/
  3751. bf->l_start += offset;
  3752. break;
  3753. case 2: /*SEEK_END*/
  3754. bf->l_start += ip->i_size;
  3755. break;
  3756. default:
  3757. return XFS_ERROR(EINVAL);
  3758. }
  3759. llen = bf->l_len > 0 ? bf->l_len - 1 : bf->l_len;
  3760. if ( (bf->l_start < 0)
  3761. || (bf->l_start > XFS_MAXIOFFSET(mp))
  3762. || (bf->l_start + llen < 0)
  3763. || (bf->l_start + llen > XFS_MAXIOFFSET(mp)))
  3764. return XFS_ERROR(EINVAL);
  3765. bf->l_whence = 0;
  3766. startoffset = bf->l_start;
  3767. fsize = ip->i_size;
  3768. /*
  3769. * XFS_IOC_RESVSP and XFS_IOC_UNRESVSP will reserve or unreserve
  3770. * file space.
  3771. * These calls do NOT zero the data space allocated to the file,
  3772. * nor do they change the file size.
  3773. *
  3774. * XFS_IOC_ALLOCSP and XFS_IOC_FREESP will allocate and free file
  3775. * space.
  3776. * These calls cause the new file data to be zeroed and the file
  3777. * size to be changed.
  3778. */
  3779. setprealloc = clrprealloc = 0;
  3780. switch (cmd) {
  3781. case XFS_IOC_RESVSP:
  3782. case XFS_IOC_RESVSP64:
  3783. error = xfs_alloc_file_space(ip, startoffset, bf->l_len,
  3784. 1, attr_flags);
  3785. if (error)
  3786. return error;
  3787. setprealloc = 1;
  3788. break;
  3789. case XFS_IOC_UNRESVSP:
  3790. case XFS_IOC_UNRESVSP64:
  3791. if ((error = xfs_free_file_space(ip, startoffset, bf->l_len,
  3792. attr_flags)))
  3793. return error;
  3794. break;
  3795. case XFS_IOC_ALLOCSP:
  3796. case XFS_IOC_ALLOCSP64:
  3797. case XFS_IOC_FREESP:
  3798. case XFS_IOC_FREESP64:
  3799. if (startoffset > fsize) {
  3800. error = xfs_alloc_file_space(ip, fsize,
  3801. startoffset - fsize, 0, attr_flags);
  3802. if (error)
  3803. break;
  3804. }
  3805. va.va_mask = XFS_AT_SIZE;
  3806. va.va_size = startoffset;
  3807. error = xfs_setattr(ip, &va, attr_flags, credp);
  3808. if (error)
  3809. return error;
  3810. clrprealloc = 1;
  3811. break;
  3812. default:
  3813. ASSERT(0);
  3814. return XFS_ERROR(EINVAL);
  3815. }
  3816. /*
  3817. * update the inode timestamp, mode, and prealloc flag bits
  3818. */
  3819. tp = xfs_trans_alloc(mp, XFS_TRANS_WRITEID);
  3820. if ((error = xfs_trans_reserve(tp, 0, XFS_WRITEID_LOG_RES(mp),
  3821. 0, 0, 0))) {
  3822. /* ASSERT(0); */
  3823. xfs_trans_cancel(tp, 0);
  3824. return error;
  3825. }
  3826. xfs_ilock(ip, XFS_ILOCK_EXCL);
  3827. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  3828. xfs_trans_ihold(tp, ip);
  3829. if ((attr_flags & ATTR_DMI) == 0) {
  3830. ip->i_d.di_mode &= ~S_ISUID;
  3831. /*
  3832. * Note that we don't have to worry about mandatory
  3833. * file locking being disabled here because we only
  3834. * clear the S_ISGID bit if the Group execute bit is
  3835. * on, but if it was on then mandatory locking wouldn't
  3836. * have been enabled.
  3837. */
  3838. if (ip->i_d.di_mode & S_IXGRP)
  3839. ip->i_d.di_mode &= ~S_ISGID;
  3840. xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  3841. }
  3842. if (setprealloc)
  3843. ip->i_d.di_flags |= XFS_DIFLAG_PREALLOC;
  3844. else if (clrprealloc)
  3845. ip->i_d.di_flags &= ~XFS_DIFLAG_PREALLOC;
  3846. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  3847. xfs_trans_set_sync(tp);
  3848. error = xfs_trans_commit(tp, 0);
  3849. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  3850. return error;
  3851. }