xfs_vnodeops.c 115 KB

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