xfs_vnodeops.c 117 KB

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