xfs_vnodeops.c 115 KB

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