xfs_vnodeops.c 115 KB

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