xfs_vnodeops.c 105 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237
  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. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  1327. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1328. if (error)
  1329. goto error_unlock;
  1330. error = xfs_attr_inactive(ip);
  1331. if (error)
  1332. goto error_unlock;
  1333. tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
  1334. error = xfs_trans_reserve(tp, 0,
  1335. XFS_IFREE_LOG_RES(mp),
  1336. 0, XFS_TRANS_PERM_LOG_RES,
  1337. XFS_INACTIVE_LOG_COUNT);
  1338. if (error)
  1339. goto error_cancel;
  1340. xfs_ilock(ip, XFS_ILOCK_EXCL);
  1341. xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
  1342. xfs_trans_ihold(tp, ip);
  1343. xfs_idestroy_fork(ip, XFS_ATTR_FORK);
  1344. ASSERT(ip->i_d.di_anextents == 0);
  1345. *tpp = tp;
  1346. return 0;
  1347. error_cancel:
  1348. ASSERT(XFS_FORCED_SHUTDOWN(mp));
  1349. xfs_trans_cancel(tp, 0);
  1350. error_unlock:
  1351. *tpp = NULL;
  1352. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  1353. return error;
  1354. }
  1355. int
  1356. xfs_release(
  1357. xfs_inode_t *ip)
  1358. {
  1359. bhv_vnode_t *vp = XFS_ITOV(ip);
  1360. xfs_mount_t *mp = ip->i_mount;
  1361. int error;
  1362. if (!VN_ISREG(vp) || (ip->i_d.di_mode == 0))
  1363. return 0;
  1364. /* If this is a read-only mount, don't do this (would generate I/O) */
  1365. if (mp->m_flags & XFS_MOUNT_RDONLY)
  1366. return 0;
  1367. if (!XFS_FORCED_SHUTDOWN(mp)) {
  1368. int truncated;
  1369. /*
  1370. * If we are using filestreams, and we have an unlinked
  1371. * file that we are processing the last close on, then nothing
  1372. * will be able to reopen and write to this file. Purge this
  1373. * inode from the filestreams cache so that it doesn't delay
  1374. * teardown of the inode.
  1375. */
  1376. if ((ip->i_d.di_nlink == 0) && xfs_inode_is_filestream(ip))
  1377. xfs_filestream_deassociate(ip);
  1378. /*
  1379. * If we previously truncated this file and removed old data
  1380. * in the process, we want to initiate "early" writeout on
  1381. * the last close. This is an attempt to combat the notorious
  1382. * NULL files problem which is particularly noticable from a
  1383. * truncate down, buffered (re-)write (delalloc), followed by
  1384. * a crash. What we are effectively doing here is
  1385. * significantly reducing the time window where we'd otherwise
  1386. * be exposed to that problem.
  1387. */
  1388. truncated = xfs_iflags_test_and_clear(ip, XFS_ITRUNCATED);
  1389. if (truncated && VN_DIRTY(vp) && ip->i_delayed_blks > 0)
  1390. xfs_flush_pages(ip, 0, -1, XFS_B_ASYNC, FI_NONE);
  1391. }
  1392. if (ip->i_d.di_nlink != 0) {
  1393. if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
  1394. ((ip->i_size > 0) || (VN_CACHED(vp) > 0 ||
  1395. ip->i_delayed_blks > 0)) &&
  1396. (ip->i_df.if_flags & XFS_IFEXTENTS)) &&
  1397. (!(ip->i_d.di_flags &
  1398. (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)))) {
  1399. error = xfs_free_eofblocks(mp, ip, XFS_FREE_EOF_LOCK);
  1400. if (error)
  1401. return error;
  1402. }
  1403. }
  1404. return 0;
  1405. }
  1406. /*
  1407. * xfs_inactive
  1408. *
  1409. * This is called when the vnode reference count for the vnode
  1410. * goes to zero. If the file has been unlinked, then it must
  1411. * now be truncated. Also, we clear all of the read-ahead state
  1412. * kept for the inode here since the file is now closed.
  1413. */
  1414. int
  1415. xfs_inactive(
  1416. xfs_inode_t *ip)
  1417. {
  1418. bhv_vnode_t *vp = XFS_ITOV(ip);
  1419. xfs_bmap_free_t free_list;
  1420. xfs_fsblock_t first_block;
  1421. int committed;
  1422. xfs_trans_t *tp;
  1423. xfs_mount_t *mp;
  1424. int error;
  1425. int truncate;
  1426. xfs_itrace_entry(ip);
  1427. /*
  1428. * If the inode is already free, then there can be nothing
  1429. * to clean up here.
  1430. */
  1431. if (ip->i_d.di_mode == 0 || VN_BAD(vp)) {
  1432. ASSERT(ip->i_df.if_real_bytes == 0);
  1433. ASSERT(ip->i_df.if_broot_bytes == 0);
  1434. return VN_INACTIVE_CACHE;
  1435. }
  1436. /*
  1437. * Only do a truncate if it's a regular file with
  1438. * some actual space in it. It's OK to look at the
  1439. * inode's fields without the lock because we're the
  1440. * only one with a reference to the inode.
  1441. */
  1442. truncate = ((ip->i_d.di_nlink == 0) &&
  1443. ((ip->i_d.di_size != 0) || (ip->i_size != 0) ||
  1444. (ip->i_d.di_nextents > 0) || (ip->i_delayed_blks > 0)) &&
  1445. ((ip->i_d.di_mode & S_IFMT) == S_IFREG));
  1446. mp = ip->i_mount;
  1447. if (ip->i_d.di_nlink == 0 && DM_EVENT_ENABLED(ip, DM_EVENT_DESTROY))
  1448. XFS_SEND_DESTROY(mp, ip, DM_RIGHT_NULL);
  1449. error = 0;
  1450. /* If this is a read-only mount, don't do this (would generate I/O) */
  1451. if (mp->m_flags & XFS_MOUNT_RDONLY)
  1452. goto out;
  1453. if (ip->i_d.di_nlink != 0) {
  1454. if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
  1455. ((ip->i_size > 0) || (VN_CACHED(vp) > 0 ||
  1456. ip->i_delayed_blks > 0)) &&
  1457. (ip->i_df.if_flags & XFS_IFEXTENTS) &&
  1458. (!(ip->i_d.di_flags &
  1459. (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)) ||
  1460. (ip->i_delayed_blks != 0)))) {
  1461. error = xfs_free_eofblocks(mp, ip, XFS_FREE_EOF_LOCK);
  1462. if (error)
  1463. return VN_INACTIVE_CACHE;
  1464. }
  1465. goto out;
  1466. }
  1467. ASSERT(ip->i_d.di_nlink == 0);
  1468. if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
  1469. return VN_INACTIVE_CACHE;
  1470. tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
  1471. if (truncate) {
  1472. /*
  1473. * Do the xfs_itruncate_start() call before
  1474. * reserving any log space because itruncate_start
  1475. * will call into the buffer cache and we can't
  1476. * do that within a transaction.
  1477. */
  1478. xfs_ilock(ip, XFS_IOLOCK_EXCL);
  1479. error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE, 0);
  1480. if (error) {
  1481. xfs_trans_cancel(tp, 0);
  1482. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  1483. return VN_INACTIVE_CACHE;
  1484. }
  1485. error = xfs_trans_reserve(tp, 0,
  1486. XFS_ITRUNCATE_LOG_RES(mp),
  1487. 0, XFS_TRANS_PERM_LOG_RES,
  1488. XFS_ITRUNCATE_LOG_COUNT);
  1489. if (error) {
  1490. /* Don't call itruncate_cleanup */
  1491. ASSERT(XFS_FORCED_SHUTDOWN(mp));
  1492. xfs_trans_cancel(tp, 0);
  1493. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  1494. return VN_INACTIVE_CACHE;
  1495. }
  1496. xfs_ilock(ip, XFS_ILOCK_EXCL);
  1497. xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
  1498. xfs_trans_ihold(tp, ip);
  1499. /*
  1500. * normally, we have to run xfs_itruncate_finish sync.
  1501. * But if filesystem is wsync and we're in the inactive
  1502. * path, then we know that nlink == 0, and that the
  1503. * xaction that made nlink == 0 is permanently committed
  1504. * since xfs_remove runs as a synchronous transaction.
  1505. */
  1506. error = xfs_itruncate_finish(&tp, ip, 0, XFS_DATA_FORK,
  1507. (!(mp->m_flags & XFS_MOUNT_WSYNC) ? 1 : 0));
  1508. if (error) {
  1509. xfs_trans_cancel(tp,
  1510. XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
  1511. xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
  1512. return VN_INACTIVE_CACHE;
  1513. }
  1514. } else if ((ip->i_d.di_mode & S_IFMT) == S_IFLNK) {
  1515. /*
  1516. * If we get an error while cleaning up a
  1517. * symlink we bail out.
  1518. */
  1519. error = (ip->i_d.di_size > XFS_IFORK_DSIZE(ip)) ?
  1520. xfs_inactive_symlink_rmt(ip, &tp) :
  1521. xfs_inactive_symlink_local(ip, &tp);
  1522. if (error) {
  1523. ASSERT(tp == NULL);
  1524. return VN_INACTIVE_CACHE;
  1525. }
  1526. xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
  1527. xfs_trans_ihold(tp, ip);
  1528. } else {
  1529. error = xfs_trans_reserve(tp, 0,
  1530. XFS_IFREE_LOG_RES(mp),
  1531. 0, XFS_TRANS_PERM_LOG_RES,
  1532. XFS_INACTIVE_LOG_COUNT);
  1533. if (error) {
  1534. ASSERT(XFS_FORCED_SHUTDOWN(mp));
  1535. xfs_trans_cancel(tp, 0);
  1536. return VN_INACTIVE_CACHE;
  1537. }
  1538. xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
  1539. xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
  1540. xfs_trans_ihold(tp, ip);
  1541. }
  1542. /*
  1543. * If there are attributes associated with the file
  1544. * then blow them away now. The code calls a routine
  1545. * that recursively deconstructs the attribute fork.
  1546. * We need to just commit the current transaction
  1547. * because we can't use it for xfs_attr_inactive().
  1548. */
  1549. if (ip->i_d.di_anextents > 0) {
  1550. error = xfs_inactive_attrs(ip, &tp);
  1551. /*
  1552. * If we got an error, the transaction is already
  1553. * cancelled, and the inode is unlocked. Just get out.
  1554. */
  1555. if (error)
  1556. return VN_INACTIVE_CACHE;
  1557. } else if (ip->i_afp) {
  1558. xfs_idestroy_fork(ip, XFS_ATTR_FORK);
  1559. }
  1560. /*
  1561. * Free the inode.
  1562. */
  1563. XFS_BMAP_INIT(&free_list, &first_block);
  1564. error = xfs_ifree(tp, ip, &free_list);
  1565. if (error) {
  1566. /*
  1567. * If we fail to free the inode, shut down. The cancel
  1568. * might do that, we need to make sure. Otherwise the
  1569. * inode might be lost for a long time or forever.
  1570. */
  1571. if (!XFS_FORCED_SHUTDOWN(mp)) {
  1572. cmn_err(CE_NOTE,
  1573. "xfs_inactive: xfs_ifree() returned an error = %d on %s",
  1574. error, mp->m_fsname);
  1575. xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
  1576. }
  1577. xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
  1578. } else {
  1579. /*
  1580. * Credit the quota account(s). The inode is gone.
  1581. */
  1582. XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, XFS_TRANS_DQ_ICOUNT, -1);
  1583. /*
  1584. * Just ignore errors at this point. There is nothing we can
  1585. * do except to try to keep going. Make sure it's not a silent
  1586. * error.
  1587. */
  1588. error = xfs_bmap_finish(&tp, &free_list, &committed);
  1589. if (error)
  1590. xfs_fs_cmn_err(CE_NOTE, mp, "xfs_inactive: "
  1591. "xfs_bmap_finish() returned error %d", error);
  1592. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  1593. if (error)
  1594. xfs_fs_cmn_err(CE_NOTE, mp, "xfs_inactive: "
  1595. "xfs_trans_commit() returned error %d", error);
  1596. }
  1597. /*
  1598. * Release the dquots held by inode, if any.
  1599. */
  1600. XFS_QM_DQDETACH(mp, ip);
  1601. xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
  1602. out:
  1603. return VN_INACTIVE_CACHE;
  1604. }
  1605. int
  1606. xfs_lookup(
  1607. xfs_inode_t *dp,
  1608. struct xfs_name *name,
  1609. xfs_inode_t **ipp)
  1610. {
  1611. xfs_inode_t *ip;
  1612. xfs_ino_t e_inum;
  1613. int error;
  1614. uint lock_mode;
  1615. xfs_itrace_entry(dp);
  1616. if (XFS_FORCED_SHUTDOWN(dp->i_mount))
  1617. return XFS_ERROR(EIO);
  1618. lock_mode = xfs_ilock_map_shared(dp);
  1619. error = xfs_dir_lookup_int(dp, lock_mode, name, &e_inum, &ip);
  1620. if (!error) {
  1621. *ipp = ip;
  1622. xfs_itrace_ref(ip);
  1623. }
  1624. xfs_iunlock_map_shared(dp, lock_mode);
  1625. return error;
  1626. }
  1627. int
  1628. xfs_create(
  1629. xfs_inode_t *dp,
  1630. struct xfs_name *name,
  1631. mode_t mode,
  1632. xfs_dev_t rdev,
  1633. xfs_inode_t **ipp,
  1634. cred_t *credp)
  1635. {
  1636. xfs_mount_t *mp = dp->i_mount;
  1637. xfs_inode_t *ip;
  1638. xfs_trans_t *tp;
  1639. int error;
  1640. xfs_bmap_free_t free_list;
  1641. xfs_fsblock_t first_block;
  1642. boolean_t unlock_dp_on_error = B_FALSE;
  1643. int dm_event_sent = 0;
  1644. uint cancel_flags;
  1645. int committed;
  1646. xfs_prid_t prid;
  1647. struct xfs_dquot *udqp, *gdqp;
  1648. uint resblks;
  1649. ASSERT(!*ipp);
  1650. xfs_itrace_entry(dp);
  1651. if (DM_EVENT_ENABLED(dp, DM_EVENT_CREATE)) {
  1652. error = XFS_SEND_NAMESP(mp, DM_EVENT_CREATE,
  1653. dp, DM_RIGHT_NULL, NULL,
  1654. DM_RIGHT_NULL, name->name, NULL,
  1655. mode, 0, 0);
  1656. if (error)
  1657. return error;
  1658. dm_event_sent = 1;
  1659. }
  1660. if (XFS_FORCED_SHUTDOWN(mp))
  1661. return XFS_ERROR(EIO);
  1662. /* Return through std_return after this point. */
  1663. udqp = gdqp = NULL;
  1664. if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
  1665. prid = dp->i_d.di_projid;
  1666. else
  1667. prid = (xfs_prid_t)dfltprid;
  1668. /*
  1669. * Make sure that we have allocated dquot(s) on disk.
  1670. */
  1671. error = XFS_QM_DQVOPALLOC(mp, dp,
  1672. current_fsuid(credp), current_fsgid(credp), prid,
  1673. XFS_QMOPT_QUOTALL|XFS_QMOPT_INHERIT, &udqp, &gdqp);
  1674. if (error)
  1675. goto std_return;
  1676. ip = NULL;
  1677. tp = xfs_trans_alloc(mp, XFS_TRANS_CREATE);
  1678. cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
  1679. resblks = XFS_CREATE_SPACE_RES(mp, name->len);
  1680. /*
  1681. * Initially assume that the file does not exist and
  1682. * reserve the resources for that case. If that is not
  1683. * the case we'll drop the one we have and get a more
  1684. * appropriate transaction later.
  1685. */
  1686. error = xfs_trans_reserve(tp, resblks, XFS_CREATE_LOG_RES(mp), 0,
  1687. XFS_TRANS_PERM_LOG_RES, XFS_CREATE_LOG_COUNT);
  1688. if (error == ENOSPC) {
  1689. resblks = 0;
  1690. error = xfs_trans_reserve(tp, 0, XFS_CREATE_LOG_RES(mp), 0,
  1691. XFS_TRANS_PERM_LOG_RES, XFS_CREATE_LOG_COUNT);
  1692. }
  1693. if (error) {
  1694. cancel_flags = 0;
  1695. goto error_return;
  1696. }
  1697. xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
  1698. unlock_dp_on_error = B_TRUE;
  1699. XFS_BMAP_INIT(&free_list, &first_block);
  1700. ASSERT(ip == NULL);
  1701. /*
  1702. * Reserve disk quota and the inode.
  1703. */
  1704. error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
  1705. if (error)
  1706. goto error_return;
  1707. error = xfs_dir_canenter(tp, dp, name, resblks);
  1708. if (error)
  1709. goto error_return;
  1710. error = xfs_dir_ialloc(&tp, dp, mode, 1,
  1711. rdev, credp, prid, resblks > 0,
  1712. &ip, &committed);
  1713. if (error) {
  1714. if (error == ENOSPC)
  1715. goto error_return;
  1716. goto abort_return;
  1717. }
  1718. xfs_itrace_ref(ip);
  1719. /*
  1720. * At this point, we've gotten a newly allocated inode.
  1721. * It is locked (and joined to the transaction).
  1722. */
  1723. ASSERT(ismrlocked (&ip->i_lock, MR_UPDATE));
  1724. /*
  1725. * Now we join the directory inode to the transaction. We do not do it
  1726. * earlier because xfs_dir_ialloc might commit the previous transaction
  1727. * (and release all the locks). An error from here on will result in
  1728. * the transaction cancel unlocking dp so don't do it explicitly in the
  1729. * error path.
  1730. */
  1731. IHOLD(dp);
  1732. xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
  1733. unlock_dp_on_error = B_FALSE;
  1734. error = xfs_dir_createname(tp, dp, name, ip->i_ino,
  1735. &first_block, &free_list, resblks ?
  1736. resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
  1737. if (error) {
  1738. ASSERT(error != ENOSPC);
  1739. goto abort_return;
  1740. }
  1741. xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  1742. xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
  1743. /*
  1744. * If this is a synchronous mount, make sure that the
  1745. * create transaction goes to disk before returning to
  1746. * the user.
  1747. */
  1748. if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
  1749. xfs_trans_set_sync(tp);
  1750. }
  1751. dp->i_gen++;
  1752. /*
  1753. * Attach the dquot(s) to the inodes and modify them incore.
  1754. * These ids of the inode couldn't have changed since the new
  1755. * inode has been locked ever since it was created.
  1756. */
  1757. XFS_QM_DQVOPCREATE(mp, tp, ip, udqp, gdqp);
  1758. /*
  1759. * xfs_trans_commit normally decrements the vnode ref count
  1760. * when it unlocks the inode. Since we want to return the
  1761. * vnode to the caller, we bump the vnode ref count now.
  1762. */
  1763. IHOLD(ip);
  1764. error = xfs_bmap_finish(&tp, &free_list, &committed);
  1765. if (error) {
  1766. xfs_bmap_cancel(&free_list);
  1767. goto abort_rele;
  1768. }
  1769. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  1770. if (error) {
  1771. IRELE(ip);
  1772. tp = NULL;
  1773. goto error_return;
  1774. }
  1775. XFS_QM_DQRELE(mp, udqp);
  1776. XFS_QM_DQRELE(mp, gdqp);
  1777. *ipp = ip;
  1778. /* Fallthrough to std_return with error = 0 */
  1779. std_return:
  1780. if ((*ipp || (error != 0 && dm_event_sent != 0)) &&
  1781. DM_EVENT_ENABLED(dp, DM_EVENT_POSTCREATE)) {
  1782. (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTCREATE,
  1783. dp, DM_RIGHT_NULL,
  1784. *ipp ? ip : NULL,
  1785. DM_RIGHT_NULL, name->name, NULL,
  1786. mode, error, 0);
  1787. }
  1788. return error;
  1789. abort_return:
  1790. cancel_flags |= XFS_TRANS_ABORT;
  1791. /* FALLTHROUGH */
  1792. error_return:
  1793. if (tp != NULL)
  1794. xfs_trans_cancel(tp, cancel_flags);
  1795. XFS_QM_DQRELE(mp, udqp);
  1796. XFS_QM_DQRELE(mp, gdqp);
  1797. if (unlock_dp_on_error)
  1798. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  1799. goto std_return;
  1800. abort_rele:
  1801. /*
  1802. * Wait until after the current transaction is aborted to
  1803. * release the inode. This prevents recursive transactions
  1804. * and deadlocks from xfs_inactive.
  1805. */
  1806. cancel_flags |= XFS_TRANS_ABORT;
  1807. xfs_trans_cancel(tp, cancel_flags);
  1808. IRELE(ip);
  1809. XFS_QM_DQRELE(mp, udqp);
  1810. XFS_QM_DQRELE(mp, gdqp);
  1811. goto std_return;
  1812. }
  1813. #ifdef DEBUG
  1814. /*
  1815. * Some counters to see if (and how often) we are hitting some deadlock
  1816. * prevention code paths.
  1817. */
  1818. int xfs_rm_locks;
  1819. int xfs_rm_lock_delays;
  1820. int xfs_rm_attempts;
  1821. #endif
  1822. /*
  1823. * The following routine will lock the inodes associated with the
  1824. * directory and the named entry in the directory. The locks are
  1825. * acquired in increasing inode number.
  1826. *
  1827. * If the entry is "..", then only the directory is locked. The
  1828. * vnode ref count will still include that from the .. entry in
  1829. * this case.
  1830. *
  1831. * There is a deadlock we need to worry about. If the locked directory is
  1832. * in the AIL, it might be blocking up the log. The next inode we lock
  1833. * could be already locked by another thread waiting for log space (e.g
  1834. * a permanent log reservation with a long running transaction (see
  1835. * xfs_itruncate_finish)). To solve this, we must check if the directory
  1836. * is in the ail and use lock_nowait. If we can't lock, we need to
  1837. * drop the inode lock on the directory and try again. xfs_iunlock will
  1838. * potentially push the tail if we were holding up the log.
  1839. */
  1840. STATIC int
  1841. xfs_lock_dir_and_entry(
  1842. xfs_inode_t *dp,
  1843. xfs_inode_t *ip) /* inode of entry 'name' */
  1844. {
  1845. int attempts;
  1846. xfs_ino_t e_inum;
  1847. xfs_inode_t *ips[2];
  1848. xfs_log_item_t *lp;
  1849. #ifdef DEBUG
  1850. xfs_rm_locks++;
  1851. #endif
  1852. attempts = 0;
  1853. again:
  1854. xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
  1855. e_inum = ip->i_ino;
  1856. xfs_itrace_ref(ip);
  1857. /*
  1858. * We want to lock in increasing inum. Since we've already
  1859. * acquired the lock on the directory, we may need to release
  1860. * if if the inum of the entry turns out to be less.
  1861. */
  1862. if (e_inum > dp->i_ino) {
  1863. /*
  1864. * We are already in the right order, so just
  1865. * lock on the inode of the entry.
  1866. * We need to use nowait if dp is in the AIL.
  1867. */
  1868. lp = (xfs_log_item_t *)dp->i_itemp;
  1869. if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
  1870. if (!xfs_ilock_nowait(ip, XFS_ILOCK_EXCL)) {
  1871. attempts++;
  1872. #ifdef DEBUG
  1873. xfs_rm_attempts++;
  1874. #endif
  1875. /*
  1876. * Unlock dp and try again.
  1877. * xfs_iunlock will try to push the tail
  1878. * if the inode is in the AIL.
  1879. */
  1880. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  1881. if ((attempts % 5) == 0) {
  1882. delay(1); /* Don't just spin the CPU */
  1883. #ifdef DEBUG
  1884. xfs_rm_lock_delays++;
  1885. #endif
  1886. }
  1887. goto again;
  1888. }
  1889. } else {
  1890. xfs_ilock(ip, XFS_ILOCK_EXCL);
  1891. }
  1892. } else if (e_inum < dp->i_ino) {
  1893. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  1894. ips[0] = ip;
  1895. ips[1] = dp;
  1896. xfs_lock_inodes(ips, 2, 0, XFS_ILOCK_EXCL);
  1897. }
  1898. /* else e_inum == dp->i_ino */
  1899. /* This can happen if we're asked to lock /x/..
  1900. * the entry is "..", which is also the parent directory.
  1901. */
  1902. return 0;
  1903. }
  1904. #ifdef DEBUG
  1905. int xfs_locked_n;
  1906. int xfs_small_retries;
  1907. int xfs_middle_retries;
  1908. int xfs_lots_retries;
  1909. int xfs_lock_delays;
  1910. #endif
  1911. /*
  1912. * Bump the subclass so xfs_lock_inodes() acquires each lock with
  1913. * a different value
  1914. */
  1915. static inline int
  1916. xfs_lock_inumorder(int lock_mode, int subclass)
  1917. {
  1918. if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL))
  1919. lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_IOLOCK_SHIFT;
  1920. if (lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL))
  1921. lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_ILOCK_SHIFT;
  1922. return lock_mode;
  1923. }
  1924. /*
  1925. * The following routine will lock n inodes in exclusive mode.
  1926. * We assume the caller calls us with the inodes in i_ino order.
  1927. *
  1928. * We need to detect deadlock where an inode that we lock
  1929. * is in the AIL and we start waiting for another inode that is locked
  1930. * by a thread in a long running transaction (such as truncate). This can
  1931. * result in deadlock since the long running trans might need to wait
  1932. * for the inode we just locked in order to push the tail and free space
  1933. * in the log.
  1934. */
  1935. void
  1936. xfs_lock_inodes(
  1937. xfs_inode_t **ips,
  1938. int inodes,
  1939. int first_locked,
  1940. uint lock_mode)
  1941. {
  1942. int attempts = 0, i, j, try_lock;
  1943. xfs_log_item_t *lp;
  1944. ASSERT(ips && (inodes >= 2)); /* we need at least two */
  1945. if (first_locked) {
  1946. try_lock = 1;
  1947. i = 1;
  1948. } else {
  1949. try_lock = 0;
  1950. i = 0;
  1951. }
  1952. again:
  1953. for (; i < inodes; i++) {
  1954. ASSERT(ips[i]);
  1955. if (i && (ips[i] == ips[i-1])) /* Already locked */
  1956. continue;
  1957. /*
  1958. * If try_lock is not set yet, make sure all locked inodes
  1959. * are not in the AIL.
  1960. * If any are, set try_lock to be used later.
  1961. */
  1962. if (!try_lock) {
  1963. for (j = (i - 1); j >= 0 && !try_lock; j--) {
  1964. lp = (xfs_log_item_t *)ips[j]->i_itemp;
  1965. if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
  1966. try_lock++;
  1967. }
  1968. }
  1969. }
  1970. /*
  1971. * If any of the previous locks we have locked is in the AIL,
  1972. * we must TRY to get the second and subsequent locks. If
  1973. * we can't get any, we must release all we have
  1974. * and try again.
  1975. */
  1976. if (try_lock) {
  1977. /* try_lock must be 0 if i is 0. */
  1978. /*
  1979. * try_lock means we have an inode locked
  1980. * that is in the AIL.
  1981. */
  1982. ASSERT(i != 0);
  1983. if (!xfs_ilock_nowait(ips[i], xfs_lock_inumorder(lock_mode, i))) {
  1984. attempts++;
  1985. /*
  1986. * Unlock all previous guys and try again.
  1987. * xfs_iunlock will try to push the tail
  1988. * if the inode is in the AIL.
  1989. */
  1990. for(j = i - 1; j >= 0; j--) {
  1991. /*
  1992. * Check to see if we've already
  1993. * unlocked this one.
  1994. * Not the first one going back,
  1995. * and the inode ptr is the same.
  1996. */
  1997. if ((j != (i - 1)) && ips[j] ==
  1998. ips[j+1])
  1999. continue;
  2000. xfs_iunlock(ips[j], lock_mode);
  2001. }
  2002. if ((attempts % 5) == 0) {
  2003. delay(1); /* Don't just spin the CPU */
  2004. #ifdef DEBUG
  2005. xfs_lock_delays++;
  2006. #endif
  2007. }
  2008. i = 0;
  2009. try_lock = 0;
  2010. goto again;
  2011. }
  2012. } else {
  2013. xfs_ilock(ips[i], xfs_lock_inumorder(lock_mode, i));
  2014. }
  2015. }
  2016. #ifdef DEBUG
  2017. if (attempts) {
  2018. if (attempts < 5) xfs_small_retries++;
  2019. else if (attempts < 100) xfs_middle_retries++;
  2020. else xfs_lots_retries++;
  2021. } else {
  2022. xfs_locked_n++;
  2023. }
  2024. #endif
  2025. }
  2026. #ifdef DEBUG
  2027. #define REMOVE_DEBUG_TRACE(x) {remove_which_error_return = (x);}
  2028. int remove_which_error_return = 0;
  2029. #else /* ! DEBUG */
  2030. #define REMOVE_DEBUG_TRACE(x)
  2031. #endif /* ! DEBUG */
  2032. int
  2033. xfs_remove(
  2034. xfs_inode_t *dp,
  2035. struct xfs_name *name,
  2036. xfs_inode_t *ip)
  2037. {
  2038. xfs_mount_t *mp = dp->i_mount;
  2039. xfs_trans_t *tp = NULL;
  2040. int error = 0;
  2041. xfs_bmap_free_t free_list;
  2042. xfs_fsblock_t first_block;
  2043. int cancel_flags;
  2044. int committed;
  2045. int link_zero;
  2046. uint resblks;
  2047. xfs_itrace_entry(dp);
  2048. if (XFS_FORCED_SHUTDOWN(mp))
  2049. return XFS_ERROR(EIO);
  2050. if (DM_EVENT_ENABLED(dp, DM_EVENT_REMOVE)) {
  2051. error = XFS_SEND_NAMESP(mp, DM_EVENT_REMOVE, dp, DM_RIGHT_NULL,
  2052. NULL, DM_RIGHT_NULL, name->name, NULL,
  2053. ip->i_d.di_mode, 0, 0);
  2054. if (error)
  2055. return error;
  2056. }
  2057. /*
  2058. * We need to get a reference to ip before we get our log
  2059. * reservation. The reason for this is that we cannot call
  2060. * xfs_iget for an inode for which we do not have a reference
  2061. * once we've acquired a log reservation. This is because the
  2062. * inode we are trying to get might be in xfs_inactive going
  2063. * for a log reservation. Since we'll have to wait for the
  2064. * inactive code to complete before returning from xfs_iget,
  2065. * we need to make sure that we don't have log space reserved
  2066. * when we call xfs_iget. Instead we get an unlocked reference
  2067. * to the inode before getting our log reservation.
  2068. */
  2069. IHOLD(ip);
  2070. xfs_itrace_entry(ip);
  2071. xfs_itrace_ref(ip);
  2072. error = XFS_QM_DQATTACH(mp, dp, 0);
  2073. if (!error && dp != ip)
  2074. error = XFS_QM_DQATTACH(mp, ip, 0);
  2075. if (error) {
  2076. REMOVE_DEBUG_TRACE(__LINE__);
  2077. IRELE(ip);
  2078. goto std_return;
  2079. }
  2080. tp = xfs_trans_alloc(mp, XFS_TRANS_REMOVE);
  2081. cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
  2082. /*
  2083. * We try to get the real space reservation first,
  2084. * allowing for directory btree deletion(s) implying
  2085. * possible bmap insert(s). If we can't get the space
  2086. * reservation then we use 0 instead, and avoid the bmap
  2087. * btree insert(s) in the directory code by, if the bmap
  2088. * insert tries to happen, instead trimming the LAST
  2089. * block from the directory.
  2090. */
  2091. resblks = XFS_REMOVE_SPACE_RES(mp);
  2092. error = xfs_trans_reserve(tp, resblks, XFS_REMOVE_LOG_RES(mp), 0,
  2093. XFS_TRANS_PERM_LOG_RES, XFS_REMOVE_LOG_COUNT);
  2094. if (error == ENOSPC) {
  2095. resblks = 0;
  2096. error = xfs_trans_reserve(tp, 0, XFS_REMOVE_LOG_RES(mp), 0,
  2097. XFS_TRANS_PERM_LOG_RES, XFS_REMOVE_LOG_COUNT);
  2098. }
  2099. if (error) {
  2100. ASSERT(error != ENOSPC);
  2101. REMOVE_DEBUG_TRACE(__LINE__);
  2102. xfs_trans_cancel(tp, 0);
  2103. IRELE(ip);
  2104. return error;
  2105. }
  2106. error = xfs_lock_dir_and_entry(dp, ip);
  2107. if (error) {
  2108. REMOVE_DEBUG_TRACE(__LINE__);
  2109. xfs_trans_cancel(tp, cancel_flags);
  2110. IRELE(ip);
  2111. goto std_return;
  2112. }
  2113. /*
  2114. * At this point, we've gotten both the directory and the entry
  2115. * inodes locked.
  2116. */
  2117. xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
  2118. if (dp != ip) {
  2119. /*
  2120. * Increment vnode ref count only in this case since
  2121. * there's an extra vnode reference in the case where
  2122. * dp == ip.
  2123. */
  2124. IHOLD(dp);
  2125. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  2126. }
  2127. /*
  2128. * Entry must exist since we did a lookup in xfs_lock_dir_and_entry.
  2129. */
  2130. XFS_BMAP_INIT(&free_list, &first_block);
  2131. error = xfs_dir_removename(tp, dp, name, ip->i_ino,
  2132. &first_block, &free_list, 0);
  2133. if (error) {
  2134. ASSERT(error != ENOENT);
  2135. REMOVE_DEBUG_TRACE(__LINE__);
  2136. goto error1;
  2137. }
  2138. xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  2139. dp->i_gen++;
  2140. xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
  2141. error = xfs_droplink(tp, ip);
  2142. if (error) {
  2143. REMOVE_DEBUG_TRACE(__LINE__);
  2144. goto error1;
  2145. }
  2146. /* Determine if this is the last link while
  2147. * we are in the transaction.
  2148. */
  2149. link_zero = (ip)->i_d.di_nlink==0;
  2150. /*
  2151. * Take an extra ref on the inode so that it doesn't
  2152. * go to xfs_inactive() from within the commit.
  2153. */
  2154. IHOLD(ip);
  2155. /*
  2156. * If this is a synchronous mount, make sure that the
  2157. * remove transaction goes to disk before returning to
  2158. * the user.
  2159. */
  2160. if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
  2161. xfs_trans_set_sync(tp);
  2162. }
  2163. error = xfs_bmap_finish(&tp, &free_list, &committed);
  2164. if (error) {
  2165. REMOVE_DEBUG_TRACE(__LINE__);
  2166. goto error_rele;
  2167. }
  2168. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  2169. if (error) {
  2170. IRELE(ip);
  2171. goto std_return;
  2172. }
  2173. /*
  2174. * If we are using filestreams, kill the stream association.
  2175. * If the file is still open it may get a new one but that
  2176. * will get killed on last close in xfs_close() so we don't
  2177. * have to worry about that.
  2178. */
  2179. if (link_zero && xfs_inode_is_filestream(ip))
  2180. xfs_filestream_deassociate(ip);
  2181. xfs_itrace_exit(ip);
  2182. IRELE(ip);
  2183. /* Fall through to std_return with error = 0 */
  2184. std_return:
  2185. if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTREMOVE)) {
  2186. (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTREMOVE,
  2187. dp, DM_RIGHT_NULL,
  2188. NULL, DM_RIGHT_NULL,
  2189. name->name, NULL, ip->i_d.di_mode, error, 0);
  2190. }
  2191. return error;
  2192. error1:
  2193. xfs_bmap_cancel(&free_list);
  2194. cancel_flags |= XFS_TRANS_ABORT;
  2195. xfs_trans_cancel(tp, cancel_flags);
  2196. goto std_return;
  2197. error_rele:
  2198. /*
  2199. * In this case make sure to not release the inode until after
  2200. * the current transaction is aborted. Releasing it beforehand
  2201. * can cause us to go to xfs_inactive and start a recursive
  2202. * transaction which can easily deadlock with the current one.
  2203. */
  2204. xfs_bmap_cancel(&free_list);
  2205. cancel_flags |= XFS_TRANS_ABORT;
  2206. xfs_trans_cancel(tp, cancel_flags);
  2207. IRELE(ip);
  2208. goto std_return;
  2209. }
  2210. int
  2211. xfs_link(
  2212. xfs_inode_t *tdp,
  2213. xfs_inode_t *sip,
  2214. struct xfs_name *target_name)
  2215. {
  2216. xfs_mount_t *mp = tdp->i_mount;
  2217. xfs_trans_t *tp;
  2218. xfs_inode_t *ips[2];
  2219. int error;
  2220. xfs_bmap_free_t free_list;
  2221. xfs_fsblock_t first_block;
  2222. int cancel_flags;
  2223. int committed;
  2224. int resblks;
  2225. xfs_itrace_entry(tdp);
  2226. xfs_itrace_entry(sip);
  2227. ASSERT(!S_ISDIR(sip->i_d.di_mode));
  2228. if (XFS_FORCED_SHUTDOWN(mp))
  2229. return XFS_ERROR(EIO);
  2230. if (DM_EVENT_ENABLED(tdp, DM_EVENT_LINK)) {
  2231. error = XFS_SEND_NAMESP(mp, DM_EVENT_LINK,
  2232. tdp, DM_RIGHT_NULL,
  2233. sip, DM_RIGHT_NULL,
  2234. target_name->name, NULL, 0, 0, 0);
  2235. if (error)
  2236. return error;
  2237. }
  2238. /* Return through std_return after this point. */
  2239. error = XFS_QM_DQATTACH(mp, sip, 0);
  2240. if (!error && sip != tdp)
  2241. error = XFS_QM_DQATTACH(mp, tdp, 0);
  2242. if (error)
  2243. goto std_return;
  2244. tp = xfs_trans_alloc(mp, XFS_TRANS_LINK);
  2245. cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
  2246. resblks = XFS_LINK_SPACE_RES(mp, target_name->len);
  2247. error = xfs_trans_reserve(tp, resblks, XFS_LINK_LOG_RES(mp), 0,
  2248. XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
  2249. if (error == ENOSPC) {
  2250. resblks = 0;
  2251. error = xfs_trans_reserve(tp, 0, XFS_LINK_LOG_RES(mp), 0,
  2252. XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
  2253. }
  2254. if (error) {
  2255. cancel_flags = 0;
  2256. goto error_return;
  2257. }
  2258. if (sip->i_ino < tdp->i_ino) {
  2259. ips[0] = sip;
  2260. ips[1] = tdp;
  2261. } else {
  2262. ips[0] = tdp;
  2263. ips[1] = sip;
  2264. }
  2265. xfs_lock_inodes(ips, 2, 0, XFS_ILOCK_EXCL);
  2266. /*
  2267. * Increment vnode ref counts since xfs_trans_commit &
  2268. * xfs_trans_cancel will both unlock the inodes and
  2269. * decrement the associated ref counts.
  2270. */
  2271. IHOLD(sip);
  2272. IHOLD(tdp);
  2273. xfs_trans_ijoin(tp, sip, XFS_ILOCK_EXCL);
  2274. xfs_trans_ijoin(tp, tdp, XFS_ILOCK_EXCL);
  2275. /*
  2276. * If the source has too many links, we can't make any more to it.
  2277. */
  2278. if (sip->i_d.di_nlink >= XFS_MAXLINK) {
  2279. error = XFS_ERROR(EMLINK);
  2280. goto error_return;
  2281. }
  2282. /*
  2283. * If we are using project inheritance, we only allow hard link
  2284. * creation in our tree when the project IDs are the same; else
  2285. * the tree quota mechanism could be circumvented.
  2286. */
  2287. if (unlikely((tdp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
  2288. (tdp->i_d.di_projid != sip->i_d.di_projid))) {
  2289. error = XFS_ERROR(EXDEV);
  2290. goto error_return;
  2291. }
  2292. error = xfs_dir_canenter(tp, tdp, target_name, resblks);
  2293. if (error)
  2294. goto error_return;
  2295. XFS_BMAP_INIT(&free_list, &first_block);
  2296. error = xfs_dir_createname(tp, tdp, target_name, sip->i_ino,
  2297. &first_block, &free_list, 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->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. struct xfs_name *dir_name,
  2342. mode_t mode,
  2343. xfs_inode_t **ipp,
  2344. cred_t *credp)
  2345. {
  2346. xfs_mount_t *mp = dp->i_mount;
  2347. xfs_inode_t *cdp; /* inode of created dir */
  2348. xfs_trans_t *tp;
  2349. int cancel_flags;
  2350. int error;
  2351. int committed;
  2352. xfs_bmap_free_t free_list;
  2353. xfs_fsblock_t first_block;
  2354. boolean_t unlock_dp_on_error = B_FALSE;
  2355. boolean_t created = B_FALSE;
  2356. int dm_event_sent = 0;
  2357. xfs_prid_t prid;
  2358. struct xfs_dquot *udqp, *gdqp;
  2359. uint resblks;
  2360. if (XFS_FORCED_SHUTDOWN(mp))
  2361. return XFS_ERROR(EIO);
  2362. tp = NULL;
  2363. if (DM_EVENT_ENABLED(dp, DM_EVENT_CREATE)) {
  2364. error = XFS_SEND_NAMESP(mp, DM_EVENT_CREATE,
  2365. dp, DM_RIGHT_NULL, NULL,
  2366. DM_RIGHT_NULL, dir_name->name, NULL,
  2367. mode, 0, 0);
  2368. if (error)
  2369. return error;
  2370. dm_event_sent = 1;
  2371. }
  2372. /* Return through std_return after this point. */
  2373. xfs_itrace_entry(dp);
  2374. mp = dp->i_mount;
  2375. udqp = gdqp = NULL;
  2376. if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
  2377. prid = dp->i_d.di_projid;
  2378. else
  2379. prid = (xfs_prid_t)dfltprid;
  2380. /*
  2381. * Make sure that we have allocated dquot(s) on disk.
  2382. */
  2383. error = XFS_QM_DQVOPALLOC(mp, dp,
  2384. current_fsuid(credp), current_fsgid(credp), prid,
  2385. XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
  2386. if (error)
  2387. goto std_return;
  2388. tp = xfs_trans_alloc(mp, XFS_TRANS_MKDIR);
  2389. cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
  2390. resblks = XFS_MKDIR_SPACE_RES(mp, dir_name->len);
  2391. error = xfs_trans_reserve(tp, resblks, XFS_MKDIR_LOG_RES(mp), 0,
  2392. XFS_TRANS_PERM_LOG_RES, XFS_MKDIR_LOG_COUNT);
  2393. if (error == ENOSPC) {
  2394. resblks = 0;
  2395. error = xfs_trans_reserve(tp, 0, XFS_MKDIR_LOG_RES(mp), 0,
  2396. XFS_TRANS_PERM_LOG_RES,
  2397. XFS_MKDIR_LOG_COUNT);
  2398. }
  2399. if (error) {
  2400. cancel_flags = 0;
  2401. goto error_return;
  2402. }
  2403. xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
  2404. unlock_dp_on_error = B_TRUE;
  2405. /*
  2406. * Check for directory link count overflow.
  2407. */
  2408. if (dp->i_d.di_nlink >= XFS_MAXLINK) {
  2409. error = XFS_ERROR(EMLINK);
  2410. goto error_return;
  2411. }
  2412. /*
  2413. * Reserve disk quota and the inode.
  2414. */
  2415. error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
  2416. if (error)
  2417. goto error_return;
  2418. error = xfs_dir_canenter(tp, dp, dir_name, resblks);
  2419. if (error)
  2420. goto error_return;
  2421. /*
  2422. * create the directory inode.
  2423. */
  2424. error = xfs_dir_ialloc(&tp, dp, mode, 2,
  2425. 0, credp, prid, resblks > 0,
  2426. &cdp, NULL);
  2427. if (error) {
  2428. if (error == ENOSPC)
  2429. goto error_return;
  2430. goto abort_return;
  2431. }
  2432. xfs_itrace_ref(cdp);
  2433. /*
  2434. * Now we add the directory inode to the transaction.
  2435. * We waited until now since xfs_dir_ialloc might start
  2436. * a new transaction. Had we joined the transaction
  2437. * earlier, the locks might have gotten released. An error
  2438. * from here on will result in the transaction cancel
  2439. * unlocking dp so don't do it explicitly in the error path.
  2440. */
  2441. IHOLD(dp);
  2442. xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
  2443. unlock_dp_on_error = B_FALSE;
  2444. XFS_BMAP_INIT(&free_list, &first_block);
  2445. error = xfs_dir_createname(tp, dp, dir_name, cdp->i_ino,
  2446. &first_block, &free_list, resblks ?
  2447. resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
  2448. if (error) {
  2449. ASSERT(error != ENOSPC);
  2450. goto error1;
  2451. }
  2452. xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  2453. /*
  2454. * Bump the in memory version number of the parent directory
  2455. * so that other processes accessing it will recognize that
  2456. * the directory has changed.
  2457. */
  2458. dp->i_gen++;
  2459. error = xfs_dir_init(tp, cdp, dp);
  2460. if (error)
  2461. goto error2;
  2462. cdp->i_gen = 1;
  2463. error = xfs_bumplink(tp, dp);
  2464. if (error)
  2465. goto error2;
  2466. created = B_TRUE;
  2467. *ipp = cdp;
  2468. IHOLD(cdp);
  2469. /*
  2470. * Attach the dquots to the new inode and modify the icount incore.
  2471. */
  2472. XFS_QM_DQVOPCREATE(mp, tp, cdp, udqp, gdqp);
  2473. /*
  2474. * If this is a synchronous mount, make sure that the
  2475. * mkdir transaction goes to disk before returning to
  2476. * the user.
  2477. */
  2478. if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
  2479. xfs_trans_set_sync(tp);
  2480. }
  2481. error = xfs_bmap_finish(&tp, &free_list, &committed);
  2482. if (error) {
  2483. IRELE(cdp);
  2484. goto error2;
  2485. }
  2486. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  2487. XFS_QM_DQRELE(mp, udqp);
  2488. XFS_QM_DQRELE(mp, gdqp);
  2489. if (error) {
  2490. IRELE(cdp);
  2491. }
  2492. /* Fall through to std_return with error = 0 or errno from
  2493. * xfs_trans_commit. */
  2494. std_return:
  2495. if ((created || (error != 0 && dm_event_sent != 0)) &&
  2496. DM_EVENT_ENABLED(dp, DM_EVENT_POSTCREATE)) {
  2497. (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTCREATE,
  2498. dp, DM_RIGHT_NULL,
  2499. created ? cdp : NULL,
  2500. DM_RIGHT_NULL,
  2501. dir_name->name, NULL,
  2502. mode, error, 0);
  2503. }
  2504. return error;
  2505. error2:
  2506. error1:
  2507. xfs_bmap_cancel(&free_list);
  2508. abort_return:
  2509. cancel_flags |= XFS_TRANS_ABORT;
  2510. error_return:
  2511. xfs_trans_cancel(tp, cancel_flags);
  2512. XFS_QM_DQRELE(mp, udqp);
  2513. XFS_QM_DQRELE(mp, gdqp);
  2514. if (unlock_dp_on_error)
  2515. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  2516. goto std_return;
  2517. }
  2518. int
  2519. xfs_rmdir(
  2520. xfs_inode_t *dp,
  2521. struct xfs_name *name,
  2522. xfs_inode_t *cdp)
  2523. {
  2524. bhv_vnode_t *dir_vp = XFS_ITOV(dp);
  2525. xfs_mount_t *mp = dp->i_mount;
  2526. xfs_trans_t *tp;
  2527. int error;
  2528. xfs_bmap_free_t free_list;
  2529. xfs_fsblock_t first_block;
  2530. int cancel_flags;
  2531. int committed;
  2532. int last_cdp_link;
  2533. uint resblks;
  2534. xfs_itrace_entry(dp);
  2535. if (XFS_FORCED_SHUTDOWN(mp))
  2536. return XFS_ERROR(EIO);
  2537. if (DM_EVENT_ENABLED(dp, DM_EVENT_REMOVE)) {
  2538. error = XFS_SEND_NAMESP(mp, DM_EVENT_REMOVE,
  2539. dp, DM_RIGHT_NULL,
  2540. NULL, DM_RIGHT_NULL, name->name,
  2541. NULL, cdp->i_d.di_mode, 0, 0);
  2542. if (error)
  2543. return XFS_ERROR(error);
  2544. }
  2545. /*
  2546. * We need to get a reference to cdp before we get our log
  2547. * reservation. The reason for this is that we cannot call
  2548. * xfs_iget for an inode for which we do not have a reference
  2549. * once we've acquired a log reservation. This is because the
  2550. * inode we are trying to get might be in xfs_inactive going
  2551. * for a log reservation. Since we'll have to wait for the
  2552. * inactive code to complete before returning from xfs_iget,
  2553. * we need to make sure that we don't have log space reserved
  2554. * when we call xfs_iget. Instead we get an unlocked reference
  2555. * to the inode before getting our log reservation.
  2556. */
  2557. IHOLD(cdp);
  2558. /*
  2559. * Get the dquots for the inodes.
  2560. */
  2561. error = XFS_QM_DQATTACH(mp, dp, 0);
  2562. if (!error && dp != cdp)
  2563. error = XFS_QM_DQATTACH(mp, cdp, 0);
  2564. if (error) {
  2565. IRELE(cdp);
  2566. REMOVE_DEBUG_TRACE(__LINE__);
  2567. goto std_return;
  2568. }
  2569. tp = xfs_trans_alloc(mp, XFS_TRANS_RMDIR);
  2570. cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
  2571. /*
  2572. * We try to get the real space reservation first,
  2573. * allowing for directory btree deletion(s) implying
  2574. * possible bmap insert(s). If we can't get the space
  2575. * reservation then we use 0 instead, and avoid the bmap
  2576. * btree insert(s) in the directory code by, if the bmap
  2577. * insert tries to happen, instead trimming the LAST
  2578. * block from the directory.
  2579. */
  2580. resblks = XFS_REMOVE_SPACE_RES(mp);
  2581. error = xfs_trans_reserve(tp, resblks, XFS_REMOVE_LOG_RES(mp), 0,
  2582. XFS_TRANS_PERM_LOG_RES, XFS_DEFAULT_LOG_COUNT);
  2583. if (error == ENOSPC) {
  2584. resblks = 0;
  2585. error = xfs_trans_reserve(tp, 0, XFS_REMOVE_LOG_RES(mp), 0,
  2586. XFS_TRANS_PERM_LOG_RES, XFS_DEFAULT_LOG_COUNT);
  2587. }
  2588. if (error) {
  2589. ASSERT(error != ENOSPC);
  2590. cancel_flags = 0;
  2591. IRELE(cdp);
  2592. goto error_return;
  2593. }
  2594. XFS_BMAP_INIT(&free_list, &first_block);
  2595. /*
  2596. * Now lock the child directory inode and the parent directory
  2597. * inode in the proper order. This will take care of validating
  2598. * that the directory entry for the child directory inode has
  2599. * not changed while we were obtaining a log reservation.
  2600. */
  2601. error = xfs_lock_dir_and_entry(dp, cdp);
  2602. if (error) {
  2603. xfs_trans_cancel(tp, cancel_flags);
  2604. IRELE(cdp);
  2605. goto std_return;
  2606. }
  2607. xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
  2608. if (dp != cdp) {
  2609. /*
  2610. * Only increment the parent directory vnode count if
  2611. * we didn't bump it in looking up cdp. The only time
  2612. * we don't bump it is when we're looking up ".".
  2613. */
  2614. VN_HOLD(dir_vp);
  2615. }
  2616. xfs_itrace_ref(cdp);
  2617. xfs_trans_ijoin(tp, cdp, XFS_ILOCK_EXCL);
  2618. ASSERT(cdp->i_d.di_nlink >= 2);
  2619. if (cdp->i_d.di_nlink != 2) {
  2620. error = XFS_ERROR(ENOTEMPTY);
  2621. goto error_return;
  2622. }
  2623. if (!xfs_dir_isempty(cdp)) {
  2624. error = XFS_ERROR(ENOTEMPTY);
  2625. goto error_return;
  2626. }
  2627. error = xfs_dir_removename(tp, dp, name, cdp->i_ino,
  2628. &first_block, &free_list, resblks);
  2629. if (error)
  2630. goto error1;
  2631. xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  2632. /*
  2633. * Bump the in memory generation count on the parent
  2634. * directory so that other can know that it has changed.
  2635. */
  2636. dp->i_gen++;
  2637. /*
  2638. * Drop the link from cdp's "..".
  2639. */
  2640. error = xfs_droplink(tp, dp);
  2641. if (error) {
  2642. goto error1;
  2643. }
  2644. /*
  2645. * Drop the link from dp to cdp.
  2646. */
  2647. error = xfs_droplink(tp, cdp);
  2648. if (error) {
  2649. goto error1;
  2650. }
  2651. /*
  2652. * Drop the "." link from cdp to self.
  2653. */
  2654. error = xfs_droplink(tp, cdp);
  2655. if (error) {
  2656. goto error1;
  2657. }
  2658. /* Determine these before committing transaction */
  2659. last_cdp_link = (cdp)->i_d.di_nlink==0;
  2660. /*
  2661. * Take an extra ref on the child vnode so that it
  2662. * does not go to xfs_inactive() from within the commit.
  2663. */
  2664. IHOLD(cdp);
  2665. /*
  2666. * If this is a synchronous mount, make sure that the
  2667. * rmdir transaction goes to disk before returning to
  2668. * the user.
  2669. */
  2670. if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
  2671. xfs_trans_set_sync(tp);
  2672. }
  2673. error = xfs_bmap_finish (&tp, &free_list, &committed);
  2674. if (error) {
  2675. xfs_bmap_cancel(&free_list);
  2676. xfs_trans_cancel(tp, (XFS_TRANS_RELEASE_LOG_RES |
  2677. XFS_TRANS_ABORT));
  2678. IRELE(cdp);
  2679. goto std_return;
  2680. }
  2681. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  2682. if (error) {
  2683. IRELE(cdp);
  2684. goto std_return;
  2685. }
  2686. IRELE(cdp);
  2687. /* Fall through to std_return with error = 0 or the errno
  2688. * from xfs_trans_commit. */
  2689. std_return:
  2690. if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTREMOVE)) {
  2691. (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTREMOVE,
  2692. dp, DM_RIGHT_NULL,
  2693. NULL, DM_RIGHT_NULL,
  2694. name->name, NULL, cdp->i_d.di_mode,
  2695. error, 0);
  2696. }
  2697. return error;
  2698. error1:
  2699. xfs_bmap_cancel(&free_list);
  2700. cancel_flags |= XFS_TRANS_ABORT;
  2701. /* FALLTHROUGH */
  2702. error_return:
  2703. xfs_trans_cancel(tp, cancel_flags);
  2704. goto std_return;
  2705. }
  2706. int
  2707. xfs_symlink(
  2708. xfs_inode_t *dp,
  2709. struct xfs_name *link_name,
  2710. const char *target_path,
  2711. mode_t mode,
  2712. xfs_inode_t **ipp,
  2713. cred_t *credp)
  2714. {
  2715. xfs_mount_t *mp = dp->i_mount;
  2716. xfs_trans_t *tp;
  2717. xfs_inode_t *ip;
  2718. int error;
  2719. int pathlen;
  2720. xfs_bmap_free_t free_list;
  2721. xfs_fsblock_t first_block;
  2722. boolean_t unlock_dp_on_error = B_FALSE;
  2723. uint cancel_flags;
  2724. int committed;
  2725. xfs_fileoff_t first_fsb;
  2726. xfs_filblks_t fs_blocks;
  2727. int nmaps;
  2728. xfs_bmbt_irec_t mval[SYMLINK_MAPS];
  2729. xfs_daddr_t d;
  2730. const char *cur_chunk;
  2731. int byte_cnt;
  2732. int n;
  2733. xfs_buf_t *bp;
  2734. xfs_prid_t prid;
  2735. struct xfs_dquot *udqp, *gdqp;
  2736. uint resblks;
  2737. *ipp = NULL;
  2738. error = 0;
  2739. ip = NULL;
  2740. tp = NULL;
  2741. xfs_itrace_entry(dp);
  2742. if (XFS_FORCED_SHUTDOWN(mp))
  2743. return XFS_ERROR(EIO);
  2744. /*
  2745. * Check component lengths of the target path name.
  2746. */
  2747. pathlen = strlen(target_path);
  2748. if (pathlen >= MAXPATHLEN) /* total string too long */
  2749. return XFS_ERROR(ENAMETOOLONG);
  2750. if (DM_EVENT_ENABLED(dp, DM_EVENT_SYMLINK)) {
  2751. error = XFS_SEND_NAMESP(mp, DM_EVENT_SYMLINK, dp,
  2752. DM_RIGHT_NULL, NULL, DM_RIGHT_NULL,
  2753. link_name->name, target_path, 0, 0, 0);
  2754. if (error)
  2755. return error;
  2756. }
  2757. /* Return through std_return after this point. */
  2758. udqp = gdqp = NULL;
  2759. if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
  2760. prid = dp->i_d.di_projid;
  2761. else
  2762. prid = (xfs_prid_t)dfltprid;
  2763. /*
  2764. * Make sure that we have allocated dquot(s) on disk.
  2765. */
  2766. error = XFS_QM_DQVOPALLOC(mp, dp,
  2767. current_fsuid(credp), current_fsgid(credp), prid,
  2768. XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
  2769. if (error)
  2770. goto std_return;
  2771. tp = xfs_trans_alloc(mp, XFS_TRANS_SYMLINK);
  2772. cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
  2773. /*
  2774. * The symlink will fit into the inode data fork?
  2775. * There can't be any attributes so we get the whole variable part.
  2776. */
  2777. if (pathlen <= XFS_LITINO(mp))
  2778. fs_blocks = 0;
  2779. else
  2780. fs_blocks = XFS_B_TO_FSB(mp, pathlen);
  2781. resblks = XFS_SYMLINK_SPACE_RES(mp, link_name->len, fs_blocks);
  2782. error = xfs_trans_reserve(tp, resblks, XFS_SYMLINK_LOG_RES(mp), 0,
  2783. XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
  2784. if (error == ENOSPC && fs_blocks == 0) {
  2785. resblks = 0;
  2786. error = xfs_trans_reserve(tp, 0, XFS_SYMLINK_LOG_RES(mp), 0,
  2787. XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
  2788. }
  2789. if (error) {
  2790. cancel_flags = 0;
  2791. goto error_return;
  2792. }
  2793. xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
  2794. unlock_dp_on_error = B_TRUE;
  2795. /*
  2796. * Check whether the directory allows new symlinks or not.
  2797. */
  2798. if (dp->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) {
  2799. error = XFS_ERROR(EPERM);
  2800. goto error_return;
  2801. }
  2802. /*
  2803. * Reserve disk quota : blocks and inode.
  2804. */
  2805. error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
  2806. if (error)
  2807. goto error_return;
  2808. /*
  2809. * Check for ability to enter directory entry, if no space reserved.
  2810. */
  2811. error = xfs_dir_canenter(tp, dp, link_name, resblks);
  2812. if (error)
  2813. goto error_return;
  2814. /*
  2815. * Initialize the bmap freelist prior to calling either
  2816. * bmapi or the directory create code.
  2817. */
  2818. XFS_BMAP_INIT(&free_list, &first_block);
  2819. /*
  2820. * Allocate an inode for the symlink.
  2821. */
  2822. error = xfs_dir_ialloc(&tp, dp, S_IFLNK | (mode & ~S_IFMT),
  2823. 1, 0, credp, prid, resblks > 0, &ip, NULL);
  2824. if (error) {
  2825. if (error == ENOSPC)
  2826. goto error_return;
  2827. goto error1;
  2828. }
  2829. xfs_itrace_ref(ip);
  2830. /*
  2831. * An error after we've joined dp to the transaction will result in the
  2832. * transaction cancel unlocking dp so don't do it explicitly in the
  2833. * error path.
  2834. */
  2835. IHOLD(dp);
  2836. xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
  2837. unlock_dp_on_error = B_FALSE;
  2838. /*
  2839. * Also attach the dquot(s) to it, if applicable.
  2840. */
  2841. XFS_QM_DQVOPCREATE(mp, tp, ip, udqp, gdqp);
  2842. if (resblks)
  2843. resblks -= XFS_IALLOC_SPACE_RES(mp);
  2844. /*
  2845. * If the symlink will fit into the inode, write it inline.
  2846. */
  2847. if (pathlen <= XFS_IFORK_DSIZE(ip)) {
  2848. xfs_idata_realloc(ip, pathlen, XFS_DATA_FORK);
  2849. memcpy(ip->i_df.if_u1.if_data, target_path, pathlen);
  2850. ip->i_d.di_size = pathlen;
  2851. /*
  2852. * The inode was initially created in extent format.
  2853. */
  2854. ip->i_df.if_flags &= ~(XFS_IFEXTENTS | XFS_IFBROOT);
  2855. ip->i_df.if_flags |= XFS_IFINLINE;
  2856. ip->i_d.di_format = XFS_DINODE_FMT_LOCAL;
  2857. xfs_trans_log_inode(tp, ip, XFS_ILOG_DDATA | XFS_ILOG_CORE);
  2858. } else {
  2859. first_fsb = 0;
  2860. nmaps = SYMLINK_MAPS;
  2861. error = xfs_bmapi(tp, ip, first_fsb, fs_blocks,
  2862. XFS_BMAPI_WRITE | XFS_BMAPI_METADATA,
  2863. &first_block, resblks, mval, &nmaps,
  2864. &free_list, NULL);
  2865. if (error) {
  2866. goto error1;
  2867. }
  2868. if (resblks)
  2869. resblks -= fs_blocks;
  2870. ip->i_d.di_size = pathlen;
  2871. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  2872. cur_chunk = target_path;
  2873. for (n = 0; n < nmaps; n++) {
  2874. d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
  2875. byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
  2876. bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
  2877. BTOBB(byte_cnt), 0);
  2878. ASSERT(bp && !XFS_BUF_GETERROR(bp));
  2879. if (pathlen < byte_cnt) {
  2880. byte_cnt = pathlen;
  2881. }
  2882. pathlen -= byte_cnt;
  2883. memcpy(XFS_BUF_PTR(bp), cur_chunk, byte_cnt);
  2884. cur_chunk += byte_cnt;
  2885. xfs_trans_log_buf(tp, bp, 0, byte_cnt - 1);
  2886. }
  2887. }
  2888. /*
  2889. * Create the directory entry for the symlink.
  2890. */
  2891. error = xfs_dir_createname(tp, dp, link_name, ip->i_ino,
  2892. &first_block, &free_list, resblks);
  2893. if (error)
  2894. goto error1;
  2895. xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  2896. xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
  2897. /*
  2898. * Bump the in memory version number of the parent directory
  2899. * so that other processes accessing it will recognize that
  2900. * the directory has changed.
  2901. */
  2902. dp->i_gen++;
  2903. /*
  2904. * If this is a synchronous mount, make sure that the
  2905. * symlink transaction goes to disk before returning to
  2906. * the user.
  2907. */
  2908. if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
  2909. xfs_trans_set_sync(tp);
  2910. }
  2911. /*
  2912. * xfs_trans_commit normally decrements the vnode ref count
  2913. * when it unlocks the inode. Since we want to return the
  2914. * vnode to the caller, we bump the vnode ref count now.
  2915. */
  2916. IHOLD(ip);
  2917. error = xfs_bmap_finish(&tp, &free_list, &committed);
  2918. if (error) {
  2919. goto error2;
  2920. }
  2921. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  2922. XFS_QM_DQRELE(mp, udqp);
  2923. XFS_QM_DQRELE(mp, gdqp);
  2924. /* Fall through to std_return with error = 0 or errno from
  2925. * xfs_trans_commit */
  2926. std_return:
  2927. if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTSYMLINK)) {
  2928. (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTSYMLINK,
  2929. dp, DM_RIGHT_NULL,
  2930. error ? NULL : ip,
  2931. DM_RIGHT_NULL, link_name->name,
  2932. target_path, 0, error, 0);
  2933. }
  2934. if (!error)
  2935. *ipp = ip;
  2936. return error;
  2937. error2:
  2938. IRELE(ip);
  2939. error1:
  2940. xfs_bmap_cancel(&free_list);
  2941. cancel_flags |= XFS_TRANS_ABORT;
  2942. error_return:
  2943. xfs_trans_cancel(tp, cancel_flags);
  2944. XFS_QM_DQRELE(mp, udqp);
  2945. XFS_QM_DQRELE(mp, gdqp);
  2946. if (unlock_dp_on_error)
  2947. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  2948. goto std_return;
  2949. }
  2950. int
  2951. xfs_inode_flush(
  2952. xfs_inode_t *ip,
  2953. int flags)
  2954. {
  2955. xfs_mount_t *mp = ip->i_mount;
  2956. int error = 0;
  2957. if (XFS_FORCED_SHUTDOWN(mp))
  2958. return XFS_ERROR(EIO);
  2959. /*
  2960. * Bypass inodes which have already been cleaned by
  2961. * the inode flush clustering code inside xfs_iflush
  2962. */
  2963. if (xfs_inode_clean(ip))
  2964. return 0;
  2965. /*
  2966. * We make this non-blocking if the inode is contended,
  2967. * return EAGAIN to indicate to the caller that they
  2968. * did not succeed. This prevents the flush path from
  2969. * blocking on inodes inside another operation right
  2970. * now, they get caught later by xfs_sync.
  2971. */
  2972. if (flags & FLUSH_SYNC) {
  2973. xfs_ilock(ip, XFS_ILOCK_SHARED);
  2974. xfs_iflock(ip);
  2975. } else if (xfs_ilock_nowait(ip, XFS_ILOCK_SHARED)) {
  2976. if (xfs_ipincount(ip) || !xfs_iflock_nowait(ip)) {
  2977. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  2978. return EAGAIN;
  2979. }
  2980. } else {
  2981. return EAGAIN;
  2982. }
  2983. error = xfs_iflush(ip, (flags & FLUSH_SYNC) ? XFS_IFLUSH_SYNC
  2984. : XFS_IFLUSH_ASYNC_NOBLOCK);
  2985. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  2986. return error;
  2987. }
  2988. int
  2989. xfs_set_dmattrs(
  2990. xfs_inode_t *ip,
  2991. u_int evmask,
  2992. u_int16_t state)
  2993. {
  2994. xfs_mount_t *mp = ip->i_mount;
  2995. xfs_trans_t *tp;
  2996. int error;
  2997. if (!capable(CAP_SYS_ADMIN))
  2998. return XFS_ERROR(EPERM);
  2999. if (XFS_FORCED_SHUTDOWN(mp))
  3000. return XFS_ERROR(EIO);
  3001. tp = xfs_trans_alloc(mp, XFS_TRANS_SET_DMATTRS);
  3002. error = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES (mp), 0, 0, 0);
  3003. if (error) {
  3004. xfs_trans_cancel(tp, 0);
  3005. return error;
  3006. }
  3007. xfs_ilock(ip, XFS_ILOCK_EXCL);
  3008. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  3009. ip->i_d.di_dmevmask = evmask;
  3010. ip->i_d.di_dmstate = state;
  3011. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  3012. IHOLD(ip);
  3013. error = xfs_trans_commit(tp, 0);
  3014. return error;
  3015. }
  3016. int
  3017. xfs_reclaim(
  3018. xfs_inode_t *ip)
  3019. {
  3020. bhv_vnode_t *vp = XFS_ITOV(ip);
  3021. xfs_itrace_entry(ip);
  3022. ASSERT(!VN_MAPPED(vp));
  3023. /* bad inode, get out here ASAP */
  3024. if (VN_BAD(vp)) {
  3025. xfs_ireclaim(ip);
  3026. return 0;
  3027. }
  3028. vn_iowait(ip);
  3029. ASSERT(XFS_FORCED_SHUTDOWN(ip->i_mount) || ip->i_delayed_blks == 0);
  3030. /*
  3031. * Make sure the atime in the XFS inode is correct before freeing the
  3032. * Linux inode.
  3033. */
  3034. xfs_synchronize_atime(ip);
  3035. /*
  3036. * If we have nothing to flush with this inode then complete the
  3037. * teardown now, otherwise break the link between the xfs inode and the
  3038. * linux inode and clean up the xfs inode later. This avoids flushing
  3039. * the inode to disk during the delete operation itself.
  3040. *
  3041. * When breaking the link, we need to set the XFS_IRECLAIMABLE flag
  3042. * first to ensure that xfs_iunpin() will never see an xfs inode
  3043. * that has a linux inode being reclaimed. Synchronisation is provided
  3044. * by the i_flags_lock.
  3045. */
  3046. if (!ip->i_update_core && (ip->i_itemp == NULL)) {
  3047. xfs_ilock(ip, XFS_ILOCK_EXCL);
  3048. xfs_iflock(ip);
  3049. return xfs_finish_reclaim(ip, 1, XFS_IFLUSH_DELWRI_ELSE_SYNC);
  3050. } else {
  3051. xfs_mount_t *mp = ip->i_mount;
  3052. /* Protect sync and unpin from us */
  3053. XFS_MOUNT_ILOCK(mp);
  3054. spin_lock(&ip->i_flags_lock);
  3055. __xfs_iflags_set(ip, XFS_IRECLAIMABLE);
  3056. vn_to_inode(vp)->i_private = NULL;
  3057. ip->i_vnode = NULL;
  3058. spin_unlock(&ip->i_flags_lock);
  3059. list_add_tail(&ip->i_reclaim, &mp->m_del_inodes);
  3060. XFS_MOUNT_IUNLOCK(mp);
  3061. }
  3062. return 0;
  3063. }
  3064. int
  3065. xfs_finish_reclaim(
  3066. xfs_inode_t *ip,
  3067. int locked,
  3068. int sync_mode)
  3069. {
  3070. xfs_perag_t *pag = xfs_get_perag(ip->i_mount, ip->i_ino);
  3071. bhv_vnode_t *vp = XFS_ITOV_NULL(ip);
  3072. int error;
  3073. if (vp && VN_BAD(vp))
  3074. goto reclaim;
  3075. /* The hash lock here protects a thread in xfs_iget_core from
  3076. * racing with us on linking the inode back with a vnode.
  3077. * Once we have the XFS_IRECLAIM flag set it will not touch
  3078. * us.
  3079. */
  3080. write_lock(&pag->pag_ici_lock);
  3081. spin_lock(&ip->i_flags_lock);
  3082. if (__xfs_iflags_test(ip, XFS_IRECLAIM) ||
  3083. (!__xfs_iflags_test(ip, XFS_IRECLAIMABLE) && vp == NULL)) {
  3084. spin_unlock(&ip->i_flags_lock);
  3085. write_unlock(&pag->pag_ici_lock);
  3086. if (locked) {
  3087. xfs_ifunlock(ip);
  3088. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  3089. }
  3090. return 1;
  3091. }
  3092. __xfs_iflags_set(ip, XFS_IRECLAIM);
  3093. spin_unlock(&ip->i_flags_lock);
  3094. write_unlock(&pag->pag_ici_lock);
  3095. xfs_put_perag(ip->i_mount, pag);
  3096. /*
  3097. * If the inode is still dirty, then flush it out. If the inode
  3098. * is not in the AIL, then it will be OK to flush it delwri as
  3099. * long as xfs_iflush() does not keep any references to the inode.
  3100. * We leave that decision up to xfs_iflush() since it has the
  3101. * knowledge of whether it's OK to simply do a delwri flush of
  3102. * the inode or whether we need to wait until the inode is
  3103. * pulled from the AIL.
  3104. * We get the flush lock regardless, though, just to make sure
  3105. * we don't free it while it is being flushed.
  3106. */
  3107. if (!locked) {
  3108. xfs_ilock(ip, XFS_ILOCK_EXCL);
  3109. xfs_iflock(ip);
  3110. }
  3111. if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
  3112. if (ip->i_update_core ||
  3113. ((ip->i_itemp != NULL) &&
  3114. (ip->i_itemp->ili_format.ilf_fields != 0))) {
  3115. error = xfs_iflush(ip, sync_mode);
  3116. /*
  3117. * If we hit an error, typically because of filesystem
  3118. * shutdown, we don't need to let vn_reclaim to know
  3119. * because we're gonna reclaim the inode anyway.
  3120. */
  3121. if (error) {
  3122. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  3123. goto reclaim;
  3124. }
  3125. xfs_iflock(ip); /* synchronize with xfs_iflush_done */
  3126. }
  3127. ASSERT(ip->i_update_core == 0);
  3128. ASSERT(ip->i_itemp == NULL ||
  3129. ip->i_itemp->ili_format.ilf_fields == 0);
  3130. }
  3131. xfs_ifunlock(ip);
  3132. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  3133. reclaim:
  3134. xfs_ireclaim(ip);
  3135. return 0;
  3136. }
  3137. int
  3138. xfs_finish_reclaim_all(xfs_mount_t *mp, int noblock)
  3139. {
  3140. int purged;
  3141. xfs_inode_t *ip, *n;
  3142. int done = 0;
  3143. while (!done) {
  3144. purged = 0;
  3145. XFS_MOUNT_ILOCK(mp);
  3146. list_for_each_entry_safe(ip, n, &mp->m_del_inodes, i_reclaim) {
  3147. if (noblock) {
  3148. if (xfs_ilock_nowait(ip, XFS_ILOCK_EXCL) == 0)
  3149. continue;
  3150. if (xfs_ipincount(ip) ||
  3151. !xfs_iflock_nowait(ip)) {
  3152. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  3153. continue;
  3154. }
  3155. }
  3156. XFS_MOUNT_IUNLOCK(mp);
  3157. if (xfs_finish_reclaim(ip, noblock,
  3158. XFS_IFLUSH_DELWRI_ELSE_ASYNC))
  3159. delay(1);
  3160. purged = 1;
  3161. break;
  3162. }
  3163. done = !purged;
  3164. }
  3165. XFS_MOUNT_IUNLOCK(mp);
  3166. return 0;
  3167. }
  3168. /*
  3169. * xfs_alloc_file_space()
  3170. * This routine allocates disk space for the given file.
  3171. *
  3172. * If alloc_type == 0, this request is for an ALLOCSP type
  3173. * request which will change the file size. In this case, no
  3174. * DMAPI event will be generated by the call. A TRUNCATE event
  3175. * will be generated later by xfs_setattr.
  3176. *
  3177. * If alloc_type != 0, this request is for a RESVSP type
  3178. * request, and a DMAPI DM_EVENT_WRITE will be generated if the
  3179. * lower block boundary byte address is less than the file's
  3180. * length.
  3181. *
  3182. * RETURNS:
  3183. * 0 on success
  3184. * errno on error
  3185. *
  3186. */
  3187. STATIC int
  3188. xfs_alloc_file_space(
  3189. xfs_inode_t *ip,
  3190. xfs_off_t offset,
  3191. xfs_off_t len,
  3192. int alloc_type,
  3193. int attr_flags)
  3194. {
  3195. xfs_mount_t *mp = ip->i_mount;
  3196. xfs_off_t count;
  3197. xfs_filblks_t allocated_fsb;
  3198. xfs_filblks_t allocatesize_fsb;
  3199. xfs_extlen_t extsz, temp;
  3200. xfs_fileoff_t startoffset_fsb;
  3201. xfs_fsblock_t firstfsb;
  3202. int nimaps;
  3203. int bmapi_flag;
  3204. int quota_flag;
  3205. int rt;
  3206. xfs_trans_t *tp;
  3207. xfs_bmbt_irec_t imaps[1], *imapp;
  3208. xfs_bmap_free_t free_list;
  3209. uint qblocks, resblks, resrtextents;
  3210. int committed;
  3211. int error;
  3212. xfs_itrace_entry(ip);
  3213. if (XFS_FORCED_SHUTDOWN(mp))
  3214. return XFS_ERROR(EIO);
  3215. if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
  3216. return error;
  3217. if (len <= 0)
  3218. return XFS_ERROR(EINVAL);
  3219. rt = XFS_IS_REALTIME_INODE(ip);
  3220. extsz = xfs_get_extsz_hint(ip);
  3221. count = len;
  3222. imapp = &imaps[0];
  3223. nimaps = 1;
  3224. bmapi_flag = XFS_BMAPI_WRITE | (alloc_type ? XFS_BMAPI_PREALLOC : 0);
  3225. startoffset_fsb = XFS_B_TO_FSBT(mp, offset);
  3226. allocatesize_fsb = XFS_B_TO_FSB(mp, count);
  3227. /* Generate a DMAPI event if needed. */
  3228. if (alloc_type != 0 && offset < ip->i_size &&
  3229. (attr_flags&ATTR_DMI) == 0 &&
  3230. DM_EVENT_ENABLED(ip, DM_EVENT_WRITE)) {
  3231. xfs_off_t end_dmi_offset;
  3232. end_dmi_offset = offset+len;
  3233. if (end_dmi_offset > ip->i_size)
  3234. end_dmi_offset = ip->i_size;
  3235. error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, ip, offset,
  3236. end_dmi_offset - offset, 0, NULL);
  3237. if (error)
  3238. return error;
  3239. }
  3240. /*
  3241. * Allocate file space until done or until there is an error
  3242. */
  3243. retry:
  3244. while (allocatesize_fsb && !error) {
  3245. xfs_fileoff_t s, e;
  3246. /*
  3247. * Determine space reservations for data/realtime.
  3248. */
  3249. if (unlikely(extsz)) {
  3250. s = startoffset_fsb;
  3251. do_div(s, extsz);
  3252. s *= extsz;
  3253. e = startoffset_fsb + allocatesize_fsb;
  3254. if ((temp = do_mod(startoffset_fsb, extsz)))
  3255. e += temp;
  3256. if ((temp = do_mod(e, extsz)))
  3257. e += extsz - temp;
  3258. } else {
  3259. s = 0;
  3260. e = allocatesize_fsb;
  3261. }
  3262. if (unlikely(rt)) {
  3263. resrtextents = qblocks = (uint)(e - s);
  3264. resrtextents /= mp->m_sb.sb_rextsize;
  3265. resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
  3266. quota_flag = XFS_QMOPT_RES_RTBLKS;
  3267. } else {
  3268. resrtextents = 0;
  3269. resblks = qblocks = \
  3270. XFS_DIOSTRAT_SPACE_RES(mp, (uint)(e - s));
  3271. quota_flag = XFS_QMOPT_RES_REGBLKS;
  3272. }
  3273. /*
  3274. * Allocate and setup the transaction.
  3275. */
  3276. tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
  3277. error = xfs_trans_reserve(tp, resblks,
  3278. XFS_WRITE_LOG_RES(mp), resrtextents,
  3279. XFS_TRANS_PERM_LOG_RES,
  3280. XFS_WRITE_LOG_COUNT);
  3281. /*
  3282. * Check for running out of space
  3283. */
  3284. if (error) {
  3285. /*
  3286. * Free the transaction structure.
  3287. */
  3288. ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
  3289. xfs_trans_cancel(tp, 0);
  3290. break;
  3291. }
  3292. xfs_ilock(ip, XFS_ILOCK_EXCL);
  3293. error = XFS_TRANS_RESERVE_QUOTA_NBLKS(mp, tp, ip,
  3294. qblocks, 0, quota_flag);
  3295. if (error)
  3296. goto error1;
  3297. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  3298. xfs_trans_ihold(tp, ip);
  3299. /*
  3300. * Issue the xfs_bmapi() call to allocate the blocks
  3301. */
  3302. XFS_BMAP_INIT(&free_list, &firstfsb);
  3303. error = xfs_bmapi(tp, ip, startoffset_fsb,
  3304. allocatesize_fsb, bmapi_flag,
  3305. &firstfsb, 0, imapp, &nimaps,
  3306. &free_list, NULL);
  3307. if (error) {
  3308. goto error0;
  3309. }
  3310. /*
  3311. * Complete the transaction
  3312. */
  3313. error = xfs_bmap_finish(&tp, &free_list, &committed);
  3314. if (error) {
  3315. goto error0;
  3316. }
  3317. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  3318. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  3319. if (error) {
  3320. break;
  3321. }
  3322. allocated_fsb = imapp->br_blockcount;
  3323. if (nimaps == 0) {
  3324. error = XFS_ERROR(ENOSPC);
  3325. break;
  3326. }
  3327. startoffset_fsb += allocated_fsb;
  3328. allocatesize_fsb -= allocated_fsb;
  3329. }
  3330. dmapi_enospc_check:
  3331. if (error == ENOSPC && (attr_flags & ATTR_DMI) == 0 &&
  3332. DM_EVENT_ENABLED(ip, DM_EVENT_NOSPACE)) {
  3333. error = XFS_SEND_NAMESP(mp, DM_EVENT_NOSPACE,
  3334. ip, DM_RIGHT_NULL,
  3335. ip, DM_RIGHT_NULL,
  3336. NULL, NULL, 0, 0, 0); /* Delay flag intentionally unused */
  3337. if (error == 0)
  3338. goto retry; /* Maybe DMAPI app. has made space */
  3339. /* else fall through with error from XFS_SEND_DATA */
  3340. }
  3341. return error;
  3342. error0: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
  3343. xfs_bmap_cancel(&free_list);
  3344. XFS_TRANS_UNRESERVE_QUOTA_NBLKS(mp, tp, ip, qblocks, 0, quota_flag);
  3345. error1: /* Just cancel transaction */
  3346. xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
  3347. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  3348. goto dmapi_enospc_check;
  3349. }
  3350. /*
  3351. * Zero file bytes between startoff and endoff inclusive.
  3352. * The iolock is held exclusive and no blocks are buffered.
  3353. */
  3354. STATIC int
  3355. xfs_zero_remaining_bytes(
  3356. xfs_inode_t *ip,
  3357. xfs_off_t startoff,
  3358. xfs_off_t endoff)
  3359. {
  3360. xfs_bmbt_irec_t imap;
  3361. xfs_fileoff_t offset_fsb;
  3362. xfs_off_t lastoffset;
  3363. xfs_off_t offset;
  3364. xfs_buf_t *bp;
  3365. xfs_mount_t *mp = ip->i_mount;
  3366. int nimap;
  3367. int error = 0;
  3368. bp = xfs_buf_get_noaddr(mp->m_sb.sb_blocksize,
  3369. XFS_IS_REALTIME_INODE(ip) ?
  3370. mp->m_rtdev_targp : mp->m_ddev_targp);
  3371. for (offset = startoff; offset <= endoff; offset = lastoffset + 1) {
  3372. offset_fsb = XFS_B_TO_FSBT(mp, offset);
  3373. nimap = 1;
  3374. error = xfs_bmapi(NULL, ip, offset_fsb, 1, 0,
  3375. NULL, 0, &imap, &nimap, NULL, NULL);
  3376. if (error || nimap < 1)
  3377. break;
  3378. ASSERT(imap.br_blockcount >= 1);
  3379. ASSERT(imap.br_startoff == offset_fsb);
  3380. lastoffset = XFS_FSB_TO_B(mp, imap.br_startoff + 1) - 1;
  3381. if (lastoffset > endoff)
  3382. lastoffset = endoff;
  3383. if (imap.br_startblock == HOLESTARTBLOCK)
  3384. continue;
  3385. ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
  3386. if (imap.br_state == XFS_EXT_UNWRITTEN)
  3387. continue;
  3388. XFS_BUF_UNDONE(bp);
  3389. XFS_BUF_UNWRITE(bp);
  3390. XFS_BUF_READ(bp);
  3391. XFS_BUF_SET_ADDR(bp, XFS_FSB_TO_DB(ip, imap.br_startblock));
  3392. xfsbdstrat(mp, bp);
  3393. error = xfs_iowait(bp);
  3394. if (error) {
  3395. xfs_ioerror_alert("xfs_zero_remaining_bytes(read)",
  3396. mp, bp, XFS_BUF_ADDR(bp));
  3397. break;
  3398. }
  3399. memset(XFS_BUF_PTR(bp) +
  3400. (offset - XFS_FSB_TO_B(mp, imap.br_startoff)),
  3401. 0, lastoffset - offset + 1);
  3402. XFS_BUF_UNDONE(bp);
  3403. XFS_BUF_UNREAD(bp);
  3404. XFS_BUF_WRITE(bp);
  3405. xfsbdstrat(mp, bp);
  3406. error = xfs_iowait(bp);
  3407. if (error) {
  3408. xfs_ioerror_alert("xfs_zero_remaining_bytes(write)",
  3409. mp, bp, XFS_BUF_ADDR(bp));
  3410. break;
  3411. }
  3412. }
  3413. xfs_buf_free(bp);
  3414. return error;
  3415. }
  3416. /*
  3417. * xfs_free_file_space()
  3418. * This routine frees disk space for the given file.
  3419. *
  3420. * This routine is only called by xfs_change_file_space
  3421. * for an UNRESVSP type call.
  3422. *
  3423. * RETURNS:
  3424. * 0 on success
  3425. * errno on error
  3426. *
  3427. */
  3428. STATIC int
  3429. xfs_free_file_space(
  3430. xfs_inode_t *ip,
  3431. xfs_off_t offset,
  3432. xfs_off_t len,
  3433. int attr_flags)
  3434. {
  3435. bhv_vnode_t *vp;
  3436. int committed;
  3437. int done;
  3438. xfs_off_t end_dmi_offset;
  3439. xfs_fileoff_t endoffset_fsb;
  3440. int error;
  3441. xfs_fsblock_t firstfsb;
  3442. xfs_bmap_free_t free_list;
  3443. xfs_bmbt_irec_t imap;
  3444. xfs_off_t ioffset;
  3445. xfs_extlen_t mod=0;
  3446. xfs_mount_t *mp;
  3447. int nimap;
  3448. uint resblks;
  3449. uint rounding;
  3450. int rt;
  3451. xfs_fileoff_t startoffset_fsb;
  3452. xfs_trans_t *tp;
  3453. int need_iolock = 1;
  3454. vp = XFS_ITOV(ip);
  3455. mp = ip->i_mount;
  3456. xfs_itrace_entry(ip);
  3457. if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
  3458. return error;
  3459. error = 0;
  3460. if (len <= 0) /* if nothing being freed */
  3461. return error;
  3462. rt = XFS_IS_REALTIME_INODE(ip);
  3463. startoffset_fsb = XFS_B_TO_FSB(mp, offset);
  3464. end_dmi_offset = offset + len;
  3465. endoffset_fsb = XFS_B_TO_FSBT(mp, end_dmi_offset);
  3466. if (offset < ip->i_size && (attr_flags & ATTR_DMI) == 0 &&
  3467. DM_EVENT_ENABLED(ip, DM_EVENT_WRITE)) {
  3468. if (end_dmi_offset > ip->i_size)
  3469. end_dmi_offset = ip->i_size;
  3470. error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, ip,
  3471. offset, end_dmi_offset - offset,
  3472. AT_DELAY_FLAG(attr_flags), NULL);
  3473. if (error)
  3474. return error;
  3475. }
  3476. if (attr_flags & ATTR_NOLOCK)
  3477. need_iolock = 0;
  3478. if (need_iolock) {
  3479. xfs_ilock(ip, XFS_IOLOCK_EXCL);
  3480. vn_iowait(ip); /* wait for the completion of any pending DIOs */
  3481. }
  3482. rounding = max_t(uint, 1 << mp->m_sb.sb_blocklog, PAGE_CACHE_SIZE);
  3483. ioffset = offset & ~(rounding - 1);
  3484. if (VN_CACHED(vp) != 0) {
  3485. xfs_inval_cached_trace(ip, ioffset, -1, ioffset, -1);
  3486. error = xfs_flushinval_pages(ip, ioffset, -1, FI_REMAPF_LOCKED);
  3487. if (error)
  3488. goto out_unlock_iolock;
  3489. }
  3490. /*
  3491. * Need to zero the stuff we're not freeing, on disk.
  3492. * If its a realtime file & can't use unwritten extents then we
  3493. * actually need to zero the extent edges. Otherwise xfs_bunmapi
  3494. * will take care of it for us.
  3495. */
  3496. if (rt && !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
  3497. nimap = 1;
  3498. error = xfs_bmapi(NULL, ip, startoffset_fsb,
  3499. 1, 0, NULL, 0, &imap, &nimap, NULL, NULL);
  3500. if (error)
  3501. goto out_unlock_iolock;
  3502. ASSERT(nimap == 0 || nimap == 1);
  3503. if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
  3504. xfs_daddr_t block;
  3505. ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
  3506. block = imap.br_startblock;
  3507. mod = do_div(block, mp->m_sb.sb_rextsize);
  3508. if (mod)
  3509. startoffset_fsb += mp->m_sb.sb_rextsize - mod;
  3510. }
  3511. nimap = 1;
  3512. error = xfs_bmapi(NULL, ip, endoffset_fsb - 1,
  3513. 1, 0, NULL, 0, &imap, &nimap, NULL, NULL);
  3514. if (error)
  3515. goto out_unlock_iolock;
  3516. ASSERT(nimap == 0 || nimap == 1);
  3517. if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
  3518. ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
  3519. mod++;
  3520. if (mod && (mod != mp->m_sb.sb_rextsize))
  3521. endoffset_fsb -= mod;
  3522. }
  3523. }
  3524. if ((done = (endoffset_fsb <= startoffset_fsb)))
  3525. /*
  3526. * One contiguous piece to clear
  3527. */
  3528. error = xfs_zero_remaining_bytes(ip, offset, offset + len - 1);
  3529. else {
  3530. /*
  3531. * Some full blocks, possibly two pieces to clear
  3532. */
  3533. if (offset < XFS_FSB_TO_B(mp, startoffset_fsb))
  3534. error = xfs_zero_remaining_bytes(ip, offset,
  3535. XFS_FSB_TO_B(mp, startoffset_fsb) - 1);
  3536. if (!error &&
  3537. XFS_FSB_TO_B(mp, endoffset_fsb) < offset + len)
  3538. error = xfs_zero_remaining_bytes(ip,
  3539. XFS_FSB_TO_B(mp, endoffset_fsb),
  3540. offset + len - 1);
  3541. }
  3542. /*
  3543. * free file space until done or until there is an error
  3544. */
  3545. resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
  3546. while (!error && !done) {
  3547. /*
  3548. * allocate and setup the transaction. Allow this
  3549. * transaction to dip into the reserve blocks to ensure
  3550. * the freeing of the space succeeds at ENOSPC.
  3551. */
  3552. tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
  3553. tp->t_flags |= XFS_TRANS_RESERVE;
  3554. error = xfs_trans_reserve(tp,
  3555. resblks,
  3556. XFS_WRITE_LOG_RES(mp),
  3557. 0,
  3558. XFS_TRANS_PERM_LOG_RES,
  3559. XFS_WRITE_LOG_COUNT);
  3560. /*
  3561. * check for running out of space
  3562. */
  3563. if (error) {
  3564. /*
  3565. * Free the transaction structure.
  3566. */
  3567. ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
  3568. xfs_trans_cancel(tp, 0);
  3569. break;
  3570. }
  3571. xfs_ilock(ip, XFS_ILOCK_EXCL);
  3572. error = XFS_TRANS_RESERVE_QUOTA(mp, tp,
  3573. ip->i_udquot, ip->i_gdquot, resblks, 0,
  3574. XFS_QMOPT_RES_REGBLKS);
  3575. if (error)
  3576. goto error1;
  3577. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  3578. xfs_trans_ihold(tp, ip);
  3579. /*
  3580. * issue the bunmapi() call to free the blocks
  3581. */
  3582. XFS_BMAP_INIT(&free_list, &firstfsb);
  3583. error = xfs_bunmapi(tp, ip, startoffset_fsb,
  3584. endoffset_fsb - startoffset_fsb,
  3585. 0, 2, &firstfsb, &free_list, NULL, &done);
  3586. if (error) {
  3587. goto error0;
  3588. }
  3589. /*
  3590. * complete the transaction
  3591. */
  3592. error = xfs_bmap_finish(&tp, &free_list, &committed);
  3593. if (error) {
  3594. goto error0;
  3595. }
  3596. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  3597. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  3598. }
  3599. out_unlock_iolock:
  3600. if (need_iolock)
  3601. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  3602. return error;
  3603. error0:
  3604. xfs_bmap_cancel(&free_list);
  3605. error1:
  3606. xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
  3607. xfs_iunlock(ip, need_iolock ? (XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL) :
  3608. XFS_ILOCK_EXCL);
  3609. return error;
  3610. }
  3611. /*
  3612. * xfs_change_file_space()
  3613. * This routine allocates or frees disk space for the given file.
  3614. * The user specified parameters are checked for alignment and size
  3615. * limitations.
  3616. *
  3617. * RETURNS:
  3618. * 0 on success
  3619. * errno on error
  3620. *
  3621. */
  3622. int
  3623. xfs_change_file_space(
  3624. xfs_inode_t *ip,
  3625. int cmd,
  3626. xfs_flock64_t *bf,
  3627. xfs_off_t offset,
  3628. cred_t *credp,
  3629. int attr_flags)
  3630. {
  3631. xfs_mount_t *mp = ip->i_mount;
  3632. int clrprealloc;
  3633. int error;
  3634. xfs_fsize_t fsize;
  3635. int setprealloc;
  3636. xfs_off_t startoffset;
  3637. xfs_off_t llen;
  3638. xfs_trans_t *tp;
  3639. bhv_vattr_t va;
  3640. xfs_itrace_entry(ip);
  3641. if (!S_ISREG(ip->i_d.di_mode))
  3642. return XFS_ERROR(EINVAL);
  3643. switch (bf->l_whence) {
  3644. case 0: /*SEEK_SET*/
  3645. break;
  3646. case 1: /*SEEK_CUR*/
  3647. bf->l_start += offset;
  3648. break;
  3649. case 2: /*SEEK_END*/
  3650. bf->l_start += ip->i_size;
  3651. break;
  3652. default:
  3653. return XFS_ERROR(EINVAL);
  3654. }
  3655. llen = bf->l_len > 0 ? bf->l_len - 1 : bf->l_len;
  3656. if ( (bf->l_start < 0)
  3657. || (bf->l_start > XFS_MAXIOFFSET(mp))
  3658. || (bf->l_start + llen < 0)
  3659. || (bf->l_start + llen > XFS_MAXIOFFSET(mp)))
  3660. return XFS_ERROR(EINVAL);
  3661. bf->l_whence = 0;
  3662. startoffset = bf->l_start;
  3663. fsize = ip->i_size;
  3664. /*
  3665. * XFS_IOC_RESVSP and XFS_IOC_UNRESVSP will reserve or unreserve
  3666. * file space.
  3667. * These calls do NOT zero the data space allocated to the file,
  3668. * nor do they change the file size.
  3669. *
  3670. * XFS_IOC_ALLOCSP and XFS_IOC_FREESP will allocate and free file
  3671. * space.
  3672. * These calls cause the new file data to be zeroed and the file
  3673. * size to be changed.
  3674. */
  3675. setprealloc = clrprealloc = 0;
  3676. switch (cmd) {
  3677. case XFS_IOC_RESVSP:
  3678. case XFS_IOC_RESVSP64:
  3679. error = xfs_alloc_file_space(ip, startoffset, bf->l_len,
  3680. 1, attr_flags);
  3681. if (error)
  3682. return error;
  3683. setprealloc = 1;
  3684. break;
  3685. case XFS_IOC_UNRESVSP:
  3686. case XFS_IOC_UNRESVSP64:
  3687. if ((error = xfs_free_file_space(ip, startoffset, bf->l_len,
  3688. attr_flags)))
  3689. return error;
  3690. break;
  3691. case XFS_IOC_ALLOCSP:
  3692. case XFS_IOC_ALLOCSP64:
  3693. case XFS_IOC_FREESP:
  3694. case XFS_IOC_FREESP64:
  3695. if (startoffset > fsize) {
  3696. error = xfs_alloc_file_space(ip, fsize,
  3697. startoffset - fsize, 0, attr_flags);
  3698. if (error)
  3699. break;
  3700. }
  3701. va.va_mask = XFS_AT_SIZE;
  3702. va.va_size = startoffset;
  3703. error = xfs_setattr(ip, &va, attr_flags, credp);
  3704. if (error)
  3705. return error;
  3706. clrprealloc = 1;
  3707. break;
  3708. default:
  3709. ASSERT(0);
  3710. return XFS_ERROR(EINVAL);
  3711. }
  3712. /*
  3713. * update the inode timestamp, mode, and prealloc flag bits
  3714. */
  3715. tp = xfs_trans_alloc(mp, XFS_TRANS_WRITEID);
  3716. if ((error = xfs_trans_reserve(tp, 0, XFS_WRITEID_LOG_RES(mp),
  3717. 0, 0, 0))) {
  3718. /* ASSERT(0); */
  3719. xfs_trans_cancel(tp, 0);
  3720. return error;
  3721. }
  3722. xfs_ilock(ip, XFS_ILOCK_EXCL);
  3723. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  3724. xfs_trans_ihold(tp, ip);
  3725. if ((attr_flags & ATTR_DMI) == 0) {
  3726. ip->i_d.di_mode &= ~S_ISUID;
  3727. /*
  3728. * Note that we don't have to worry about mandatory
  3729. * file locking being disabled here because we only
  3730. * clear the S_ISGID bit if the Group execute bit is
  3731. * on, but if it was on then mandatory locking wouldn't
  3732. * have been enabled.
  3733. */
  3734. if (ip->i_d.di_mode & S_IXGRP)
  3735. ip->i_d.di_mode &= ~S_ISGID;
  3736. xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  3737. }
  3738. if (setprealloc)
  3739. ip->i_d.di_flags |= XFS_DIFLAG_PREALLOC;
  3740. else if (clrprealloc)
  3741. ip->i_d.di_flags &= ~XFS_DIFLAG_PREALLOC;
  3742. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  3743. xfs_trans_set_sync(tp);
  3744. error = xfs_trans_commit(tp, 0);
  3745. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  3746. return error;
  3747. }