xfs_vnodeops.c 105 KB

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