xfs_vnodeops.c 115 KB

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