xfs_vnodeops.c 87 KB

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