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