xfs_vnodeops.c 91 KB

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