xfs_vnodeops.c 113 KB

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