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