xfs_vnodeops.c 112 KB

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