xfs_vnodeops.c 78 KB

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