xfs_vnodeops.c 78 KB

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