xfs_vnodeops.c 82 KB

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