xfs_vnodeops.c 78 KB

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