xfs_vnodeops.c 70 KB

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