xfs_vnodeops.c 69 KB

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