xfs_vnodeops.c 95 KB

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