xfs_vnodeops.c 70 KB

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