xfs_vnodeops.c 99 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009
  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. #ifdef DEBUG
  1899. #define REMOVE_DEBUG_TRACE(x) {remove_which_error_return = (x);}
  1900. int remove_which_error_return = 0;
  1901. #else /* ! DEBUG */
  1902. #define REMOVE_DEBUG_TRACE(x)
  1903. #endif /* ! DEBUG */
  1904. int
  1905. xfs_remove(
  1906. xfs_inode_t *dp,
  1907. struct xfs_name *name,
  1908. xfs_inode_t *ip)
  1909. {
  1910. xfs_mount_t *mp = dp->i_mount;
  1911. xfs_trans_t *tp = NULL;
  1912. int error = 0;
  1913. xfs_bmap_free_t free_list;
  1914. xfs_fsblock_t first_block;
  1915. int cancel_flags;
  1916. int committed;
  1917. int link_zero;
  1918. uint resblks;
  1919. xfs_itrace_entry(dp);
  1920. if (XFS_FORCED_SHUTDOWN(mp))
  1921. return XFS_ERROR(EIO);
  1922. if (DM_EVENT_ENABLED(dp, DM_EVENT_REMOVE)) {
  1923. error = XFS_SEND_NAMESP(mp, DM_EVENT_REMOVE, dp, DM_RIGHT_NULL,
  1924. NULL, DM_RIGHT_NULL, name->name, NULL,
  1925. ip->i_d.di_mode, 0, 0);
  1926. if (error)
  1927. return error;
  1928. }
  1929. xfs_itrace_entry(ip);
  1930. xfs_itrace_ref(ip);
  1931. error = XFS_QM_DQATTACH(mp, dp, 0);
  1932. if (!error)
  1933. error = XFS_QM_DQATTACH(mp, ip, 0);
  1934. if (error) {
  1935. REMOVE_DEBUG_TRACE(__LINE__);
  1936. goto std_return;
  1937. }
  1938. tp = xfs_trans_alloc(mp, XFS_TRANS_REMOVE);
  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, XFS_REMOVE_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, XFS_REMOVE_LOG_COUNT);
  1956. }
  1957. if (error) {
  1958. ASSERT(error != ENOSPC);
  1959. REMOVE_DEBUG_TRACE(__LINE__);
  1960. xfs_trans_cancel(tp, 0);
  1961. return error;
  1962. }
  1963. error = xfs_lock_dir_and_entry(dp, ip);
  1964. if (error) {
  1965. REMOVE_DEBUG_TRACE(__LINE__);
  1966. xfs_trans_cancel(tp, cancel_flags);
  1967. goto std_return;
  1968. }
  1969. /*
  1970. * At this point, we've gotten both the directory and the entry
  1971. * inodes locked.
  1972. */
  1973. IHOLD(ip);
  1974. xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
  1975. IHOLD(dp);
  1976. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  1977. /*
  1978. * Entry must exist since we did a lookup in xfs_lock_dir_and_entry.
  1979. */
  1980. XFS_BMAP_INIT(&free_list, &first_block);
  1981. error = xfs_dir_removename(tp, dp, name, ip->i_ino,
  1982. &first_block, &free_list, resblks);
  1983. if (error) {
  1984. ASSERT(error != ENOENT);
  1985. REMOVE_DEBUG_TRACE(__LINE__);
  1986. goto error1;
  1987. }
  1988. xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  1989. dp->i_gen++;
  1990. xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
  1991. error = xfs_droplink(tp, ip);
  1992. if (error) {
  1993. REMOVE_DEBUG_TRACE(__LINE__);
  1994. goto error1;
  1995. }
  1996. /* Determine if this is the last link while
  1997. * we are in the transaction.
  1998. */
  1999. link_zero = (ip)->i_d.di_nlink==0;
  2000. /*
  2001. * If this is a synchronous mount, make sure that the
  2002. * remove transaction goes to disk before returning to
  2003. * the user.
  2004. */
  2005. if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
  2006. xfs_trans_set_sync(tp);
  2007. }
  2008. error = xfs_bmap_finish(&tp, &free_list, &committed);
  2009. if (error) {
  2010. REMOVE_DEBUG_TRACE(__LINE__);
  2011. goto error_rele;
  2012. }
  2013. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  2014. if (error)
  2015. goto std_return;
  2016. /*
  2017. * If we are using filestreams, kill the stream association.
  2018. * If the file is still open it may get a new one but that
  2019. * will get killed on last close in xfs_close() so we don't
  2020. * have to worry about that.
  2021. */
  2022. if (link_zero && xfs_inode_is_filestream(ip))
  2023. xfs_filestream_deassociate(ip);
  2024. xfs_itrace_exit(ip);
  2025. /* Fall through to std_return with error = 0 */
  2026. std_return:
  2027. if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTREMOVE)) {
  2028. (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTREMOVE,
  2029. dp, DM_RIGHT_NULL,
  2030. NULL, DM_RIGHT_NULL,
  2031. name->name, NULL, ip->i_d.di_mode, error, 0);
  2032. }
  2033. return error;
  2034. error1:
  2035. xfs_bmap_cancel(&free_list);
  2036. cancel_flags |= XFS_TRANS_ABORT;
  2037. xfs_trans_cancel(tp, cancel_flags);
  2038. goto std_return;
  2039. error_rele:
  2040. /*
  2041. * In this case make sure to not release the inode until after
  2042. * the current transaction is aborted. Releasing it beforehand
  2043. * can cause us to go to xfs_inactive and start a recursive
  2044. * transaction which can easily deadlock with the current one.
  2045. */
  2046. xfs_bmap_cancel(&free_list);
  2047. cancel_flags |= XFS_TRANS_ABORT;
  2048. xfs_trans_cancel(tp, cancel_flags);
  2049. goto std_return;
  2050. }
  2051. int
  2052. xfs_link(
  2053. xfs_inode_t *tdp,
  2054. xfs_inode_t *sip,
  2055. struct xfs_name *target_name)
  2056. {
  2057. xfs_mount_t *mp = tdp->i_mount;
  2058. xfs_trans_t *tp;
  2059. xfs_inode_t *ips[2];
  2060. int error;
  2061. xfs_bmap_free_t free_list;
  2062. xfs_fsblock_t first_block;
  2063. int cancel_flags;
  2064. int committed;
  2065. int resblks;
  2066. xfs_itrace_entry(tdp);
  2067. xfs_itrace_entry(sip);
  2068. ASSERT(!S_ISDIR(sip->i_d.di_mode));
  2069. if (XFS_FORCED_SHUTDOWN(mp))
  2070. return XFS_ERROR(EIO);
  2071. if (DM_EVENT_ENABLED(tdp, DM_EVENT_LINK)) {
  2072. error = XFS_SEND_NAMESP(mp, DM_EVENT_LINK,
  2073. tdp, DM_RIGHT_NULL,
  2074. sip, DM_RIGHT_NULL,
  2075. target_name->name, NULL, 0, 0, 0);
  2076. if (error)
  2077. return error;
  2078. }
  2079. /* Return through std_return after this point. */
  2080. error = XFS_QM_DQATTACH(mp, sip, 0);
  2081. if (!error && sip != tdp)
  2082. error = XFS_QM_DQATTACH(mp, tdp, 0);
  2083. if (error)
  2084. goto std_return;
  2085. tp = xfs_trans_alloc(mp, XFS_TRANS_LINK);
  2086. cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
  2087. resblks = XFS_LINK_SPACE_RES(mp, target_name->len);
  2088. error = xfs_trans_reserve(tp, resblks, XFS_LINK_LOG_RES(mp), 0,
  2089. XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
  2090. if (error == ENOSPC) {
  2091. resblks = 0;
  2092. error = xfs_trans_reserve(tp, 0, XFS_LINK_LOG_RES(mp), 0,
  2093. XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
  2094. }
  2095. if (error) {
  2096. cancel_flags = 0;
  2097. goto error_return;
  2098. }
  2099. if (sip->i_ino < tdp->i_ino) {
  2100. ips[0] = sip;
  2101. ips[1] = tdp;
  2102. } else {
  2103. ips[0] = tdp;
  2104. ips[1] = sip;
  2105. }
  2106. xfs_lock_inodes(ips, 2, XFS_ILOCK_EXCL);
  2107. /*
  2108. * Increment vnode ref counts since xfs_trans_commit &
  2109. * xfs_trans_cancel will both unlock the inodes and
  2110. * decrement the associated ref counts.
  2111. */
  2112. IHOLD(sip);
  2113. IHOLD(tdp);
  2114. xfs_trans_ijoin(tp, sip, XFS_ILOCK_EXCL);
  2115. xfs_trans_ijoin(tp, tdp, XFS_ILOCK_EXCL);
  2116. /*
  2117. * If the source has too many links, we can't make any more to it.
  2118. */
  2119. if (sip->i_d.di_nlink >= XFS_MAXLINK) {
  2120. error = XFS_ERROR(EMLINK);
  2121. goto error_return;
  2122. }
  2123. /*
  2124. * If we are using project inheritance, we only allow hard link
  2125. * creation in our tree when the project IDs are the same; else
  2126. * the tree quota mechanism could be circumvented.
  2127. */
  2128. if (unlikely((tdp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
  2129. (tdp->i_d.di_projid != sip->i_d.di_projid))) {
  2130. error = XFS_ERROR(EXDEV);
  2131. goto error_return;
  2132. }
  2133. error = xfs_dir_canenter(tp, tdp, target_name, resblks);
  2134. if (error)
  2135. goto error_return;
  2136. XFS_BMAP_INIT(&free_list, &first_block);
  2137. error = xfs_dir_createname(tp, tdp, target_name, sip->i_ino,
  2138. &first_block, &free_list, resblks);
  2139. if (error)
  2140. goto abort_return;
  2141. xfs_ichgtime(tdp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  2142. tdp->i_gen++;
  2143. xfs_trans_log_inode(tp, tdp, XFS_ILOG_CORE);
  2144. error = xfs_bumplink(tp, sip);
  2145. if (error)
  2146. goto abort_return;
  2147. /*
  2148. * If this is a synchronous mount, make sure that the
  2149. * link transaction goes to disk before returning to
  2150. * the user.
  2151. */
  2152. if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
  2153. xfs_trans_set_sync(tp);
  2154. }
  2155. error = xfs_bmap_finish (&tp, &free_list, &committed);
  2156. if (error) {
  2157. xfs_bmap_cancel(&free_list);
  2158. goto abort_return;
  2159. }
  2160. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  2161. if (error)
  2162. goto std_return;
  2163. /* Fall through to std_return with error = 0. */
  2164. std_return:
  2165. if (DM_EVENT_ENABLED(sip, DM_EVENT_POSTLINK)) {
  2166. (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTLINK,
  2167. tdp, DM_RIGHT_NULL,
  2168. sip, DM_RIGHT_NULL,
  2169. target_name->name, NULL, 0, error, 0);
  2170. }
  2171. return error;
  2172. abort_return:
  2173. cancel_flags |= XFS_TRANS_ABORT;
  2174. /* FALLTHROUGH */
  2175. error_return:
  2176. xfs_trans_cancel(tp, cancel_flags);
  2177. goto std_return;
  2178. }
  2179. int
  2180. xfs_mkdir(
  2181. xfs_inode_t *dp,
  2182. struct xfs_name *dir_name,
  2183. mode_t mode,
  2184. xfs_inode_t **ipp,
  2185. cred_t *credp)
  2186. {
  2187. xfs_mount_t *mp = dp->i_mount;
  2188. xfs_inode_t *cdp; /* inode of created dir */
  2189. xfs_trans_t *tp;
  2190. int cancel_flags;
  2191. int error;
  2192. int committed;
  2193. xfs_bmap_free_t free_list;
  2194. xfs_fsblock_t first_block;
  2195. boolean_t unlock_dp_on_error = B_FALSE;
  2196. boolean_t created = B_FALSE;
  2197. int dm_event_sent = 0;
  2198. xfs_prid_t prid;
  2199. struct xfs_dquot *udqp, *gdqp;
  2200. uint resblks;
  2201. if (XFS_FORCED_SHUTDOWN(mp))
  2202. return XFS_ERROR(EIO);
  2203. tp = NULL;
  2204. if (DM_EVENT_ENABLED(dp, DM_EVENT_CREATE)) {
  2205. error = XFS_SEND_NAMESP(mp, DM_EVENT_CREATE,
  2206. dp, DM_RIGHT_NULL, NULL,
  2207. DM_RIGHT_NULL, dir_name->name, NULL,
  2208. mode, 0, 0);
  2209. if (error)
  2210. return error;
  2211. dm_event_sent = 1;
  2212. }
  2213. /* Return through std_return after this point. */
  2214. xfs_itrace_entry(dp);
  2215. mp = dp->i_mount;
  2216. udqp = gdqp = NULL;
  2217. if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
  2218. prid = dp->i_d.di_projid;
  2219. else
  2220. prid = (xfs_prid_t)dfltprid;
  2221. /*
  2222. * Make sure that we have allocated dquot(s) on disk.
  2223. */
  2224. error = XFS_QM_DQVOPALLOC(mp, dp,
  2225. current_fsuid(credp), current_fsgid(credp), prid,
  2226. XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
  2227. if (error)
  2228. goto std_return;
  2229. tp = xfs_trans_alloc(mp, XFS_TRANS_MKDIR);
  2230. cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
  2231. resblks = XFS_MKDIR_SPACE_RES(mp, dir_name->len);
  2232. error = xfs_trans_reserve(tp, resblks, XFS_MKDIR_LOG_RES(mp), 0,
  2233. XFS_TRANS_PERM_LOG_RES, XFS_MKDIR_LOG_COUNT);
  2234. if (error == ENOSPC) {
  2235. resblks = 0;
  2236. error = xfs_trans_reserve(tp, 0, XFS_MKDIR_LOG_RES(mp), 0,
  2237. XFS_TRANS_PERM_LOG_RES,
  2238. XFS_MKDIR_LOG_COUNT);
  2239. }
  2240. if (error) {
  2241. cancel_flags = 0;
  2242. goto error_return;
  2243. }
  2244. xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
  2245. unlock_dp_on_error = B_TRUE;
  2246. /*
  2247. * Check for directory link count overflow.
  2248. */
  2249. if (dp->i_d.di_nlink >= XFS_MAXLINK) {
  2250. error = XFS_ERROR(EMLINK);
  2251. goto error_return;
  2252. }
  2253. /*
  2254. * Reserve disk quota and the inode.
  2255. */
  2256. error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
  2257. if (error)
  2258. goto error_return;
  2259. error = xfs_dir_canenter(tp, dp, dir_name, resblks);
  2260. if (error)
  2261. goto error_return;
  2262. /*
  2263. * create the directory inode.
  2264. */
  2265. error = xfs_dir_ialloc(&tp, dp, mode, 2,
  2266. 0, credp, prid, resblks > 0,
  2267. &cdp, NULL);
  2268. if (error) {
  2269. if (error == ENOSPC)
  2270. goto error_return;
  2271. goto abort_return;
  2272. }
  2273. xfs_itrace_ref(cdp);
  2274. /*
  2275. * Now we add the directory inode to the transaction.
  2276. * We waited until now since xfs_dir_ialloc might start
  2277. * a new transaction. Had we joined the transaction
  2278. * earlier, the locks might have gotten released. An error
  2279. * from here on will result in the transaction cancel
  2280. * unlocking dp so don't do it explicitly in the error path.
  2281. */
  2282. IHOLD(dp);
  2283. xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
  2284. unlock_dp_on_error = B_FALSE;
  2285. XFS_BMAP_INIT(&free_list, &first_block);
  2286. error = xfs_dir_createname(tp, dp, dir_name, cdp->i_ino,
  2287. &first_block, &free_list, resblks ?
  2288. resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
  2289. if (error) {
  2290. ASSERT(error != ENOSPC);
  2291. goto error1;
  2292. }
  2293. xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  2294. /*
  2295. * Bump the in memory version number of the parent directory
  2296. * so that other processes accessing it will recognize that
  2297. * the directory has changed.
  2298. */
  2299. dp->i_gen++;
  2300. error = xfs_dir_init(tp, cdp, dp);
  2301. if (error)
  2302. goto error2;
  2303. cdp->i_gen = 1;
  2304. error = xfs_bumplink(tp, dp);
  2305. if (error)
  2306. goto error2;
  2307. created = B_TRUE;
  2308. *ipp = cdp;
  2309. IHOLD(cdp);
  2310. /*
  2311. * Attach the dquots to the new inode and modify the icount incore.
  2312. */
  2313. XFS_QM_DQVOPCREATE(mp, tp, cdp, udqp, gdqp);
  2314. /*
  2315. * If this is a synchronous mount, make sure that the
  2316. * mkdir transaction goes to disk before returning to
  2317. * the user.
  2318. */
  2319. if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
  2320. xfs_trans_set_sync(tp);
  2321. }
  2322. error = xfs_bmap_finish(&tp, &free_list, &committed);
  2323. if (error) {
  2324. IRELE(cdp);
  2325. goto error2;
  2326. }
  2327. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  2328. XFS_QM_DQRELE(mp, udqp);
  2329. XFS_QM_DQRELE(mp, gdqp);
  2330. if (error) {
  2331. IRELE(cdp);
  2332. }
  2333. /* Fall through to std_return with error = 0 or errno from
  2334. * xfs_trans_commit. */
  2335. std_return:
  2336. if ((created || (error != 0 && dm_event_sent != 0)) &&
  2337. DM_EVENT_ENABLED(dp, DM_EVENT_POSTCREATE)) {
  2338. (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTCREATE,
  2339. dp, DM_RIGHT_NULL,
  2340. created ? cdp : NULL,
  2341. DM_RIGHT_NULL,
  2342. dir_name->name, NULL,
  2343. mode, error, 0);
  2344. }
  2345. return error;
  2346. error2:
  2347. error1:
  2348. xfs_bmap_cancel(&free_list);
  2349. abort_return:
  2350. cancel_flags |= XFS_TRANS_ABORT;
  2351. error_return:
  2352. xfs_trans_cancel(tp, cancel_flags);
  2353. XFS_QM_DQRELE(mp, udqp);
  2354. XFS_QM_DQRELE(mp, gdqp);
  2355. if (unlock_dp_on_error)
  2356. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  2357. goto std_return;
  2358. }
  2359. int
  2360. xfs_rmdir(
  2361. xfs_inode_t *dp,
  2362. struct xfs_name *name,
  2363. xfs_inode_t *cdp)
  2364. {
  2365. xfs_mount_t *mp = dp->i_mount;
  2366. xfs_trans_t *tp;
  2367. int error;
  2368. xfs_bmap_free_t free_list;
  2369. xfs_fsblock_t first_block;
  2370. int cancel_flags;
  2371. int committed;
  2372. int last_cdp_link;
  2373. uint resblks;
  2374. xfs_itrace_entry(dp);
  2375. if (XFS_FORCED_SHUTDOWN(mp))
  2376. return XFS_ERROR(EIO);
  2377. if (DM_EVENT_ENABLED(dp, DM_EVENT_REMOVE)) {
  2378. error = XFS_SEND_NAMESP(mp, DM_EVENT_REMOVE,
  2379. dp, DM_RIGHT_NULL,
  2380. NULL, DM_RIGHT_NULL, name->name,
  2381. NULL, cdp->i_d.di_mode, 0, 0);
  2382. if (error)
  2383. return XFS_ERROR(error);
  2384. }
  2385. /*
  2386. * Get the dquots for the inodes.
  2387. */
  2388. error = XFS_QM_DQATTACH(mp, dp, 0);
  2389. if (!error)
  2390. error = XFS_QM_DQATTACH(mp, cdp, 0);
  2391. if (error) {
  2392. REMOVE_DEBUG_TRACE(__LINE__);
  2393. goto std_return;
  2394. }
  2395. tp = xfs_trans_alloc(mp, XFS_TRANS_RMDIR);
  2396. cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
  2397. /*
  2398. * We try to get the real space reservation first,
  2399. * allowing for directory btree deletion(s) implying
  2400. * possible bmap insert(s). If we can't get the space
  2401. * reservation then we use 0 instead, and avoid the bmap
  2402. * btree insert(s) in the directory code by, if the bmap
  2403. * insert tries to happen, instead trimming the LAST
  2404. * block from the directory.
  2405. */
  2406. resblks = XFS_REMOVE_SPACE_RES(mp);
  2407. error = xfs_trans_reserve(tp, resblks, XFS_REMOVE_LOG_RES(mp), 0,
  2408. XFS_TRANS_PERM_LOG_RES, XFS_DEFAULT_LOG_COUNT);
  2409. if (error == ENOSPC) {
  2410. resblks = 0;
  2411. error = xfs_trans_reserve(tp, 0, XFS_REMOVE_LOG_RES(mp), 0,
  2412. XFS_TRANS_PERM_LOG_RES, XFS_DEFAULT_LOG_COUNT);
  2413. }
  2414. if (error) {
  2415. ASSERT(error != ENOSPC);
  2416. cancel_flags = 0;
  2417. goto error_return;
  2418. }
  2419. XFS_BMAP_INIT(&free_list, &first_block);
  2420. /*
  2421. * Now lock the child directory inode and the parent directory
  2422. * inode in the proper order. This will take care of validating
  2423. * that the directory entry for the child directory inode has
  2424. * not changed while we were obtaining a log reservation.
  2425. */
  2426. error = xfs_lock_dir_and_entry(dp, cdp);
  2427. if (error) {
  2428. xfs_trans_cancel(tp, cancel_flags);
  2429. goto std_return;
  2430. }
  2431. IHOLD(dp);
  2432. xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
  2433. IHOLD(cdp);
  2434. xfs_trans_ijoin(tp, cdp, XFS_ILOCK_EXCL);
  2435. ASSERT(cdp->i_d.di_nlink >= 2);
  2436. if (cdp->i_d.di_nlink != 2) {
  2437. error = XFS_ERROR(ENOTEMPTY);
  2438. goto error_return;
  2439. }
  2440. if (!xfs_dir_isempty(cdp)) {
  2441. error = XFS_ERROR(ENOTEMPTY);
  2442. goto error_return;
  2443. }
  2444. error = xfs_dir_removename(tp, dp, name, cdp->i_ino,
  2445. &first_block, &free_list, resblks);
  2446. if (error)
  2447. goto error1;
  2448. xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  2449. /*
  2450. * Bump the in memory generation count on the parent
  2451. * directory so that other can know that it has changed.
  2452. */
  2453. dp->i_gen++;
  2454. /*
  2455. * Drop the link from cdp's "..".
  2456. */
  2457. error = xfs_droplink(tp, dp);
  2458. if (error) {
  2459. goto error1;
  2460. }
  2461. /*
  2462. * Drop the link from dp to cdp.
  2463. */
  2464. error = xfs_droplink(tp, cdp);
  2465. if (error) {
  2466. goto error1;
  2467. }
  2468. /*
  2469. * Drop the "." link from cdp to self.
  2470. */
  2471. error = xfs_droplink(tp, cdp);
  2472. if (error) {
  2473. goto error1;
  2474. }
  2475. /* Determine these before committing transaction */
  2476. last_cdp_link = (cdp)->i_d.di_nlink==0;
  2477. /*
  2478. * If this is a synchronous mount, make sure that the
  2479. * rmdir transaction goes to disk before returning to
  2480. * the user.
  2481. */
  2482. if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
  2483. xfs_trans_set_sync(tp);
  2484. }
  2485. error = xfs_bmap_finish (&tp, &free_list, &committed);
  2486. if (error) {
  2487. xfs_bmap_cancel(&free_list);
  2488. xfs_trans_cancel(tp, (XFS_TRANS_RELEASE_LOG_RES |
  2489. XFS_TRANS_ABORT));
  2490. goto std_return;
  2491. }
  2492. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  2493. if (error) {
  2494. goto std_return;
  2495. }
  2496. /* Fall through to std_return with error = 0 or the errno
  2497. * from xfs_trans_commit. */
  2498. std_return:
  2499. if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTREMOVE)) {
  2500. (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTREMOVE,
  2501. dp, DM_RIGHT_NULL,
  2502. NULL, DM_RIGHT_NULL,
  2503. name->name, NULL, cdp->i_d.di_mode,
  2504. error, 0);
  2505. }
  2506. return error;
  2507. error1:
  2508. xfs_bmap_cancel(&free_list);
  2509. cancel_flags |= XFS_TRANS_ABORT;
  2510. /* FALLTHROUGH */
  2511. error_return:
  2512. xfs_trans_cancel(tp, cancel_flags);
  2513. goto std_return;
  2514. }
  2515. int
  2516. xfs_symlink(
  2517. xfs_inode_t *dp,
  2518. struct xfs_name *link_name,
  2519. const char *target_path,
  2520. mode_t mode,
  2521. xfs_inode_t **ipp,
  2522. cred_t *credp)
  2523. {
  2524. xfs_mount_t *mp = dp->i_mount;
  2525. xfs_trans_t *tp;
  2526. xfs_inode_t *ip;
  2527. int error;
  2528. int pathlen;
  2529. xfs_bmap_free_t free_list;
  2530. xfs_fsblock_t first_block;
  2531. boolean_t unlock_dp_on_error = B_FALSE;
  2532. uint cancel_flags;
  2533. int committed;
  2534. xfs_fileoff_t first_fsb;
  2535. xfs_filblks_t fs_blocks;
  2536. int nmaps;
  2537. xfs_bmbt_irec_t mval[SYMLINK_MAPS];
  2538. xfs_daddr_t d;
  2539. const char *cur_chunk;
  2540. int byte_cnt;
  2541. int n;
  2542. xfs_buf_t *bp;
  2543. xfs_prid_t prid;
  2544. struct xfs_dquot *udqp, *gdqp;
  2545. uint resblks;
  2546. *ipp = NULL;
  2547. error = 0;
  2548. ip = NULL;
  2549. tp = NULL;
  2550. xfs_itrace_entry(dp);
  2551. if (XFS_FORCED_SHUTDOWN(mp))
  2552. return XFS_ERROR(EIO);
  2553. /*
  2554. * Check component lengths of the target path name.
  2555. */
  2556. pathlen = strlen(target_path);
  2557. if (pathlen >= MAXPATHLEN) /* total string too long */
  2558. return XFS_ERROR(ENAMETOOLONG);
  2559. if (DM_EVENT_ENABLED(dp, DM_EVENT_SYMLINK)) {
  2560. error = XFS_SEND_NAMESP(mp, DM_EVENT_SYMLINK, dp,
  2561. DM_RIGHT_NULL, NULL, DM_RIGHT_NULL,
  2562. link_name->name, target_path, 0, 0, 0);
  2563. if (error)
  2564. return error;
  2565. }
  2566. /* Return through std_return after this point. */
  2567. udqp = gdqp = NULL;
  2568. if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
  2569. prid = dp->i_d.di_projid;
  2570. else
  2571. prid = (xfs_prid_t)dfltprid;
  2572. /*
  2573. * Make sure that we have allocated dquot(s) on disk.
  2574. */
  2575. error = XFS_QM_DQVOPALLOC(mp, dp,
  2576. current_fsuid(credp), current_fsgid(credp), prid,
  2577. XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
  2578. if (error)
  2579. goto std_return;
  2580. tp = xfs_trans_alloc(mp, XFS_TRANS_SYMLINK);
  2581. cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
  2582. /*
  2583. * The symlink will fit into the inode data fork?
  2584. * There can't be any attributes so we get the whole variable part.
  2585. */
  2586. if (pathlen <= XFS_LITINO(mp))
  2587. fs_blocks = 0;
  2588. else
  2589. fs_blocks = XFS_B_TO_FSB(mp, pathlen);
  2590. resblks = XFS_SYMLINK_SPACE_RES(mp, link_name->len, fs_blocks);
  2591. error = xfs_trans_reserve(tp, resblks, XFS_SYMLINK_LOG_RES(mp), 0,
  2592. XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
  2593. if (error == ENOSPC && fs_blocks == 0) {
  2594. resblks = 0;
  2595. error = xfs_trans_reserve(tp, 0, XFS_SYMLINK_LOG_RES(mp), 0,
  2596. XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
  2597. }
  2598. if (error) {
  2599. cancel_flags = 0;
  2600. goto error_return;
  2601. }
  2602. xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
  2603. unlock_dp_on_error = B_TRUE;
  2604. /*
  2605. * Check whether the directory allows new symlinks or not.
  2606. */
  2607. if (dp->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) {
  2608. error = XFS_ERROR(EPERM);
  2609. goto error_return;
  2610. }
  2611. /*
  2612. * Reserve disk quota : blocks and inode.
  2613. */
  2614. error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
  2615. if (error)
  2616. goto error_return;
  2617. /*
  2618. * Check for ability to enter directory entry, if no space reserved.
  2619. */
  2620. error = xfs_dir_canenter(tp, dp, link_name, resblks);
  2621. if (error)
  2622. goto error_return;
  2623. /*
  2624. * Initialize the bmap freelist prior to calling either
  2625. * bmapi or the directory create code.
  2626. */
  2627. XFS_BMAP_INIT(&free_list, &first_block);
  2628. /*
  2629. * Allocate an inode for the symlink.
  2630. */
  2631. error = xfs_dir_ialloc(&tp, dp, S_IFLNK | (mode & ~S_IFMT),
  2632. 1, 0, credp, prid, resblks > 0, &ip, NULL);
  2633. if (error) {
  2634. if (error == ENOSPC)
  2635. goto error_return;
  2636. goto error1;
  2637. }
  2638. xfs_itrace_ref(ip);
  2639. /*
  2640. * An error after we've joined dp to the transaction will result in the
  2641. * transaction cancel unlocking dp so don't do it explicitly in the
  2642. * error path.
  2643. */
  2644. IHOLD(dp);
  2645. xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
  2646. unlock_dp_on_error = B_FALSE;
  2647. /*
  2648. * Also attach the dquot(s) to it, if applicable.
  2649. */
  2650. XFS_QM_DQVOPCREATE(mp, tp, ip, udqp, gdqp);
  2651. if (resblks)
  2652. resblks -= XFS_IALLOC_SPACE_RES(mp);
  2653. /*
  2654. * If the symlink will fit into the inode, write it inline.
  2655. */
  2656. if (pathlen <= XFS_IFORK_DSIZE(ip)) {
  2657. xfs_idata_realloc(ip, pathlen, XFS_DATA_FORK);
  2658. memcpy(ip->i_df.if_u1.if_data, target_path, pathlen);
  2659. ip->i_d.di_size = pathlen;
  2660. /*
  2661. * The inode was initially created in extent format.
  2662. */
  2663. ip->i_df.if_flags &= ~(XFS_IFEXTENTS | XFS_IFBROOT);
  2664. ip->i_df.if_flags |= XFS_IFINLINE;
  2665. ip->i_d.di_format = XFS_DINODE_FMT_LOCAL;
  2666. xfs_trans_log_inode(tp, ip, XFS_ILOG_DDATA | XFS_ILOG_CORE);
  2667. } else {
  2668. first_fsb = 0;
  2669. nmaps = SYMLINK_MAPS;
  2670. error = xfs_bmapi(tp, ip, first_fsb, fs_blocks,
  2671. XFS_BMAPI_WRITE | XFS_BMAPI_METADATA,
  2672. &first_block, resblks, mval, &nmaps,
  2673. &free_list, NULL);
  2674. if (error) {
  2675. goto error1;
  2676. }
  2677. if (resblks)
  2678. resblks -= fs_blocks;
  2679. ip->i_d.di_size = pathlen;
  2680. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  2681. cur_chunk = target_path;
  2682. for (n = 0; n < nmaps; n++) {
  2683. d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
  2684. byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
  2685. bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
  2686. BTOBB(byte_cnt), 0);
  2687. ASSERT(bp && !XFS_BUF_GETERROR(bp));
  2688. if (pathlen < byte_cnt) {
  2689. byte_cnt = pathlen;
  2690. }
  2691. pathlen -= byte_cnt;
  2692. memcpy(XFS_BUF_PTR(bp), cur_chunk, byte_cnt);
  2693. cur_chunk += byte_cnt;
  2694. xfs_trans_log_buf(tp, bp, 0, byte_cnt - 1);
  2695. }
  2696. }
  2697. /*
  2698. * Create the directory entry for the symlink.
  2699. */
  2700. error = xfs_dir_createname(tp, dp, link_name, ip->i_ino,
  2701. &first_block, &free_list, resblks);
  2702. if (error)
  2703. goto error1;
  2704. xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  2705. xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
  2706. /*
  2707. * Bump the in memory version number of the parent directory
  2708. * so that other processes accessing it will recognize that
  2709. * the directory has changed.
  2710. */
  2711. dp->i_gen++;
  2712. /*
  2713. * If this is a synchronous mount, make sure that the
  2714. * symlink transaction goes to disk before returning to
  2715. * the user.
  2716. */
  2717. if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
  2718. xfs_trans_set_sync(tp);
  2719. }
  2720. /*
  2721. * xfs_trans_commit normally decrements the vnode ref count
  2722. * when it unlocks the inode. Since we want to return the
  2723. * vnode to the caller, we bump the vnode ref count now.
  2724. */
  2725. IHOLD(ip);
  2726. error = xfs_bmap_finish(&tp, &free_list, &committed);
  2727. if (error) {
  2728. goto error2;
  2729. }
  2730. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  2731. XFS_QM_DQRELE(mp, udqp);
  2732. XFS_QM_DQRELE(mp, gdqp);
  2733. /* Fall through to std_return with error = 0 or errno from
  2734. * xfs_trans_commit */
  2735. std_return:
  2736. if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTSYMLINK)) {
  2737. (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTSYMLINK,
  2738. dp, DM_RIGHT_NULL,
  2739. error ? NULL : ip,
  2740. DM_RIGHT_NULL, link_name->name,
  2741. target_path, 0, error, 0);
  2742. }
  2743. if (!error)
  2744. *ipp = ip;
  2745. return error;
  2746. error2:
  2747. IRELE(ip);
  2748. error1:
  2749. xfs_bmap_cancel(&free_list);
  2750. cancel_flags |= XFS_TRANS_ABORT;
  2751. error_return:
  2752. xfs_trans_cancel(tp, cancel_flags);
  2753. XFS_QM_DQRELE(mp, udqp);
  2754. XFS_QM_DQRELE(mp, gdqp);
  2755. if (unlock_dp_on_error)
  2756. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  2757. goto std_return;
  2758. }
  2759. int
  2760. xfs_inode_flush(
  2761. xfs_inode_t *ip,
  2762. int flags)
  2763. {
  2764. xfs_mount_t *mp = ip->i_mount;
  2765. int error = 0;
  2766. if (XFS_FORCED_SHUTDOWN(mp))
  2767. return XFS_ERROR(EIO);
  2768. /*
  2769. * Bypass inodes which have already been cleaned by
  2770. * the inode flush clustering code inside xfs_iflush
  2771. */
  2772. if (xfs_inode_clean(ip))
  2773. return 0;
  2774. /*
  2775. * We make this non-blocking if the inode is contended,
  2776. * return EAGAIN to indicate to the caller that they
  2777. * did not succeed. This prevents the flush path from
  2778. * blocking on inodes inside another operation right
  2779. * now, they get caught later by xfs_sync.
  2780. */
  2781. if (flags & FLUSH_SYNC) {
  2782. xfs_ilock(ip, XFS_ILOCK_SHARED);
  2783. xfs_iflock(ip);
  2784. } else if (xfs_ilock_nowait(ip, XFS_ILOCK_SHARED)) {
  2785. if (xfs_ipincount(ip) || !xfs_iflock_nowait(ip)) {
  2786. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  2787. return EAGAIN;
  2788. }
  2789. } else {
  2790. return EAGAIN;
  2791. }
  2792. error = xfs_iflush(ip, (flags & FLUSH_SYNC) ? XFS_IFLUSH_SYNC
  2793. : XFS_IFLUSH_ASYNC_NOBLOCK);
  2794. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  2795. return error;
  2796. }
  2797. int
  2798. xfs_set_dmattrs(
  2799. xfs_inode_t *ip,
  2800. u_int evmask,
  2801. u_int16_t state)
  2802. {
  2803. xfs_mount_t *mp = ip->i_mount;
  2804. xfs_trans_t *tp;
  2805. int error;
  2806. if (!capable(CAP_SYS_ADMIN))
  2807. return XFS_ERROR(EPERM);
  2808. if (XFS_FORCED_SHUTDOWN(mp))
  2809. return XFS_ERROR(EIO);
  2810. tp = xfs_trans_alloc(mp, XFS_TRANS_SET_DMATTRS);
  2811. error = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES (mp), 0, 0, 0);
  2812. if (error) {
  2813. xfs_trans_cancel(tp, 0);
  2814. return error;
  2815. }
  2816. xfs_ilock(ip, XFS_ILOCK_EXCL);
  2817. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  2818. ip->i_d.di_dmevmask = evmask;
  2819. ip->i_d.di_dmstate = state;
  2820. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  2821. IHOLD(ip);
  2822. error = xfs_trans_commit(tp, 0);
  2823. return error;
  2824. }
  2825. int
  2826. xfs_reclaim(
  2827. xfs_inode_t *ip)
  2828. {
  2829. bhv_vnode_t *vp = XFS_ITOV(ip);
  2830. xfs_itrace_entry(ip);
  2831. ASSERT(!VN_MAPPED(vp));
  2832. /* bad inode, get out here ASAP */
  2833. if (VN_BAD(vp)) {
  2834. xfs_ireclaim(ip);
  2835. return 0;
  2836. }
  2837. vn_iowait(ip);
  2838. ASSERT(XFS_FORCED_SHUTDOWN(ip->i_mount) || ip->i_delayed_blks == 0);
  2839. /*
  2840. * Make sure the atime in the XFS inode is correct before freeing the
  2841. * Linux inode.
  2842. */
  2843. xfs_synchronize_atime(ip);
  2844. /*
  2845. * If we have nothing to flush with this inode then complete the
  2846. * teardown now, otherwise break the link between the xfs inode and the
  2847. * linux inode and clean up the xfs inode later. This avoids flushing
  2848. * the inode to disk during the delete operation itself.
  2849. *
  2850. * When breaking the link, we need to set the XFS_IRECLAIMABLE flag
  2851. * first to ensure that xfs_iunpin() will never see an xfs inode
  2852. * that has a linux inode being reclaimed. Synchronisation is provided
  2853. * by the i_flags_lock.
  2854. */
  2855. if (!ip->i_update_core && (ip->i_itemp == NULL)) {
  2856. xfs_ilock(ip, XFS_ILOCK_EXCL);
  2857. xfs_iflock(ip);
  2858. return xfs_finish_reclaim(ip, 1, XFS_IFLUSH_DELWRI_ELSE_SYNC);
  2859. } else {
  2860. xfs_mount_t *mp = ip->i_mount;
  2861. /* Protect sync and unpin from us */
  2862. XFS_MOUNT_ILOCK(mp);
  2863. spin_lock(&ip->i_flags_lock);
  2864. __xfs_iflags_set(ip, XFS_IRECLAIMABLE);
  2865. vn_to_inode(vp)->i_private = NULL;
  2866. ip->i_vnode = NULL;
  2867. spin_unlock(&ip->i_flags_lock);
  2868. list_add_tail(&ip->i_reclaim, &mp->m_del_inodes);
  2869. XFS_MOUNT_IUNLOCK(mp);
  2870. }
  2871. return 0;
  2872. }
  2873. int
  2874. xfs_finish_reclaim(
  2875. xfs_inode_t *ip,
  2876. int locked,
  2877. int sync_mode)
  2878. {
  2879. xfs_perag_t *pag = xfs_get_perag(ip->i_mount, ip->i_ino);
  2880. bhv_vnode_t *vp = XFS_ITOV_NULL(ip);
  2881. if (vp && VN_BAD(vp))
  2882. goto reclaim;
  2883. /* The hash lock here protects a thread in xfs_iget_core from
  2884. * racing with us on linking the inode back with a vnode.
  2885. * Once we have the XFS_IRECLAIM flag set it will not touch
  2886. * us.
  2887. */
  2888. write_lock(&pag->pag_ici_lock);
  2889. spin_lock(&ip->i_flags_lock);
  2890. if (__xfs_iflags_test(ip, XFS_IRECLAIM) ||
  2891. (!__xfs_iflags_test(ip, XFS_IRECLAIMABLE) && vp == NULL)) {
  2892. spin_unlock(&ip->i_flags_lock);
  2893. write_unlock(&pag->pag_ici_lock);
  2894. if (locked) {
  2895. xfs_ifunlock(ip);
  2896. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  2897. }
  2898. return 1;
  2899. }
  2900. __xfs_iflags_set(ip, XFS_IRECLAIM);
  2901. spin_unlock(&ip->i_flags_lock);
  2902. write_unlock(&pag->pag_ici_lock);
  2903. xfs_put_perag(ip->i_mount, pag);
  2904. /*
  2905. * If the inode is still dirty, then flush it out. If the inode
  2906. * is not in the AIL, then it will be OK to flush it delwri as
  2907. * long as xfs_iflush() does not keep any references to the inode.
  2908. * We leave that decision up to xfs_iflush() since it has the
  2909. * knowledge of whether it's OK to simply do a delwri flush of
  2910. * the inode or whether we need to wait until the inode is
  2911. * pulled from the AIL.
  2912. * We get the flush lock regardless, though, just to make sure
  2913. * we don't free it while it is being flushed.
  2914. */
  2915. if (!locked) {
  2916. xfs_ilock(ip, XFS_ILOCK_EXCL);
  2917. xfs_iflock(ip);
  2918. }
  2919. /*
  2920. * In the case of a forced shutdown we rely on xfs_iflush() to
  2921. * wait for the inode to be unpinned before returning an error.
  2922. */
  2923. if (xfs_iflush(ip, sync_mode) == 0) {
  2924. /* synchronize with xfs_iflush_done */
  2925. xfs_iflock(ip);
  2926. xfs_ifunlock(ip);
  2927. }
  2928. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  2929. reclaim:
  2930. xfs_ireclaim(ip);
  2931. return 0;
  2932. }
  2933. int
  2934. xfs_finish_reclaim_all(xfs_mount_t *mp, int noblock)
  2935. {
  2936. int purged;
  2937. xfs_inode_t *ip, *n;
  2938. int done = 0;
  2939. while (!done) {
  2940. purged = 0;
  2941. XFS_MOUNT_ILOCK(mp);
  2942. list_for_each_entry_safe(ip, n, &mp->m_del_inodes, i_reclaim) {
  2943. if (noblock) {
  2944. if (xfs_ilock_nowait(ip, XFS_ILOCK_EXCL) == 0)
  2945. continue;
  2946. if (xfs_ipincount(ip) ||
  2947. !xfs_iflock_nowait(ip)) {
  2948. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  2949. continue;
  2950. }
  2951. }
  2952. XFS_MOUNT_IUNLOCK(mp);
  2953. if (xfs_finish_reclaim(ip, noblock,
  2954. XFS_IFLUSH_DELWRI_ELSE_ASYNC))
  2955. delay(1);
  2956. purged = 1;
  2957. break;
  2958. }
  2959. done = !purged;
  2960. }
  2961. XFS_MOUNT_IUNLOCK(mp);
  2962. return 0;
  2963. }
  2964. /*
  2965. * xfs_alloc_file_space()
  2966. * This routine allocates disk space for the given file.
  2967. *
  2968. * If alloc_type == 0, this request is for an ALLOCSP type
  2969. * request which will change the file size. In this case, no
  2970. * DMAPI event will be generated by the call. A TRUNCATE event
  2971. * will be generated later by xfs_setattr.
  2972. *
  2973. * If alloc_type != 0, this request is for a RESVSP type
  2974. * request, and a DMAPI DM_EVENT_WRITE will be generated if the
  2975. * lower block boundary byte address is less than the file's
  2976. * length.
  2977. *
  2978. * RETURNS:
  2979. * 0 on success
  2980. * errno on error
  2981. *
  2982. */
  2983. STATIC int
  2984. xfs_alloc_file_space(
  2985. xfs_inode_t *ip,
  2986. xfs_off_t offset,
  2987. xfs_off_t len,
  2988. int alloc_type,
  2989. int attr_flags)
  2990. {
  2991. xfs_mount_t *mp = ip->i_mount;
  2992. xfs_off_t count;
  2993. xfs_filblks_t allocated_fsb;
  2994. xfs_filblks_t allocatesize_fsb;
  2995. xfs_extlen_t extsz, temp;
  2996. xfs_fileoff_t startoffset_fsb;
  2997. xfs_fsblock_t firstfsb;
  2998. int nimaps;
  2999. int bmapi_flag;
  3000. int quota_flag;
  3001. int rt;
  3002. xfs_trans_t *tp;
  3003. xfs_bmbt_irec_t imaps[1], *imapp;
  3004. xfs_bmap_free_t free_list;
  3005. uint qblocks, resblks, resrtextents;
  3006. int committed;
  3007. int error;
  3008. xfs_itrace_entry(ip);
  3009. if (XFS_FORCED_SHUTDOWN(mp))
  3010. return XFS_ERROR(EIO);
  3011. if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
  3012. return error;
  3013. if (len <= 0)
  3014. return XFS_ERROR(EINVAL);
  3015. rt = XFS_IS_REALTIME_INODE(ip);
  3016. extsz = xfs_get_extsz_hint(ip);
  3017. count = len;
  3018. imapp = &imaps[0];
  3019. nimaps = 1;
  3020. bmapi_flag = XFS_BMAPI_WRITE | (alloc_type ? XFS_BMAPI_PREALLOC : 0);
  3021. startoffset_fsb = XFS_B_TO_FSBT(mp, offset);
  3022. allocatesize_fsb = XFS_B_TO_FSB(mp, count);
  3023. /* Generate a DMAPI event if needed. */
  3024. if (alloc_type != 0 && offset < ip->i_size &&
  3025. (attr_flags&ATTR_DMI) == 0 &&
  3026. DM_EVENT_ENABLED(ip, DM_EVENT_WRITE)) {
  3027. xfs_off_t end_dmi_offset;
  3028. end_dmi_offset = offset+len;
  3029. if (end_dmi_offset > ip->i_size)
  3030. end_dmi_offset = ip->i_size;
  3031. error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, ip, offset,
  3032. end_dmi_offset - offset, 0, NULL);
  3033. if (error)
  3034. return error;
  3035. }
  3036. /*
  3037. * Allocate file space until done or until there is an error
  3038. */
  3039. retry:
  3040. while (allocatesize_fsb && !error) {
  3041. xfs_fileoff_t s, e;
  3042. /*
  3043. * Determine space reservations for data/realtime.
  3044. */
  3045. if (unlikely(extsz)) {
  3046. s = startoffset_fsb;
  3047. do_div(s, extsz);
  3048. s *= extsz;
  3049. e = startoffset_fsb + allocatesize_fsb;
  3050. if ((temp = do_mod(startoffset_fsb, extsz)))
  3051. e += temp;
  3052. if ((temp = do_mod(e, extsz)))
  3053. e += extsz - temp;
  3054. } else {
  3055. s = 0;
  3056. e = allocatesize_fsb;
  3057. }
  3058. if (unlikely(rt)) {
  3059. resrtextents = qblocks = (uint)(e - s);
  3060. resrtextents /= mp->m_sb.sb_rextsize;
  3061. resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
  3062. quota_flag = XFS_QMOPT_RES_RTBLKS;
  3063. } else {
  3064. resrtextents = 0;
  3065. resblks = qblocks = \
  3066. XFS_DIOSTRAT_SPACE_RES(mp, (uint)(e - s));
  3067. quota_flag = XFS_QMOPT_RES_REGBLKS;
  3068. }
  3069. /*
  3070. * Allocate and setup the transaction.
  3071. */
  3072. tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
  3073. error = xfs_trans_reserve(tp, resblks,
  3074. XFS_WRITE_LOG_RES(mp), resrtextents,
  3075. XFS_TRANS_PERM_LOG_RES,
  3076. XFS_WRITE_LOG_COUNT);
  3077. /*
  3078. * Check for running out of space
  3079. */
  3080. if (error) {
  3081. /*
  3082. * Free the transaction structure.
  3083. */
  3084. ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
  3085. xfs_trans_cancel(tp, 0);
  3086. break;
  3087. }
  3088. xfs_ilock(ip, XFS_ILOCK_EXCL);
  3089. error = XFS_TRANS_RESERVE_QUOTA_NBLKS(mp, tp, ip,
  3090. qblocks, 0, quota_flag);
  3091. if (error)
  3092. goto error1;
  3093. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  3094. xfs_trans_ihold(tp, ip);
  3095. /*
  3096. * Issue the xfs_bmapi() call to allocate the blocks
  3097. */
  3098. XFS_BMAP_INIT(&free_list, &firstfsb);
  3099. error = xfs_bmapi(tp, ip, startoffset_fsb,
  3100. allocatesize_fsb, bmapi_flag,
  3101. &firstfsb, 0, imapp, &nimaps,
  3102. &free_list, NULL);
  3103. if (error) {
  3104. goto error0;
  3105. }
  3106. /*
  3107. * Complete the transaction
  3108. */
  3109. error = xfs_bmap_finish(&tp, &free_list, &committed);
  3110. if (error) {
  3111. goto error0;
  3112. }
  3113. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  3114. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  3115. if (error) {
  3116. break;
  3117. }
  3118. allocated_fsb = imapp->br_blockcount;
  3119. if (nimaps == 0) {
  3120. error = XFS_ERROR(ENOSPC);
  3121. break;
  3122. }
  3123. startoffset_fsb += allocated_fsb;
  3124. allocatesize_fsb -= allocated_fsb;
  3125. }
  3126. dmapi_enospc_check:
  3127. if (error == ENOSPC && (attr_flags & ATTR_DMI) == 0 &&
  3128. DM_EVENT_ENABLED(ip, DM_EVENT_NOSPACE)) {
  3129. error = XFS_SEND_NAMESP(mp, DM_EVENT_NOSPACE,
  3130. ip, DM_RIGHT_NULL,
  3131. ip, DM_RIGHT_NULL,
  3132. NULL, NULL, 0, 0, 0); /* Delay flag intentionally unused */
  3133. if (error == 0)
  3134. goto retry; /* Maybe DMAPI app. has made space */
  3135. /* else fall through with error from XFS_SEND_DATA */
  3136. }
  3137. return error;
  3138. error0: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
  3139. xfs_bmap_cancel(&free_list);
  3140. XFS_TRANS_UNRESERVE_QUOTA_NBLKS(mp, tp, ip, qblocks, 0, quota_flag);
  3141. error1: /* Just cancel transaction */
  3142. xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
  3143. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  3144. goto dmapi_enospc_check;
  3145. }
  3146. /*
  3147. * Zero file bytes between startoff and endoff inclusive.
  3148. * The iolock is held exclusive and no blocks are buffered.
  3149. */
  3150. STATIC int
  3151. xfs_zero_remaining_bytes(
  3152. xfs_inode_t *ip,
  3153. xfs_off_t startoff,
  3154. xfs_off_t endoff)
  3155. {
  3156. xfs_bmbt_irec_t imap;
  3157. xfs_fileoff_t offset_fsb;
  3158. xfs_off_t lastoffset;
  3159. xfs_off_t offset;
  3160. xfs_buf_t *bp;
  3161. xfs_mount_t *mp = ip->i_mount;
  3162. int nimap;
  3163. int error = 0;
  3164. bp = xfs_buf_get_noaddr(mp->m_sb.sb_blocksize,
  3165. XFS_IS_REALTIME_INODE(ip) ?
  3166. mp->m_rtdev_targp : mp->m_ddev_targp);
  3167. for (offset = startoff; offset <= endoff; offset = lastoffset + 1) {
  3168. offset_fsb = XFS_B_TO_FSBT(mp, offset);
  3169. nimap = 1;
  3170. error = xfs_bmapi(NULL, ip, offset_fsb, 1, 0,
  3171. NULL, 0, &imap, &nimap, NULL, NULL);
  3172. if (error || nimap < 1)
  3173. break;
  3174. ASSERT(imap.br_blockcount >= 1);
  3175. ASSERT(imap.br_startoff == offset_fsb);
  3176. lastoffset = XFS_FSB_TO_B(mp, imap.br_startoff + 1) - 1;
  3177. if (lastoffset > endoff)
  3178. lastoffset = endoff;
  3179. if (imap.br_startblock == HOLESTARTBLOCK)
  3180. continue;
  3181. ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
  3182. if (imap.br_state == XFS_EXT_UNWRITTEN)
  3183. continue;
  3184. XFS_BUF_UNDONE(bp);
  3185. XFS_BUF_UNWRITE(bp);
  3186. XFS_BUF_READ(bp);
  3187. XFS_BUF_SET_ADDR(bp, XFS_FSB_TO_DB(ip, imap.br_startblock));
  3188. xfsbdstrat(mp, bp);
  3189. error = xfs_iowait(bp);
  3190. if (error) {
  3191. xfs_ioerror_alert("xfs_zero_remaining_bytes(read)",
  3192. mp, bp, XFS_BUF_ADDR(bp));
  3193. break;
  3194. }
  3195. memset(XFS_BUF_PTR(bp) +
  3196. (offset - XFS_FSB_TO_B(mp, imap.br_startoff)),
  3197. 0, lastoffset - offset + 1);
  3198. XFS_BUF_UNDONE(bp);
  3199. XFS_BUF_UNREAD(bp);
  3200. XFS_BUF_WRITE(bp);
  3201. xfsbdstrat(mp, bp);
  3202. error = xfs_iowait(bp);
  3203. if (error) {
  3204. xfs_ioerror_alert("xfs_zero_remaining_bytes(write)",
  3205. mp, bp, XFS_BUF_ADDR(bp));
  3206. break;
  3207. }
  3208. }
  3209. xfs_buf_free(bp);
  3210. return error;
  3211. }
  3212. /*
  3213. * xfs_free_file_space()
  3214. * This routine frees disk space for the given file.
  3215. *
  3216. * This routine is only called by xfs_change_file_space
  3217. * for an UNRESVSP type call.
  3218. *
  3219. * RETURNS:
  3220. * 0 on success
  3221. * errno on error
  3222. *
  3223. */
  3224. STATIC int
  3225. xfs_free_file_space(
  3226. xfs_inode_t *ip,
  3227. xfs_off_t offset,
  3228. xfs_off_t len,
  3229. int attr_flags)
  3230. {
  3231. bhv_vnode_t *vp;
  3232. int committed;
  3233. int done;
  3234. xfs_off_t end_dmi_offset;
  3235. xfs_fileoff_t endoffset_fsb;
  3236. int error;
  3237. xfs_fsblock_t firstfsb;
  3238. xfs_bmap_free_t free_list;
  3239. xfs_bmbt_irec_t imap;
  3240. xfs_off_t ioffset;
  3241. xfs_extlen_t mod=0;
  3242. xfs_mount_t *mp;
  3243. int nimap;
  3244. uint resblks;
  3245. uint rounding;
  3246. int rt;
  3247. xfs_fileoff_t startoffset_fsb;
  3248. xfs_trans_t *tp;
  3249. int need_iolock = 1;
  3250. vp = XFS_ITOV(ip);
  3251. mp = ip->i_mount;
  3252. xfs_itrace_entry(ip);
  3253. if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
  3254. return error;
  3255. error = 0;
  3256. if (len <= 0) /* if nothing being freed */
  3257. return error;
  3258. rt = XFS_IS_REALTIME_INODE(ip);
  3259. startoffset_fsb = XFS_B_TO_FSB(mp, offset);
  3260. end_dmi_offset = offset + len;
  3261. endoffset_fsb = XFS_B_TO_FSBT(mp, end_dmi_offset);
  3262. if (offset < ip->i_size && (attr_flags & ATTR_DMI) == 0 &&
  3263. DM_EVENT_ENABLED(ip, DM_EVENT_WRITE)) {
  3264. if (end_dmi_offset > ip->i_size)
  3265. end_dmi_offset = ip->i_size;
  3266. error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, ip,
  3267. offset, end_dmi_offset - offset,
  3268. AT_DELAY_FLAG(attr_flags), NULL);
  3269. if (error)
  3270. return error;
  3271. }
  3272. if (attr_flags & ATTR_NOLOCK)
  3273. need_iolock = 0;
  3274. if (need_iolock) {
  3275. xfs_ilock(ip, XFS_IOLOCK_EXCL);
  3276. vn_iowait(ip); /* wait for the completion of any pending DIOs */
  3277. }
  3278. rounding = max_t(uint, 1 << mp->m_sb.sb_blocklog, PAGE_CACHE_SIZE);
  3279. ioffset = offset & ~(rounding - 1);
  3280. if (VN_CACHED(vp) != 0) {
  3281. xfs_inval_cached_trace(ip, ioffset, -1, ioffset, -1);
  3282. error = xfs_flushinval_pages(ip, ioffset, -1, FI_REMAPF_LOCKED);
  3283. if (error)
  3284. goto out_unlock_iolock;
  3285. }
  3286. /*
  3287. * Need to zero the stuff we're not freeing, on disk.
  3288. * If its a realtime file & can't use unwritten extents then we
  3289. * actually need to zero the extent edges. Otherwise xfs_bunmapi
  3290. * will take care of it for us.
  3291. */
  3292. if (rt && !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
  3293. nimap = 1;
  3294. error = xfs_bmapi(NULL, ip, startoffset_fsb,
  3295. 1, 0, NULL, 0, &imap, &nimap, NULL, NULL);
  3296. if (error)
  3297. goto out_unlock_iolock;
  3298. ASSERT(nimap == 0 || nimap == 1);
  3299. if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
  3300. xfs_daddr_t block;
  3301. ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
  3302. block = imap.br_startblock;
  3303. mod = do_div(block, mp->m_sb.sb_rextsize);
  3304. if (mod)
  3305. startoffset_fsb += mp->m_sb.sb_rextsize - mod;
  3306. }
  3307. nimap = 1;
  3308. error = xfs_bmapi(NULL, ip, endoffset_fsb - 1,
  3309. 1, 0, NULL, 0, &imap, &nimap, NULL, NULL);
  3310. if (error)
  3311. goto out_unlock_iolock;
  3312. ASSERT(nimap == 0 || nimap == 1);
  3313. if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
  3314. ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
  3315. mod++;
  3316. if (mod && (mod != mp->m_sb.sb_rextsize))
  3317. endoffset_fsb -= mod;
  3318. }
  3319. }
  3320. if ((done = (endoffset_fsb <= startoffset_fsb)))
  3321. /*
  3322. * One contiguous piece to clear
  3323. */
  3324. error = xfs_zero_remaining_bytes(ip, offset, offset + len - 1);
  3325. else {
  3326. /*
  3327. * Some full blocks, possibly two pieces to clear
  3328. */
  3329. if (offset < XFS_FSB_TO_B(mp, startoffset_fsb))
  3330. error = xfs_zero_remaining_bytes(ip, offset,
  3331. XFS_FSB_TO_B(mp, startoffset_fsb) - 1);
  3332. if (!error &&
  3333. XFS_FSB_TO_B(mp, endoffset_fsb) < offset + len)
  3334. error = xfs_zero_remaining_bytes(ip,
  3335. XFS_FSB_TO_B(mp, endoffset_fsb),
  3336. offset + len - 1);
  3337. }
  3338. /*
  3339. * free file space until done or until there is an error
  3340. */
  3341. resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
  3342. while (!error && !done) {
  3343. /*
  3344. * allocate and setup the transaction. Allow this
  3345. * transaction to dip into the reserve blocks to ensure
  3346. * the freeing of the space succeeds at ENOSPC.
  3347. */
  3348. tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
  3349. tp->t_flags |= XFS_TRANS_RESERVE;
  3350. error = xfs_trans_reserve(tp,
  3351. resblks,
  3352. XFS_WRITE_LOG_RES(mp),
  3353. 0,
  3354. XFS_TRANS_PERM_LOG_RES,
  3355. XFS_WRITE_LOG_COUNT);
  3356. /*
  3357. * check for running out of space
  3358. */
  3359. if (error) {
  3360. /*
  3361. * Free the transaction structure.
  3362. */
  3363. ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
  3364. xfs_trans_cancel(tp, 0);
  3365. break;
  3366. }
  3367. xfs_ilock(ip, XFS_ILOCK_EXCL);
  3368. error = XFS_TRANS_RESERVE_QUOTA(mp, tp,
  3369. ip->i_udquot, ip->i_gdquot, resblks, 0,
  3370. XFS_QMOPT_RES_REGBLKS);
  3371. if (error)
  3372. goto error1;
  3373. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  3374. xfs_trans_ihold(tp, ip);
  3375. /*
  3376. * issue the bunmapi() call to free the blocks
  3377. */
  3378. XFS_BMAP_INIT(&free_list, &firstfsb);
  3379. error = xfs_bunmapi(tp, ip, startoffset_fsb,
  3380. endoffset_fsb - startoffset_fsb,
  3381. 0, 2, &firstfsb, &free_list, NULL, &done);
  3382. if (error) {
  3383. goto error0;
  3384. }
  3385. /*
  3386. * complete the transaction
  3387. */
  3388. error = xfs_bmap_finish(&tp, &free_list, &committed);
  3389. if (error) {
  3390. goto error0;
  3391. }
  3392. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  3393. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  3394. }
  3395. out_unlock_iolock:
  3396. if (need_iolock)
  3397. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  3398. return error;
  3399. error0:
  3400. xfs_bmap_cancel(&free_list);
  3401. error1:
  3402. xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
  3403. xfs_iunlock(ip, need_iolock ? (XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL) :
  3404. XFS_ILOCK_EXCL);
  3405. return error;
  3406. }
  3407. /*
  3408. * xfs_change_file_space()
  3409. * This routine allocates or frees disk space for the given file.
  3410. * The user specified parameters are checked for alignment and size
  3411. * limitations.
  3412. *
  3413. * RETURNS:
  3414. * 0 on success
  3415. * errno on error
  3416. *
  3417. */
  3418. int
  3419. xfs_change_file_space(
  3420. xfs_inode_t *ip,
  3421. int cmd,
  3422. xfs_flock64_t *bf,
  3423. xfs_off_t offset,
  3424. cred_t *credp,
  3425. int attr_flags)
  3426. {
  3427. xfs_mount_t *mp = ip->i_mount;
  3428. int clrprealloc;
  3429. int error;
  3430. xfs_fsize_t fsize;
  3431. int setprealloc;
  3432. xfs_off_t startoffset;
  3433. xfs_off_t llen;
  3434. xfs_trans_t *tp;
  3435. bhv_vattr_t va;
  3436. xfs_itrace_entry(ip);
  3437. if (!S_ISREG(ip->i_d.di_mode))
  3438. return XFS_ERROR(EINVAL);
  3439. switch (bf->l_whence) {
  3440. case 0: /*SEEK_SET*/
  3441. break;
  3442. case 1: /*SEEK_CUR*/
  3443. bf->l_start += offset;
  3444. break;
  3445. case 2: /*SEEK_END*/
  3446. bf->l_start += ip->i_size;
  3447. break;
  3448. default:
  3449. return XFS_ERROR(EINVAL);
  3450. }
  3451. llen = bf->l_len > 0 ? bf->l_len - 1 : bf->l_len;
  3452. if ( (bf->l_start < 0)
  3453. || (bf->l_start > XFS_MAXIOFFSET(mp))
  3454. || (bf->l_start + llen < 0)
  3455. || (bf->l_start + llen > XFS_MAXIOFFSET(mp)))
  3456. return XFS_ERROR(EINVAL);
  3457. bf->l_whence = 0;
  3458. startoffset = bf->l_start;
  3459. fsize = ip->i_size;
  3460. /*
  3461. * XFS_IOC_RESVSP and XFS_IOC_UNRESVSP will reserve or unreserve
  3462. * file space.
  3463. * These calls do NOT zero the data space allocated to the file,
  3464. * nor do they change the file size.
  3465. *
  3466. * XFS_IOC_ALLOCSP and XFS_IOC_FREESP will allocate and free file
  3467. * space.
  3468. * These calls cause the new file data to be zeroed and the file
  3469. * size to be changed.
  3470. */
  3471. setprealloc = clrprealloc = 0;
  3472. switch (cmd) {
  3473. case XFS_IOC_RESVSP:
  3474. case XFS_IOC_RESVSP64:
  3475. error = xfs_alloc_file_space(ip, startoffset, bf->l_len,
  3476. 1, attr_flags);
  3477. if (error)
  3478. return error;
  3479. setprealloc = 1;
  3480. break;
  3481. case XFS_IOC_UNRESVSP:
  3482. case XFS_IOC_UNRESVSP64:
  3483. if ((error = xfs_free_file_space(ip, startoffset, bf->l_len,
  3484. attr_flags)))
  3485. return error;
  3486. break;
  3487. case XFS_IOC_ALLOCSP:
  3488. case XFS_IOC_ALLOCSP64:
  3489. case XFS_IOC_FREESP:
  3490. case XFS_IOC_FREESP64:
  3491. if (startoffset > fsize) {
  3492. error = xfs_alloc_file_space(ip, fsize,
  3493. startoffset - fsize, 0, attr_flags);
  3494. if (error)
  3495. break;
  3496. }
  3497. va.va_mask = XFS_AT_SIZE;
  3498. va.va_size = startoffset;
  3499. error = xfs_setattr(ip, &va, attr_flags, credp);
  3500. if (error)
  3501. return error;
  3502. clrprealloc = 1;
  3503. break;
  3504. default:
  3505. ASSERT(0);
  3506. return XFS_ERROR(EINVAL);
  3507. }
  3508. /*
  3509. * update the inode timestamp, mode, and prealloc flag bits
  3510. */
  3511. tp = xfs_trans_alloc(mp, XFS_TRANS_WRITEID);
  3512. if ((error = xfs_trans_reserve(tp, 0, XFS_WRITEID_LOG_RES(mp),
  3513. 0, 0, 0))) {
  3514. /* ASSERT(0); */
  3515. xfs_trans_cancel(tp, 0);
  3516. return error;
  3517. }
  3518. xfs_ilock(ip, XFS_ILOCK_EXCL);
  3519. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  3520. xfs_trans_ihold(tp, ip);
  3521. if ((attr_flags & ATTR_DMI) == 0) {
  3522. ip->i_d.di_mode &= ~S_ISUID;
  3523. /*
  3524. * Note that we don't have to worry about mandatory
  3525. * file locking being disabled here because we only
  3526. * clear the S_ISGID bit if the Group execute bit is
  3527. * on, but if it was on then mandatory locking wouldn't
  3528. * have been enabled.
  3529. */
  3530. if (ip->i_d.di_mode & S_IXGRP)
  3531. ip->i_d.di_mode &= ~S_ISGID;
  3532. xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  3533. }
  3534. if (setprealloc)
  3535. ip->i_d.di_flags |= XFS_DIFLAG_PREALLOC;
  3536. else if (clrprealloc)
  3537. ip->i_d.di_flags &= ~XFS_DIFLAG_PREALLOC;
  3538. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  3539. xfs_trans_set_sync(tp);
  3540. error = xfs_trans_commit(tp, 0);
  3541. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  3542. return error;
  3543. }