xfs_vnodeops.c 77 KB

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