xfs_vnodeops.c 99 KB

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