xfs_vnodeops.c 70 KB

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