xfs_vnodeops.c 70 KB

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