xfs_vnodeops.c 86 KB

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