xfs_vnodeops.c 74 KB

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