xfs_vnodeops.c 88 KB

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