xfs_vnodeops.c 104 KB

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